Search in sources :

Example 1 with CredentialStore

use of org.wildfly.security.credential.store.CredentialStore in project fuse-karaf by jboss-fuse.

the class RemoveFromCredentialStore method execute.

@Override
public Object execute() throws Exception {
    final CredentialStore credentialStore = CredentialStoreHelper.credentialStoreFromEnvironment();
    credentialStore.remove(alias, Credential.class);
    credentialStore.flush();
    return null;
}
Also used : CredentialStore(org.wildfly.security.credential.store.CredentialStore)

Example 2 with CredentialStore

use of org.wildfly.security.credential.store.CredentialStore in project fuse-karaf by jboss-fuse.

the class CredentialStoreHelper method credentialStoreFromEnvironment.

/**
 * Creates the credential store specified by the configuration in environment variables.
 *
 * @return the credential store
 * @throws GeneralSecurityException
 * @throws IOException
 */
public static CredentialStore credentialStoreFromEnvironment() throws GeneralSecurityException, IOException {
    final String credentialStoreAlgorithm = Optional.ofNullable(getenv(CREDENTIAL_STORE_ALGORITHM_ENV)).orElse(Defaults.CREDENTIAL_STORE_ALGORITHM);
    final Provider provider = ProviderHelper.provider(Optional.ofNullable(getenv(CREDENTIAL_STORE_PROVIDER_ENV)).orElse(ProviderHelper.WILDFLY_PROVIDER));
    final CredentialStore credentialStore = CredentialStore.getInstance(credentialStoreAlgorithm, provider);
    final Map<String, String> attributes = defaultCredentialStoreAttributesFor(credentialStoreAlgorithm);
    attributes.putAll(attributesFromEnvironment(CREDENTIAL_STORE_ATTR_ENV_PREFIX));
    final ProtectionParameter protectionParameter = createProtectionParameterFromEnvironment();
    credentialStore.initialize(attributes, protectionParameter);
    return credentialStore;
}
Also used : CredentialStore(org.wildfly.security.credential.store.CredentialStore) KeyStoreCredentialStore(org.wildfly.security.credential.store.impl.KeyStoreCredentialStore) Provider(java.security.Provider) ProtectionParameter(org.wildfly.security.credential.store.CredentialStore.ProtectionParameter)

Example 3 with CredentialStore

use of org.wildfly.security.credential.store.CredentialStore in project fuse-karaf by jboss-fuse.

the class CredentialStoreHelperTest method accessCredentialStore.

@Test
public void accessCredentialStore() throws Exception {
    Security.addProvider(new WildFlyElytronProvider());
    // KeyStoreCredentialStore is default algorithm when using
    // org.jboss.fuse.credential.store.karaf.util.CredentialStoreHelper.credentialStoreFromEnvironment()
    // it's a credential store which is backed by a key store
    CredentialStore cs1 = CredentialStore.getInstance("KeyStoreCredentialStore");
    // Credential store implementation which uses the legacy "vault" format
    CredentialStore cs2 = CredentialStore.getInstance("VaultCredentialStore");
    // map-backed credential store implementation
    CredentialStore cs3 = CredentialStore.getInstance("MapCredentialStore");
    LOG.info("Credential Store 1: {}, aliases: {}", cs1, cs1.getAliases());
    LOG.info("Credential Store 2: {}, aliases: {}", cs2, /*cs2.getAliases()*/
    null);
    LOG.info("Credential Store 3: {}, aliases: {}", cs3, cs3.getAliases());
    // KeyStoreCredentialStore uses 3 parameters/attributes
    // - location
    // - modifiable
    // - keyStoreType
    // CHECKSTYLE:OFF
    // from $JAVA_HOME/jre/lib/security/java.security, keystore.type
    LOG.info("Default KeyStore type: {}", KeyStore.getDefaultType());
    LOG.info("KeyStore providers / algorithms:");
    for (Provider p : Providers.getProviderList().providers()) {
        for (Provider.Service s : p.getServices()) {
            if ("KeyStore".equals(s.getType())) {
                LOG.info(" - {} / {}", s.getProvider().getName(), s.getAlgorithm());
            }
        }
    }
    LOG.info("PasswordFactory providers / algorithms:");
    for (Provider p : Providers.getProviderList().providers()) {
        for (Provider.Service s : p.getServices()) {
            if ("PasswordFactory".equals(s.getType())) {
                LOG.info(" - {} / {}", s.getProvider().getName(), s.getAlgorithm());
            }
        }
    }
    LOG.info("SecretKeyFactory providers / algorithms:");
    for (Provider p : Providers.getProviderList().providers()) {
        for (Provider.Service s : p.getServices()) {
            if ("SecretKeyFactory".equals(s.getType())) {
                LOG.info(" - {} / {}", s.getProvider().getName(), s.getAlgorithm());
            }
        }
    }
    LOG.info("Cipher providers / algorithms:");
    for (Provider p : Providers.getProviderList().providers()) {
        for (Provider.Service s : p.getServices()) {
            if ("Cipher".equals(s.getType())) {
                LOG.info(" - {} / {}", s.getProvider().getName(), s.getAlgorithm());
            }
        }
    }
    // CHECKSTYLE:ON
    Password pwd1 = PasswordFactory.getInstance("clear").generatePassword(new ClearPasswordSpec("secret1".toCharArray()));
    Password pwd2 = PasswordFactory.getInstance("clear").generatePassword(new ClearPasswordSpec("secret2".toCharArray()));
    CredentialSource cs = IdentityCredentials.NONE.withCredential(new PasswordCredential(pwd1));
    CredentialStore.ProtectionParameter pp = new CredentialStore.CredentialSourceProtectionParameter(cs);
    Map<String, String> attrs = new HashMap<>();
    attrs.put("keyStoreType", "PKCS12");
    attrs.put("location", String.format("target/credentials-%12d.store", new Date().getTime()));
    cs1.initialize(attrs, pp);
    cs1.store("alias1", new PasswordCredential(pwd2));
    cs1.flush();
    LOG.info("Credential Store 1: {}, aliases: {}", cs1, cs1.getAliases());
    PasswordCredential pwd = cs1.retrieve("alias1", PasswordCredential.class);
    LOG.info("Retrieved password: {}", new String(((ClearPassword) pwd.getPassword()).getPassword()));
}
Also used : ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) HashMap(java.util.HashMap) PasswordCredential(org.wildfly.security.credential.PasswordCredential) ClearPasswordSpec(org.wildfly.security.password.spec.ClearPasswordSpec) WildFlyElytronProvider(org.wildfly.security.WildFlyElytronProvider) Date(java.util.Date) WildFlyElytronProvider(org.wildfly.security.WildFlyElytronProvider) Provider(java.security.Provider) CredentialStore(org.wildfly.security.credential.store.CredentialStore) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) CredentialSource(org.wildfly.security.credential.source.CredentialSource) Test(org.junit.Test)

Example 4 with CredentialStore

use of org.wildfly.security.credential.store.CredentialStore in project fuse-karaf by jboss-fuse.

the class ActivatorTest method initializeCredentialStore.

@Before
public void initializeCredentialStore() throws Exception {
    activator.start(null);
    final WildFlyElytronProvider elytron = new WildFlyElytronProvider();
    Security.addProvider(elytron);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR, elytron);
    final Password password = passwordFactory.generatePassword(new ClearPasswordSpec("it was the best of times it was the worst of times".toCharArray()));
    final Credential credential = new PasswordCredential(password);
    final CredentialSource credentialSource = IdentityCredentials.NONE.withCredential(credential);
    credentialStore = CredentialStore.getInstance(KeyStoreCredentialStore.KEY_STORE_CREDENTIAL_STORE, elytron);
    final String storePath = new File(tmp.getRoot(), "credential.store").getAbsolutePath();
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("location", storePath);
    parameters.put("keyStoreType", "JCEKS");
    credentialStore.initialize(parameters, new CredentialStore.CredentialSourceProtectionParameter(credentialSource));
    final Password secret = passwordFactory.generatePassword(new ClearPasswordSpec("this is a password".toCharArray()));
    final Credential value = new PasswordCredential(secret);
    credentialStore.store("alias", value);
    credentialStore.flush();
}
Also used : PasswordCredential(org.wildfly.security.credential.PasswordCredential) Credential(org.wildfly.security.credential.Credential) HashMap(java.util.HashMap) PasswordCredential(org.wildfly.security.credential.PasswordCredential) ClearPasswordSpec(org.wildfly.security.password.spec.ClearPasswordSpec) WildFlyElytronProvider(org.wildfly.security.WildFlyElytronProvider) PasswordFactory(org.wildfly.security.password.PasswordFactory) CredentialStore(org.wildfly.security.credential.store.CredentialStore) KeyStoreCredentialStore(org.wildfly.security.credential.store.impl.KeyStoreCredentialStore) File(java.io.File) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) CredentialSource(org.wildfly.security.credential.source.CredentialSource) Before(org.junit.Before)

Example 5 with CredentialStore

use of org.wildfly.security.credential.store.CredentialStore in project fuse-karaf by jboss-fuse.

the class Activator method start.

/**
 * If there are any Credential store references as values in the system properties, adds
 * {@link WildFlyElytronProvider} to {@link Security} providers, replaces those values with the values from the
 * Credential store and installs the JMX filter to prevent the clear text value leakage.
 *
 * @param context
 *            OSGI bundle context
 */
@Override
public void start(final BundleContext context) throws Exception {
    this.context = context;
    final WildFlyElytronProvider elytronProvider = new WildFlyElytronProvider();
    providerName = elytronProvider.getName();
    Security.addProvider(elytronProvider);
    final Properties properties = System.getProperties();
    @SuppressWarnings("unchecked") final Collection<String> values = (Collection) properties.values();
    final boolean hasValuesFromCredentialStore = CredentialStoreHelper.containsStoreReferences(values);
    if (!hasValuesFromCredentialStore) {
        return;
    }
    CredentialStore credentialStore;
    try {
        credentialStore = CredentialStoreHelper.credentialStoreFromEnvironment();
    } catch (final Exception e) {
        final String message = e.getMessage();
        System.err.println("\r\nUnable to initialize credential store, destroying container: " + message);
        LOG.error("Unable to initialize credential store, destroying container: {}", message);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Logging exception stack trace", e);
        }
        final Bundle frameworkBundle = context.getBundle(0);
        frameworkBundle.stop();
        return;
    }
    @SuppressWarnings("unchecked") final Hashtable<String, String> propertiesAsStringEntries = (Hashtable) properties;
    for (final Entry<String, String> property : propertiesAsStringEntries.entrySet()) {
        final String key = property.getKey();
        final String value = property.getValue();
        if (replaced(credentialStore, key, value)) {
            replacedProperties.put(key, value);
        }
    }
    if (!replacedProperties.isEmpty()) {
        mbeanServerTracker = new ServiceTracker<>(context, MBeanServer.class, this);
        mbeanServerTracker.open();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Properties(java.util.Properties) WildFlyElytronProvider(org.wildfly.security.WildFlyElytronProvider) JMException(javax.management.JMException) CredentialStoreException(org.wildfly.security.credential.store.CredentialStoreException) CredentialStore(org.wildfly.security.credential.store.CredentialStore) Collection(java.util.Collection) MBeanServer(javax.management.MBeanServer)

Aggregations

CredentialStore (org.wildfly.security.credential.store.CredentialStore)9 PasswordCredential (org.wildfly.security.credential.PasswordCredential)4 Password (org.wildfly.security.password.Password)4 WildFlyElytronProvider (org.wildfly.security.WildFlyElytronProvider)3 ClearPassword (org.wildfly.security.password.interfaces.ClearPassword)3 ClearPasswordSpec (org.wildfly.security.password.spec.ClearPasswordSpec)3 Provider (java.security.Provider)2 HashMap (java.util.HashMap)2 CredentialSource (org.wildfly.security.credential.source.CredentialSource)2 CredentialStoreException (org.wildfly.security.credential.store.CredentialStoreException)2 KeyStoreCredentialStore (org.wildfly.security.credential.store.impl.KeyStoreCredentialStore)2 PasswordFactory (org.wildfly.security.password.PasswordFactory)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 JMException (javax.management.JMException)1 MBeanServer (javax.management.MBeanServer)1