Skip to content

Commit 62256bf

Browse files
authored
ci: github token check Cloud Build yaml file (#1680)
In #1646 we're creating a periodic check to validate the tokens stored in the secret manager. This pull request creates the trigger's Cloud Build YAML file. This iterates the repository in the `repositories.yaml` file and checks the permissions using the token stored in the secret manager. This relies on the convention of having "-github-token" for each repository.
1 parent 7b883ee commit 62256bf

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

infra/test/token-access-test.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# This Cloud Build configuration is used by a Louhi flow for the Artifact
15+
# Registry (AR) Exit Gate process (go/cloud-sdk-ar-exit-gate-onboarding).
16+
#
17+
# This runs the `librarian generate` command with a provided repository,
18+
# secret name, and optional library ID
19+
steps:
20+
- name: 'gcr.io/cloud-builders/gcloud-slim'
21+
id: validate-credentials
22+
waitFor: ['-']
23+
script: |
24+
#!/usr/bin/env bash
25+
echo "Your project ID is $PROJECT_ID"
26+
echo "gcloud config get-value core/account:"
27+
gcloud config get-value core/account
28+
echo "pwd is $(pwd)"
29+
echo "ls -la ."
30+
ls -la .
31+
echo "Finding YAML files:"
32+
find /workspace/librarian -name '*.yaml'
33+
ROBOT_ACCOUNT=cloud-sdk-librarian-robot
34+
if [[ $- == *x* ]]; then
35+
echo "xtrace is ON. Exiting to avoid credentials showing up in logs."
36+
exit 1
37+
fi
38+
cat infra/prod/repositories.yaml | grep '^\s*-\s*name:' |awk '{print $NF}' |tr -d '"' | while read -r repo_name; do
39+
echo "Validating credentials for repository: $repo_name"
40+
GITHUB_TOKEN=$(gcloud secrets versions access latest --secret="${repo_name}-github-token")
41+
curl --fail -H "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/repos/googleapis/${repo_name}/collaborators/${ROBOT_ACCOUNT}/permission"
42+
if [[ $? -ne 0 ]]; then
43+
echo "Failed to validate credentials for repository: $repo_name"
44+
exit 1
45+
fi
46+
done
47+
echo "Finished validating credentials."
48+
options:
49+
logging: CLOUD_LOGGING_ONLY

0 commit comments

Comments
 (0)