use of org.glassfish.security.common.FileRealmHelper in project Payara by payara.
the class FileRealm method init.
/**
* Initialize a realm with some properties. This can be used
* when instantiating realms from their descriptions. This
* method is invoked from Realm during initialization.
*
* @param props Initialization parameters used by this realm.
* @exception BadRealmException If the configuration parameters
* identify a corrupt realm.
* @exception NoSuchRealmException If the configuration parameters
* specify a realm which doesn't exist.
*/
@Override
protected void init(Properties props) throws BadRealmException, NoSuchRealmException {
super.init(props);
String file = props.getProperty(PARAM_KEYFILE);
if (file == null) {
String msg = sm.getString("filerealm.nofile");
throw new BadRealmException(msg);
}
if (file.contains("$")) {
file = RelativePathResolver.resolvePath(file);
}
this.setProperty(PARAM_KEYFILE, file);
String jaasCtx = props.getProperty(IASRealm.JAAS_CONTEXT_PARAM);
if (jaasCtx == null) {
String msg = sm.getString("filerealm.nomodule");
throw new BadRealmException(msg);
}
this.setProperty(IASRealm.JAAS_CONTEXT_PARAM, jaasCtx);
_logger.log(Level.FINE, "FileRealm : " + PARAM_KEYFILE + "={0}", file);
_logger.log(Level.FINE, "FileRealm : " + IASRealm.JAAS_CONTEXT_PARAM + "={0}", jaasCtx);
try {
if (Util.isEmbeddedServer()) {
String embeddedFilePath = Util.writeConfigFileToTempDir(file).getAbsolutePath();
file = embeddedFilePath;
}
helper = new FileRealmHelper(file);
} catch (IOException ioe) {
String msg = sm.getString("filerealm.noaccess", ioe.toString());
throw new BadRealmException(msg);
}
}
use of org.glassfish.security.common.FileRealmHelper in project Payara by payara.
the class ChangeAdminPasswordCommand method changeAdminPasswordLocally.
private int changeAdminPasswordLocally(String domainDir, String domainName) throws CommandException {
if (!isLocalHost(programOpts.getHost())) {
throw new CommandException(strings.get("CannotExecuteLocally"));
}
GFLauncher launcher = null;
try {
launcher = GFLauncherFactory.getInstance(RuntimeType.DAS);
GFLauncherInfo info = launcher.getInfo();
info.setDomainName(domainName);
info.setDomainParentDir(domainDir);
launcher.setup();
// throw new exception
if (launcher.isSecureAdminEnabled()) {
if ((newpassword == null) || (newpassword.isEmpty())) {
throw new CommandException(strings.get("NullNewPassword"));
}
}
String adminKeyFile = launcher.getAdminRealmKeyFile();
if (adminKeyFile != null) {
// This is a FileRealm, instantiate it.
FileRealmHelper helper = new FileRealmHelper(adminKeyFile);
// Authenticate the old password
String[] groups = helper.authenticate(programOpts.getUser(), password.toCharArray());
if (groups == null) {
throw new CommandException(strings.get("InvalidCredentials", programOpts.getUser()));
}
helper.updateUser(programOpts.getUser(), programOpts.getUser(), newpassword.toCharArray(), null);
helper.persist();
return SUCCESS;
} else {
// Cannot change password locally for non file realms
throw new CommandException(strings.get("NotFileRealmCannotChangeLocally"));
}
} catch (MiniXmlParserException ex) {
throw new CommandException(ex);
} catch (GFLauncherException ex) {
throw new CommandException(ex);
} catch (IOException ex) {
throw new CommandException(ex);
}
}
use of org.glassfish.security.common.FileRealmHelper in project Payara by payara.
the class StartDomainCommand method doAdminPasswordCheck.
/*
* Check to make sure that at least one admin user is able to login.
* If none is found, then prompt for an admin password.
*
* NOTE: this depends on launcher.setup having already been called.
*/
private void doAdminPasswordCheck() throws CommandException {
String arfile = launcher.getAdminRealmKeyFile();
if (arfile != null) {
try {
FileRealmHelper ar = new FileRealmHelper(arfile);
if (!ar.hasAuthenticatableUser()) {
// Prompt for the password for the first user and set it
Set<String> names = ar.getUserNames();
if (names == null || names.isEmpty()) {
throw new CommandException("no admin users");
}
String auser = names.iterator().next();
ParamModelData npwo = new ParamModelData(newpwName, String.class, false, null);
npwo.prompt = strings.get("new.adminpw", auser);
npwo.promptAgain = strings.get("new.adminpw.again", auser);
npwo.param._password = true;
logger.info(strings.get("new.adminpw.prompt"));
char[] npwArr = super.getPassword(npwo, null, true);
String npw = npwArr != null ? new String(npwArr) : null;
if (npw == null) {
throw new CommandException(strings.get("no.console"));
}
ar.updateUser(auser, auser, npw.toCharArray(), null);
ar.persist();
}
} catch (IOException ioe) {
throw new CommandException(ioe);
}
}
}
use of org.glassfish.security.common.FileRealmHelper in project Payara by payara.
the class DomainSecurity method processAdminKeyFile.
/**
* Modifies the contents of given keyfile with administrator's user-name and
* password. Uses the FileRealm classes that application server's Runtime
* uses.
*
* @param keyFile File to store encrypted admin credentials.
* @param user Username.
* @param password Password.
*/
void processAdminKeyFile(File keyFile, String user, String password, final String[] adminUserGroups) throws IOException {
final String keyFilePath = keyFile.getAbsolutePath();
final FileRealmHelper fileRealm = new FileRealmHelper(keyFilePath);
final String[] group = adminUserGroups;
fileRealm.addUser(user, password.toCharArray(), group);
fileRealm.persist();
}
Aggregations