Search in sources :

Example 66 with PwmApplication

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();
}
Also used : PwmApplication(password.pwm.PwmApplication) Configuration(password.pwm.config.Configuration) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) LocalDB(password.pwm.util.localdb.LocalDB) ConfigurationReader(password.pwm.config.stored.ConfigurationReader) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 67 with PwmApplication

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());
}
Also used : PwmApplication(password.pwm.PwmApplication) StatisticsManager(password.pwm.svc.stats.StatisticsManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 68 with PwmApplication

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());
}
Also used : PwmApplication(password.pwm.PwmApplication) UserIdentity(password.pwm.bean.UserIdentity) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 69 with PwmApplication

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());
    }
}
Also used : PwmApplication(password.pwm.PwmApplication) ChallengeSet(com.novell.ldapchai.cr.ChallengeSet) InputStreamReader(java.io.InputStreamReader) UserIdentity(password.pwm.bean.UserIdentity) ChallengeProfile(password.pwm.config.profile.ChallengeProfile) ResponseInfoBean(password.pwm.bean.ResponseInfoBean) FileInputStream(java.io.FileInputStream) ChaiUser(com.novell.ldapchai.ChaiUser) BufferedReader(java.io.BufferedReader) RestChallengesServer(password.pwm.ws.server.rest.RestChallengesServer) File(java.io.File)

Example 70 with PwmApplication

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);
    }
}
Also used : PwmException(password.pwm.error.PwmException) PwmApplication(password.pwm.PwmApplication) HashMap(java.util.HashMap) UserInfo(password.pwm.ldap.UserInfo) HashMap(java.util.HashMap) Map(java.util.Map) PublicUserInfoBean(password.pwm.bean.pub.PublicUserInfoBean)

Aggregations

PwmApplication (password.pwm.PwmApplication)120 PwmSession (password.pwm.http.PwmSession)55 ErrorInformation (password.pwm.error.ErrorInformation)54 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)49 PwmOperationalException (password.pwm.error.PwmOperationalException)36 Configuration (password.pwm.config.Configuration)33 UserIdentity (password.pwm.bean.UserIdentity)27 FormConfiguration (password.pwm.config.value.data.FormConfiguration)25 PwmException (password.pwm.error.PwmException)25 IOException (java.io.IOException)22 ServletException (javax.servlet.ServletException)18 UserInfo (password.pwm.ldap.UserInfo)18 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)17 ChaiUser (com.novell.ldapchai.ChaiUser)16 Locale (java.util.Locale)13 ActionConfiguration (password.pwm.config.value.data.ActionConfiguration)13 SearchConfiguration (password.pwm.ldap.search.SearchConfiguration)13 MacroMachine (password.pwm.util.macro.MacroMachine)12 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)11 Instant (java.time.Instant)10