-
Notifications
You must be signed in to change notification settings - Fork 358
SCI Embedding - read git.properties file
#3135
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
Merged
rochdev
merged 3 commits into
master
from
juan-fernandez/attempt-to-read-git-properties
May 11, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| const commitSHARegex = /git\.commit\.sha=([a-f\d]{40})/ | ||
| const repositoryUrlRegex = /git\.repository_url=([\w\d:@/.-]+)/ | ||
|
|
||
| function getGitMetadataFromGitProperties (gitPropertiesString) { | ||
| if (!gitPropertiesString) { | ||
| return {} | ||
| } | ||
| const commitSHAMatch = gitPropertiesString.match(commitSHARegex) | ||
| const repositoryUrlMatch = gitPropertiesString.match(repositoryUrlRegex) | ||
|
|
||
| const repositoryUrl = repositoryUrlMatch ? repositoryUrlMatch[1] : undefined | ||
| let parsedUrl = repositoryUrl | ||
|
|
||
| if (repositoryUrl) { | ||
| try { | ||
| // repository URLs can contain username and password, so we want to filter those out | ||
| parsedUrl = new URL(repositoryUrl) | ||
| if (parsedUrl.password) { | ||
| parsedUrl = `${parsedUrl.origin}${parsedUrl.pathname}` | ||
| } | ||
| } catch (e) { | ||
| // if protocol isn't https, no password will be used | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| commitSHA: commitSHAMatch ? commitSHAMatch[1] : undefined, | ||
| repositoryUrl: parsedUrl | ||
| } | ||
| } | ||
|
|
||
| module.exports = { getGitMetadataFromGitProperties } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ describe('Config', () => { | |
| const BLOCKED_TEMPLATE_HTML = readFileSync(BLOCKED_TEMPLATE_HTML_PATH, { encoding: 'utf8' }) | ||
| const BLOCKED_TEMPLATE_JSON_PATH = require.resolve('./fixtures/config/appsec-blocked-template.json') | ||
| const BLOCKED_TEMPLATE_JSON = readFileSync(BLOCKED_TEMPLATE_JSON_PATH, { encoding: 'utf8' }) | ||
| const DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties') | ||
|
|
||
| beforeEach(() => { | ||
| pkg = { | ||
|
|
@@ -918,7 +919,7 @@ describe('Config', () => { | |
| } | ||
| }) | ||
|
|
||
| expect(log.error).to.be.calledThrice | ||
| expect(log.error).to.be.callCount(4) | ||
| expect(log.error.firstCall).to.have.been.calledWithExactly(error) | ||
| expect(log.error.secondCall).to.have.been.calledWithExactly(error) | ||
| expect(log.error.thirdCall).to.have.been.calledWithExactly(error) | ||
|
|
@@ -1058,4 +1059,73 @@ describe('Config', () => { | |
| }) | ||
| }) | ||
| }) | ||
|
|
||
| context('sci embedding', () => { | ||
| const DUMMY_COMMIT_SHA = 'b7b5dfa992008c77ab3f8a10eb8711e0092445b0' | ||
| const DUMMY_REPOSITORY_URL = '[email protected]:DataDog/dd-trace-js.git' | ||
| let ddTags | ||
| beforeEach(() => { | ||
| ddTags = process.env.DD_TAGS | ||
| }) | ||
| afterEach(() => { | ||
| delete process.env.DD_GIT_PROPERTIES_FILE | ||
| delete process.env.DD_GIT_COMMIT_SHA | ||
| delete process.env.DD_GIT_REPOSITORY_URL | ||
| delete process.env.DD_TRACE_GIT_METADATA_ENABLED | ||
| process.env.DD_TAGS = ddTags | ||
| }) | ||
| it('reads DD_GIT_* env vars', () => { | ||
| process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA | ||
| process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) | ||
| expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) | ||
| }) | ||
| it('reads DD_TAGS env var', () => { | ||
| process.env.DD_TAGS = `git.commit.sha:${DUMMY_COMMIT_SHA},git.repository_url:${DUMMY_REPOSITORY_URL}` | ||
| process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) | ||
| expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) | ||
| }) | ||
| it('reads git.properties if it is available', () => { | ||
| process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') | ||
| expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) | ||
| }) | ||
| it('does not crash if git.properties is not available', () => { | ||
| process.env.DD_GIT_PROPERTIES_FILE = '/does/not/exist' | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', undefined) | ||
| expect(config).to.have.property('repositoryUrl', undefined) | ||
| }) | ||
| it('does not read git.properties if env vars are passed', () => { | ||
| process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE | ||
| process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA | ||
| process.env.DD_GIT_REPOSITORY_URL = 'https://github.com:env-var/dd-trace-js.git' | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) | ||
| expect(config).to.have.property('repositoryUrl', 'https://github.com:env-var/dd-trace-js.git') | ||
| }) | ||
| it('still reads git.properties if one of the env vars is missing', () => { | ||
| process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE | ||
| process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA) | ||
| expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL) | ||
| }) | ||
| it('reads git.properties and filters out credentials', () => { | ||
| process.env.DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties.credentials') | ||
| const config = new Config({}) | ||
| expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') | ||
| expect(config).to.have.property('repositoryUrl', 'https://github.com/datadog/dd-trace-js') | ||
| }) | ||
| it('does not read git metadata if DD_TRACE_GIT_METADATA_ENABLED is false', () => { | ||
| process.env.DD_TRACE_GIT_METADATA_ENABLED = 'false' | ||
| const config = new Config({}) | ||
| expect(config).not.to.have.property('commitSHA') | ||
| expect(config).not.to.have.property('repositoryUrl') | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d | ||
| [email protected]:DataDog/dd-trace-js.git |
3 changes: 3 additions & 0 deletions
3
packages/dd-trace/test/fixtures/config/git.properties.credentials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d | ||
| git.repository_url=https://username:[email protected]/datadog/dd-trace-js | ||
| [email protected] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| require('./setup/tap') | ||
|
|
||
| const { getGitMetadataFromGitProperties } = require('../src/git_properties') | ||
|
|
||
| describe('git_properties', () => { | ||
| context('getGitMetadataFromGitProperties', () => { | ||
| it('reads commit SHA and repository URL', () => { | ||
| const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(` | ||
| git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d | ||
| [email protected]:DataDog/dd-trace-js.git | ||
| `) | ||
| expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') | ||
| expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git') | ||
| }) | ||
| it('filters out credentials', () => { | ||
| const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(` | ||
| git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d | ||
| git.repository_url=https://username:[email protected]/datadog/dd-trace-js.git | ||
| `) | ||
| expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') | ||
| expect(repositoryUrl).to.equal('https://github.com/datadog/dd-trace-js.git') | ||
| }) | ||
| it('ignores other fields', () => { | ||
| const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(` | ||
| git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d | ||
| [email protected]:DataDog/dd-trace-js.git | ||
| [email protected] | ||
| `) | ||
| expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d') | ||
| expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git') | ||
| }) | ||
| it('ignores badly formatted files', () => { | ||
| const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(` | ||
| git.commit.sha=; rm -rf ; | ||
| git.repository_url=; rm -rf ; | ||
| `) | ||
| expect(commitSHA).to.equal(undefined) | ||
| expect(repositoryUrl).to.equal(undefined) | ||
| }) | ||
| it('does not crash with empty files', () => { | ||
| const emptyStringResult = getGitMetadataFromGitProperties('') | ||
| expect(emptyStringResult.commitSHA).to.equal(undefined) | ||
| expect(emptyStringResult.repositoryUrl).to.equal(undefined) | ||
| const undefinedResult = getGitMetadataFromGitProperties(undefined) | ||
| expect(undefinedResult.commitSHA).to.equal(undefined) | ||
| expect(undefinedResult.repositoryUrl).to.equal(undefined) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker for this PR, but this is starting to be a lot of code in
config.jswhich should probably be moved out at some point.