Search in sources :

Example 16 with AuthConfig

use of io.fabric8.docker.api.model.AuthConfig in project docker-maven-plugin by fabric8io.

the class AuthConfigTest method mapConstructor.

@Test
public void mapConstructor() {
    AuthConfig config = new AuthConfig("roland", "secret", "roland@jolokia.org", null);
    check(config);
}
Also used : AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Test(org.junit.Test)

Example 17 with AuthConfig

use of io.fabric8.docker.api.model.AuthConfig in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method createStandardAuthConfig.

/**
 * Create an authentication config object which can be used for communication with a Docker registry
 *
 * The authentication information is looked up at various places (in this order):
 *
 * <ul>
 *    <li>From system properties</li>
 *    <li>From the provided map which can contain key-value pairs</li>
 *    <li>From the openshift settings in ~/.config/kube</li>
 *    <li>From the Maven settings stored typically in ~/.m2/settings.xml</li>
 * </ul>
 *
 * The following properties (prefix with 'docker.') and config key are evaluated:
 *
 * <ul>
 *     <li>username: User to authenticate</li>
 *     <li>password: Password to authenticate. Can be encrypted</li>
 *     <li>email: Optional EMail address which is send to the registry, too</li>
 * </ul>
 *
 * @param isPush if true this AuthConfig is created for a push, if false it's for a pull
 * @param authConfigMap String-String Map holding configuration info from the plugin's configuration. Can be <code>null</code> in
 *                   which case the settings are consulted.
 * @param settings the global Maven settings object
 * @param user user to check for
 * @param registry registry to use, might be null in which case a default registry is checked,
 * @return the authentication configuration or <code>null</code> if none could be found
 *
 * @throws MojoFailureException
 */
private AuthConfig createStandardAuthConfig(boolean isPush, Map authConfigMap, Settings settings, String user, String registry) throws MojoExecutionException {
    AuthConfig ret;
    // Check first for specific configuration based on direction (pull or push), then for a default value
    for (LookupMode lookupMode : new LookupMode[] { getLookupMode(isPush), LookupMode.DEFAULT }) {
        // System properties docker.username and docker.password always take precedence
        ret = getAuthConfigFromSystemProperties(lookupMode);
        if (ret != null) {
            log.debug("AuthConfig: credentials from system properties");
            return ret;
        }
        // Check for openshift authentication either from the plugin config or from system props
        ret = getAuthConfigFromOpenShiftConfig(lookupMode, authConfigMap);
        if (ret != null) {
            log.debug("AuthConfig: OpenShift credentials");
            return ret;
        }
        // Get configuration from global plugin config
        ret = getAuthConfigFromPluginConfiguration(lookupMode, authConfigMap);
        if (ret != null) {
            log.debug("AuthConfig: credentials from plugin config");
            return ret;
        }
    }
    // ===================================================================
    // These are lookups based on registry only, so the direction (push or pull) doesn't matter:
    // Now lets lookup the registry & user from ~/.m2/setting.xml
    ret = getAuthConfigFromSettings(settings, user, registry);
    if (ret != null) {
        log.debug("AuthConfig: credentials from ~/.m2/setting.xml");
        return ret;
    }
    // No authentication found
    return null;
}
Also used : AuthConfig(io.fabric8.maven.docker.access.AuthConfig)

Example 18 with AuthConfig

use of io.fabric8.docker.api.model.AuthConfig in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method getAuthConfigFromOpenShiftConfig.

private AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map authConfigMap) throws MojoExecutionException {
    Properties props = System.getProperties();
    String useOpenAuthModeProp = lookupMode.asSysProperty(AUTH_USE_OPENSHIFT_AUTH);
    // Check for system property
    if (props.containsKey(useOpenAuthModeProp)) {
        boolean useOpenShift = Boolean.valueOf(props.getProperty(useOpenAuthModeProp));
        if (useOpenShift) {
            AuthConfig ret = parseOpenShiftConfig();
            if (ret == null) {
                throw new MojoExecutionException("System property " + useOpenAuthModeProp + " " + "set, but not active user and/or token found in ~/.config/kube. " + "Please use 'oc login' for connecting to OpenShift.");
            }
            return ret;
        } else {
            return null;
        }
    }
    // Check plugin config
    Map mapToCheck = getAuthConfigMapToCheck(lookupMode, authConfigMap);
    if (mapToCheck != null && mapToCheck.containsKey(AUTH_USE_OPENSHIFT_AUTH) && Boolean.valueOf((String) mapToCheck.get(AUTH_USE_OPENSHIFT_AUTH))) {
        AuthConfig ret = parseOpenShiftConfig();
        if (ret == null) {
            throw new MojoExecutionException("Authentication configured for OpenShift, but no active user and/or " + "token found in ~/.config/kube. Please use 'oc login' for " + "connecting to OpenShift.");
        }
        return ret;
    } else {
        return null;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with AuthConfig

use of io.fabric8.docker.api.model.AuthConfig in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method getAuthConfigFromPluginConfiguration.

private AuthConfig getAuthConfigFromPluginConfiguration(LookupMode lookupMode, Map authConfig) throws MojoExecutionException {
    Map mapToCheck = getAuthConfigMapToCheck(lookupMode, authConfig);
    if (mapToCheck != null && mapToCheck.containsKey(AUTH_USERNAME)) {
        if (!mapToCheck.containsKey(AUTH_PASSWORD)) {
            throw new MojoExecutionException("No 'password' given while using <authConfig> in configuration for mode " + lookupMode);
        }
        Map<String, String> cloneConfig = new HashMap<>(mapToCheck);
        cloneConfig.put(AUTH_PASSWORD, decrypt(cloneConfig.get(AUTH_PASSWORD)));
        return new AuthConfig(cloneConfig);
    } else {
        return null;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with AuthConfig

use of io.fabric8.docker.api.model.AuthConfig in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method extractAuthConfigFromAuths.

private AuthConfig extractAuthConfigFromAuths(String registryToLookup, JSONObject auths) {
    JSONObject credentials = getCredentialsNode(auths, registryToLookup);
    if (credentials == null || !credentials.has("auth")) {
        return null;
    }
    String auth = credentials.getString("auth");
    String email = credentials.has("email") ? credentials.getString("email") : null;
    return new AuthConfig(auth, email);
}
Also used : JSONObject(org.json.JSONObject) AuthConfig(io.fabric8.maven.docker.access.AuthConfig)

Aggregations

AuthConfig (io.fabric8.maven.docker.access.AuthConfig)22 Test (org.junit.Test)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 Date (java.util.Date)3 Collections.singletonMap (java.util.Collections.singletonMap)2 Properties (java.util.Properties)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2 JSONObject (org.json.JSONObject)2 AuthConfig (io.fabric8.docker.api.model.AuthConfig)1 AuthConfigBuilder (io.fabric8.docker.api.model.AuthConfigBuilder)1 Config (io.fabric8.docker.client.Config)1 ConfigBuilder (io.fabric8.docker.client.ConfigBuilder)1 DefaultDockerClient (io.fabric8.docker.client.DefaultDockerClient)1 DockerClient (io.fabric8.docker.client.DockerClient)1 EventListener (io.fabric8.docker.dsl.EventListener)1 OutputHandle (io.fabric8.docker.dsl.OutputHandle)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1