Skip to content

Commit 8aa42b7

Browse files
author
Hierlmeier, Richard
committed
Issue e-gineering#131: Inject authentication for Mirrored ArtifactRepositories
1 parent 3ef53e2 commit 8aa42b7

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

src/main/java/com/e_gineering/maven/gitflowhelper/AbstractGitflowBasedRepositoryMojo.java

+56-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.eclipse.aether.RepositorySystemSession;
1717
import org.eclipse.aether.artifact.DefaultArtifact;
1818
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
19+
import org.eclipse.aether.repository.AuthenticationContext;
20+
import org.eclipse.aether.repository.AuthenticationSelector;
1921
import org.eclipse.aether.repository.LocalRepository;
2022
import org.eclipse.aether.repository.RemoteRepository;
2123
import org.eclipse.aether.repository.RepositoryPolicy;
@@ -49,7 +51,7 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
4951
Objects.requireNonNull(catalog, "catalog must not be null");
5052
return new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), UTF_8));
5153
}
52-
54+
5355
@Parameter(property = "releaseDeploymentRepositoryId", required = true)
5456
String releaseDeploymentRepository;
5557

@@ -58,25 +60,25 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
5860

5961
@Parameter(property = "snapshotDeploymentRepositoryId", required = true)
6062
String snapshotDeploymentRepository;
61-
63+
6264
@Parameter(property = "otherDeployBranchPattern", required = false)
6365
String otherDeployBranchPattern;
6466

6567
@Parameter(defaultValue = "+", required = true)
6668
String otherBranchVersionDelimiter;
67-
69+
6870
@Parameter(defaultValue = "${repositorySystemSession}", required = true)
6971
RepositorySystemSession repositorySystemSession;
70-
72+
7173
@Parameter(defaultValue = "${project.build.directory}", required = true)
7274
File buildDirectory;
73-
75+
7476
@Component
7577
private RepositorySystem repositorySystem;
76-
78+
7779
@Component
7880
private EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;
79-
81+
8082
@Component
8183
private MavenProjectHelper projectHelper;
8284

@@ -99,11 +101,47 @@ ArtifactRepository getDeploymentRepository(final String id) throws MojoFailureEx
99101
Optional<ArtifactRepository> mirroredRepo = project.getRemoteArtifactRepositories().stream()
100102
.flatMap(r -> r.getMirroredRepositories().stream()).filter(r -> r.getId().equals(id)).findFirst();
101103
if(mirroredRepo.isPresent()) {
102-
return mirroredRepo.get();
104+
ArtifactRepository artifactRepository = mirroredRepo.get();
105+
if(artifactRepository.getAuthentication() == null) {
106+
artifactRepository.setAuthentication(getAuthentication(artifactRepository));
107+
}
108+
return artifactRepository;
103109
}
104110
throw new MojoFailureException("No Repository with id `" + id + "` is defined.");
105111
}
106112

113+
/**
114+
* Get the authentication object for an artifact repository
115+
* @param repository the artifact repository
116+
* @return der authentication object or @Code{null} when no authentication was found
117+
*/
118+
private org.apache.maven.artifact.repository.Authentication getAuthentication( ArtifactRepository repository )
119+
{
120+
AuthenticationSelector selector = repositorySystemSession.getAuthenticationSelector();
121+
if (selector == null)
122+
{
123+
return null;
124+
}
125+
RemoteRepository repo = RepositoryUtils.toRepo(repository);
126+
org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
127+
if(auth == null)
128+
{
129+
return null;
130+
}
131+
RemoteRepository repoWithAuth = new RemoteRepository.Builder(repo).setAuthentication(auth).build();
132+
AuthenticationContext authCtx = AuthenticationContext.forRepository(repositorySystemSession, repoWithAuth);
133+
if(authCtx == null) {
134+
return null;
135+
}
136+
org.apache.maven.artifact.repository.Authentication result =
137+
new org.apache.maven.artifact.repository.Authentication(authCtx.get(AuthenticationContext.USERNAME),
138+
authCtx.get(AuthenticationContext.PASSWORD));
139+
result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
140+
result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
141+
authCtx.close();
142+
return result;
143+
}
144+
107145
/**
108146
* Creates and attaches an artifact containing a list of attached artifacts, each line in the file contains
109147
* group:artifact:type:classifier:version
@@ -173,7 +211,7 @@ private void catalogArtifact(PrintWriter writer, Artifact artifact) {
173211
*/
174212
void attachExistingArtifacts(@Nullable final String sourceRepository, final boolean disableLocal)
175213
throws MojoExecutionException, MojoFailureException {
176-
214+
177215
List<ArtifactRepository> remoteArtifactRepositories = new ArrayList<>();
178216
Optional<ArtifactRepository> repo = project.getRemoteArtifactRepositories().stream().filter(r -> r.getId().equals(sourceRepository)).findFirst();
179217
if (repo.isPresent()) {
@@ -185,7 +223,7 @@ void attachExistingArtifacts(@Nullable final String sourceRepository, final bool
185223
getLog().debug("Resolving existing artifacts from local repository only.");
186224
}
187225
List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(remoteArtifactRepositories);
188-
226+
189227
// A place to store our resolved files...
190228
List<ArtifactResult> resolvedArtifacts = new ArrayList<>();
191229

@@ -320,8 +358,8 @@ private boolean hasFile(@Nullable Artifact artifact) {
320358
&& project.getArtifact().getFile().exists()
321359
&& project.getArtifact().getFile().isFile();
322360
}
323-
324-
361+
362+
325363
private String getCoordinates(ArtifactResult artifactResult) {
326364
return getCoordinates(
327365
emptyToNull(artifactResult.getArtifact().getGroupId()),
@@ -331,25 +369,25 @@ private String getCoordinates(ArtifactResult artifactResult) {
331369
emptyToNull(artifactResult.getArtifact().getClassifier())
332370
);
333371
}
334-
372+
335373
private static String emptyToNull(final String s) {
336374
return StringUtils.isBlank(s) ? null : s;
337375
}
338-
376+
339377
private String getCoordinates(Artifact artifact) {
340378
getLog().debug(" Encoding Coordinates For: " + artifact);
341-
379+
342380
// Get the extension according to the artifact type.
343381
String extension = artifact.getArtifactHandler().getExtension();
344-
382+
345383
return getCoordinates(
346384
artifact.getGroupId(),
347385
artifact.getArtifactId(),
348386
project.getVersion(),
349387
extension, artifact.hasClassifier() ? artifact.getClassifier() : null
350388
);
351389
}
352-
390+
353391
private String getCoordinates(String groupId,
354392
String artifactId,
355393
String version,
@@ -358,7 +396,7 @@ private String getCoordinates(String groupId,
358396
Objects.requireNonNull(groupId, "groupId must not be null");
359397
Objects.requireNonNull(artifactId, "artifactId must not be null");
360398
Objects.requireNonNull(version, "version must not be null");
361-
399+
362400
StringBuilder result = new StringBuilder();
363401
for (String s : new String[]{groupId, artifactId, extension, classifier, version}) {
364402
if (s != null) {

0 commit comments

Comments
 (0)