Skip to content

Commit 10309cf

Browse files
juan-fernandezthedavl
authored andcommitted
SCI Embedding - read git.properties file (#3135)
* read git.properties * more and better tests * fix tests
1 parent 606a2ae commit 10309cf

File tree

7 files changed

+172
-3
lines changed

7 files changed

+172
-3
lines changed

packages/dd-trace/src/config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
const fs = require('fs')
44
const os = require('os')
5+
const uuid = require('crypto-randomuuid')
56
const URL = require('url').URL
67
const log = require('./log')
78
const pkg = require('./pkg')
89
const coalesce = require('koalas')
910
const tagger = require('./tagger')
1011
const { isTrue, isFalse } = require('./util')
11-
const uuid = require('crypto-randomuuid')
1212
const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA } = require('./plugins/util/tags')
13+
const { getGitMetadataFromGitProperties } = require('./git_properties')
1314

1415
const fromEntries = Object.fromEntries || (entries =>
1516
entries.reduce((obj, [k, v]) => Object.assign(obj, { [k]: v }), {}))
@@ -543,6 +544,18 @@ ken|consumer_?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?)
543544
process.env.DD_GIT_COMMIT_SHA,
544545
this.tags[GIT_COMMIT_SHA]
545546
)
547+
if (!this.repositoryUrl || !this.commitSHA) {
548+
const DD_GIT_PROPERTIES_FILE = coalesce(
549+
process.env.DD_GIT_PROPERTIES_FILE,
550+
`${process.cwd()}/git.properties`
551+
)
552+
const gitPropertiesString = maybeFile(DD_GIT_PROPERTIES_FILE)
553+
if (gitPropertiesString) {
554+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(gitPropertiesString)
555+
this.commitSHA = this.commitSHA || commitSHA
556+
this.repositoryUrl = this.repositoryUrl || repositoryUrl
557+
}
558+
}
546559
}
547560

548561
this.stats = {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const commitSHARegex = /git\.commit\.sha=([a-f\d]{40})/
2+
const repositoryUrlRegex = /git\.repository_url=([\w\d:@/.-]+)/
3+
4+
function getGitMetadataFromGitProperties (gitPropertiesString) {
5+
if (!gitPropertiesString) {
6+
return {}
7+
}
8+
const commitSHAMatch = gitPropertiesString.match(commitSHARegex)
9+
const repositoryUrlMatch = gitPropertiesString.match(repositoryUrlRegex)
10+
11+
const repositoryUrl = repositoryUrlMatch ? repositoryUrlMatch[1] : undefined
12+
let parsedUrl = repositoryUrl
13+
14+
if (repositoryUrl) {
15+
try {
16+
// repository URLs can contain username and password, so we want to filter those out
17+
parsedUrl = new URL(repositoryUrl)
18+
if (parsedUrl.password) {
19+
parsedUrl = `${parsedUrl.origin}${parsedUrl.pathname}`
20+
}
21+
} catch (e) {
22+
// if protocol isn't https, no password will be used
23+
}
24+
}
25+
26+
return {
27+
commitSHA: commitSHAMatch ? commitSHAMatch[1] : undefined,
28+
repositoryUrl: parsedUrl
29+
}
30+
}
31+
32+
module.exports = { getGitMetadataFromGitProperties }

packages/dd-trace/test/config.spec.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Config', () => {
2424
const BLOCKED_TEMPLATE_HTML = readFileSync(BLOCKED_TEMPLATE_HTML_PATH, { encoding: 'utf8' })
2525
const BLOCKED_TEMPLATE_JSON_PATH = require.resolve('./fixtures/config/appsec-blocked-template.json')
2626
const BLOCKED_TEMPLATE_JSON = readFileSync(BLOCKED_TEMPLATE_JSON_PATH, { encoding: 'utf8' })
27+
const DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties')
2728

2829
beforeEach(() => {
2930
pkg = {
@@ -936,7 +937,7 @@ describe('Config', () => {
936937
}
937938
})
938939

939-
expect(log.error).to.be.calledThrice
940+
expect(log.error).to.be.callCount(4)
940941
expect(log.error.firstCall).to.have.been.calledWithExactly(error)
941942
expect(log.error.secondCall).to.have.been.calledWithExactly(error)
942943
expect(log.error.thirdCall).to.have.been.calledWithExactly(error)
@@ -1076,4 +1077,73 @@ describe('Config', () => {
10761077
})
10771078
})
10781079
})
1080+
1081+
context('sci embedding', () => {
1082+
const DUMMY_COMMIT_SHA = 'b7b5dfa992008c77ab3f8a10eb8711e0092445b0'
1083+
const DUMMY_REPOSITORY_URL = '[email protected]:DataDog/dd-trace-js.git'
1084+
let ddTags
1085+
beforeEach(() => {
1086+
ddTags = process.env.DD_TAGS
1087+
})
1088+
afterEach(() => {
1089+
delete process.env.DD_GIT_PROPERTIES_FILE
1090+
delete process.env.DD_GIT_COMMIT_SHA
1091+
delete process.env.DD_GIT_REPOSITORY_URL
1092+
delete process.env.DD_TRACE_GIT_METADATA_ENABLED
1093+
process.env.DD_TAGS = ddTags
1094+
})
1095+
it('reads DD_GIT_* env vars', () => {
1096+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1097+
process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1098+
const config = new Config({})
1099+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1100+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1101+
})
1102+
it('reads DD_TAGS env var', () => {
1103+
process.env.DD_TAGS = `git.commit.sha:${DUMMY_COMMIT_SHA},git.repository_url:${DUMMY_REPOSITORY_URL}`
1104+
process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1105+
const config = new Config({})
1106+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1107+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1108+
})
1109+
it('reads git.properties if it is available', () => {
1110+
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1111+
const config = new Config({})
1112+
expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
1113+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1114+
})
1115+
it('does not crash if git.properties is not available', () => {
1116+
process.env.DD_GIT_PROPERTIES_FILE = '/does/not/exist'
1117+
const config = new Config({})
1118+
expect(config).to.have.property('commitSHA', undefined)
1119+
expect(config).to.have.property('repositoryUrl', undefined)
1120+
})
1121+
it('does not read git.properties if env vars are passed', () => {
1122+
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1123+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1124+
process.env.DD_GIT_REPOSITORY_URL = 'https://github.com:env-var/dd-trace-js.git'
1125+
const config = new Config({})
1126+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1127+
expect(config).to.have.property('repositoryUrl', 'https://github.com:env-var/dd-trace-js.git')
1128+
})
1129+
it('still reads git.properties if one of the env vars is missing', () => {
1130+
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1131+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1132+
const config = new Config({})
1133+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1134+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1135+
})
1136+
it('reads git.properties and filters out credentials', () => {
1137+
process.env.DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties.credentials')
1138+
const config = new Config({})
1139+
expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
1140+
expect(config).to.have.property('repositoryUrl', 'https://github.com/datadog/dd-trace-js')
1141+
})
1142+
it('does not read git metadata if DD_TRACE_GIT_METADATA_ENABLED is false', () => {
1143+
process.env.DD_TRACE_GIT_METADATA_ENABLED = 'false'
1144+
const config = new Config({})
1145+
expect(config).not.to.have.property('commitSHA')
1146+
expect(config).not.to.have.property('repositoryUrl')
1147+
})
1148+
})
10791149
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
2+
git.repository_url[email protected]:DataDog/dd-trace-js.git
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
2+
git.repository_url=https://username:[email protected]/datadog/dd-trace-js
3+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require('./setup/tap')
2+
3+
const { getGitMetadataFromGitProperties } = require('../src/git_properties')
4+
5+
describe('git_properties', () => {
6+
context('getGitMetadataFromGitProperties', () => {
7+
it('reads commit SHA and repository URL', () => {
8+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
9+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
10+
[email protected]:DataDog/dd-trace-js.git
11+
`)
12+
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
13+
expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git')
14+
})
15+
it('filters out credentials', () => {
16+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
17+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
18+
git.repository_url=https://username:[email protected]/datadog/dd-trace-js.git
19+
`)
20+
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
21+
expect(repositoryUrl).to.equal('https://github.com/datadog/dd-trace-js.git')
22+
})
23+
it('ignores other fields', () => {
24+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
25+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
26+
[email protected]:DataDog/dd-trace-js.git
27+
28+
`)
29+
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
30+
expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git')
31+
})
32+
it('ignores badly formatted files', () => {
33+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
34+
git.commit.sha=; rm -rf ;
35+
git.repository_url=; rm -rf ;
36+
`)
37+
expect(commitSHA).to.equal(undefined)
38+
expect(repositoryUrl).to.equal(undefined)
39+
})
40+
it('does not crash with empty files', () => {
41+
const emptyStringResult = getGitMetadataFromGitProperties('')
42+
expect(emptyStringResult.commitSHA).to.equal(undefined)
43+
expect(emptyStringResult.repositoryUrl).to.equal(undefined)
44+
const undefinedResult = getGitMetadataFromGitProperties(undefined)
45+
expect(undefinedResult.commitSHA).to.equal(undefined)
46+
expect(undefinedResult.repositoryUrl).to.equal(undefined)
47+
})
48+
})
49+
})

packages/dd-trace/test/serverless.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Serverless', () => {
7575

7676
// trying to spawn with an invalid path will return a non-descriptive error, so we want to catch
7777
// invalid paths and log our own error.
78-
expect(logErrorSpy).to.have.been.calledOnceWith(
78+
expect(logErrorSpy).to.have.been.calledWith(
7979
'Serverless Mini Agent did not start. Could not find mini agent binary.'
8080
)
8181
existsSyncStub.returns(true)

0 commit comments

Comments
 (0)