Skip to content

Commit d5cf727

Browse files
Merge pull request #2726 from DataDog/aleksandr-gringauz/RUM-10224/github-app-migration-for-pat
RUM-10224: GitHub app migration for PAT
2 parents f583493 + 2632f5f commit d5cf727

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

.gitlab-ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ stages:
6464
- export OSSRH_USERNAME=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.signing.ossrh_username --with-decryption --query "Parameter.Value" --out text)
6565
- export OSSRH_PASSWORD=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.signing.ossrh_password --with-decryption --query "Parameter.Value" --out text)
6666
- export GPG_PUBLIC_FINGERPRINT=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.signing.gpg_public_key --with-decryption --query "Parameter.Value" --out text | gpg --import --import-options show-only | grep -E -o -e "[A-F0-9]{40}")
67+
set-github-installation-token:
68+
- 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)
69+
- 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)
70+
- export GITHUB_TOKEN=$(aws ssm get-parameter --region us-east-1 --name ci.dd-sdk-android.gh_app_private_key --with-decryption --query "Parameter.Value" --out text | bash ./create_github_installation_token.sh "$GITHUB_APP_CLIENT_ID" "$GITHUB_APP_INSTALLATION_ID")
6771

6872
# CI IMAGE
6973

@@ -978,8 +982,8 @@ notify:dogfood-app:
978982
stage: notify
979983
when: on_success
980984
script:
985+
- !reference [ .snippets, set-github-installation-token ]
981986
- 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
983987
- python3 dogfood.py -v $CI_COMMIT_TAG -t app
984988

985989
notify:dogfood-demo:
@@ -990,8 +994,8 @@ notify:dogfood-demo:
990994
stage: notify
991995
when: on_success
992996
script:
997+
- !reference [ .snippets, set-github-installation-token ]
993998
- 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
995999
- python3 dogfood.py -v $CI_COMMIT_TAG -t demo
9961000

9971001
notify:dogfood-gradle-plugin:
@@ -1002,8 +1006,8 @@ notify:dogfood-gradle-plugin:
10021006
stage: notify
10031007
when: on_success
10041008
script:
1009+
- !reference [ .snippets, set-github-installation-token ]
10051010
- 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
10071011
- python3 dogfood.py -v $CI_COMMIT_TAG -t gradle-plugin
10081012

10091013
notify:merge-verification-metadata:

create_github_installation_token.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
client_id=$1
12+
installation_id=$2
13+
14+
now=$(date +%s)
15+
iat=$((${now} - 60)) # Issues 60 seconds in the past
16+
exp=$((${now} + 600)) # Expires 10 minutes in the future
17+
18+
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
19+
20+
header_json='{
21+
"typ":"JWT",
22+
"alg":"RS256"
23+
}'
24+
# Header encode
25+
header=$(echo -n "${header_json}" | b64enc)
26+
27+
payload_json="{
28+
\"iat\":${iat},
29+
\"exp\":${exp},
30+
\"iss\":\"${client_id}\"
31+
}"
32+
33+
# Payload encode
34+
payload=$(echo -n "${payload_json}" | b64enc)
35+
36+
# Signature
37+
header_payload="${header}"."${payload}"
38+
signature=$(openssl dgst -sha256 -sign /dev/stdin <(echo -n "${header_payload}") | b64enc)
39+
40+
# Create JWT
41+
jwt_token="${header_payload}"."${signature}"
42+
43+
# Fetch installation token
44+
installation_token=$(curl \
45+
-s \
46+
-X POST \
47+
-H "Authorization: Bearer $jwt_token" \
48+
-H "Accept: application/vnd.github+json" \
49+
https://api.github.com/app/installations/$installation_id/access_tokens)
50+
51+
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)