Skip to content

Commit e15e48d

Browse files
committed
simple auth for maven
Remove properties from repository in config schema Removed 'repository' properties from the schema. feat: maven aouth
1 parent c60a637 commit e15e48d

File tree

4 files changed

+131
-12
lines changed

4 files changed

+131
-12
lines changed

apps/generator-cli/src/README.md

Lines changed: 4 additions & 2 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,7 +189,9 @@ 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
}
195197
}

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

Lines changed: 105 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,9 @@ 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+
],
376393
after: [
377394
chalk.red(`Download failed, because of: "HTTP 404 Not Found"`),
378395
],
@@ -424,7 +441,9 @@ describe('VersionManagerService', () => {
424441
describe('logging', () => {
425442
it('logs the correct messages', () => {
426443
expect(logMessages).toEqual({
427-
before: [chalk.yellow(`Download 4.2.0 ...`)],
444+
before: [
445+
chalk.yellow(`Download 4.2.0 ...`),
446+
],
428447
after: [chalk.green(`Downloaded 4.2.0`)],
429448
});
430449
});
@@ -443,7 +462,9 @@ describe('VersionManagerService', () => {
443462
await compile();
444463
await fixture.download('4.2.0');
445464
expect(logMessages).toEqual({
446-
before: [chalk.yellow(`Download 4.2.0 ...`)],
465+
before: [
466+
chalk.yellow(`Download 4.2.0 ...`),
467+
],
447468
after: [
448469
chalk.green(
449470
`Downloaded 4.2.0 to custom storage location ${expected}`,
@@ -601,5 +622,85 @@ describe('VersionManagerService', () => {
601622
});
602623
});
603624
});
625+
626+
describe('repository authentication', () => {
627+
const mavenDocs = {
628+
data: {
629+
response: {
630+
docs: [
631+
{ v: '4.2.0', timestamp: 1599197918000 },
632+
{ v: '4.3.1', timestamp: 1588758220000 },
633+
],
634+
},
635+
},
636+
};
637+
638+
describe('when username and password are configured', () => {
639+
beforeEach(async () => {
640+
getUsername.mockReturnValue('myuser');
641+
getPassword.mockReturnValue('mypass');
642+
await compile();
643+
get.mockReturnValue(of(mavenDocs));
644+
});
645+
646+
it('passes auth to the query request', async () => {
647+
await fixture.getAll().toPromise();
648+
expect(get).toHaveBeenCalledWith(
649+
expect.any(String),
650+
{ auth: { username: 'myuser', password: 'mypass' } },
651+
);
652+
});
653+
654+
it('passes auth to the download request', async () => {
655+
const data = { pipe: jest.fn() };
656+
const file = {
657+
on: jest.fn().mockImplementation((listener, res) => {
658+
if (listener === 'finish') return res();
659+
}),
660+
};
661+
662+
fs.mkdtempSync.mockReturnValue('/tmp/generator-cli-abcDEF');
663+
fs.createWriteStream.mockReturnValue(file);
664+
get.mockReturnValue(of({ data }));
665+
666+
await fixture.download('4.2.0');
667+
expect(get).toHaveBeenCalledWith(
668+
expect.any(String),
669+
{ responseType: 'stream', auth: { username: 'myuser', password: 'mypass' } },
670+
);
671+
});
672+
});
673+
674+
describe('when only username is configured', () => {
675+
beforeEach(async () => {
676+
getUsername.mockReturnValue('myuser');
677+
getPassword.mockReturnValue(undefined);
678+
await compile();
679+
get.mockReturnValue(of(mavenDocs));
680+
});
681+
682+
it('does not pass auth to the query request', async () => {
683+
await fixture.getAll();
684+
expect(get).toHaveBeenCalledWith(
685+
expect.any(String),
686+
{},
687+
);
688+
});
689+
});
690+
691+
describe('when neither username nor password is configured', () => {
692+
beforeEach(async () => {
693+
get.mockReturnValue(of(mavenDocs));
694+
});
695+
696+
it('does not pass auth to the query request', async () => {
697+
await fixture.getAll();
698+
expect(get).toHaveBeenCalledWith(
699+
expect.any(String),
700+
{},
701+
);
702+
});
703+
});
704+
});
604705
});
605-
});
706+
});

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

Lines changed: 13 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) => ({
@@ -168,7 +168,7 @@ export class VersionManagerService {
168168

169169
try {
170170
await this.httpService
171-
.get<Stream>(downloadLink, { responseType: 'stream' })
171+
.get<Stream>(downloadLink, { responseType: 'stream', ...this.getAuthConfig() })
172172
.pipe(
173173
switchMap(
174174
(res) =>
@@ -243,8 +243,7 @@ export class VersionManagerService {
243243
private createDownloadLink(versionName: string) {
244244
return this.replacePlaceholders(
245245
this.configService.get<string>('generator-cli.repository.downloadUrl') ||
246-
configSchema.properties['generator-cli'].properties.repository
247-
.downloadUrl.default,
246+
configSchema.properties['generator-cli'].properties.repository.downloadUrl.default,
248247
{ versionName }
249248
);
250249
}
@@ -265,6 +264,15 @@ export class VersionManagerService {
265264
return str;
266265
}
267266

267+
private getAuthConfig() {
268+
const username = this.configService.get<string>('generator-cli.repository.username');
269+
const password = this.configService.get<string>('generator-cli.repository.password');
270+
if (username && password) {
271+
return { auth: { username, password } };
272+
}
273+
return {};
274+
}
275+
268276
private printResponseError(error: AxiosError) {
269277
try {
270278
if (error.isAxiosError) {
@@ -836,4 +844,4 @@ export class VersionManagerService {
836844
downloadLink: 'https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/3.0.0/openapi-generator-cli-3.0.0.jar'
837845
}
838846
];
839-
}
847+
}

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)