in tools/build_service_agents.py [0:0]
def main(e2e=False):
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:
# - %s@cloudbuild.gserviceaccount.com
# - service-%s@gcp-sa-cloudbuild.iam.gserviceaccount.com
if identity == 'PROJECT_NUMBER@cloudbuild.gserviceaccount.com':
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', '${project_number}')
identity = identity.replace('.iam.gserviceaccount.',
'.${universe_domain}iam.gserviceaccount.')
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)
if not e2e:
# 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))
else:
jit_services = {}
result = {"locals": {"jit_services": jit_services}}
for a in agents:
if a.is_primary and a.api in E2E_SERVICES:
jit_services[a.api] = a.role
print(json.dumps(result, indent=2))