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
Prev Previous commit
Next Next commit
Infers common credential helper names from target registry.
coollog committed Feb 6, 2018
commit 77e3078fff4662fbc49f157bba2225163e283e45
Original file line number Diff line number Diff line change
@@ -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;

@@ -103,16 +117,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// Infer common credential helper names if credentialHelperName is not set.
if (credentialHelperName == null) {
if (registry.endsWith("gcr.io")) {
credentialHelperName = "gcr";

} else if (registry.endsWith("amazonaws.com")) {
credentialHelperName = "ecr-login";
}
// TODO: Add more common credential helpers.

credentialHelperName = inferCredentialHelperName(registry);
if (credentialHelperName != null) {
getLog().info("Using docker-credential-" + credentialHelperName + " for authentication - specify a 'credentialHelperName' to override");
getLog()
.info(
"Using docker-credential-"
+ credentialHelperName
+ " for authentication - specify a 'credentialHelperName' to override");
}
}

Original file line number Diff line number Diff line change
@@ -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);