Skip to content

Commit e2fddd8

Browse files
committed
feat!: Add secrets support when downloading remote artifacts
1 parent 5ecdbd5 commit e2fddd8

4 files changed

Lines changed: 41 additions & 17 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ const testRequest: TestRequest = {
185185
}
186186
```
187187

188+
You may also reference secrets when downloading remote artifacts:
189+
190+
```ts
191+
microcksContainer
192+
.withMainRemoteArtifacts([
193+
{ url: "https://gitlab.com/user/repo/artifact1.yaml", secretName: "gl-secret" },
194+
{ url: "https://github.com/user/repo/artifact2.yaml" }
195+
])
196+
.withSecondaryRemoteArtifacts([
197+
{ url: "https://example.com/user/repo/examples.yaml", secretName: "gl-secret" }
198+
]);
199+
```
200+
201+
188202
### Advanced features with MicrocksContainersEnsemble
189203

190204
The `MicrocksContainer` referenced above supports essential features of Microcks provided by the main Microcks container. The list of supported features is the following:

src/microcks-container.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("MicrocksContainer", () => {
3535
const container = await new MicrocksContainer()
3636
.withSnapshots([path.resolve(resourcesDir, "microcks-repository.json")])
3737
.withMainArtifacts([path.resolve(resourcesDir, "apipastries-openapi.yaml")])
38-
.withMainRemoteArtifacts(["https://raw.githubusercontent.com/microcks/microcks/master/samples/APIPastry-openapi.yaml"])
38+
.withMainRemoteArtifacts([{ url: "https://raw.githubusercontent.com/microcks/microcks/master/samples/APIPastry-openapi.yaml"} ])
3939
.withSecondaryArtifacts([path.resolve(resourcesDir, "apipastries-postman-collection.json")])
4040
.start();
4141

src/microcks-container.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export class MicrocksContainer extends GenericContainer {
2424
private snapshots: string[] = [];
2525
private mainArtifacts: string[] = [];
2626
private secondaryArtifacts: string[] = [];
27-
private mainRemoteArtifacts: string[] = [];
28-
private secondaryRemoteArtifacts: string[] = [];
27+
private mainRemoteArtifacts: RemoteArtifact[] = [];
28+
private secondaryRemoteArtifacts: RemoteArtifact[] = [];
2929
private secrets: Secret[] = [];
3030

3131
constructor(image = "quay.io/microcks/microcks-uber:1.11.0") {
@@ -62,7 +62,7 @@ export class MicrocksContainer extends GenericContainer {
6262
* @param {[String]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
6363
* @returns this
6464
*/
65-
public withMainRemoteArtifacts(remoteArtifactUrls: string[]): this {
65+
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
6666
this.mainRemoteArtifacts = this.mainRemoteArtifacts.concat(remoteArtifactUrls);
6767
return this;
6868
}
@@ -73,7 +73,7 @@ export class MicrocksContainer extends GenericContainer {
7373
* @param {[String]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
7474
* @returns this
7575
*/
76-
public withSecondaryRemoteArtifacts(remoteArtifactUrls: string[]): this {
76+
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
7777
this.secondaryRemoteArtifacts = this.secondaryRemoteArtifacts.concat(remoteArtifactUrls);
7878
return this;
7979
}
@@ -115,10 +115,12 @@ export class MicrocksContainer extends GenericContainer {
115115
}
116116
// Load remote artifacts before local ones.
117117
for (let i=0; i<this.mainRemoteArtifacts.length; i++) {
118-
await startedContainer.downloadAsMainArtifact(this.mainRemoteArtifacts[i]);
118+
const {url , secretName} = this.mainRemoteArtifacts[i];
119+
await startedContainer.downloadAsMainArtifact(url, secretName);
119120
}
120121
for (let i=0; i<this.secondaryRemoteArtifacts.length; i++) {
121-
await startedContainer.downloadAsSecondaryArtifact(this.secondaryRemoteArtifacts[i]);
122+
const {url , secretName} = this.mainRemoteArtifacts[i];
123+
await startedContainer.downloadAsSecondaryArtifact(url, secretName);
122124
}
123125
// Import artifacts declared in configuration.
124126
for (let i=0; i<this.mainArtifacts.length; i++) {
@@ -287,6 +289,11 @@ export interface DailyInvocationStatistic {
287289
minuteCount: {string: number};
288290
}
289291

292+
export interface RemoteArtifact {
293+
url: string;
294+
secretName?: string;
295+
}
296+
290297

291298
export class StartedMicrocksContainer extends AbstractStartedContainer {
292299
private readonly httpPort: number;
@@ -439,17 +446,17 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
439446
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
440447
* @returns Success or error via Promise
441448
*/
442-
public async downloadAsMainArtifact(remoteArtifactUrl: string): Promise<void> {
443-
return this.downloadArtifact(remoteArtifactUrl, true);
449+
public async downloadAsMainArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
450+
return this.downloadArtifact(remoteArtifactUrl, true, secretName);
444451
}
445452

446453
/**
447454
* Download a remote artifact as a secondary one within the Microcks container.
448455
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
449456
* @returns Success or error via Promise
450457
*/
451-
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string): Promise<void> {
452-
return this.downloadArtifact(remoteArtifactUrl, false);
458+
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
459+
return this.downloadArtifact(remoteArtifactUrl, false, secretName);
453460
}
454461

455462
/**
@@ -613,11 +620,14 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
613620
}
614621
}
615622

616-
private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean): Promise<void> {
623+
private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean, secretName?: string): Promise<void> {
617624
let formBody = new URLSearchParams({
618625
"mainArtifact": String(mainArtifact),
619626
"url": remoteArtifactUrl
620627
});
628+
if (secretName) {
629+
formBody.append("secretName", secretName);
630+
}
621631

622632
// Prepare headers with content type and length.
623633
const headers: Record<string, string> = {

src/microcks-containers-ensemble.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { GenericContainer, StartedNetwork, StartedTestContainer, StopOptions, Wait } from "testcontainers";
17-
import { MicrocksContainer, Secret, StartedMicrocksContainer } from "./microcks-container";
17+
import { MicrocksContainer, Secret, StartedMicrocksContainer, RemoteArtifact } from "./microcks-container";
1818
import {
1919
AmazonServiceConnection, GenericConnection, KafkaConnection,
2020
MicrocksAsyncMinionContainer, StartedMicrocksAsyncMinionContainer
@@ -162,21 +162,21 @@ export class MicrocksContainersEnsemble {
162162
/**
163163
* Provide urls of remote artifacts that will be imported as primary or main ones within the Microcks container
164164
* once it will be started and healthy.
165-
* @param {[String]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
165+
* @param {[RemoteArtifact]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
166166
* @returns this
167167
*/
168-
public withMainRemoteArtifacts(remoteArtifactUrls: string[]): this {
168+
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
169169
this.microcksContainer.withMainRemoteArtifacts(remoteArtifactUrls);
170170
return this;
171171
}
172172

173173
/**
174174
* Provide urls of remote artifacts that will be imported as secondary ones within the Microcks container
175175
* once it will be started and healthy.
176-
* @param {[String]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
176+
* @param {[RemoteArtifact]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
177177
* @returns this
178178
*/
179-
public withSecondaryRemoteArtifacts(remoteArtifactUrls: string[]): this {
179+
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
180180
this.microcksContainer.withSecondaryRemoteArtifacts(remoteArtifactUrls);
181181
return this;
182182
}

0 commit comments

Comments
 (0)