Skip to content

Commit db12064

Browse files
committed
simple auth for maven
1 parent c60a637 commit db12064

File tree

4 files changed

+138
-14
lines changed

4 files changed

+138
-14
lines changed

apps/generator-cli/src/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ is automatically used to generate your code. 🎉
179179

180180
### Using custom / private maven registry
181181

182-
If you're using a private maven registry you can configure the `downloadUrl` and `queryUrl` like this:
182+
If you're using a private maven registry you can configure the `downloadUrl` and `queryUrl` and optional `username` and `password` like this:
183183

184184
```json
185185
{
@@ -189,11 +189,12 @@ If you're using a private maven registry you can configure the `downloadUrl` and
189189
"version": "7.8.0",
190190
"repository": {
191191
"queryUrl": "https://private.maven.intern/solrsearch/select?q=g:${group.id}+AND+a:${artifact.id}&core=gav&start=0&rows=200",
192-
"downloadUrl": "https://private.maven.intern/maven2/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar"
192+
"downloadUrl": "https://private.maven.intern/maven2/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar",
193+
"username": "${env.MAVEN_USERNAME}",
194+
"password": "${env.MAVEN_PASSWORD}"
193195
}
194196
}
195-
}
196-
```
197+
}```
197198

198199
If the `version` property param is set it is not necessary to configure the `queryUrl`.
199200

apps/generator-cli/src/app/services/version-manager.service.spec.ts

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ describe('VersionManagerService', () => {
2323

2424
const getVersion = jest.fn().mockReturnValue('4.3.0');
2525
const getStorageDir = jest.fn().mockReturnValue(undefined);
26+
const getUsername = jest.fn().mockReturnValue(undefined);
27+
const getPassword = jest.fn().mockReturnValue(undefined);
2628
const setVersion = jest.fn();
2729

2830
let testBed: TestingModule;
@@ -50,6 +52,14 @@ describe('VersionManagerService', () => {
5052
// return 'https://search.maven.custom/solrsearch/select?q=g:${repository.groupId}+AND+a:${repository.artifactId}&core=gav&start=0&rows=250';
5153
}
5254

55+
if (k === 'generator-cli.repository.username') {
56+
return getUsername();
57+
}
58+
59+
if (k === 'generator-cli.repository.password') {
60+
return getPassword();
61+
}
62+
5363
return getVersion(k);
5464
},
5565
set: setVersion,
@@ -66,6 +76,8 @@ describe('VersionManagerService', () => {
6676
beforeEach(async () => {
6777
[get].forEach((fn) => fn.mockClear());
6878
getStorageDir.mockReturnValue(undefined);
79+
getUsername.mockReturnValue(undefined);
80+
getPassword.mockReturnValue(undefined);
6981
await compile();
7082
fs.existsSync
7183
.mockReset()
@@ -143,6 +155,7 @@ describe('VersionManagerService', () => {
143155
expect(get).toHaveBeenNthCalledWith(
144156
1,
145157
'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200',
158+
{},
146159
);
147160
});
148161

@@ -185,6 +198,7 @@ describe('VersionManagerService', () => {
185198
expect(get).toHaveBeenNthCalledWith(
186199
1,
187200
'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200',
201+
{},
188202
);
189203
});
190204

@@ -220,6 +234,7 @@ describe('VersionManagerService', () => {
220234
expect(get).toHaveBeenNthCalledWith(
221235
1,
222236
'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200',
237+
{},
223238
);
224239
});
225240

@@ -372,7 +387,10 @@ describe('VersionManagerService', () => {
372387

373388
it('logs the correct messages', () => {
374389
expect(logMessages).toEqual({
375-
before: [chalk.yellow(`Download 4.2.0 ...`)],
390+
before: [
391+
chalk.yellow(`Download 4.2.0 ...`),
392+
chalk.yellow(`Downloading from repo1.maven.org ...`),
393+
],
376394
after: [
377395
chalk.red(`Download failed, because of: "HTTP 404 Not Found"`),
378396
],
@@ -424,7 +442,10 @@ describe('VersionManagerService', () => {
424442
describe('logging', () => {
425443
it('logs the correct messages', () => {
426444
expect(logMessages).toEqual({
427-
before: [chalk.yellow(`Download 4.2.0 ...`)],
445+
before: [
446+
chalk.yellow(`Download 4.2.0 ...`),
447+
chalk.yellow(`Downloading from repo1.maven.org ...`),
448+
],
428449
after: [chalk.green(`Downloaded 4.2.0`)],
429450
});
430451
});
@@ -443,7 +464,10 @@ describe('VersionManagerService', () => {
443464
await compile();
444465
await fixture.download('4.2.0');
445466
expect(logMessages).toEqual({
446-
before: [chalk.yellow(`Download 4.2.0 ...`)],
467+
before: [
468+
chalk.yellow(`Download 4.2.0 ...`),
469+
chalk.yellow(`Downloading from repo1.maven.org ...`),
470+
],
447471
after: [
448472
chalk.green(
449473
`Downloaded 4.2.0 to custom storage location ${expected}`,
@@ -601,5 +625,85 @@ describe('VersionManagerService', () => {
601625
});
602626
});
603627
});
628+
629+
describe('repository authentication', () => {
630+
const mavenDocs = {
631+
data: {
632+
response: {
633+
docs: [
634+
{ v: '4.2.0', timestamp: 1599197918000 },
635+
{ v: '4.3.1', timestamp: 1588758220000 },
636+
],
637+
},
638+
},
639+
};
640+
641+
describe('when username and password are configured', () => {
642+
beforeEach(async () => {
643+
getUsername.mockReturnValue('myuser');
644+
getPassword.mockReturnValue('mypass');
645+
await compile();
646+
get.mockReturnValue(of(mavenDocs));
647+
});
648+
649+
it('passes auth to the query request', async () => {
650+
await fixture.getAll().toPromise();
651+
expect(get).toHaveBeenCalledWith(
652+
expect.any(String),
653+
{ auth: { username: 'myuser', password: 'mypass' } },
654+
);
655+
});
656+
657+
it('passes auth to the download request', async () => {
658+
const data = { pipe: jest.fn() };
659+
const file = {
660+
on: jest.fn().mockImplementation((listener, res) => {
661+
if (listener === 'finish') return res();
662+
}),
663+
};
664+
665+
fs.mkdtempSync.mockReturnValue('/tmp/generator-cli-abcDEF');
666+
fs.createWriteStream.mockReturnValue(file);
667+
get.mockReturnValue(of({ data }));
668+
669+
await fixture.download('4.2.0');
670+
expect(get).toHaveBeenCalledWith(
671+
expect.any(String),
672+
{ responseType: 'stream', auth: { username: 'myuser', password: 'mypass' } },
673+
);
674+
});
675+
});
676+
677+
describe('when only username is configured', () => {
678+
beforeEach(async () => {
679+
getUsername.mockReturnValue('myuser');
680+
getPassword.mockReturnValue(undefined);
681+
await compile();
682+
get.mockReturnValue(of(mavenDocs));
683+
});
684+
685+
it('does not pass auth to the query request', async () => {
686+
await fixture.getAll();
687+
expect(get).toHaveBeenCalledWith(
688+
expect.any(String),
689+
{},
690+
);
691+
});
692+
});
693+
694+
describe('when neither username nor password is configured', () => {
695+
beforeEach(async () => {
696+
get.mockReturnValue(of(mavenDocs));
697+
});
698+
699+
it('does not pass auth to the query request', async () => {
700+
await fixture.getAll();
701+
expect(get).toHaveBeenCalledWith(
702+
expect.any(String),
703+
{},
704+
);
705+
});
706+
});
707+
});
604708
});
605-
});
709+
});

apps/generator-cli/src/app/services/version-manager.service.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class VersionManagerService {
6868
.default
6969
);
7070

71-
return this.httpService.get(queryUrl).pipe(
71+
return this.httpService.get(queryUrl, this.getAuthConfig()).pipe(
7272
map(({ data }) => data.response.docs),
7373
map((docs) =>
7474
docs.map((doc) => ({
@@ -166,9 +166,12 @@ export class VersionManagerService {
166166
const downloadLink = this.createDownloadLink(versionName);
167167
const filePath = this.filePath(versionName);
168168

169+
const { hostname } = new URL(downloadLink);
170+
this.logger.log(chalk.yellow(`Downloading from ${hostname} ...`));
171+
169172
try {
170173
await this.httpService
171-
.get<Stream>(downloadLink, { responseType: 'stream' })
174+
.get<Stream>(downloadLink, { responseType: 'stream', ...this.getAuthConfig() })
172175
.pipe(
173176
switchMap(
174177
(res) =>
@@ -243,8 +246,7 @@ export class VersionManagerService {
243246
private createDownloadLink(versionName: string) {
244247
return this.replacePlaceholders(
245248
this.configService.get<string>('generator-cli.repository.downloadUrl') ||
246-
configSchema.properties['generator-cli'].properties.repository
247-
.downloadUrl.default,
249+
configSchema.properties['generator-cli'].properties.repository.downloadUrl.default,
248250
{ versionName }
249251
);
250252
}
@@ -265,6 +267,15 @@ export class VersionManagerService {
265267
return str;
266268
}
267269

270+
private getAuthConfig() {
271+
const username = this.configService.get<string>('generator-cli.repository.username');
272+
const password = this.configService.get<string>('generator-cli.repository.password');
273+
if (username && password) {
274+
return { auth: { username, password } };
275+
}
276+
return {};
277+
}
278+
268279
private printResponseError(error: AxiosError) {
269280
try {
270281
if (error.isAxiosError) {
@@ -836,4 +847,4 @@ export class VersionManagerService {
836847
downloadLink: 'https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/3.0.0/openapi-generator-cli-3.0.0.jar'
837848
}
838849
];
839-
}
850+
}

apps/generator-cli/src/config.schema.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
"downloadUrl": {
3232
"type": "string",
3333
"default": "https://repo1.maven.org/maven2/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar"
34+
},
35+
"username": {
36+
"type": "string",
37+
"description": "Username for authenticating against the maven repository. Supports ${env.VAR_NAME} placeholders."
38+
},
39+
"password": {
40+
"type": "string",
41+
"description": "Password for authenticating against the maven repository. Supports ${env.VAR_NAME} placeholders."
3442
}
3543
},
3644
"useDocker": {
@@ -517,4 +525,4 @@
517525
}
518526
}
519527
}
520-
}
528+
}

0 commit comments

Comments
 (0)