Skip to content

Commit

Permalink
Introduce property RegistryID for amazon ecr.
Browse files Browse the repository at this point in the history
  • Loading branch information
varshavaradarajan committed Apr 3, 2019
1 parent fb7edfc commit 4c8a53c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 38 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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` |

Expand Down
Binary file modified images/artifact_store_ecr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/artifact_store_other.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AmazonECRClientBuilder, AmazonECR> builder;

AWSTokenRequestGenerator() {
Expand All @@ -51,7 +51,7 @@ AwsSyncClientBuilder<AmazonECRClientBuilder, AmazonECR> 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(":");
}
Expand All @@ -68,8 +68,4 @@ void setCredentialsProvider(ArtifactStoreConfig artifactStoreConfig) {
}
}

private String getAwsAccountIdFromURL(String registryUrl) {
return registryUrl.split("//")[1].split("\\.")[0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -109,6 +115,10 @@ public String getAwsRegion() {
return awsRegion;
}

public String getRegistryId() {
return registryId;
}

public boolean isRegistryTypeEcr() {
return "ecr".equals(registryType);
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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.");
}
Expand All @@ -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;
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/resources/artifact-store.template.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<div class="form_item_block">
<label ng-class="{'is-invalid-label': GOINPUTNAME[RegistryURL].$error.server}">Docker Registry Url:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[RegistryURL].$error.server}" type="text" ng-model="RegistryURL" ng-required="true" placeholder="value"/>
<span class="form_error form-error" ng-class="{'is-visible': GOINPUTNAME[RegistryURL].$error.server}" ng-show="GOINPUTNAME[RegistryURL].$error.server">{{GOINPUTNAME[RegistryURL].$error.server}}</span>
</div>

<div class="form_item_block">
<div class="form_item_block" ng-init="RegistryType = 'ecr'">
<label>Registry Type:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[RegistryType].$error.server}" type="radio" ng-model="RegistryType" value="ecr"/>
<label ng-class="{'is-invalid-label': GOINPUTNAME[RegistryType].$error.server}">Amazon ECR</label>
Expand All @@ -15,6 +9,12 @@
</div>

<div ng-show="RegistryType == 'ecr'">
<div class="form_item_block">
<label ng-class="{'is-invalid-label': GOINPUTNAME[RegistryID].$error.server}">ECR Registry ID:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[RegistryID].$error.server}" type="text" ng-model="RegistryID" ng-required="true" placeholder="value"/>
<span class="form_error form-error" ng-class="{'is-visible': GOINPUTNAME[RegistryID].$error.server}" ng-show="GOINPUTNAME[RegistryID].$error.server">{{GOINPUTNAME[RegistryID].$error.server}}</span>
</div>

<div class="form_item_block">
<label ng-class="{'is-invalid-label': GOINPUTNAME[AWSRegion].$error.server}">AWS Region:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[AWSRegion].$error.server}" type="text" ng-model="AWSRegion" ng-required="true" placeholder="value"/>
Expand All @@ -35,6 +35,13 @@
</div>

<div ng-show="RegistryType == 'other'">

<div class="form_item_block">
<label ng-class="{'is-invalid-label': GOINPUTNAME[RegistryURL].$error.server}">Docker Registry Url:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[RegistryURL].$error.server}" type="text" ng-model="RegistryURL" ng-required="true" placeholder="value"/>
<span class="form_error form-error" ng-class="{'is-visible': GOINPUTNAME[RegistryURL].$error.server}" ng-show="GOINPUTNAME[RegistryURL].$error.server">{{GOINPUTNAME[RegistryURL].$error.server}}</span>
</div>

<div class="form_item_block">
<label ng-class="{'is-invalid-label': GOINPUTNAME[Username].$error.server}">Username:<span class='asterix'>*</span></label>
<input ng-class="{'is-invalid-input': GOINPUTNAME[Username].$error.server}" type="text" ng-model="Username" ng-required="true" placeholder="value"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void shouldSetUsernameAndPasswordByMakingARequestToECRIfTypeIsEcr() {

public class MockAwsECRClientBuilder extends AwsSyncClientBuilder<AmazonECRClientBuilder, AmazonECR> {

protected MockAwsECRClientBuilder(ClientConfigurationFactory clientConfigFactory) {
MockAwsECRClientBuilder(ClientConfigurationFactory clientConfigFactory) {
super(clientConfigFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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")
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 4c8a53c

Please sign in to comment.