Skip to content

Commit

Permalink
- added support for codeartifact domains that contain hyphens (#15)
Browse files Browse the repository at this point in the history
- bump 'dangernoodle-io-build-pom' to 14.1.2

original credit to @tonistaht
  • Loading branch information
jgangemi authored Sep 17, 2024
1 parent ed042d7 commit b38ead3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.dangernoodle</groupId>
<artifactId>dangernoodle-io-build-pom</artifactId>
<version>14.1.1</version>
<version>14.1.2</version>
</parent>

<artifactId>codeartifact-maven-extension</artifactId>
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/io/dangernoodle/codeartifact/maven/CodeArtifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Delegate class responsible for interfacing with <code>CodeArtifact</code>.
Expand All @@ -21,6 +22,8 @@ public class CodeArtifact
{
private static final String KEY = "codeartifact.maven.token.duration";

private static final Pattern DOMAIN_AND_DOMAIN_OWNER_PATTERN = Pattern.compile("(.*)-(.*)");

private CodeArtifact.Credentials cached;

public CodeArtifact.Credentials createCredentials(String host, CodeArtifact.Credentials credentials)
Expand All @@ -42,8 +45,8 @@ public boolean isCodeArtifactRepository(String url)
CodeartifactClient createClient(String region, Credentials credentials, CodeartifactClientBuilder builder)
{
return builder.credentialsProvider(createProvider(credentials))
.region(Region.of(region))
.build();
.region(Region.of(region))
.build();
}

private AwsCredentialsProvider createProvider(Credentials credentials)
Expand All @@ -58,14 +61,17 @@ private AwsCredentialsProvider createStaticCredentials(Credentials credentials)
return StaticCredentialsProvider.create(AwsBasicCredentials.create(credentials.username, credentials.password));
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private GetAuthorizationTokenRequest createTokenRequest(String domainOwner)
{
String[] parts = domainOwner.split("-");
Matcher matcher = DOMAIN_AND_DOMAIN_OWNER_PATTERN.matcher(domainOwner);
matcher.matches();

return GetAuthorizationTokenRequest.builder()
.domain(parts[0])
.domainOwner(parts[1])
.durationSeconds(getTokenDuration())
.build();
.domain(matcher.group(1))
.domainOwner(matcher.group(2))
.durationSeconds(getTokenDuration())
.build();
}

private Credentials generateCredentials(String host, Credentials credentials)
Expand All @@ -85,15 +91,15 @@ private Credentials generateCredentials(String host, Credentials credentials)
private long getTokenDuration()
{
return Duration.ofMinutes(Long.parseLong(System.getProperty(KEY, "15")))
.getSeconds();
.getSeconds();
}

private boolean hasExpired(Instant instant)
{
return Optional.ofNullable(instant)
.filter(Instant.now()::isAfter)
.map(i -> true)
.isPresent();
.filter(Instant.now()::isAfter)
.map(i -> true)
.isPresent();
}

public static class Credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class CodeArtifactTest

private static final String HOST = "domain-account.d.codeartifact.region.amazonaws.com";

private static final String HOST_WITH_HYPHENS = "domain-with-hyphens-account.d.codeartifact.region.amazonaws.com";

private CodeArtifact.Credentials credentials;

private CodeArtifact delegate;
Expand Down Expand Up @@ -84,6 +86,10 @@ public void testCodeArtifactUrl()
whenIsCodeArtifactUrl();
thenUrlIsCodeArtifact();

givenACodeArtifactUrlWithHyphens();
whenIsCodeArtifactUrl();
thenUrlIsCodeArtifact();

givenNotACodeArtifactUrl();
whenIsCodeArtifactUrl();
thenUrlIsNotCodeArtifact();
Expand All @@ -99,6 +105,10 @@ public void testCreateCredentials()
thenTokenRequestIsCorrect();
thenCredentialsAreCreated();

whenCreateCredentialsHyphen();
thenTokenRequestIsCorrect();
thenCredentialsAreCreated();

whenCreateCredentials();
thenCachedCredentialsUsed();
thenCredentialsAreCreated();
Expand Down Expand Up @@ -147,7 +157,12 @@ public void testStaticCredentials()

private void givenACodeArtifactUrl()
{
url = "https://domain-account.d.codeartifact.region.amazonaws.com/maven/repository";
url = HOST + "/maven/repository";
}

private void givenACodeArtifactUrlWithHyphens()
{
url = HOST_WITH_HYPHENS + "/maven/repository";
}

private void givenANonExpiredToken()
Expand Down Expand Up @@ -259,6 +274,11 @@ private void whenCreateCredentials()
credentials = delegate.createCredentials(HOST, mockCredentials);
}

private void whenCreateCredentialsHyphen()
{
credentials = delegate.createCredentials(HOST_WITH_HYPHENS, mockCredentials);
}

private void whenIsCodeArtifactUrl()
{
isCodeArtifact = delegate.isCodeArtifactRepository(url);
Expand Down

0 comments on commit b38ead3

Please sign in to comment.