Example usage for javax.security.auth.login AppConfigurationEntry getOptions

List of usage examples for javax.security.auth.login AppConfigurationEntry getOptions

Introduction

In this page you can find the example usage for javax.security.auth.login AppConfigurationEntry getOptions.

Prototype

public Map<String, ?> getOptions() 

Source Link

Document

Get the options configured for this LoginModule .

Usage

From source file:gov.nih.nci.caintegrator.application.registration.RegistrationServiceImpl.java

/**
 * {@inheritDoc}/*w  w  w  .j a  v a2s. c  o m*/
 */
@Override
@SuppressWarnings("unchecked")
public Map<String, String> getLdapContextParams() {
    Map<String, String> ldapContextParams = new HashMap<String, String>();
    XMLLoginConfigImpl config = retrieveConfigFile();
    if (config == null) {
        return ldapContextParams;
    }
    AppConfigurationEntry[] entries = config.getAppConfigurationEntry("caintegrator");
    for (AppConfigurationEntry entry : entries) {
        if (StringUtils.containsIgnoreCase(entry.getLoginModuleName(), "ldap")) {
            Map<String, String> entryMap = (Map<String, String>) entry.getOptions();
            for (String entryKey : entryMap.keySet()) {
                ldapContextParams.put(entryKey, entryMap.get(entryKey));
            }
        }
    }
    return ldapContextParams;
}

From source file:org.apache.activemq.artemis.spi.core.security.jaas.PropertiesLoginModuleConfigurator.java

public PropertiesLoginModuleConfigurator(String entryName, String brokerEtc) throws Exception {
    if (entryName == null || entryName.length() == 0) {
        entryName = "activemq";
    }//from  ww  w . j  a  v  a2  s.c  o  m

    Configuration securityConfig = Configuration.getConfiguration();
    AppConfigurationEntry[] entries = securityConfig.getAppConfigurationEntry(entryName);

    if (entries == null || entries.length == 0) {
        throw ActiveMQMessageBundle.BUNDLE.failedToLoadSecurityConfig();
    }

    int entriesInspected = 0;
    for (AppConfigurationEntry entry : entries) {
        entriesInspected++;
        if (entry.getLoginModuleName().equals(PropertiesLoginModule.class.getName())) {
            String userFileName = (String) entry.getOptions().get(USER_FILE_PROP_NAME);
            String roleFileName = (String) entry.getOptions().get(ROLE_FILE_PROP_NAME);

            File etcDir = new File(brokerEtc);
            File userFile = new File(etcDir, userFileName);
            File roleFile = new File(etcDir, roleFileName);

            if (!userFile.exists()) {
                throw ActiveMQMessageBundle.BUNDLE.failedToLoadUserFile(brokerEtc + userFileName);
            }

            if (!roleFile.exists()) {
                throw ActiveMQMessageBundle.BUNDLE.failedToLoadRoleFile(brokerEtc + roleFileName);
            }

            Configurations configs = new Configurations();
            userBuilder = configs.propertiesBuilder(userFile);
            roleBuilder = configs.propertiesBuilder(roleFile);
            userConfig = userBuilder.getConfiguration();
            roleConfig = roleBuilder.getConfiguration();

            String roleHeader = roleConfig.getLayout().getHeaderComment();
            String userHeader = userConfig.getLayout().getHeaderComment();

            if (userHeader == null) {
                if (userConfig.isEmpty()) {
                    //clean and reset header
                    userConfig.clear();
                    userConfig.setHeader(LICENSE_HEADER);
                }
            }

            if (roleHeader == null) {
                if (roleConfig.isEmpty()) {
                    //clean and reset header
                    roleConfig.clear();
                    roleConfig.setHeader(LICENSE_HEADER);
                }
            }
            return;
        }
    }

    if (entriesInspected == entries.length) {
        throw ActiveMQMessageBundle.BUNDLE.failedToFindLoginModuleEntry(entryName);
    }
}

From source file:org.apache.phoenix.mapreduce.index.automation.PhoenixMRJobSubmitter.java

private void enableKeyTabSecurity() throws IOException {

    final String PRINCIPAL = "principal";
    final String KEYTAB = "keyTab";
    // Login with the credentials from the keytab to retrieve the TGT . The
    // renewal of the TGT happens in a Zookeeper thread
    String principal = null;//from w w w .ja v  a2s.  c  om
    String keyTabPath = null;
    AppConfigurationEntry entries[] = javax.security.auth.login.Configuration.getConfiguration()
            .getAppConfigurationEntry("Client");
    LOG.info("Security - Fetched App Login Configuration Entries");
    if (entries != null) {
        for (AppConfigurationEntry entry : entries) {
            if (entry.getOptions().get(PRINCIPAL) != null) {
                principal = (String) entry.getOptions().get(PRINCIPAL);
            }
            if (entry.getOptions().get(KEYTAB) != null) {
                keyTabPath = (String) entry.getOptions().get(KEYTAB);
            }
        }
        LOG.info("Security - Got Principal = " + principal + "");
        if (principal != null && keyTabPath != null) {
            LOG.info("Security - Retreiving the TGT with principal:" + principal + " and keytab:" + keyTabPath);
            UserGroupInformation.loginUserFromKeytab(principal, keyTabPath);
            LOG.info("Security - Retrieved TGT with principal:" + principal + " and keytab:" + keyTabPath);
        }
    }
}

From source file:org.apache.ranger.audit.provider.MiscUtil.java

public static void authWithConfig(String appName, Configuration config) {
    try {/*from  w  w w  .j  a va2s  .c  o m*/
        if (config != null) {
            logger.info(
                    "Getting AppConfigrationEntry[] for appName=" + appName + ", config=" + config.toString());
            AppConfigurationEntry[] entries = config.getAppConfigurationEntry(appName);
            if (entries != null) {
                logger.info("Got " + entries.length + "  AppConfigrationEntry elements for appName=" + appName);
                for (AppConfigurationEntry appEntry : entries) {
                    logger.info("APP_ENTRY:getLoginModuleName()=" + appEntry.getLoginModuleName());
                    logger.info("APP_ENTRY:getControlFlag()=" + appEntry.getControlFlag());
                    logger.info("APP_ENTRY.getOptions()=" + appEntry.getOptions());
                }
            }

            LoginContext loginContext = new LoginContext(appName, new Subject(), null, config);
            logger.info("Login in for appName=" + appName);
            loginContext.login();
            logger.info("Principals after login=" + loginContext.getSubject().getPrincipals());
            logger.info("UserGroupInformation.loginUserFromSubject(): appName=" + appName + ", principals="
                    + loginContext.getSubject().getPrincipals());

            UserGroupInformation ugi = MiscUtil.createUGIFromSubject(loginContext.getSubject());
            if (ugi != null) {
                MiscUtil.setUGILoginUser(ugi, loginContext.getSubject());
            }

            // UserGroupInformation.loginUserFromSubject(loginContext
            // .getSubject());
            logger.info("POST UserGroupInformation.loginUserFromSubject UGI="
                    + UserGroupInformation.getLoginUser());
        }
    } catch (Throwable t) {
        logger.fatal("Error logging as appName=" + appName + ", config=" + config.toString() + ", error="
                + t.getMessage());
    }
}

From source file:org.apache.storm.security.auth.AuthUtils.java

/**
 * Pull a set of keys out of a Configuration.
 * @param configuration The config to pull the key/value pairs out of.
 * @param section The app configuration entry name to get stuff from.
 * @return Return a map of the configs in conf.
 *///from   www  .  j av a  2 s  .c  om
public static SortedMap<String, ?> pullConfig(Configuration configuration, String section) throws IOException {
    AppConfigurationEntry[] configurationEntries = AuthUtils.getEntries(configuration, section);

    if (configurationEntries == null) {
        return null;
    }

    TreeMap<String, Object> results = new TreeMap<>();

    for (AppConfigurationEntry entry : configurationEntries) {
        Map<String, ?> options = entry.getOptions();
        for (String key : options.keySet()) {
            results.put(key, options.get(key));
        }
    }

    return results;
}

From source file:org.apache.storm.security.auth.AuthUtils.java

/**
 * Pull a the value given section and key from Configuration
 * @param configuration The config to pull the key/value pairs out of.
 * @param section The app configuration entry name to get stuff from.
 * @param key The key to look up inside of the section
 * @return Return a the String value of the configuration value
 *//*from   www.  j ava  2 s .  c o  m*/
public static String get(Configuration configuration, String section, String key) throws IOException {
    AppConfigurationEntry[] configurationEntries = AuthUtils.getEntries(configuration, section);

    if (configurationEntries == null) {
        return null;
    }

    for (AppConfigurationEntry entry : configurationEntries) {
        Object val = entry.getOptions().get(key);
        if (val != null)
            return (String) val;
    }
    return null;
}

From source file:org.apache.storm.security.auth.AuthUtilsTest.java

@Test
public void getNonExistentSectionTest() throws IOException {
    Map<String, String> optionMap = new HashMap<String, String>();
    AppConfigurationEntry entry = Mockito.mock(AppConfigurationEntry.class);

    Mockito.<Map<String, ?>>when(entry.getOptions()).thenReturn(optionMap);
    String section = "bogus-section";
    Configuration mockConfig = Mockito.mock(Configuration.class);
    Mockito.when(mockConfig.getAppConfigurationEntry(section))
            .thenReturn(new AppConfigurationEntry[] { entry });
    Assert.assertNull(AuthUtils.get(mockConfig, section, "nonexistent-key"));
}

From source file:org.apache.storm.security.auth.AuthUtilsTest.java

@Test
public void getFirstValueForValidKeyTest() throws IOException {
    String k = "the-key";
    String expected = "good-value";

    Map<String, String> optionMap = new HashMap<String, String>();
    optionMap.put(k, expected);//from  w  w w  .  j  av  a2 s .  c  om

    Map<String, String> badOptionMap = new HashMap<String, String>();
    badOptionMap.put(k, "bad-value");

    AppConfigurationEntry emptyEntry = Mockito.mock(AppConfigurationEntry.class);
    AppConfigurationEntry badEntry = Mockito.mock(AppConfigurationEntry.class);
    AppConfigurationEntry goodEntry = Mockito.mock(AppConfigurationEntry.class);

    Mockito.<Map<String, ?>>when(emptyEntry.getOptions()).thenReturn(new HashMap<String, String>());
    Mockito.<Map<String, ?>>when(badEntry.getOptions()).thenReturn(badOptionMap);
    Mockito.<Map<String, ?>>when(goodEntry.getOptions()).thenReturn(optionMap);

    String section = "bogus-section";
    Configuration mockConfig = Mockito.mock(Configuration.class);
    Mockito.when(mockConfig.getAppConfigurationEntry(section))
            .thenReturn(new AppConfigurationEntry[] { emptyEntry, goodEntry, badEntry });

    Assert.assertEquals(AuthUtils.get(mockConfig, section, k), expected);
}

From source file:org.apache.storm.security.auth.AuthUtilsTest.java

@Test
public void makeDigestPayloadTest() throws NoSuchAlgorithmException {
    String section = "user-pass-section";
    Map<String, String> optionMap = new HashMap<String, String>();
    String user = "user";
    String pass = "pass";
    optionMap.put("username", user);
    optionMap.put("password", pass);
    AppConfigurationEntry entry = Mockito.mock(AppConfigurationEntry.class);

    Mockito.<Map<String, ?>>when(entry.getOptions()).thenReturn(optionMap);
    Configuration mockConfig = Mockito.mock(Configuration.class);
    Mockito.when(mockConfig.getAppConfigurationEntry(section))
            .thenReturn(new AppConfigurationEntry[] { entry });

    MessageDigest digest = MessageDigest.getInstance("SHA-512");
    byte[] output = digest.digest((user + ":" + pass).getBytes());
    String sha = Hex.encodeHexString(output);

    // previous code used this method to generate the string, ensure the two match
    StringBuilder builder = new StringBuilder();
    for (byte b : output) {
        builder.append(String.format("%02x", b));
    }//from w w  w .jav  a2s . c o m
    String stringFormatMethod = builder.toString();

    Assert.assertEquals(AuthUtils.makeDigestPayload(mockConfig, "user-pass-section"), sha);

    Assert.assertEquals(sha, stringFormatMethod);
}

From source file:org.apache.storm.security.auth.ClientAuthUtils.java

/**
 * Pull a set of keys out of a Configuration.
 *
 * @param configuration The config to pull the key/value pairs out of.
 * @param section       The app configuration entry name to get stuff from.
 * @return Return a map of the configs in conf.
 *//*from w  w  w.  ja v a 2  s.  co m*/
public static SortedMap<String, ?> pullConfig(Configuration configuration, String section) throws IOException {
    AppConfigurationEntry[] configurationEntries = ClientAuthUtils.getEntries(configuration, section);

    if (configurationEntries == null) {
        return null;
    }

    TreeMap<String, Object> results = new TreeMap<>();

    for (AppConfigurationEntry entry : configurationEntries) {
        Map<String, ?> options = entry.getOptions();
        for (String key : options.keySet()) {
            results.put(key, options.get(key));
        }
    }

    return results;
}