Skip to content

Commit 21af666

Browse files
authored
Add flag allow-all-domains and skip matching AR domain if set (#75)
1 parent a050572 commit 21af666

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

src/config.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
// limitations under the License.
1414

1515

16-
const registryRegex = /(@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*:)?registry=https:(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/)/;
17-
const authTokenRegex = /(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/):_authToken=(.*)/;
18-
const passwordRegex = /(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/):_password=(.*)/;
16+
const registryARRegex = /(@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*:)?registry=https:(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/)/;
17+
const authTokenARRegex = /(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/):_authToken=(.*)/;
18+
const passwordARRegex = /(\/\/[a-zA-Z0-9-]+[-]npm[.]pkg[.]dev\/.*\/):_password=(.*)/;
19+
20+
const registryAllDomainRegex = /(@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*:)?registry=https:(\/\/.*)/;
21+
const authTokenAllDomainRegex = /(\/\/.*\/):_authToken=(.*)/;
22+
const passwordAllDomainRegex = /(\/\/.*\/):_password=(.*)/;
23+
1924

2025
const configType = {
2126
Default: "Default",
@@ -24,7 +29,16 @@ const configType = {
2429
Password: "Password",
2530
}
2631

27-
function parseConfig(text) {
32+
function parseConfig(text, allowAllDomains) {
33+
registryRegex = registryARRegex;
34+
authTokenRegex = authTokenARRegex;
35+
passwordRegex = passwordARRegex;
36+
if (allowAllDomains) {
37+
registryRegex = registryAllDomainRegex;
38+
authTokenRegex = authTokenAllDomainRegex;
39+
passwordRegex = passwordAllDomainRegex;
40+
}
41+
2842
let m = text.match(registryRegex);
2943
if (m) {
3044
return {

src/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ async function main() {
8080
describe: 'If set, update the .npmrc file with this token rather than the token obtained from ADC or gcloud.',
8181
default: '',
8282
})
83+
.option('allow-all-domains', {
84+
type: 'boolean',
85+
describe: 'If set, it allows all registry domains to attach the auth token to.',
86+
default: false,
87+
})
8388
.help()
8489
.argv;
8590

@@ -94,7 +99,7 @@ async function main() {
9499
+ 'in future versions. Run the plugin with `--repo-config` and `--credential-config`.');
95100
await update.updateConfigFile(configPath, creds);
96101
} else {
97-
await update.updateConfigFiles(allArgs.repoConfig, allArgs.credentialConfig, creds);
102+
await update.updateConfigFiles(allArgs.repoConfig, allArgs.credentialConfig, creds, allArgs.allowAllDomains);
98103
}
99104
console.log("Success!");
100105
} catch (err) {

src/update.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const {logger} = require('./logger');
2424
* @param {string} fromConfigPath Path to the npmrc file to read scope registry configs from, should be the project npmrc file.
2525
* @param {string} toConfigPath Path to npmrc file to write authentication configs to, should be the user npmrc file.
2626
* @param {string} creds Encrypted credentials.
27+
* @param {boolean} allowAllDomains Set if allow all domains.
2728
* @return {!Promise<undefined>}
2829
*/
29-
async function updateConfigFiles(fromConfigPath, toConfigPath, creds) {
30+
async function updateConfigFiles(fromConfigPath, toConfigPath, creds, allowAllDomains) {
3031
fromConfigPath = path.resolve(fromConfigPath);
3132
toConfigPath = path.resolve(toConfigPath);
3233

@@ -45,7 +46,7 @@ async function updateConfigFiles(fromConfigPath, toConfigPath, creds) {
4546
// - password config, print a warning and move it to the user npmrc file.
4647
// - everything else, keep it in the project npmrc file.
4748
for (const line of fromConfigLines.split('\n')) {
48-
let config = c.parseConfig(line.trim());
49+
let config = c.parseConfig(line.trim(), allowAllDomains);
4950
switch (config.type) {
5051
case c.configType.Registry:
5152
fromConfigs.push(config);
@@ -78,7 +79,7 @@ async function updateConfigFiles(fromConfigPath, toConfigPath, creds) {
7879
if (line == "") {
7980
continue;
8081
}
81-
let config = c.parseConfig(line.trim());
82+
let config = c.parseConfig(line.trim(), allowAllDomains);
8283
if (config.type == c.configType.AuthToken || config.type == c.configType.Password) {
8384
registryAuthConfigs.delete(config.registry);
8485
}

test/test.update.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('#update', () => {
204204
toConfigPath = getConfigPath(`${this.test.title}-to`)
205205
fs.writeFileSync(fromConfigPath, `registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
206206
fs.writeFileSync(toConfigPath, ``);
207-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
207+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
208208

209209
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
210210
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -217,7 +217,7 @@ describe('#update', () => {
217217
toConfigPath = getConfigPath(`${this.test.title}-to`)
218218
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
219219
fs.writeFileSync(toConfigPath, ``);
220-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
220+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
221221

222222
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
223223
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -231,7 +231,7 @@ describe('#update', () => {
231231
toConfigPath = getConfigPath(`${this.test.title}-to`)
232232
fs.writeFileSync(fromConfigPath, `@my.scope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
233233
fs.writeFileSync(toConfigPath, ``);
234-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
234+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
235235

236236
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
237237
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -244,7 +244,7 @@ describe('#update', () => {
244244
toConfigPath = getConfigPath(`${this.test.title}-to`)
245245
fs.writeFileSync(fromConfigPath, `@~myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
246246
fs.writeFileSync(toConfigPath, ``);
247-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
247+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
248248

249249
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
250250
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -256,7 +256,7 @@ describe('#update', () => {
256256
fromConfigPath = getConfigPath(`${this.test.title}-from`);
257257
toConfigPath = getConfigPath(`${this.test.title}-to`)
258258
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
259-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
259+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
260260

261261
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
262262
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -269,7 +269,7 @@ describe('#update', () => {
269269
toConfigPath = getConfigPath(`${this.test.title}-to`)
270270
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
271271
fs.writeFileSync(toConfigPath, `//us-west1-npm.pkg.dev/my-project/my-repo/:_authToken=oldToken`);
272-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
272+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
273273

274274
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
275275
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -282,7 +282,7 @@ describe('#update', () => {
282282
toConfigPath = fromConfigPath;
283283
fs.writeFileSync(fromConfigPath, `registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
284284
@cba:registry=https://asia-npm.pkg.dev/my-project/my-other-repo/`);
285-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
285+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
286286

287287
const got = fs.readFileSync(fromConfigPath, 'utf8');
288288
assert.equal(got, `registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
@@ -296,7 +296,7 @@ describe('#update', () => {
296296
toConfigPath = getConfigPath(`${this.test.title}-to`)
297297
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/`);
298298
fs.writeFileSync(toConfigPath, `//us-west1-npm.pkg.dev/my-project/my-repo/:_password=mypassword`);
299-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
299+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
300300

301301
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
302302
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -310,7 +310,7 @@ describe('#update', () => {
310310
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
311311
//us-west1-npm.pkg.dev/my-project/my-repo/:_authToken=oldToken`);
312312
fs.writeFileSync(toConfigPath, `//us-west1-npm.pkg.dev/my-project/my-repo/:_authToken=oldToken`);
313-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
313+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
314314

315315
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
316316
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -323,7 +323,7 @@ describe('#update', () => {
323323
toConfigPath = getConfigPath(`${this.test.title}-to`)
324324
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
325325
//us-west1-npm.pkg.dev/my-project/my-repo/:_password=mypassword`);
326-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
326+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
327327

328328
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
329329
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -337,7 +337,7 @@ describe('#update', () => {
337337
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
338338
//us-west1-npm.pkg.dev/my-project/my-repo/:_password="YWJjZA=="
339339
//us-west1-npm.pkg.dev/my-project/my-repo/:username=oauth2accesstoken`);
340-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
340+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
341341

342342
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
343343
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -352,7 +352,7 @@ describe('#update', () => {
352352
@anotherscope:registry=https://us-west1-npm.pkg.dev/another-proj/another-repo/
353353
myregistry.myproperty=myvalue`);
354354
fs.writeFileSync(toConfigPath, `myregistry.myproperty=myvalue`);
355-
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds);
355+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
356356

357357
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
358358
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
@@ -364,6 +364,44 @@ myregistry.myproperty=myvalue`);
364364
//us-west1-npm.pkg.dev/another-proj/another-repo/:_authToken=abcd`);
365365
});
366366

367+
it('only allow AR domain', async function(){
368+
fromConfigPath = getConfigPath(`${this.test.title}-from`);
369+
toConfigPath = getConfigPath(`${this.test.title}-to`)
370+
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
371+
@anotherscope:registry=https://random-npm.com/another-proj/another-repo/
372+
myregistry.myproperty=myvalue`);
373+
fs.writeFileSync(toConfigPath, `myregistry.myproperty=myvalue`);
374+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, false);
375+
376+
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
377+
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
378+
assert.equal(gotFrom, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
379+
@anotherscope:registry=https://random-npm.com/another-proj/another-repo/
380+
myregistry.myproperty=myvalue`);
381+
assert.equal(gotTo, `myregistry.myproperty=myvalue
382+
//us-west1-npm.pkg.dev/my-project/my-repo/:_authToken=abcd`);
383+
});
384+
385+
it('allow all domains', async function(){
386+
fromConfigPath = getConfigPath(`${this.test.title}-from`);
387+
toConfigPath = getConfigPath(`${this.test.title}-to`)
388+
fs.writeFileSync(fromConfigPath, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
389+
@anotherscope:registry=https://random-npm.com/another-proj/another-repo/
390+
myregistry.myproperty=myvalue`);
391+
fs.writeFileSync(toConfigPath, `myregistry.myproperty=myvalue`);
392+
await update.updateConfigFiles(fromConfigPath, toConfigPath, creds, true);
393+
394+
const gotFrom = fs.readFileSync(fromConfigPath, 'utf8');
395+
const gotTo = fs.readFileSync(toConfigPath, 'utf8');
396+
assert.equal(gotFrom, `@myscope:registry=https://us-west1-npm.pkg.dev/my-project/my-repo/
397+
@anotherscope:registry=https://random-npm.com/another-proj/another-repo/
398+
myregistry.myproperty=myvalue`);
399+
assert.equal(gotTo, `myregistry.myproperty=myvalue
400+
//us-west1-npm.pkg.dev/my-project/my-repo/:_authToken=abcd
401+
//random-npm.com/another-proj/another-repo/:_authToken=abcd`);
402+
});
403+
404+
367405
it('rejects if input does not exist', async function() {
368406
fromConfigPath = getConfigPath(`${this.test.title}-from`);
369407
toConfigPath = getConfigPath(`${this.test.title}-to`)

0 commit comments

Comments
 (0)