Skip to content

Commit

Permalink
Infers common credential helper names from target registry. (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
coollog authored Feb 8, 2018
1 parent 98cb00b commit d6d9467
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ All notable changes to this project will be documented in this file.
## 0.1.1
### Added
- Simple example `helloworld` project under `examples/` ([#62](https://github.com/google/jib/pull/62))
- Infers common credential helper names (for GCR and ECR) ([#64](https://github.com/google/jib/pull/64))
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@ Configure the plugin by changing `registry`, `repository`, and `credentialHelper

#### I am using Google Container Registry (GCR)

*Make sure you have the [`docker-credential-gcr` command line tool](https://cloud.google.com/container-registry/docs/advanced-authentication#docker_credential_helper).*
*Make sure you have the [`docker-credential-gcr` command line tool](https://cloud.google.com/container-registry/docs/advanced-authentication#docker_credential_helper). Jib automatically uses `docker-credential-gcr` for obtaining credentials. To use a different credential helper, set the [`credentialHelperName`](#extended-usage) configuration.*

For example, to build the image `gcr.io/my-gcp-project/my-app`, the configuration would be:

```xml
<configuration>
<registry>gcr.io</registry>
<repository>my-gcp-project/my-app</repository>
<credentialHelperName>gcr</credentialHelperName>
</configuration>
```

#### I am using Amazon Elastic Container Registry (ECR)

*Make sure you have the [`docker-credential-ecr-login` command line tool](https://github.com/awslabs/amazon-ecr-credential-helper).*
*Make sure you have the [`docker-credential-ecr-login` command line tool](https://github.com/awslabs/amazon-ecr-credential-helper). Jib automatically uses `docker-credential-ecr-login` for obtaining credentials. To use a different credential helper, set the [`credentialHelperName`](#extended-usage) configuration.*

For example, to build the image `aws_account_id.dkr.ecr.region.amazonaws.com/my-app`, the configuration would be:

```xml
<configuration>
<registry>aws_account_id.dkr.ecr.region.amazonaws.com</registry>
<repository>my-app</repository>
<credentialHelperName>ecr-login</credentialHelperName>
</configuration>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public class BuildImageMojo extends AbstractMojo {
/** {@code User-Agent} header suffix to send to the registry. */
private static final String USER_AGENT_SUFFIX = "jib-maven-plugin";

/** Attempts to infer a known credential helper name from a specified registry. */
@VisibleForTesting
@Nullable
static String inferCredentialHelperName(String registry) {
if (registry.endsWith("gcr.io")) {
return "gcr";

} else if (registry.endsWith("amazonaws.com")) {
return "ecr-login";
}
return null;
}

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

Expand Down Expand Up @@ -101,6 +114,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// Parse 'from' into image reference.
ImageReference baseImage = getImageReference();

// Infer common credential helper names if credentialHelperName is not set.
if (credentialHelperName == null) {
credentialHelperName = inferCredentialHelperName(registry);
if (credentialHelperName != null) {
getLog()
.info(
"Using docker-credential-"
+ credentialHelperName
+ " for authentication - specify a 'credentialHelperName' to override");
}
}

BuildConfiguration buildConfiguration =
BuildConfiguration.builder()
.setBuildLogger(new MavenBuildLogger(getLog()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public class BuildImageMojoTest {

private final BuildImageMojo testBuildImageMojo = new BuildImageMojo();

@Test
public void testInferCredentialHelperName() {
Assert.assertEquals("gcr", BuildImageMojo.inferCredentialHelperName("gcr.io"));
Assert.assertEquals("gcr", BuildImageMojo.inferCredentialHelperName("asia.gcr.io"));
Assert.assertEquals("ecr-login", BuildImageMojo.inferCredentialHelperName("amazonaws.com"));
Assert.assertEquals(
"ecr-login",
BuildImageMojo.inferCredentialHelperName("aws_account_id.dkr.ecr.region.amazonaws.com"));
Assert.assertNull(BuildImageMojo.inferCredentialHelperName("localhost"));
}

@Test
public void testBuildImage_pass() throws MojoExecutionException {
testBuildImageMojo.buildImage(mockBuildImageSteps);
Expand Down

0 comments on commit d6d9467

Please sign in to comment.