Skip to content

Commit 2d2acd4

Browse files
RUM-10224: GitHub app migration for PAT
1 parent 221bc2e commit 2d2acd4

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

.gitlab-ci.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,16 +970,34 @@ notify:publish-release-failure:
970970
- 'MESSAGE_TEXT=":status_alert: $CI_PROJECT_NAME $CI_COMMIT_TAG publish pipeline <$BUILD_URL|$COMMIT_MESSAGE> failed."'
971971
- postmessage "#mobile-sdk-ops" "$MESSAGE_TEXT"
972972

973+
notify:prepare-github-token:
974+
tags: [ "arch:amd64" ]
975+
only:
976+
- tags
977+
image: $CI_IMAGE_DOCKER
978+
stage: notify
979+
when: on_success
980+
script:
981+
- aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_app_private_key --with-decryption --query "Parameter.Value" --out text >> ./gh_private_key.pem
982+
- export GITHUB_APP_CLIENT_ID=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_app_client_id --with-decryption --query "Parameter.Value" --out text)
983+
- export GITHUB_APP_INSTALLATION_ID=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_app_installation_id --with-decryption --query "Parameter.Value" --out text)
984+
- echo "GITHUB_TOKEN=$(bash ./create_github_installation_token.sh)" >> github.env
985+
artifacts:
986+
reports:
987+
dotenv: github.env
988+
access: none
989+
973990
notify:dogfood-app:
974991
tags: [ "arch:amd64" ]
975992
only:
976993
- tags
977994
image: $CI_IMAGE_DOCKER
978995
stage: notify
979996
when: on_success
997+
needs:
998+
- notify:prepare-github-token
980999
script:
9811000
- pip3 install GitPython requests
982-
- aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_token --with-decryption --query "Parameter.Value" --out text >> ./gh_token
9831001
- python3 dogfood.py -v $CI_COMMIT_TAG -t app
9841002

9851003
notify:dogfood-demo:
@@ -989,9 +1007,10 @@ notify:dogfood-demo:
9891007
image: $CI_IMAGE_DOCKER
9901008
stage: notify
9911009
when: on_success
1010+
needs:
1011+
- notify:prepare-github-token
9921012
script:
9931013
- pip3 install GitPython requests
994-
- aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_token --with-decryption --query "Parameter.Value" --out text >> ./gh_token
9951014
- python3 dogfood.py -v $CI_COMMIT_TAG -t demo
9961015

9971016
notify:dogfood-gradle-plugin:
@@ -1003,7 +1022,6 @@ notify:dogfood-gradle-plugin:
10031022
when: on_success
10041023
script:
10051024
- pip3 install GitPython requests
1006-
- aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_token --with-decryption --query "Parameter.Value" --out text >> ./gh_token
10071025
- python3 dogfood.py -v $CI_COMMIT_TAG -t gradle-plugin
10081026

10091027
notify:merge-verification-metadata:

create_github_installation_token.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
5+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
# Copyright 2016-Present Datadog, Inc.
7+
#
8+
9+
set -o pipefail
10+
11+
pem='./gh_private_key.pem'
12+
13+
now=$(date +%s)
14+
iat=$((${now} - 60)) # Issues 60 seconds in the past
15+
exp=$((${now} + 600)) # Expires 10 minutes in the future
16+
17+
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
18+
19+
header_json='{
20+
"typ":"JWT",
21+
"alg":"RS256"
22+
}'
23+
# Header encode
24+
header=$( echo -n "${header_json}" | b64enc )
25+
26+
payload_json="{
27+
\"iat\":${iat},
28+
\"exp\":${exp},
29+
\"iss\":\"${GITHUB_APP_CLIENT_ID}\"
30+
}"
31+
32+
# Payload encode
33+
payload=$( echo -n "${payload_json}" | b64enc )
34+
35+
# Signature
36+
header_payload="${header}"."${payload}"
37+
signature=$(
38+
openssl dgst -sha256 -sign "${pem}" \
39+
<(echo -n "${header_payload}") | b64enc
40+
)
41+
42+
# Create JWT
43+
jwt_token="${header_payload}"."${signature}"
44+
45+
# Fetch installation token
46+
installation_token=$(curl \
47+
-s \
48+
-X POST \
49+
-H "Authorization: Bearer $jwt_token" \
50+
-H "Accept: application/vnd.github+json" \
51+
https://api.github.com/app/installations/$GITHUB_APP_INSTALLATION_ID/access_tokens)
52+
53+
echo $installation_token | jq -r '.token'

dogfood.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def generate_target_code(target: str, temp_dir_path: str, version: str):
100100

101101
def git_clone_repository(repo_name: str, gh_token: str, temp_dir_path: str) -> Tuple[Repo, str]:
102102
print("Cloning repository " + repo_name)
103-
url = "https://" + gh_token + ":x-oauth-basic@github.com/DataDog/" + repo_name
103+
url = "https://x-access-token:" + gh_token + "@github.com/DataDog/" + repo_name
104104
repo = Repo.clone_from(url, temp_dir_path)
105105
base_name = repo.active_branch.name
106106
return repo, base_name
@@ -143,10 +143,7 @@ def update_dependant(version: str, target: str, gh_token: str, dry_run: bool) ->
143143
def run_main() -> int:
144144
cli_args = parse_arguments(sys.argv[1:])
145145

146-
# This script expects to have a valid Github Token in a "gh_token" text file
147-
# The token needs the `repo` permissions, and for now is a PAT
148-
with open('gh_token', 'r') as f:
149-
gh_token = f.read().strip()
146+
gh_token = os.getenv("GITHUB_TOKEN")
150147

151148
return update_dependant(cli_args.version, cli_args.target, gh_token, cli_args.dry_run)
152149

0 commit comments

Comments
 (0)