Skip to content

Commit

Permalink
feat(pip): support env var interpolation (#6648)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKevJames committed Jul 7, 2020
1 parent 4459b11 commit 634e553
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/manager/pip_requirements/__fixtures__/requirements7.txt
@@ -0,0 +1,12 @@
# Repositories
--extra-index-url http://$PIP_TEST_TOKEN:example.com/private-pypi/
--extra-index-url http://${PIP_TEST_TOKEN}:example.com/private-pypi/
--extra-index-url "http://$PIP_TEST_TOKEN:example.com/private-pypi/"
--extra-index-url "http://${PIP_TEST_TOKEN}:example.com/private-pypi/"
# Packages
Django[argon2]==2.0.12
celery [redis]==4.1.1
foo [bar] == 3.2.1 # handles extra white space
some-package==0.3.1
some-other-package==1.0.0
not_semver==1.9
38 changes: 38 additions & 0 deletions lib/manager/pip_requirements/extract.spec.ts
Expand Up @@ -29,7 +29,20 @@ const requirements6 = readFileSync(
'utf8'
);

const requirements7 = readFileSync(
'lib/manager/pip_requirements/__fixtures__/requirements7.txt',
'utf8'
);

describe('lib/manager/pip_requirements/extract', () => {
beforeEach(() => {
delete process.env.PIP_TEST_TOKEN;
global.trustLevel = 'low';
});
afterEach(() => {
delete process.env.PIP_TEST_TOKEN;
global.trustLevel = 'low';
});
describe('extractPackageFile()', () => {
let config;
beforeEach(() => {
Expand Down Expand Up @@ -93,5 +106,30 @@ describe('lib/manager/pip_requirements/extract', () => {
]);
expect(res.deps).toHaveLength(6);
});
it('should not replace env vars in low trust mode', () => {
process.env.PIP_TEST_TOKEN = 'its-a-secret';
const res = extractPackageFile(requirements7, 'unused_file_name', {});
expect(res.registryUrls).toEqual([
'https://pypi.org/pypi/',
'http://$PIP_TEST_TOKEN:example.com/private-pypi/',
// eslint-disable-next-line no-template-curly-in-string
'http://${PIP_TEST_TOKEN}:example.com/private-pypi/',
'http://$PIP_TEST_TOKEN:example.com/private-pypi/',
// eslint-disable-next-line no-template-curly-in-string
'http://${PIP_TEST_TOKEN}:example.com/private-pypi/',
]);
});
it('should replace env vars in high trust mode', () => {
process.env.PIP_TEST_TOKEN = 'its-a-secret';
global.trustLevel = 'high';
const res = extractPackageFile(requirements7, 'unused_file_name', {});
expect(res.registryUrls).toEqual([
'https://pypi.org/pypi/',
'http://its-a-secret:example.com/private-pypi/',
'http://its-a-secret:example.com/private-pypi/',
'http://its-a-secret:example.com/private-pypi/',
'http://its-a-secret:example.com/private-pypi/',
]);
});
});
});
17 changes: 16 additions & 1 deletion lib/manager/pip_requirements/extract.ts
Expand Up @@ -79,7 +79,22 @@ export function extractPackageFile(
}
const res: PackageFile = { deps };
if (registryUrls.length > 0) {
res.registryUrls = registryUrls;
res.registryUrls = registryUrls.map((url) => {
// handle the optional quotes in eg. `--extra-index-url "https://foo.bar"`
const cleaned = url.replace(/^"/, '').replace(/"$/, '');
if (global.trustLevel !== 'high') {
return cleaned;
}
// interpolate any environment variables
return cleaned.replace(
/(\$[A-Za-z\d_]+)|(\${[A-Za-z\d_]+})/g,
(match) => {
const envvar = match.substring(1).replace(/^{/, '').replace(/}$/, '');
const sub = process.env[envvar];
return sub || match;
}
);
});
}
return res;
}

0 comments on commit 634e553

Please sign in to comment.