Search in sources :

Example 71 with User

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

the class AuthConfigFactory method parseUser.

private AuthConfig parseUser(String userName, Map user) {
    if (user == null) {
        return null;
    }
    String token = (String) user.get("token");
    if (token == null) {
        return null;
    }
    // Strip off stuff after username
    Matcher matcher = Pattern.compile("^([^/]+).*$").matcher(userName);
    return new AuthConfig(matcher.matches() ? matcher.group(1) : userName, token, null, null);
}
Also used : Matcher(java.util.regex.Matcher) AuthConfig(io.fabric8.maven.docker.access.AuthConfig)

Example 72 with User

use of io.fabric8.openshift.api.model.User 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.' or 'registry.') 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, LookupMode.REGISTRY }) {
        // 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
        if (lookupMode != LookupMode.REGISTRY) {
            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;
    }
    // check EC2 instance role if registry is ECR
    if (EcrExtendedAuth.isAwsRegistry(registry)) {
        ret = getAuthConfigViaAwsSdk();
        if (ret != null) {
            log.debug("AuthConfig: AWS credentials from AWS SDK");
            return ret;
        }
        ret = getAuthConfigFromAwsEnvironmentVariables();
        if (ret != null) {
            log.debug("AuthConfig: AWS credentials from ENV variables");
            return ret;
        }
        try {
            ret = getAuthConfigFromEC2InstanceRole();
        } catch (ConnectTimeoutException ex) {
            log.debug("Connection timeout while retrieving instance meta-data, likely not an EC2 instance (%s)", ex.getMessage());
        } catch (IOException ex) {
            // don't make that an error since it may fail if not run on an EC2 instance
            log.warn("Error while retrieving EC2 instance credentials: %s", ex.getMessage());
        }
        if (ret != null) {
            log.debug("AuthConfig: credentials from EC2 instance role");
            return ret;
        }
        try {
            ret = getAuthConfigFromTaskRole();
        } catch (ConnectTimeoutException ex) {
            log.debug("Connection timeout while retrieving ECS meta-data, likely not an ECS instance (%s)", ex.getMessage());
        } catch (IOException ex) {
            log.warn("Error while retrieving ECS Task role credentials: %s", ex.getMessage());
        }
        if (ret != null) {
            log.debug("AuthConfig: credentials from ECS Task role");
            return ret;
        }
    }
    // No authentication found
    return null;
}
Also used : AuthConfig(io.fabric8.maven.docker.access.AuthConfig) IOException(java.io.IOException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 73 with User

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

the class JibServiceUtilTest method getSampleImageConfiguration.

private ImageConfiguration getSampleImageConfiguration() {
    Assembly assembly = new Assembly();
    FileItem fileItem = new FileItem();
    fileItem.setSource("${project.basedir}/foo");
    fileItem.setOutputDirectory("/deployments");
    assembly.addFile(fileItem);
    BuildImageConfiguration bc = new BuildImageConfiguration.Builder().from("quay.io/test/testimage:testtag").assembly(new AssemblyConfiguration.Builder().assemblyDef(assembly).build()).entryPoint(new Arguments.Builder().withParam("java").withParam("-jar").withParam("foo.jar").build()).labels(Collections.singletonMap("foo", "bar")).user("root").workdir("/home/foo").ports(Collections.singletonList("8080")).volumes(Collections.singletonList("/mnt/volume1")).build();
    return new ImageConfiguration.Builder().name("test/test-project").buildConfig(bc).build();
}
Also used : FileItem(org.apache.maven.plugins.assembly.model.FileItem) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Arguments(io.fabric8.maven.docker.config.Arguments) Assembly(org.apache.maven.plugins.assembly.model.Assembly) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 74 with User

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

the class AuthConfigFactoryTest method awsTemporaryCredentialsArePickedUpFromEnvironment.

@Test
public void awsTemporaryCredentialsArePickedUpFromEnvironment() throws MojoExecutionException {
    givenAwsSdkIsDisabled();
    String accessKeyId = randomUUID().toString();
    String secretAccessKey = randomUUID().toString();
    String sessionToken = randomUUID().toString();
    environmentVariables.set("AWS_ACCESS_KEY_ID", accessKeyId);
    environmentVariables.set("AWS_SECRET_ACCESS_KEY", secretAccessKey);
    environmentVariables.set("AWS_SESSION_TOKEN", sessionToken);
    AuthConfig authConfig = factory.createAuthConfig(false, true, null, settings, "user", ECR_NAME);
    verifyAuthConfig(authConfig, accessKeyId, secretAccessKey, null, sessionToken);
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Test(org.junit.Test)

Example 75 with User

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

the class AuthConfigFactoryTest method getAuthConfigViaAwsSdk.

@Test
public void getAuthConfigViaAwsSdk() throws MojoExecutionException {
    String accessKeyId = randomUUID().toString();
    String secretAccessKey = randomUUID().toString();
    new MockedAwsSdkAuthConfigFactory(accessKeyId, secretAccessKey);
    AuthConfig authConfig = factory.createAuthConfig(false, true, null, settings, "user", ECR_NAME);
    verifyAuthConfig(authConfig, accessKeyId, secretAccessKey, null, null);
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 File (java.io.File)19 IOException (java.io.IOException)17 HashMap (java.util.HashMap)16 Git (org.eclipse.jgit.api.Git)12 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)10 Map (java.util.Map)10 LinkedList (java.util.LinkedList)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 ObjectId (org.eclipse.jgit.lib.ObjectId)7 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)6 ArrayList (java.util.ArrayList)6 PatchException (io.fabric8.patch.management.PatchException)5 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)4 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)4 URL (java.net.URL)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3