Skip to content

Commit 7191ca9

Browse files
committed
Add helper scripts for updating tokens
1 parent c2f3d1e commit 7191ca9

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

util/encrypt-github-token.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
set -Eeuo pipefail
4+
5+
if [[ $# -ne 1 ]]; then
6+
echo "Usage: $0 <new-token>"
7+
exit 1
8+
fi
9+
10+
NEW_TOKEN="$1"
11+
12+
if [[ $(kubectl config current-context) == *"argus" ]]; then
13+
DEPLOYMENT="developer-portal-backend"
14+
elif [[ $(kubectl config current-context) == *"pollux" ]]; then
15+
DEPLOYMENT="developer-portal-dev-backend"
16+
else
17+
echo "ERROR: Load argus or pollux environment before running this script"
18+
exit 1
19+
fi
20+
21+
ENCRYPTED_TOKEN=$(
22+
kubectl get secret -n dev-portal ${DEPLOYMENT} --output yaml \
23+
| yq '
24+
. |
25+
{"apiVersion": .apiVersion, "kind": .kind, "metadata": .metadata, "stringData": (.data | with_entries(.value |= @base64d))}
26+
' \
27+
| yq ".stringData[\"github-token\"] = \"${NEW_TOKEN}\"" \
28+
| kubeseal --format yaml \
29+
| yq .spec.encryptedData.github-token
30+
)
31+
32+
echo -e "New encrypted github-token for ${DEPLOYMENT}:\n$ENCRYPTED_TOKEN"

util/rotate-gitlab-token.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
set -Eeuo pipefail
4+
5+
if [[ $(kubectl config current-context) == *"argus" ]]; then
6+
DEPLOYMENT="developer-portal-backend"
7+
elif [[ $(kubectl config current-context) == *"pollux" ]]; then
8+
DEPLOYMENT="developer-portal-dev-backend"
9+
else
10+
echo "ERROR: Load argus or pollux environment before running this script"
11+
exit 1
12+
fi
13+
14+
CURRENT_TOKEN=$(
15+
kubectl get secret -n dev-portal ${DEPLOYMENT} --output yaml \
16+
| yq .data.gitlab-token \
17+
| base64 -d
18+
)
19+
20+
if [ "$CURRENT_TOKEN" == "null" ]; then
21+
echo "ERROR: Could not find secret gitlab-token in ${DEPLOYMENT}"
22+
exit 1
23+
fi
24+
25+
EXPIRY_DATE=$(date -d "+3 months" +%Y-%m-%d)
26+
NEW_TOKEN=$(
27+
curl -sS -X POST --header "PRIVATE-TOKEN: ${CURRENT_TOKEN}" "https://gitlab.diamond.ac.uk/api/v4/personal_access_tokens/self/rotate?expires_at=${EXPIRY_DATE}" \
28+
| jq '.token'
29+
)
30+
31+
if [ "$NEW_TOKEN" == "null" ]; then
32+
echo "ERROR: Failed to request new token from GitLab"
33+
exit 1
34+
fi
35+
36+
ENCRYPTED_TOKEN=$(
37+
kubectl get secret -n dev-portal ${DEPLOYMENT} --output yaml \
38+
| yq '
39+
. |
40+
{"apiVersion": .apiVersion, "kind": .kind, "metadata": .metadata, "stringData": (.data | with_entries(.value |= @base64d))}
41+
' \
42+
| yq ".stringData[\"gitlab-token\"] = ${NEW_TOKEN}" \
43+
| kubeseal --format yaml \
44+
| yq .spec.encryptedData.gitlab-token
45+
)
46+
47+
echo -e "New encrypted gitlab-token for ${DEPLOYMENT}:\n$ENCRYPTED_TOKEN"

0 commit comments

Comments
 (0)