Skip to content

Commit 0e07e9a

Browse files
👷 [RUM-11361] Replace github PAT by using dd octo (#3725)
1 parent b412541 commit 0e07e9a

File tree

6 files changed

+97
-57
lines changed

6 files changed

+97
-57
lines changed

‎.gitlab-ci.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variables:
22
CURRENT_STAGING: staging-34
33
APP: 'browser-sdk'
4-
CURRENT_CI_IMAGE: 87
4+
CURRENT_CI_IMAGE: 88
55
BUILD_STABLE_REGISTRY: 'registry.ddbuild.io'
66
CI_IMAGE: '$BUILD_STABLE_REGISTRY/ci/$APP:$CURRENT_CI_IMAGE'
77
GIT_REPOSITORY: '[email protected]:DataDog/browser-sdk.git'
@@ -33,6 +33,9 @@ stages:
3333
tags:
3434
- 'arch:amd64'
3535
image: $CI_IMAGE
36+
id_tokens:
37+
DDOCTOSTS_ID_TOKEN:
38+
aud: dd-octo-sts
3639
retry:
3740
max: 2
3841
when:

‎Dockerfile‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ RUN test -n "$CHROME_PACKAGE_VERSION" || (echo "\nCHROME_PACKAGE_VERSION not set
77

88
# Install Chrome deps
99
RUN apt-get update && apt-get install -y -q --no-install-recommends \
10-
libgcc-s1 \
11-
libgtk-3-dev \
12-
libx11-xcb1 \
13-
libnss3 \
14-
libxss1 \
15-
libasound2 \
16-
libu2f-udev \
17-
libvulkan1 \
18-
fonts-liberation \
19-
libappindicator3-1 \
20-
lsb-release \
21-
xdg-utils \
22-
curl \
23-
ca-certificates \
24-
wget \
25-
zip
10+
libgcc-s1 \
11+
libgtk-3-dev \
12+
libx11-xcb1 \
13+
libnss3 \
14+
libxss1 \
15+
libasound2 \
16+
libu2f-udev \
17+
libvulkan1 \
18+
fonts-liberation \
19+
libappindicator3-1 \
20+
lsb-release \
21+
xdg-utils \
22+
curl \
23+
ca-certificates \
24+
wget \
25+
zip
2626

2727
# Download and install Chrome
2828
# Debian taken from https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable
2929
RUN curl --silent --show-error --fail http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_PACKAGE_VERSION}_amd64.deb --output google-chrome.deb \
30-
&& dpkg -i google-chrome.deb \
31-
&& rm google-chrome.deb
30+
&& dpkg -i google-chrome.deb \
31+
&& rm google-chrome.deb
3232

3333

3434
# Install AWS cli
@@ -51,6 +51,11 @@ RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg -o
5151
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list \
5252
&& apt-get update && apt-get install -y -q gh
5353

54+
# DD Octo STS to get security token
55+
COPY --from=registry.ddbuild.io/dd-octo-sts:v1.8.1@sha256:eb2895829cdcb1f41cc4fc9d1f3f329c7d8f6fa72b0e8bb915d8195717e02bfa /usr/local/bin/dd-octo-sts /usr/local/bin/dd-octo-sts
56+
57+
RUN apt-get update && apt-get install -y jq
58+
5459
# Webdriverio deps
5560
RUN mkdir -p /usr/share/man/man1
5661

‎scripts/lib/gitUtils.ts‎

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import os from 'os'
22
import fs from 'fs'
33
import { command } from './command.ts'
4-
import { getGithubDeployKey, getGithubAccessToken } from './secrets.ts'
4+
import {
5+
getGithubDeployKey,
6+
getGithubReadToken,
7+
getGithubReleaseToken,
8+
getGithubPullRequestToken,
9+
revokeGithubToken,
10+
} from './secrets.ts'
511
import { fetchHandlingError } from './executionUtils.ts'
612

713
interface GitHubPR {
@@ -24,7 +30,7 @@ interface GitHubReleaseParams {
2430
}
2531

2632
export async function fetchPR(localBranch: string): Promise<GitHubPR | null> {
27-
const pr = await callGitHubApi<GitHubPR[]>('GET', `pulls?head=DataDog:${localBranch}`)
33+
const pr = await callGitHubApi<GitHubPR[]>('GET', `pulls?head=DataDog:${localBranch}`, getGithubReadToken())
2834
if (pr && pr.length > 1) {
2935
throw new Error('Multiple pull requests found for the branch')
3036
}
@@ -40,31 +46,40 @@ export async function fetchPR(localBranch: string): Promise<GitHubPR | null> {
4046
*/
4147
export async function createGitHubRelease({ version, body }: GitHubReleaseParams): Promise<GitHubRelease> {
4248
try {
43-
await callGitHubApi('GET', `releases/tags/${version}`)
49+
await callGitHubApi('GET', `releases/tags/${version}`, getGithubReadToken())
4450
throw new Error(`Release ${version} already exists`)
4551
} catch (error) {
4652
if ((error as any).status !== 404) {
4753
throw error
4854
}
4955
}
5056

51-
return callGitHubApi('POST', 'releases', {
57+
// content write
58+
return callGitHubApi<GitHubRelease>('POST', 'releases', getGithubReleaseToken(), {
5259
tag_name: version,
5360
name: version,
5461
body,
5562
})
5663
}
5764

58-
async function callGitHubApi<T>(method: string, path: string, body?: any): Promise<T> {
59-
const response = await fetchHandlingError(`https://api.github.com/repos/DataDog/browser-sdk/${path}`, {
60-
method,
61-
headers: {
62-
Authorization: `token ${getGithubAccessToken()}`,
63-
'X-GitHub-Api-Version': '2022-11-28',
64-
},
65-
body: body ? JSON.stringify(body) : undefined,
66-
})
67-
return response.json() as Promise<T>
65+
export async function getPrComments(prNumber: number): Promise<Array<{ id: number; body: string }>> {
66+
const response = await callGitHubApi<Array<{ id: number; body: string }>>(
67+
'GET',
68+
`issues/${prNumber}/comments`,
69+
getGithubReadToken()
70+
)
71+
return response
72+
}
73+
74+
export function createPullRequest(mainBranch: string) {
75+
const token = getGithubPullRequestToken()
76+
try {
77+
command`gh auth login --with-token`.withInput(token).run()
78+
const pullRequestUrl = command`gh pr create --fill --base ${mainBranch}`.run()
79+
return pullRequestUrl.trim()
80+
} finally {
81+
revokeGithubToken(token)
82+
}
6883
}
6984

7085
export function getLastCommonCommit(baseBranch: string): string {
@@ -92,5 +107,20 @@ export function initGitConfig(repository: string): void {
92107
command`git config user.name ci.browser-sdk`.run()
93108
command`git remote set-url origin ${repository}`.run()
94109
}
95-
96110
export const LOCAL_BRANCH = process.env.CI_COMMIT_REF_NAME
111+
112+
async function callGitHubApi<T>(method: string, path: string, token: string, body?: any): Promise<T> {
113+
try {
114+
const response = await fetchHandlingError(`https://api.github.com/repos/DataDog/browser-sdk/${path}`, {
115+
method,
116+
headers: {
117+
Authorization: `token ${token}`,
118+
'X-GitHub-Api-Version': '2022-11-28',
119+
},
120+
body: body ? JSON.stringify(body) : undefined,
121+
})
122+
return (await response.json()) as Promise<T>
123+
} finally {
124+
revokeGithubToken(token)
125+
}
126+
}

‎scripts/lib/secrets.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@ export function getGithubDeployKey(): string {
44
return getSecretKey('ci.browser-sdk.github_deploy_key')
55
}
66

7-
export function getGithubAccessToken(): string {
8-
return getSecretKey('ci.browser-sdk.github_access_token')
7+
/**
8+
* This token is scoped to main branch only.
9+
*/
10+
export function getGithubPullRequestToken(): string {
11+
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.pull_request`.run().trim()
12+
}
13+
14+
/**
15+
* This token is scoped to tags only.
16+
*/
17+
export function getGithubReleaseToken(): string {
18+
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.release`.run().trim()
19+
}
20+
21+
export function getGithubReadToken(): string {
22+
return command`dd-octo-sts token --scope DataDog/browser-sdk --policy self.gitlab.read`.run().trim()
23+
}
24+
25+
export function revokeGithubToken(token: string): string {
26+
return command`dd-octo-sts revoke --token ${token}`.run().trim()
927
}
1028

1129
export function getOrg2ApiKey(): string {

‎scripts/performance/lib/reportAsAPrComment.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { command } from '../../lib/command.ts'
22
import { formatSize } from '../../lib/computeBundleSize.ts'
33
import { fetchHandlingError } from '../../lib/executionUtils.ts'
4-
import { LOCAL_BRANCH, getLastCommonCommit, fetchPR } from '../../lib/gitUtils.ts'
5-
import { getGithubAccessToken } from '../../lib/secrets.ts'
4+
import { LOCAL_BRANCH, getLastCommonCommit, fetchPR, getPrComments } from '../../lib/gitUtils.ts'
65
import { fetchPerformanceMetrics } from './fetchPerformanceMetrics.ts'
76

87
const PR_COMMENT_HEADER = 'Bundles Sizes Evolution'
@@ -104,16 +103,8 @@ function compare(
104103
}
105104

106105
async function retrieveExistingCommentId(prNumber: number): Promise<number | undefined> {
107-
const response = await fetchHandlingError(
108-
`https://api.github.com/repos/DataDog/browser-sdk/issues/${prNumber}/comments`,
109-
{
110-
method: 'GET',
111-
headers: {
112-
Authorization: `token ${getGithubAccessToken()}`,
113-
},
114-
}
115-
)
116-
const comments = (await response.json()) as Array<{ id: number; body: string }>
106+
const comments = await getPrComments(prNumber)
107+
117108
const targetComment = comments.find((comment) => comment.body.startsWith(`## ${PR_COMMENT_HEADER}`))
118109
if (targetComment !== undefined) {
119110
return targetComment.id

‎scripts/test/bump-chrome-version.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import fs from 'node:fs'
22
import { printLog, runMain, fetchHandlingError } from '../lib/executionUtils.ts'
33
import { command } from '../lib/command.ts'
44
import { CI_FILE, replaceCiFileVariable } from '../lib/filesUtils.ts'
5-
import { initGitConfig } from '../lib/gitUtils.ts'
6-
import { getGithubAccessToken } from '../lib/secrets.ts'
5+
import { initGitConfig, createPullRequest } from '../lib/gitUtils.ts'
76

87
const REPOSITORY = process.env.GIT_REPOSITORY
98
const MAIN_BRANCH = process.env.MAIN_BRANCH
@@ -56,7 +55,7 @@ runMain(async () => {
5655

5756
printLog('Create PR...')
5857

59-
const pullRequestUrl = createPullRequest()
58+
const pullRequestUrl = createPullRequest(MAIN_BRANCH)
6059
printLog(`Chrome version bump PR created (from ${CURRENT_PACKAGE_VERSION} to ${packageVersion}).`)
6160

6261
// used to share the pull request url to the notification jobs
@@ -77,9 +76,3 @@ function getMajor(version: string): number {
7776

7877
return Number(major)
7978
}
80-
81-
function createPullRequest(): string {
82-
command`gh auth login --with-token`.withInput(getGithubAccessToken()).run()
83-
const pullRequestUrl = command`gh pr create --fill --base ${MAIN_BRANCH}`.run()
84-
return pullRequestUrl.trim()
85-
}

0 commit comments

Comments
 (0)