diff --git a/README.md b/README.md index 40334ca8..70820aa1 100644 --- a/README.md +++ b/README.md @@ -46,16 +46,16 @@ There are three levels of configuration needed in order for publishing and fetch | Properties | Description | Mandatory | | -------------------------------| ------------------------------------------------------------------------ | ------------------------ | -| `RegistryURL` | Registry to push and pull images | `yes` | | `RegistryType` | Can be either `ecr` or `other` | `yes` | If `RegistryType` is `ecr`: | Properties | Description | Mandatory | | -------------------------------| ------------------------------------------------------------------------ | ------------------------ | +| `RegistryID` | ECR registry id to push and pull images | `yes` | +| `AWSRegion` | The aws region key to execute ecr get authorization token request | `yes` | | `AWSAccessKeyId` | The aws access key id to execute ecr get authorization token request | `no` | | `AWSSecretAccessKey` | The aws secret access key to execute ecr get authorization token request | `no` | -| `AWSRegion` | The aws region key to execute ecr get authorization token request | `yes` | Note: If the AWSAccessKeyId and AWSSecretAccessKey are not provided, the following configurations are checked: @@ -69,6 +69,7 @@ If `RegistryType` is `other` | Properties | Description | Mandatory | | -------------------------------| ------------------------------------------------------------------------ | ------------------------ | +| `RegistryURL` | Registry to push and pull images | `yes` | | `Username` | Username to authenticate with the docker registry. | `yes` | | `Password` | Password to authenticate with the docker registry. | `yes` | diff --git a/images/artifact_store_ecr.png b/images/artifact_store_ecr.png index 85bff47d..fcf06889 100644 Binary files a/images/artifact_store_ecr.png and b/images/artifact_store_ecr.png differ diff --git a/images/artifact_store_other.png b/images/artifact_store_other.png index cd8661d0..3132c3fd 100644 Binary files a/images/artifact_store_other.png and b/images/artifact_store_other.png differ diff --git a/src/main/java/cd/go/artifact/docker/registry/AWSTokenRequestGenerator.java b/src/main/java/cd/go/artifact/docker/registry/AWSTokenRequestGenerator.java index c6818973..e28aad67 100644 --- a/src/main/java/cd/go/artifact/docker/registry/AWSTokenRequestGenerator.java +++ b/src/main/java/cd/go/artifact/docker/registry/AWSTokenRequestGenerator.java @@ -32,8 +32,8 @@ import static org.apache.commons.lang.StringUtils.isNotBlank; -public class AWSTokenRequestGenerator { - public static final Logger LOG = Logger.getLoggerFor(AWSTokenRequestGenerator.class); +class AWSTokenRequestGenerator { + private static final Logger LOG = Logger.getLoggerFor(AWSTokenRequestGenerator.class); private AwsSyncClientBuilder builder; AWSTokenRequestGenerator() { @@ -51,7 +51,7 @@ AwsSyncClientBuilder getBuilder() { String[] getUsernameAndPasswordFromECRToken(ArtifactStoreConfig artifactStoreConfig) { builder.setRegion(artifactStoreConfig.getAwsRegion()); setCredentialsProvider(artifactStoreConfig); - GetAuthorizationTokenResult authorizationTokenResult = builder.build().getAuthorizationToken(new GetAuthorizationTokenRequest().withRegistryIds(getAwsAccountIdFromURL(artifactStoreConfig.getRegistryUrl()))); + GetAuthorizationTokenResult authorizationTokenResult = builder.build().getAuthorizationToken(new GetAuthorizationTokenRequest().withRegistryIds(artifactStoreConfig.getRegistryId())); String authorizationToken = authorizationTokenResult.getAuthorizationData().get(0).getAuthorizationToken(); return new String(Base64.getDecoder().decode(authorizationToken)).split(":"); } @@ -68,8 +68,4 @@ void setCredentialsProvider(ArtifactStoreConfig artifactStoreConfig) { } } - private String getAwsAccountIdFromURL(String registryUrl) { - return registryUrl.split("//")[1].split("\\.")[0]; - } - } diff --git a/src/main/java/cd/go/artifact/docker/registry/model/ArtifactStoreConfig.java b/src/main/java/cd/go/artifact/docker/registry/model/ArtifactStoreConfig.java index 3f575955..c711a4ba 100644 --- a/src/main/java/cd/go/artifact/docker/registry/model/ArtifactStoreConfig.java +++ b/src/main/java/cd/go/artifact/docker/registry/model/ArtifactStoreConfig.java @@ -25,15 +25,20 @@ import org.apache.commons.lang.StringUtils; public class ArtifactStoreConfig implements Validatable { + @Expose + @SerializedName("RegistryType") + @FieldMetadata(key = "RegistryType", required = true) + private String registryType; + @Expose @SerializedName("RegistryURL") - @FieldMetadata(key = "RegistryURL", required = true) + @FieldMetadata(key = "RegistryURL", required = false) private String registryUrl; @Expose - @SerializedName("RegistryType") - @FieldMetadata(key = "RegistryType", required = true) - private String registryType; + @SerializedName("RegistryID") + @FieldMetadata(key = "RegistryID", required = false) + private String registryId; @Expose @SerializedName("AWSAccessKeyId") @@ -63,19 +68,20 @@ public class ArtifactStoreConfig implements Validatable { public ArtifactStoreConfig() { } - public ArtifactStoreConfig(String registryUrl, String registryType) { - this.registryUrl = registryUrl; + public ArtifactStoreConfig(String registryType) { this.registryType = registryType; } public ArtifactStoreConfig(String registryUrl, String registryType, String username, String password) { - this(registryUrl, registryType); + this(registryType); + this.registryUrl = registryUrl; this.username = username; this.password = password; } - public ArtifactStoreConfig(String registryUrl, String registryType, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion) { - this(registryUrl, registryType); + public ArtifactStoreConfig(String registryId, String registryType, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion) { + this(registryType); + this.registryId = registryId; this.awsAccessKeyId = awsAccessKeyId; this.awsSecretAccessKey = awsSecretAccessKey; this.awsRegion = awsRegion; @@ -109,6 +115,10 @@ public String getAwsRegion() { return awsRegion; } + public String getRegistryId() { + return registryId; + } + public boolean isRegistryTypeEcr() { return "ecr".equals(registryType); } @@ -126,6 +136,7 @@ public boolean equals(Object o) { if (awsAccessKeyId != null ? !awsAccessKeyId.equals(that.awsAccessKeyId) : that.awsAccessKeyId != null) return false; if (awsSecretAccessKey != null ? !awsSecretAccessKey.equals(that.awsSecretAccessKey) : that.awsSecretAccessKey != null) return false; if (awsRegion != null ? !awsRegion.equals(that.awsRegion) : that.awsRegion != null) return false; + if (registryId != null ? !registryId.equals(that.registryId) : that.registryId != null) return false; return password != null ? password.equals(that.password) : that.password == null; } @@ -138,6 +149,7 @@ public int hashCode() { result = 31 * result + (awsAccessKeyId != null ? awsAccessKeyId.hashCode() : 0); result = 31 * result + (awsSecretAccessKey != null ? awsSecretAccessKey.hashCode() : 0); result = 31 * result + (awsRegion != null ? awsRegion.hashCode() : 0); + result = 31 * result + (registryId != null ? registryId.hashCode() : 0); return result; } @@ -149,9 +161,6 @@ public static ArtifactStoreConfig fromJSON(String json) { @Override public ValidationResult validate() { ValidationResult validationResult = new ValidationResult(); - if (StringUtils.isBlank(registryUrl)) { - validationResult.addError("RegistryURL", "RegistryURL must not be blank."); - } if (StringUtils.isBlank(registryType)) { validationResult.addError("RegistryType", "RegistryType must not be blank."); } @@ -165,11 +174,17 @@ else if (!"other".equals(registryType) && !"ecr".equals(registryType)) { if(StringUtils.isBlank(password)) { validationResult.addError("Password", "Password must not be blank."); } + if (StringUtils.isBlank(registryUrl)) { + validationResult.addError("RegistryURL", "RegistryURL must not be blank."); + } } if ("ecr".equals(registryType)) { if(StringUtils.isBlank(awsRegion)) { validationResult.addError("AWSRegion", "AWSRegion must not be blank."); } + if (StringUtils.isBlank(registryId)) { + validationResult.addError("RegistryID", "RegistryID must not be blank."); + } } return validationResult; } diff --git a/src/main/resources/artifact-store.template.html b/src/main/resources/artifact-store.template.html index 59798b8e..f6863b8b 100644 --- a/src/main/resources/artifact-store.template.html +++ b/src/main/resources/artifact-store.template.html @@ -1,10 +1,4 @@ -
- - - {{GOINPUTNAME[RegistryURL].$error.server}} -
- -
+
@@ -15,6 +9,12 @@
+
+ + + {{GOINPUTNAME[RegistryID].$error.server}} +
+
@@ -35,6 +35,13 @@
+ +
+ + + {{GOINPUTNAME[RegistryURL].$error.server}} +
+
diff --git a/src/test/java/cd/go/artifact/docker/registry/RegistryAuthSupplierChainTest.java b/src/test/java/cd/go/artifact/docker/registry/RegistryAuthSupplierChainTest.java index 784091fe..95ea255f 100644 --- a/src/test/java/cd/go/artifact/docker/registry/RegistryAuthSupplierChainTest.java +++ b/src/test/java/cd/go/artifact/docker/registry/RegistryAuthSupplierChainTest.java @@ -76,7 +76,7 @@ public void shouldSetUsernameAndPasswordByMakingARequestToECRIfTypeIsEcr() { public class MockAwsECRClientBuilder extends AwsSyncClientBuilder { - protected MockAwsECRClientBuilder(ClientConfigurationFactory clientConfigFactory) { + MockAwsECRClientBuilder(ClientConfigurationFactory clientConfigFactory) { super(clientConfigFactory); } diff --git a/src/test/java/cd/go/artifact/docker/registry/executors/GetArtifactStoreConfigMetadataExecutorTest.java b/src/test/java/cd/go/artifact/docker/registry/executors/GetArtifactStoreConfigMetadataExecutorTest.java index d8ce84bc..37b0221f 100644 --- a/src/test/java/cd/go/artifact/docker/registry/executors/GetArtifactStoreConfigMetadataExecutorTest.java +++ b/src/test/java/cd/go/artifact/docker/registry/executors/GetArtifactStoreConfigMetadataExecutorTest.java @@ -30,16 +30,23 @@ public void shouldReturnArtifactStoreMetadata() throws JSONException { final String expectedJSON = "[\n" + " {\n" + - " \"key\": \"RegistryURL\",\n" + + " \"key\": \"RegistryType\",\n" + " \"metadata\": {\n" + " \"required\": true,\n" + " \"secure\": false\n" + " }\n" + " },\n" + " {\n" + - " \"key\": \"RegistryType\",\n" + + " \"key\": \"RegistryURL\",\n" + " \"metadata\": {\n" + - " \"required\": true,\n" + + " \"required\": false,\n" + + " \"secure\": false\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"RegistryID\",\n" + + " \"metadata\": {\n" + + " \"required\": false,\n" + " \"secure\": false\n" + " }\n" + " },\n" + diff --git a/src/test/java/cd/go/artifact/docker/registry/executors/ValidateArtifactStoreConfigExecutorExecutorTest.java b/src/test/java/cd/go/artifact/docker/registry/executors/ValidateArtifactStoreConfigExecutorExecutorTest.java index 676abdf5..26f9ce80 100644 --- a/src/test/java/cd/go/artifact/docker/registry/executors/ValidateArtifactStoreConfigExecutorExecutorTest.java +++ b/src/test/java/cd/go/artifact/docker/registry/executors/ValidateArtifactStoreConfigExecutorExecutorTest.java @@ -45,10 +45,6 @@ public void shouldValidateMandatoryKeys() throws Exception { final GoPluginApiResponse response = new ValidateArtifactStoreConfigExecutor(request).execute(); String expectedJSON = "[\n" + - " {\n" + - " \"key\": \"RegistryURL\",\n" + - " \"message\": \"RegistryURL must not be blank.\"\n" + - " },\n" + " {\n" + " \"key\": \"RegistryType\",\n" + " \"message\": \"RegistryType must not be blank.\"\n" + @@ -75,7 +71,7 @@ public void shouldValidateProperDataIfTypeIsOther() throws JSONException { @Test public void shouldValidateProperDataIfTypeIsEcr() throws JSONException { String requestBody = new JSONObject() - .put("RegistryURL", "http://localhost/index") + .put("RegistryID", "12345") .put("RegistryType", "ecr") .put("AWSAccessKeyId", "chuck-norris-aws-access-key-id") .put("AWSSecretAccessKey", "chuck-norris-aws-secret-access-key") @@ -131,7 +127,6 @@ public void shouldValidatePresenceOfUsernameAndPasswordIfTypeIsOther() throws JS @Test public void shouldValidatePresenceOfAwsRegionIfTypeIsEcr() throws JSONException { String requestBody = new JSONObject() - .put("RegistryURL", "http://localhost/index") .put("RegistryType", "ecr") .toString(); when(request.requestBody()).thenReturn(requestBody); @@ -141,6 +136,10 @@ public void shouldValidatePresenceOfAwsRegionIfTypeIsEcr() throws JSONException " {\n" + " \"key\": \"AWSRegion\",\n" + " \"message\": \"AWSRegion must not be blank.\"\n" + + " },\n" + + " {\n" + + " \"key\": \"RegistryID\",\n" + + " \"message\": \"RegistryID must not be blank.\"\n" + " }\n" + "]"; JSONAssert.assertEquals(expectedJSON, response.responseBody(), JSONCompareMode.NON_EXTENSIBLE);