Search in sources :

Example 1 with PwmApplicationMode

use of password.pwm.PwmApplicationMode in project pwm by pwm-project.

the class ContextManager method initialize.

public void initialize() {
    try {
        Locale.setDefault(PwmConstants.DEFAULT_LOCALE);
    } catch (Exception e) {
        outputError("unable to set default locale as Java machine default locale: " + e.getMessage());
    }
    Configuration configuration = null;
    PwmApplicationMode mode = PwmApplicationMode.ERROR;
    final ParameterReader parameterReader = new ParameterReader(servletContext);
    final File applicationPath;
    {
        final String applicationPathStr = parameterReader.readApplicationPath();
        if (applicationPathStr == null || applicationPathStr.isEmpty()) {
            startupErrorInformation = new ErrorInformation(PwmError.ERROR_ENVIRONMENT_ERROR, "application path is not specified");
            return;
        } else {
            applicationPath = new File(applicationPathStr);
        }
    }
    File configurationFile = null;
    try {
        configurationFile = locateConfigurationFile(applicationPath);
        configReader = new ConfigurationReader(configurationFile);
        configReader.getStoredConfiguration().lock();
        configuration = configReader.getConfiguration();
        mode = startupErrorInformation == null ? configReader.getConfigMode() : PwmApplicationMode.ERROR;
        if (startupErrorInformation == null) {
            startupErrorInformation = configReader.getConfigFileError();
        }
        if (PwmApplicationMode.ERROR == mode) {
            outputError("Startup Error: " + (startupErrorInformation == null ? "un-specified error" : startupErrorInformation.toDebugStr()));
        }
    } catch (Throwable e) {
        handleStartupError("unable to initialize application due to configuration related error: ", e);
    }
    LOGGER.debug("configuration file was loaded from " + (configurationFile == null ? "null" : configurationFile.getAbsoluteFile()));
    final Collection<PwmEnvironment.ApplicationFlag> applicationFlags = parameterReader.readApplicationFlags();
    final Map<PwmEnvironment.ApplicationParameter, String> applicationParams = parameterReader.readApplicationParams();
    try {
        final PwmEnvironment pwmEnvironment = new PwmEnvironment.Builder(configuration, applicationPath).setApplicationMode(mode).setConfigurationFile(configurationFile).setContextManager(this).setFlags(applicationFlags).setParams(applicationParams).createPwmEnvironment();
        pwmApplication = new PwmApplication(pwmEnvironment);
    } catch (Exception e) {
        handleStartupError("unable to initialize application: ", e);
    }
    final String threadName = JavaHelper.makeThreadName(pwmApplication, this.getClass()) + " timer";
    taskMaster = new Timer(threadName, true);
    taskMaster.schedule(new RestartFlagWatcher(), 1031, 1031);
    boolean reloadOnChange = true;
    long fileScanFrequencyMs = 5000;
    {
        if (pwmApplication != null) {
            reloadOnChange = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.CONFIG_RELOAD_ON_CHANGE));
            fileScanFrequencyMs = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.CONFIG_FILE_SCAN_FREQUENCY));
        }
        if (reloadOnChange) {
            taskMaster.schedule(new ConfigFileWatcher(), fileScanFrequencyMs, fileScanFrequencyMs);
        }
        checkConfigForSaveOnRestart(configReader, pwmApplication);
    }
}
Also used : PwmApplication(password.pwm.PwmApplication) Configuration(password.pwm.config.Configuration) PwmEnvironment(password.pwm.PwmEnvironment) PwmApplicationMode(password.pwm.PwmApplicationMode) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) Timer(java.util.Timer) File(java.io.File) ConfigurationReader(password.pwm.config.stored.ConfigurationReader)

Example 2 with PwmApplicationMode

use of password.pwm.PwmApplicationMode in project pwm by pwm-project.

the class ApplicationModeFilter method checkConfigModes.

private static ProcessStatus checkConfigModes(final PwmRequest pwmRequest) throws IOException, ServletException, PwmUnrecoverableException {
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final PwmApplicationMode mode = pwmApplication.getApplicationMode();
    final PwmURL pwmURL = pwmRequest.getURL();
    if (mode == PwmApplicationMode.NEW) {
        // check if current request is actually for the config url, if it is, just do nothing.
        if (pwmURL.isCommandServletURL() || pwmURL.isRestService()) {
            return ProcessStatus.Continue;
        }
        if (pwmURL.isConfigGuideURL() || pwmURL.isReferenceURL()) {
            return ProcessStatus.Continue;
        } else {
            LOGGER.debug("unable to find a valid configuration, redirecting " + pwmURL + " to ConfigGuide");
            pwmRequest.sendRedirect(PwmServletDefinition.ConfigGuide);
            return ProcessStatus.Halt;
        }
    }
    if (mode == PwmApplicationMode.ERROR) {
        ErrorInformation rootError = ContextManager.getContextManager(pwmRequest.getHttpServletRequest().getSession()).getStartupErrorInformation();
        if (rootError == null) {
            rootError = new ErrorInformation(PwmError.ERROR_APP_UNAVAILABLE, "Application startup failed.");
        }
        pwmRequest.respondWithError(rootError);
        return ProcessStatus.Halt;
    }
    // allow oauth
    if (pwmURL.isOauthConsumer()) {
        return ProcessStatus.Continue;
    }
    // block if public request and not running or in trial
    if (!PwmConstants.TRIAL_MODE) {
        if (mode != PwmApplicationMode.RUNNING) {
            final boolean permittedURl = pwmURL.isResourceURL() || pwmURL.isIndexPage() || pwmURL.isConfigManagerURL() || pwmURL.isConfigGuideURL() || pwmURL.isCommandServletURL() || pwmURL.isReferenceURL() || pwmURL.isLoginServlet() || pwmURL.isLogoutURL() || pwmURL.isOauthConsumer() || pwmURL.isAdminUrl() || pwmURL.isRestService();
            if (!permittedURl) {
                final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_APPLICATION_NOT_RUNNING);
                pwmRequest.respondWithError(errorInformation);
                return ProcessStatus.Halt;
            }
        }
    }
    return ProcessStatus.Continue;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmApplication(password.pwm.PwmApplication) PwmURL(password.pwm.http.PwmURL) PwmApplicationMode(password.pwm.PwmApplicationMode)

Example 3 with PwmApplicationMode

use of password.pwm.PwmApplicationMode in project pwm by pwm-project.

the class MacroMachine method effectiveScopes.

private static Set<MacroImplementation.Scope> effectiveScopes(final MacroImplementation.MacroRequestInfo macroRequestInfo) {
    final Set<MacroImplementation.Scope> scopes = new HashSet<>();
    scopes.add(MacroImplementation.Scope.Static);
    final PwmApplication pwmApplication = macroRequestInfo.getPwmApplication();
    final PwmApplicationMode mode = pwmApplication != null ? pwmApplication.getApplicationMode() : PwmApplicationMode.ERROR;
    final boolean appModeOk = mode == PwmApplicationMode.RUNNING || mode == PwmApplicationMode.CONFIGURATION;
    if (appModeOk) {
        scopes.add(MacroImplementation.Scope.System);
        if (macroRequestInfo.getUserInfo() != null) {
            scopes.add(MacroImplementation.Scope.User);
        }
    }
    return Collections.unmodifiableSet(scopes);
}
Also used : PwmApplication(password.pwm.PwmApplication) PwmApplicationMode(password.pwm.PwmApplicationMode) HashSet(java.util.HashSet)

Example 4 with PwmApplicationMode

use of password.pwm.PwmApplicationMode in project pwm by pwm-project.

the class MainClass method loadPwmApplication.

private static PwmApplication loadPwmApplication(final File applicationPath, final Collection<PwmEnvironment.ApplicationFlag> flags, final Configuration config, final File configurationFile, final boolean readonly) throws LocalDBException, PwmUnrecoverableException {
    final PwmApplicationMode mode = readonly ? PwmApplicationMode.READ_ONLY : PwmApplicationMode.RUNNING;
    final Collection<PwmEnvironment.ApplicationFlag> applicationFlags = new HashSet<>();
    if (flags == null) {
        applicationFlags.addAll(PwmEnvironment.ParseHelper.readApplicationFlagsFromSystem(null));
    } else {
        applicationFlags.addAll(flags);
    }
    applicationFlags.add(PwmEnvironment.ApplicationFlag.CommandLineInstance);
    final PwmEnvironment pwmEnvironment = new PwmEnvironment.Builder(config, applicationPath).setApplicationMode(mode).setConfigurationFile(configurationFile).setFlags(applicationFlags).createPwmEnvironment();
    final PwmApplication pwmApplication = new PwmApplication(pwmEnvironment);
    final PwmApplicationMode runningMode = pwmApplication.getApplicationMode();
    if (runningMode != mode) {
        out("unable to start application in required state '" + mode + "', current state: " + runningMode);
        System.exit(-1);
    }
    return pwmApplication;
}
Also used : PwmApplication(password.pwm.PwmApplication) PwmEnvironment(password.pwm.PwmEnvironment) PwmApplicationMode(password.pwm.PwmApplicationMode) HashSet(java.util.HashSet)

Example 5 with PwmApplicationMode

use of password.pwm.PwmApplicationMode in project pwm by pwm-project.

the class AbstractPwmFilter method doFilter.

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
    final HttpServletRequest req = (HttpServletRequest) servletRequest;
    final HttpServletResponse resp = (HttpServletResponse) servletResponse;
    final PwmApplicationMode mode = PwmApplicationMode.determineMode(req);
    final boolean interested;
    try {
        final PwmURL pwmURL = new PwmURL(req);
        interested = isInterested(mode, pwmURL);
    } catch (Exception e) {
        LOGGER.error("unexpected error processing filter chain during isInterested(): " + e.getMessage(), e);
        resp.sendError(500, "unexpected error processing filter chain during isInterested");
        return;
    }
    if (interested) {
        PwmRequest pwmRequest = null;
        try {
            pwmRequest = PwmRequest.forRequest(req, resp);
        } catch (PwmException e) {
            final PwmURL pwmURL = new PwmURL(req);
            if (pwmURL.isResourceURL()) {
                filterChain.doFilter(req, resp);
                return;
            }
            LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
        }
        try {
            final PwmFilterChain pwmFilterChain = new PwmFilterChain(servletRequest, servletResponse, filterChain);
            processFilter(mode, pwmRequest, pwmFilterChain);
        } catch (PwmException e) {
            LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
        } catch (IOException e) {
            LOGGER.debug(pwmRequest, "i/o error processing request: " + e.getMessage());
        }
    } else {
        filterChain.doFilter(req, resp);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PwmException(password.pwm.error.PwmException) PwmRequest(password.pwm.http.PwmRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PwmURL(password.pwm.http.PwmURL) IOException(java.io.IOException) PwmApplicationMode(password.pwm.PwmApplicationMode) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PwmException(password.pwm.error.PwmException)

Aggregations

PwmApplicationMode (password.pwm.PwmApplicationMode)7 PwmApplication (password.pwm.PwmApplication)5 PwmException (password.pwm.error.PwmException)4 ErrorInformation (password.pwm.error.ErrorInformation)3 PwmURL (password.pwm.http.PwmURL)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 PwmEnvironment (password.pwm.PwmEnvironment)2 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)2 File (java.io.File)1 UnknownHostException (java.net.UnknownHostException)1 Timer (java.util.Timer)1 Configuration (password.pwm.config.Configuration)1 ConfigurationReader (password.pwm.config.stored.ConfigurationReader)1 ContextManager (password.pwm.http.ContextManager)1 PwmRequest (password.pwm.http.PwmRequest)1 ConfigManagerBean (password.pwm.http.bean.ConfigManagerBean)1