Skip to content

Commit 828833f

Browse files
committed
feat: #192 Add ability to specify secret on remote artifacts
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent 9449c21 commit 828833f

File tree

4 files changed

+131
-18
lines changed

4 files changed

+131
-18
lines changed

src/main/java/io/github/microcks/testcontainers/MicrocksContainer.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public class MicrocksContainer extends GenericContainer<MicrocksContainer> {
8484
private Set<String> snapshotsToImport;
8585
private Set<String> mainArtifactsToImport;
8686
private Set<String> secondaryArtifactsToImport;
87-
private Set<String> mainRemoteArtifactsToImport;
88-
private Set<String> secondaryRemoteArtifactsToImport;
87+
private Set<RemoteArtifact> mainRemoteArtifactsToImport;
88+
private Set<RemoteArtifact> secondaryRemoteArtifactsToImport;
8989
private Set<Secret> secrets;
9090

9191
/**
@@ -149,7 +149,22 @@ public MicrocksContainer withMainRemoteArtifacts(String... remoteArtifactUrls) {
149149
if (mainRemoteArtifactsToImport == null) {
150150
mainRemoteArtifactsToImport = new HashSet<>();
151151
}
152-
mainRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifactUrls).collect(Collectors.toList()));
152+
mainRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifactUrls)
153+
.map(url -> RemoteArtifact.of(url, null)).collect(Collectors.toSet()));
154+
return self();
155+
}
156+
157+
/**
158+
* Provide remote artifacts (with secret name) that will be imported as primary or main ones within the Microcks
159+
* container once it will be started and healthy.
160+
* @param remoteArtifacts A set of remote artifacts that will be loaded
161+
* @return self
162+
*/
163+
public MicrocksContainer withMainRemoteArtifacts(RemoteArtifact... remoteArtifacts) {
164+
if (mainRemoteArtifactsToImport == null) {
165+
mainRemoteArtifactsToImport = new HashSet<>();
166+
}
167+
mainRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifacts).collect(Collectors.toSet()));
153168
return self();
154169
}
155170

@@ -163,7 +178,22 @@ public MicrocksContainer withSecondaryRemoteArtifacts(String... remoteArtifactUr
163178
if (secondaryRemoteArtifactsToImport == null) {
164179
secondaryRemoteArtifactsToImport = new HashSet<>();
165180
}
166-
secondaryRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifactUrls).collect(Collectors.toList()));
181+
secondaryRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifactUrls)
182+
.map(url -> RemoteArtifact.of(url, null)).collect(Collectors.toSet()));
183+
return self();
184+
}
185+
186+
/**
187+
* Provide remote artifacts (with secret name) that will be imported as secondary ones within the Microcks
188+
* container once it will be started and healthy.
189+
* @param remoteArtifacts A set of remote artifacts that will be loaded
190+
* @return self
191+
*/
192+
public MicrocksContainer withSecondaryRemoteArtifacts(RemoteArtifact... remoteArtifacts) {
193+
if (secondaryRemoteArtifactsToImport == null) {
194+
secondaryRemoteArtifactsToImport = new HashSet<>();
195+
}
196+
secondaryRemoteArtifactsToImport.addAll(Arrays.stream(remoteArtifacts).collect(Collectors.toSet()));
167197
return self();
168198
}
169199

@@ -200,12 +230,16 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
200230
if (snapshotsToImport != null && !snapshotsToImport.isEmpty()) {
201231
snapshotsToImport.forEach(this::importSnapshot);
202232
}
233+
// Load secrets before remote artifacts as they may be needed for authentication.
234+
if (secrets != null && !secrets.isEmpty()) {
235+
secrets.forEach(this::createSecret);
236+
}
203237
// Load remote artifacts before local ones.
204238
if (mainRemoteArtifactsToImport != null && !mainRemoteArtifactsToImport.isEmpty()) {
205-
mainRemoteArtifactsToImport.forEach((String remoteArtifactUrl) -> this.downloadArtifact(remoteArtifactUrl, true));
239+
mainRemoteArtifactsToImport.forEach(remoteArtifact -> this.downloadArtifact(remoteArtifact, true));
206240
}
207241
if (secondaryRemoteArtifactsToImport != null && !secondaryRemoteArtifactsToImport.isEmpty()) {
208-
secondaryRemoteArtifactsToImport.forEach((String remoteArtifactUrl) -> this.downloadArtifact(remoteArtifactUrl, false));
242+
secondaryRemoteArtifactsToImport.forEach(remoteArtifact -> this.downloadArtifact(remoteArtifact, false));
209243
}
210244
// Load local ones that may override remote ones.
211245
if (mainArtifactsToImport != null && !mainArtifactsToImport.isEmpty()) {
@@ -214,10 +248,6 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
214248
if (secondaryArtifactsToImport != null && !secondaryArtifactsToImport.isEmpty()) {
215249
secondaryArtifactsToImport.forEach((String artifactPath) -> this.importArtifact(artifactPath, false));
216250
}
217-
// Load secrets before remote artifacts as they may be needed for authentication.
218-
if (secrets != null && !secrets.isEmpty()) {
219-
secrets.forEach(this::createSecret);
220-
}
221251
}
222252

223253
/**
@@ -625,7 +655,7 @@ public static List<UnidirectionalEvent> getEventMessagesForTestCase(String micro
625655
* @throws ArtifactLoadException If artifact cannot be correctly downloaded in container (probably not found)
626656
*/
627657
public void downloadAsMainRemoteArtifact(String remoteArtifactUrl) throws ArtifactLoadException {
628-
downloadArtifact(remoteArtifactUrl, true);
658+
downloadArtifact(new RemoteArtifact(remoteArtifactUrl, null), true);
629659
}
630660

631661
/**
@@ -634,7 +664,7 @@ public void downloadAsMainRemoteArtifact(String remoteArtifactUrl) throws Artifa
634664
* @throws ArtifactLoadException If artifact cannot be correctly downloaded in container (probably not found)
635665
*/
636666
public void downloadAsSecondaryRemoteArtifact(String remoteArtifactUrl) throws ArtifactLoadException {
637-
downloadArtifact(remoteArtifactUrl, false);
667+
downloadArtifact(new RemoteArtifact(remoteArtifactUrl, null), false);
638668
}
639669

640670
/**
@@ -803,7 +833,7 @@ private void importArtifact(String artifactPath, boolean mainArtifact) {
803833
}
804834
}
805835

806-
private void downloadArtifact(String remoteArtifactUrl, boolean mainArtifact) throws ArtifactLoadException {
836+
private void downloadArtifact(RemoteArtifact remoteArtifact, boolean mainArtifact) throws ArtifactLoadException {
807837
try {
808838
// Use the artifact/download endpoint to download the artifact.
809839
URL url = new URL(getHttpEndpoint() + "/api/artifact/download");
@@ -812,7 +842,11 @@ private void downloadArtifact(String remoteArtifactUrl, boolean mainArtifact) th
812842
httpConn.setRequestMethod("POST");
813843
httpConn.setDoOutput(true);
814844

815-
String requestBody = "mainArtifact=" + mainArtifact + "&url=" + remoteArtifactUrl;
845+
String requestBody = "mainArtifact=" + mainArtifact + "&url=" + remoteArtifact.getUrl();
846+
847+
if (remoteArtifact.getSecretName() != null) {
848+
requestBody += "&secretName=" + remoteArtifact.getSecretName();
849+
}
816850

817851
// Write the request body to the output stream of the connection
818852
try (OutputStream os = httpConn.getOutputStream();
@@ -832,8 +866,8 @@ private void downloadArtifact(String remoteArtifactUrl, boolean mainArtifact) th
832866
// Disconnect Http connection.
833867
httpConn.disconnect();
834868
} catch (Exception e) {
835-
log.error("Could not load remote artifact: {}", remoteArtifactUrl);
836-
throw new ArtifactLoadException("Error while importing remote artifact: " + remoteArtifactUrl, e);
869+
log.error("Could not load remote artifact: {}", remoteArtifact.getUrl());
870+
throw new ArtifactLoadException("Error while importing remote artifact: " + remoteArtifact.getUrl(), e);
837871
}
838872
}
839873

@@ -843,7 +877,7 @@ private void importSnapshot(String snapshotPath) {
843877
resource = MicrocksContainer.class.getClassLoader().getResource(snapshotPath);
844878
if (resource == null) {
845879
log.warn("Could not load classpath snapshot: {}", snapshotPath);
846-
throw new ArtifactLoadException("Error while importing snasphot: " + snapshotPath);
880+
throw new ArtifactLoadException("Error while importing snapshot: " + snapshotPath);
847881
}
848882
}
849883
try {

src/main/java/io/github/microcks/testcontainers/MicrocksContainersEnsemble.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ public MicrocksContainersEnsemble withMainRemoteArtifacts(String... remoteArtifa
264264
return this;
265265
}
266266

267+
/**
268+
* Provide remote artifacts that will be imported as primary or main ones within the Microcks container
269+
* once it will be started and healthy.
270+
* @param remoteArtifacts A set of remote artifacts that will be loaded
271+
* @return self
272+
*/
273+
public MicrocksContainersEnsemble withMainRemoteArtifacts(RemoteArtifact... remoteArtifacts) {
274+
microcks.withMainRemoteArtifacts(remoteArtifacts);
275+
return this;
276+
}
277+
267278
/**
268279
* Provide urls to artifacts that will be imported as secondary ones within the Microcks container
269280
* once it will be started and healthy.
@@ -275,6 +286,17 @@ public MicrocksContainersEnsemble withSecondaryRemoteArtifacts(String... remoteA
275286
return this;
276287
}
277288

289+
/**
290+
* Provide remote artifacts that will be imported as secondary ones within the Microcks container
291+
* once it will be started and healthy.
292+
* @param remoteArtifactUrl A set of remote artifacts that will be loaded
293+
* @return self
294+
*/
295+
public MicrocksContainersEnsemble withSecondaryRemoteArtifacts(RemoteArtifact... remoteArtifactUrl) {
296+
microcks.withSecondaryRemoteArtifacts(remoteArtifactUrl);
297+
return this;
298+
}
299+
278300
/**
279301
* Provide Secret that should be imported in Microcks after startup.
280302
* @param secret The description of a secret to access remote Git repository, test endpoint or broker.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers;
17+
18+
/**
19+
* Immutable bean representing a remote artifact with its URL and associated secret name.
20+
* Because we cannot use record with our Java 8 baseline, we use a simple POJO.
21+
* @author laurent
22+
*/
23+
public class RemoteArtifact {
24+
private final String url;
25+
private final String secretName;
26+
27+
/**
28+
* Constructor for RemoteArtifact.
29+
* @param url The URL of the remote artifact.
30+
* @param secretName The name of the secret to use for accessing the artifact.
31+
*/
32+
public RemoteArtifact(String url, String secretName) {
33+
this.url = url;
34+
this.secretName = secretName;
35+
}
36+
37+
/**
38+
* Build a RemoteArtifact
39+
* @param url The URL of the remote artifact.
40+
* @param secretName The name of the secret to use for accessing the artifact.
41+
* @return A new instance of RemoteArtifact.
42+
*/
43+
public static RemoteArtifact of(String url, String secretName) {
44+
return new RemoteArtifact(url, secretName);
45+
}
46+
47+
public String getUrl() {
48+
return url;
49+
}
50+
public String getSecretName() {
51+
return secretName;
52+
}
53+
}

src/test/java/io/github/microcks/testcontainers/MicrocksContainerTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ public void testContractTestingFunctionalityWithOAuth2() throws Exception {
161161
public void testSecretCreation() throws Exception {
162162
try (
163163
MicrocksContainer microcks = new MicrocksContainer(MICROCKS_IMAGE)
164-
.withSecret(new Secret.Builder().name("my-secret").token("abc-123-xyz").build());
164+
.withSecret(new Secret.Builder().name("my-secret").token("abc-123-xyz").tokenHeader("x-microcks").build())
165+
.withMainRemoteArtifacts(
166+
RemoteArtifact.of("https://raw.githubusercontent.com/microcks/microcks/master/samples/APIPastry-openapi.yaml",
167+
"my-secret"));
165168
) {
166169
microcks.start();
167170
testMicrocksConfigRetrieval(microcks.getHttpEndpoint());
@@ -174,6 +177,7 @@ public void testSecretCreation() throws Exception {
174177
assertEquals(1, secrets.jsonPath().getList(".").size());
175178
assertEquals("my-secret", secrets.jsonPath().get("[0].name"));
176179
assertEquals("abc-123-xyz", secrets.jsonPath().get("[0].token"));
180+
assertEquals("x-microcks", secrets.jsonPath().get("[0].tokenHeader"));
177181
}
178182
}
179183

0 commit comments

Comments
 (0)