Skip to content

Commit f48674b

Browse files
authored
generate credentials metadata (#1502)
1 parent 27e6d90 commit f48674b

7 files changed

Lines changed: 215 additions & 2 deletions

File tree

__tests__/main.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ describe('run', () => {
145145
jest.spyOn(ApiClient.prototype, 'getJobDetails').mockResolvedValue({
146146
'package-manager': 'npm_and_yarn',
147147
'allowed-updates': [],
148+
'credentials-metadata': [],
148149
id: '1',
149150
experiments: {}
150151
})

__tests__/updater-builder-integration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ integration('UpdaterBuilder', () => {
2424

2525
const details: JobDetails = {
2626
'allowed-updates': [],
27+
'credentials-metadata': [],
2728
id: '1',
2829
'package-manager': 'npm_and_yarn',
2930
experiments: {}

__tests__/updater.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,114 @@ describe('Updater', () => {
142142
)
143143
})
144144
})
145+
146+
describe('when given credentials', () => {
147+
const jobDetails = {...mockJobDetails}
148+
149+
new Updater(
150+
'MOCK_UPDATER_IMAGE_NAME',
151+
'MOCK_PROXY_IMAGE_NAME',
152+
mockApiClient,
153+
jobDetails,
154+
[
155+
{
156+
type: 'git_source',
157+
host: 'github.com',
158+
username: 'user',
159+
password: 'pass'
160+
},
161+
{
162+
type: 'npm_registry',
163+
host: 'registry.npmjs.org',
164+
username: 'npm_user',
165+
token: 'npm_token',
166+
'replaces-base': true
167+
}
168+
],
169+
workingDirectory
170+
)
171+
172+
it('generates credentials metadata on the job definition', () => {
173+
expect(jobDetails['credentials-metadata']).toEqual([
174+
{
175+
type: 'git_source',
176+
host: 'github.com'
177+
},
178+
{
179+
type: 'npm_registry',
180+
host: 'registry.npmjs.org',
181+
'replaces-base': true
182+
}
183+
])
184+
})
185+
})
186+
187+
describe('when given duplicate credentials', () => {
188+
const jobDetails = {...mockJobDetails}
189+
190+
new Updater(
191+
'MOCK_UPDATER_IMAGE_NAME',
192+
'MOCK_PROXY_IMAGE_NAME',
193+
mockApiClient,
194+
jobDetails,
195+
[
196+
{
197+
type: 'git_source',
198+
host: 'github.com',
199+
username: 'user',
200+
password: 'pass'
201+
},
202+
{
203+
type: 'git_source',
204+
host: 'github.com',
205+
username: 'user',
206+
password: 'pass'
207+
}
208+
],
209+
workingDirectory
210+
)
211+
212+
it('removes duplicates from the metadata', () => {
213+
expect(jobDetails['credentials-metadata']).toEqual([
214+
{
215+
type: 'git_source',
216+
host: 'github.com'
217+
}
218+
])
219+
})
220+
})
221+
222+
describe('when given a jit_access type credential', () => {
223+
const jobDetails = {...mockJobDetails}
224+
225+
new Updater(
226+
'MOCK_UPDATER_IMAGE_NAME',
227+
'MOCK_PROXY_IMAGE_NAME',
228+
mockApiClient,
229+
jobDetails,
230+
[
231+
{
232+
type: 'git_source',
233+
host: 'github.com',
234+
username: 'user',
235+
password: 'pass'
236+
},
237+
{
238+
type: 'jit_access',
239+
host: 'github.com',
240+
token: 'hello'
241+
}
242+
],
243+
workingDirectory
244+
)
245+
246+
it('removes it from the metadata', () => {
247+
expect(jobDetails['credentials-metadata']).toEqual([
248+
{
249+
type: 'git_source',
250+
host: 'github.com'
251+
}
252+
])
253+
})
254+
})
145255
})

dist/main/index.js

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type JobDetails = {
1010
}>
1111
id: string
1212
'package-manager': string
13+
// Reuse Credential here since it shares many of the same fields,
14+
// but the job details contains no secrets
15+
'credentials-metadata': Credential[]
1316
experiments: object
1417
}
1518

@@ -22,10 +25,18 @@ export type JobError = {
2225

2326
export type Credential = {
2427
type: string
25-
host: string
28+
host?: string
29+
url?: string
2630
username?: string
2731
password?: string
2832
token?: string
33+
repo?: string
34+
registry?: string
35+
organization?: string
36+
'index-url'?: string
37+
'env-key'?: string
38+
'replaces-base'?: boolean
39+
'public-key-fingerprint'?: string
2940
}
3041

3142
export type Metric = {

src/updater.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class Updater {
2323
this.docker = new Docker()
2424
this.outputHostPath = path.join(workingDirectory, 'output')
2525
this.repoHostPath = path.join(workingDirectory, 'repo')
26+
this.details['credentials-metadata'] = this.generateCredentialsMetadata()
2627
}
2728

2829
/**
@@ -57,6 +58,51 @@ export class Updater {
5758
}
5859
}
5960

61+
private generateCredentialsMetadata(): Credential[] {
62+
const unique: Set<string> = new Set()
63+
const result: Credential[] = []
64+
for (const credential of this.credentials) {
65+
if (credential.type === 'jit_access') {
66+
continue
67+
}
68+
69+
const obj: any = {type: credential.type}
70+
if (credential.host !== undefined) {
71+
obj.host = credential.host
72+
}
73+
if (credential.registry !== undefined) {
74+
obj.registry = credential.registry
75+
}
76+
if (credential['index-url'] !== undefined) {
77+
obj['index-url'] = credential['index-url']
78+
}
79+
if (credential['env-key'] !== undefined) {
80+
obj['env-key'] = credential['env-key']
81+
}
82+
if (credential.url !== undefined) {
83+
obj.url = credential.url
84+
}
85+
if (credential.organization !== undefined) {
86+
obj.organization = credential.organization
87+
}
88+
if (credential['replaces-base'] !== undefined) {
89+
obj['replaces-base'] = credential['replaces-base']
90+
}
91+
if (credential['public-key-fingerprint'] !== undefined) {
92+
obj['public-key-fingerprint'] = credential['public-key-fingerprint']
93+
}
94+
if (credential.repo !== undefined) {
95+
obj.repo = credential.repo
96+
}
97+
const key = JSON.stringify(obj)
98+
if (!unique.has(key)) {
99+
unique.add(key)
100+
result.push(obj as Credential)
101+
}
102+
}
103+
return result
104+
}
105+
60106
private async runUpdate(proxy: Proxy): Promise<void> {
61107
const name = `dependabot-job-${this.apiClient.params.jobId}`
62108
const container = await this.createContainer(proxy, name, {

0 commit comments

Comments
 (0)