use of password.pwm.PwmApplication in project pwm by pwm-project.
the class MainClass method createEnv.
private static CliEnvironment createEnv(final CliParameters parameters, final List<String> args) throws Exception {
final Map<String, Object> options = parseCommandOptions(parameters, args);
final File applicationPath = figureApplicationPath(mainOptions);
out("applicationPath=" + applicationPath.getAbsolutePath());
PwmEnvironment.verifyApplicationPath(applicationPath);
final File configurationFile = locateConfigurationFile(applicationPath);
final ConfigurationReader configReader = loadConfiguration(configurationFile);
final Configuration config = configReader.getConfiguration();
final PwmApplication pwmApplication;
final LocalDB localDB;
if (parameters.needsPwmApplication) {
pwmApplication = loadPwmApplication(applicationPath, mainOptions.getApplicationFlags(), config, configurationFile, parameters.readOnly);
localDB = pwmApplication.getLocalDB();
} else if (parameters.needsLocalDB) {
pwmApplication = null;
localDB = loadPwmDB(config, parameters.readOnly, applicationPath);
} else {
pwmApplication = null;
localDB = null;
}
out("environment initialized");
out("");
final Writer outputStream = new OutputStreamWriter(System.out, PwmConstants.DEFAULT_CHARSET);
return CliEnvironment.builder().configurationReader(configReader).configurationFile(configurationFile).config(config).applicationPath(applicationPath).pwmApplication(pwmApplication).localDB(localDB).debugWriter(outputStream).options(options).mainOptions(mainOptions).build();
}
use of password.pwm.PwmApplication in project pwm by pwm-project.
the class ExportStatsCommand method doCommand.
@Override
void doCommand() throws Exception {
final PwmApplication pwmApplication = cliEnvironment.getPwmApplication();
final StatisticsManager statsManger = pwmApplication.getStatisticsManager();
JavaHelper.pause(1000);
final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName());
final long startTime = System.currentTimeMillis();
out("beginning output to " + outputFile.getAbsolutePath());
final int counter;
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile, true)) {
counter = statsManger.outputStatsToCsv(fileOutputStream, Locale.getDefault(), true);
fileOutputStream.close();
}
out("completed writing " + counter + " rows of stats output in " + TimeDuration.fromCurrent(startTime).asLongString());
}
use of password.pwm.PwmApplication in project pwm by pwm-project.
the class ResponseStatsCommand method doCommand.
@Override
void doCommand() throws Exception {
final PwmApplication pwmApplication = cliEnvironment.getPwmApplication();
out("searching for users");
final List<UserIdentity> userIdentities = readAllUsersFromLdap(pwmApplication);
out("found " + userIdentities.size() + " users, reading....");
final ResponseStats responseStats = makeStatistics(pwmApplication, userIdentities);
final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName());
final long startTime = System.currentTimeMillis();
out("beginning output to " + outputFile.getAbsolutePath());
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile, true)) {
fileOutputStream.write(JsonUtil.serialize(responseStats, JsonUtil.Flag.PrettyPrint).getBytes(PwmConstants.DEFAULT_CHARSET));
}
out("completed writing stats output in " + TimeDuration.fromCurrent(startTime).asLongString());
}
use of password.pwm.PwmApplication in project pwm by pwm-project.
the class ImportResponsesCommand method doCommand.
@Override
void doCommand() throws Exception {
final PwmApplication pwmApplication = cliEnvironment.getPwmApplication();
final File inputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_EXISTING_INPUT_FILE.getName());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), PwmConstants.DEFAULT_CHARSET.toString()))) {
out("importing stored responses from " + inputFile.getAbsolutePath() + "....");
int counter = 0;
String line;
final long startTime = System.currentTimeMillis();
while ((line = reader.readLine()) != null) {
counter++;
final RestChallengesServer.JsonChallengesData inputData;
inputData = JsonUtil.deserialize(line, RestChallengesServer.JsonChallengesData.class);
final UserIdentity userIdentity = UserIdentity.fromDelimitedKey(inputData.username);
final ChaiUser user = pwmApplication.getProxiedChaiUser(userIdentity);
if (user.exists()) {
out("writing responses to user '" + user.getEntryDN() + "'");
try {
final ChallengeProfile challengeProfile = pwmApplication.getCrService().readUserChallengeProfile(null, userIdentity, user, PwmPasswordPolicy.defaultPolicy(), PwmConstants.DEFAULT_LOCALE);
final ChallengeSet challengeSet = challengeProfile.getChallengeSet();
final String userGuid = LdapOperationsHelper.readLdapGuidValue(pwmApplication, null, userIdentity, false);
final ResponseInfoBean responseInfoBean = inputData.toResponseInfoBean(PwmConstants.DEFAULT_LOCALE, challengeSet.getIdentifier());
pwmApplication.getCrService().writeResponses(userIdentity, user, userGuid, responseInfoBean);
} catch (Exception e) {
out("error writing responses to user '" + user.getEntryDN() + "', error: " + e.getMessage());
return;
}
} else {
out("user '" + user.getEntryDN() + "' is not a valid userDN");
return;
}
}
out("output complete, " + counter + " responses imported in " + TimeDuration.fromCurrent(startTime).asCompactString());
}
}
use of password.pwm.PwmApplication in project pwm by pwm-project.
the class ExternalRestMacro method replaceValue.
public String replaceValue(final String matchValue, final MacroRequestInfo macroRequestInfo) {
final PwmApplication pwmApplication = macroRequestInfo.getPwmApplication();
final UserInfo userInfoBean = macroRequestInfo.getUserInfo();
final String inputString = matchValue.substring(11, matchValue.length() - 1);
final Map<String, Object> sendData = new HashMap<>();
try {
if (userInfoBean != null) {
final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, PwmConstants.DEFAULT_LOCALE, SessionLabel.SYSTEM_LABEL, userInfoBean.getUserIdentity());
final PublicUserInfoBean publicUserInfoBean = PublicUserInfoBean.fromUserInfoBean(userInfoBean, pwmApplication.getConfig(), PwmConstants.DEFAULT_LOCALE, macroMachine);
sendData.put("userInfo", publicUserInfoBean);
}
sendData.put("input", inputString);
final String requestBody = JsonUtil.serializeMap(sendData);
final String responseBody = RestClientHelper.makeOutboundRestWSCall(pwmApplication, PwmConstants.DEFAULT_LOCALE, url, requestBody);
final Map<String, Object> responseMap = JsonUtil.deserialize(responseBody, new TypeToken<Map<String, Object>>() {
});
if (responseMap.containsKey("output")) {
return responseMap.get("output").toString();
} else {
return "";
}
} catch (PwmException e) {
final String errorMsg = "error while executing external macro '" + matchValue + "', error: " + e.getMessage();
LOGGER.error(errorMsg);
throw new IllegalStateException(errorMsg);
}
}
Aggregations