Skip to content

Commit f635d04

Browse files
committed
Initial commit
0 parents  commit f635d04

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

.github/workflows/build.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Java CI
2+
3+
on: [ push ]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- name: Set up JDK 11
11+
uses: actions/setup-java@v2
12+
with:
13+
java-version: '11'
14+
distribution: 'adopt'
15+
cache: maven
16+
- name: Build with Maven
17+
run: mvn verify

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/*
2+
target/

.mvn/jvm.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
2+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
3+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
4+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
5+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# codeartifact-maven-extension
2+
3+
## Problem Statement
4+
5+
Current [recommended flow](https://docs.aws.amazon.com/codeartifact/latest/ug/maven-mvn.html) for
6+
using CodeArtifact as maven repository is to export authentication token into your environment and
7+
use that environment variable as part of user setting.xml (generally at `$M2_HOME/settings.xml`)
8+
9+
This creates a couple of problems;
10+
11+
1. Engineers have to keep exporting the token into their environment every 12 hours.
12+
2. IDEs (at least IntelliJ) cant resolve maven dependency and keep showing annoying pop-up.
13+
14+
## Solution
15+
16+
The goal of this extension is to allow
17+
injecting [CodeArtifact Auth token](https://docs.aws.amazon.com/codeartifact/latest/ug/tokens-authentication.html)
18+
into maven reactor and override values coming from `$M2_HOME/settings.xml`.
19+
20+
## Notes
21+
22+
The implementation is (intentionally) quite brittle and simple.
23+
24+
When `MavenExecutionRequest` is fired in the build reactor; we intercept it and generate a token
25+
using AWS java SDK. For doing that we rely on following system properties;
26+
27+
1. `CODEARTIFACT_USERNAME` defaults to `aws`
28+
2. `CODEARTIFACT_DOMAIN`
29+
3. `CODEARTIFACT_OWNER`
30+
31+
The extension will generate a token for given code artifact domain and owner (account id). Any
32+
servers in the reactor that are using `CODEARTIFACT_USERNAME`'s value as username would have their
33+
password overridden dynamically with the geneated token value.
34+
35+
All system properties could be passed from `<properties>` block in your root pom.xml or via CLI (
36+
e.g `-DCODEARTIFACT_DOMAIN="xxx`)
37+
38+
The underlying AWS client uses default provider chain, which will allow you to override AWS profile
39+
being used by passing in `-Daws.profile` property (or setting `AWS_PROFILE` env var)

pom.xml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.indyaah</groupId>
8+
<artifactId>codeartifact-maven-extension</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
18+
<codeartifact.version>2.17.136</codeartifact.version>
19+
<maven.version>3.8.4</maven.version>
20+
<plexus.version>2.1.1</plexus.version>
21+
22+
<maven-plugin.version>3.6.4</maven-plugin.version>
23+
<fmt-maven-plugin.version>2.13</fmt-maven-plugin.version>
24+
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.apache.maven</groupId>
30+
<artifactId>maven-plugin-api</artifactId>
31+
<version>${maven.version}</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.apache.maven</groupId>
37+
<artifactId>maven-core</artifactId>
38+
<version>${maven.version}</version>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.apache.maven</groupId>
44+
<artifactId>maven-embedder</artifactId>
45+
<version>${maven.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.apache.maven.plugin-tools</groupId>
51+
<artifactId>maven-plugin-annotations</artifactId>
52+
<version>${maven-plugin.version}</version>
53+
<scope>provided</scope>
54+
</dependency>
55+
56+
57+
<dependency>
58+
<groupId>org.codehaus.plexus</groupId>
59+
<artifactId>plexus-component-annotations</artifactId>
60+
<version>${plexus.version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>software.amazon.awssdk</groupId>
64+
<artifactId>codeartifact</artifactId>
65+
<version>${codeartifact.version}</version>
66+
</dependency>
67+
</dependencies>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.codehaus.plexus</groupId>
73+
<artifactId>plexus-component-metadata</artifactId>
74+
<version>${plexus.version}</version>
75+
<executions>
76+
<execution>
77+
<goals>
78+
<goal>generate-metadata</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-plugin-plugin</artifactId>
86+
<version>${maven-plugin.version}</version>
87+
</plugin>
88+
<plugin>
89+
<groupId>com.coveo</groupId>
90+
<artifactId>fmt-maven-plugin</artifactId>
91+
<version>${fmt-maven-plugin.version}</version>
92+
<executions>
93+
<execution>
94+
<goals>
95+
<goal>check</goal>
96+
</goals>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
101+
</plugins>
102+
103+
</build>
104+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.indyaah.coreartifact.maven;
2+
3+
import java.util.Properties;
4+
import javax.inject.Named;
5+
import org.apache.maven.eventspy.AbstractEventSpy;
6+
import org.apache.maven.execution.MavenExecutionRequest;
7+
import software.amazon.awssdk.services.codeartifact.CodeartifactClient;
8+
import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenRequest;
9+
import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenResponse;
10+
11+
@Named("codeartifact-token")
12+
public class CodeArtifactTokenInjectingSpy extends AbstractEventSpy {
13+
14+
private final CodeartifactClient codeartifactClient = CodeartifactClient.builder().build();
15+
16+
@Override
17+
public void onEvent(final Object event) {
18+
if (!(event instanceof MavenExecutionRequest)) {
19+
return;
20+
}
21+
final MavenExecutionRequest request = (MavenExecutionRequest) event;
22+
final Properties systemProperties = request.getSystemProperties();
23+
24+
final String username = systemProperties.getProperty("CODEARTIFACT_USERNAME", "aws");
25+
final String domain = systemProperties.getProperty("CODEARTIFACT_DOMAIN");
26+
final String owner = systemProperties.getProperty("CODEARTIFACT_OWNER");
27+
28+
final GetAuthorizationTokenRequest tokenRequest =
29+
GetAuthorizationTokenRequest.builder().domain(domain).domainOwner(owner).build();
30+
final GetAuthorizationTokenResponse response =
31+
codeartifactClient.getAuthorizationToken(tokenRequest);
32+
final String token = response.authorizationToken();
33+
34+
request.getServers().stream()
35+
.filter(server -> username.equalsIgnoreCase(server.getUsername()))
36+
.forEach(server -> server.setPassword(token));
37+
}
38+
}

0 commit comments

Comments
 (0)