Skip to content

Commit 8f70d73

Browse files
aleksandersonmicrocks-botlbroudoux
authored
Possibility to use secrets when downloading remote artifacts (#132)
* fix: Add secrets support when downloading remote artifacts Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> * chore: update ADOPTERS.md from global .github repo (#126) Signed-off-by: microcks-bot <info@microcks.io> Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> * fix: #127 Revert project to CJS and bump node version Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com> * chore: #129 Ping dependencies in GitHub actions Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com> Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> * chore: #130 Bump Testcontainers to 1.19.0 Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com> Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> * chore: #131 Bumpo Microcks images to 1.12.0 Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com> Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> --------- Signed-off-by: Alex Manatskyi <aleksanderson@gmail.com> Signed-off-by: microcks-bot <info@microcks.io> Co-authored-by: microcks-bot <info@microcks.io> Co-authored-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent fc1f62b commit 8f70d73

3 files changed

Lines changed: 50 additions & 20 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ 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/private_repo/artifact1.yaml", secretName: "gl-secret" },
194+
"https://github.com/user/public_repo/artifact2.yaml"
195+
])
196+
.withSecondaryRemoteArtifacts([
197+
{ url: "https://gitlab.com/user/private_repo/examples1.yaml", secretName: "gl-secret" },
198+
"https://github.com/user/public_repo/examples2.yaml"
199+
]);
200+
```
201+
202+
188203
### Advanced features with MicrocksContainersEnsemble
189204

190205
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.ts

Lines changed: 30 additions & 15 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.12.0") {
@@ -59,21 +59,21 @@ 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
*/
65-
public withMainRemoteArtifacts(remoteArtifactUrls: string[]): this {
65+
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
6666
this.mainRemoteArtifacts = this.mainRemoteArtifacts.concat(remoteArtifactUrls);
6767
return this;
6868
}
6969

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
*/
76-
public withSecondaryRemoteArtifacts(remoteArtifactUrls: string[]): this {
76+
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
7777
this.secondaryRemoteArtifacts = this.secondaryRemoteArtifacts.concat(remoteArtifactUrls);
7878
return this;
7979
}
@@ -115,12 +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-
await startedContainer.downloadAsMainArtifact(this.mainRemoteArtifacts[i]);
118+
const { url, secretName } = this.extractArtifactInfo(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.extractArtifactInfo(this.secondaryRemoteArtifacts[i]);
123+
await startedContainer.downloadAsSecondaryArtifact(url, secretName);
122124
}
123-
// Import artifacts declared in configuration.
125+
// Import artifacts declared in configuration.
124126
for (let i=0; i<this.mainArtifacts.length; i++) {
125127
await startedContainer.importAsMainArtifact(this.mainArtifacts[i]);
126128
}
@@ -133,6 +135,12 @@ export class MicrocksContainer extends GenericContainer {
133135

134136
return startedContainer;
135137
}
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+
}
136144
}
137145

138146
export enum OAuth2GrantType {
@@ -287,6 +295,11 @@ export interface DailyInvocationStatistic {
287295
minuteCount: {string: number};
288296
}
289297

298+
export type RemoteArtifact = string | {
299+
url: string;
300+
secretName?: string;
301+
}
302+
290303

291304
export class StartedMicrocksContainer extends AbstractStartedContainer {
292305
private readonly httpPort: number;
@@ -439,17 +452,17 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
439452
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
440453
* @returns Success or error via Promise
441454
*/
442-
public async downloadAsMainArtifact(remoteArtifactUrl: string): Promise<void> {
443-
return this.downloadArtifact(remoteArtifactUrl, true);
455+
public async downloadAsMainArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
456+
return this.downloadArtifact(remoteArtifactUrl, true, secretName);
444457
}
445458

446459
/**
447460
* Download a remote artifact as a secondary one within the Microcks container.
448461
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
449462
* @returns Success or error via Promise
450463
*/
451-
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string): Promise<void> {
452-
return this.downloadArtifact(remoteArtifactUrl, false);
464+
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
465+
return this.downloadArtifact(remoteArtifactUrl, false, secretName);
453466
}
454467

455468
/**
@@ -597,7 +610,6 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
597610
return invocationStats.dailyCount;
598611
}
599612

600-
601613
private async importArtifact(artifactPath: string, mainArtifact: boolean): Promise<void> {
602614
const isFile = await this.isFile(artifactPath);
603615
if (!isFile) {
@@ -613,11 +625,14 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
613625
}
614626
}
615627

616-
private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean): Promise<void> {
628+
private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean, secretName?: string): Promise<void> {
617629
let formBody = new URLSearchParams({
618630
"mainArtifact": String(mainArtifact),
619631
"url": remoteArtifactUrl
620632
});
633+
if (secretName) {
634+
formBody.append("secretName", secretName);
635+
}
621636

622637
// Prepare headers with content type and length.
623638
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.js";
17+
import { MicrocksContainer, Secret, StartedMicrocksContainer, RemoteArtifact } from "./microcks-container.js";
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)