forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_service_agents.py
executable file
·126 lines (105 loc) · 3.96 KB
/
build_service_agents.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import asdict, dataclass
from itertools import chain
import requests
import yaml
from bs4 import BeautifulSoup
# BASEDIR = pathlib.Path(__file__).resolve().parents[1]
SERVICE_AGENTS_URL = "https://cloud.google.com/iam/docs/service-agents"
# old names used by Fabric
ALIASES = {
'bigquery-encryption': ['bq'],
'cloudservices': ['cloudsvc'],
'compute-system': ['compute'],
'cloudcomposer-accounts': ['composer'],
'container-engine-robot': ['container', 'container-engine'],
'dataflow-service-producer-prod': ['dataflow'],
'dataproc-accounts': ['dataproc'],
'gae-api-prod': ['gae-flex'],
'gcf-admin-robot': ['cloudfunctions', 'gcf'],
'gkehub': ['fleet'],
'gs-project-accounts': ['storage'],
'monitoring-notification': ['monitoring'],
'serverless-robot-prod': ['cloudrun', 'run'],
}
PRIMARY_OVERRIDE = {
'storage-transfer-service': True,
}
@dataclass
class Agent:
name: str
display_name: str
api: str
identity: str
role: str
is_primary: bool
aliases: list[str]
def main():
page = requests.get(SERVICE_AGENTS_URL).content
soup = BeautifulSoup(page, 'html.parser')
agents = []
for content in soup.find(id='service-agents').select('tbody tr'):
agent_text = content.get_text()
col1, col2 = content.find_all('td')
# skip agents with more than one identity
if col1.find('ul'):
continue
identity = col1.p.get_text()
# skip agents that are not contained in a project
if 'PROJECT_NUMBER' not in identity:
continue
# special case for Cloud Build that has two service agents:
# - %[email protected]
# - service-%[email protected]
if identity == '[email protected]':
name = "cloudbuild-sa" # Cloud Build Service Account
else:
# most service agents have the format
# service-PROJECT_NUMBER@gcp-sa-SERVICE_NAME.iam.gserviceaccount.com.
# We keep the SERVICE_NAME part as the agent's name
name = identity.split('@')[1].split('.')[0]
name = name.removeprefix('gcp-sa-')
identity = identity.replace('PROJECT_NUMBER', '%s')
if name == 'monitoring':
# monitoring is deprecated in favor of monitoring-notification.
# Switch names to preserve old Fabric convention
name = 'monitoring-deprecated'
is_primary = 'Primary service agent' in agent_text
agent = Agent(
name=name,
display_name=col1.h4.get_text(),
api=col1.span.code.get_text() if name != 'cloudservices' else None,
identity=identity,
role=col2.code.get_text() if 'roles/' in agent_text else None,
is_primary=PRIMARY_OVERRIDE.get(name, is_primary),
aliases=ALIASES.get(name, []),
)
if agent.name == 'cloudservices':
# cloudservices role is granted automatically, we don't want to manage it
agent.role = None
agents.append(agent)
# make sure all names and aliases are different:
names = set(agent.name for agent in agents)
assert len(names) == len(agents)
aliases = set(chain.from_iterable(agent.aliases for agent in agents))
assert aliases.isdisjoint(names)
# take the header from the first lines of this file
header = open(__file__).readlines()[2:15]
print("".join(header))
# and print all the agents
print(yaml.safe_dump([asdict(a) for a in agents], sort_keys=False))
if __name__ == '__main__':
main()