Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pip): support env var interpolation #6648

Merged
merged 3 commits into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
34 changes: 34 additions & 0 deletions lib/manager/pip_requirements/extract.spec.ts
Expand Up @@ -29,7 +29,16 @@ const requirements6 = readFileSync(
'utf8'
);

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

describe('lib/manager/pip_requirements/extract', () => {
delete process.env.PIP_TEST_TOKEN;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move that into before each please. We should also remove it after each test to be sure it would not interfere any other tests in future.

Same for trustLevel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, done. FYI, I copied this construct from lib/datasource/npm/index.spec.ts, which you might want to update in the same way.

beforeEach(() => {
global.trustLevel = 'low';
});
describe('extractPackageFile()', () => {
let config;
beforeEach(() => {
Expand Down Expand Up @@ -93,5 +102,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;
}