Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infers common credential helper names from target registry. #64

Merged
merged 12 commits into from
Feb 8, 2018
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,20 @@ 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";
}
// TODO: Add more common credential helpers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this TODO seems unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly as a note to add more inferences such as Azure's -acr-* credential helpers. Is it standard practice to not have a note like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, everyone uses them differently. I would think it's better to file a bug than leave this here, I tend to lose track of TODOs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #79

return null;
}

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

Expand Down Expand Up @@ -101,6 +115,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