Skip to content

Commit 13e68f3

Browse files
committed
feat!: Add secrets support when downloading remote artifacts (backwords compatible version)
1 parent e2fddd8 commit 13e68f3

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ You may also reference secrets when downloading remote artifacts:
190190
```ts
191191
microcksContainer
192192
.withMainRemoteArtifacts([
193-
{ url: "https://gitlab.com/user/repo/artifact1.yaml", secretName: "gl-secret" },
194-
{ url: "https://github.com/user/repo/artifact2.yaml" }
193+
{ url: "https://gitlab.com/user/private_repo/artifact1.yaml", secretName: "gl-secret" },
194+
"https://github.com/user/public_repo/artifact2.yaml"
195195
])
196196
.withSecondaryRemoteArtifacts([
197-
{ url: "https://example.com/user/repo/examples.yaml", secretName: "gl-secret" }
197+
{ url: "https://gitlab.com/user/private_repo/examples1.yaml", secretName: "gl-secret" },
198+
"https://github.com/user/public_repo/examples2.yaml"
198199
]);
199200
```
200201

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([{ url: "https://raw.githubusercontent.com/microcks/microcks/master/samples/APIPastry-openapi.yaml"} ])
38+
.withMainRemoteArtifacts(["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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MicrocksContainer extends GenericContainer {
5959
/**
6060
* Provide urls of remote artifacts that will be imported as primary or main ones within the Microcks container
6161
* once it will be started and healthy.
62-
* @param {[String]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
62+
* @param {[RemoteArtifact]} remoteArtifactUrls Array of URLs (strings) or objects with {url, secretName} (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
6363
* @returns this
6464
*/
6565
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
@@ -70,7 +70,7 @@ export class MicrocksContainer extends GenericContainer {
7070
/**
7171
* Provide urls of remote artifacts that will be imported as secondary ones within the Microcks container
7272
* once it will be started and healthy.
73-
* @param {[String]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
73+
* @param {[RemoteArtifact]} remoteArtifactUrls Array of URLs (strings) or objects with {url, secretName} (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
7474
* @returns this
7575
*/
7676
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
@@ -115,14 +115,14 @@ export class MicrocksContainer extends GenericContainer {
115115
}
116116
// Load remote artifacts before local ones.
117117
for (let i=0; i<this.mainRemoteArtifacts.length; i++) {
118-
const {url , secretName} = this.mainRemoteArtifacts[i];
118+
const { url, secretName } = this.extractArtifactInfo(this.mainRemoteArtifacts[i]);
119119
await startedContainer.downloadAsMainArtifact(url, secretName);
120120
}
121121
for (let i=0; i<this.secondaryRemoteArtifacts.length; i++) {
122-
const {url , secretName} = this.mainRemoteArtifacts[i];
122+
const { url, secretName } = this.extractArtifactInfo(this.secondaryRemoteArtifacts[i]);
123123
await startedContainer.downloadAsSecondaryArtifact(url, secretName);
124124
}
125-
// Import artifacts declared in configuration.
125+
// Import artifacts declared in configuration.
126126
for (let i=0; i<this.mainArtifacts.length; i++) {
127127
await startedContainer.importAsMainArtifact(this.mainArtifacts[i]);
128128
}
@@ -135,6 +135,12 @@ export class MicrocksContainer extends GenericContainer {
135135

136136
return startedContainer;
137137
}
138+
139+
private extractArtifactInfo(artifact: RemoteArtifact): { url: string; secretName?: string } {
140+
return typeof artifact === 'string'
141+
? { url: artifact }
142+
: { url: artifact.url, secretName: artifact.secretName };
143+
}
138144
}
139145

140146
export enum OAuth2GrantType {
@@ -289,7 +295,7 @@ export interface DailyInvocationStatistic {
289295
minuteCount: {string: number};
290296
}
291297

292-
export interface RemoteArtifact {
298+
export type RemoteArtifact = string | {
293299
url: string;
294300
secretName?: string;
295301
}
@@ -604,7 +610,6 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
604610
return invocationStats.dailyCount;
605611
}
606612

607-
608613
private async importArtifact(artifactPath: string, mainArtifact: boolean): Promise<void> {
609614
const isFile = await this.isFile(artifactPath);
610615
if (!isFile) {

0 commit comments

Comments
 (0)