-
Notifications
You must be signed in to change notification settings - Fork 5
/
cred_helper.py
executable file
·45 lines (33 loc) · 1.08 KB
/
cred_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import subprocess
import json
import re
import sys
def get_gcloud_auth_token():
with open(".user-bazelrc") as f:
all = f.read()
# The username is in the .user-bazelrc file as "# user: <username>"
# fish it out using a regex
USER = re.search(r"# user: (.*)", all).group(1)
# Run gcloud command to get the authentication token
result = subprocess.run(
["gcloud", "auth", "print-access-token", USER],
capture_output=True,
text=True,
check=True,
)
token = result.stdout.strip()
return token
def generate_credentials():
# Get the Bearer token from gcloud
bearer_token = get_gcloud_auth_token()
# Create the JSON object with the required format
credentials = {"headers": {"Authorization": [f"Bearer {bearer_token}"]}}
return credentials
def main():
if len(sys.argv) != 2 or sys.argv[1] != "get":
sys.exit("Usage: python credential_helper.py get")
credentials = generate_credentials()
print(json.dumps(credentials, indent=2))
if __name__ == "__main__":
main()