|
| 1 | +# Copyright 2021 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 | + |
| 15 | +# Prerequisites: |
| 16 | +# Make sure to run the setup in scripts/setup_external_accounts.sh |
| 17 | +# and copy the logged constant strings (_AUDIENCE_OIDC, _AUDIENCE_AWS) |
| 18 | +# into this file before running this test suite. |
| 19 | +# Once that is done, this test can be run indefinitely. |
| 20 | +# |
| 21 | +# The only requirement for this test suite to run is to set the environment |
| 22 | +# variable GOOGLE_APPLICATION_CREDENTIALS to point to the expected service |
| 23 | +# account keys whose email is referred to in the setup script. |
| 24 | +# |
| 25 | +# This script follows the following logic. |
| 26 | +# OIDC provider (file-sourced and url-sourced credentials): |
| 27 | +# Use the service account keys to generate a Google ID token using the |
| 28 | +# iamcredentials generateIdToken API, using the default STS audience. |
| 29 | +# This will use the service account client ID as the sub field of the token. |
| 30 | +# This OIDC token will be used as the external subject token to be exchanged |
| 31 | +# for a Google access token via GCP STS endpoint and then to impersonate the |
| 32 | +# original service account key. |
| 33 | + |
| 34 | + |
| 35 | +import json |
| 36 | +import os |
| 37 | +from tempfile import NamedTemporaryFile |
| 38 | + |
| 39 | +import sys |
| 40 | +import google.auth |
| 41 | +from googleapiclient import discovery |
| 42 | +from google.oauth2 import service_account |
| 43 | +import pytest |
| 44 | +from mock import patch |
| 45 | + |
| 46 | +# Populate values from the output of scripts/setup_external_accounts.sh. |
| 47 | +_AUDIENCE_OIDC = "//iam.googleapis.com/projects/79992041559/locations/global/workloadIdentityPools/pool-73wslmxn/providers/oidc-73wslmxn" |
| 48 | + |
| 49 | + |
| 50 | +def dns_access_direct(request, project_id): |
| 51 | + # First, get the default credentials. |
| 52 | + credentials, _ = google.auth.default( |
| 53 | + scopes=["https://www.googleapis.com/auth/cloud-platform.read-only"], |
| 54 | + request=request, |
| 55 | + ) |
| 56 | + |
| 57 | + # Apply the default credentials to the headers to make the request. |
| 58 | + headers = {} |
| 59 | + credentials.apply(headers) |
| 60 | + response = request( |
| 61 | + url="https://dns.googleapis.com/dns/v1/projects/{}".format(project_id), |
| 62 | + headers=headers, |
| 63 | + ) |
| 64 | + |
| 65 | + if response.status == 200: |
| 66 | + return response.data |
| 67 | + |
| 68 | + |
| 69 | +def dns_access_client_library(_, project_id): |
| 70 | + service = discovery.build("dns", "v1") |
| 71 | + request = service.projects().get(project=project_id) |
| 72 | + return request.execute() |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture(params=[dns_access_direct, dns_access_client_library]) |
| 76 | +def dns_access(request, http_request, service_account_info): |
| 77 | + # Fill in the fixtures on the functions, |
| 78 | + # so that we don't have to fill in the parameters manually. |
| 79 | + def wrapper(): |
| 80 | + return request.param(http_request, service_account_info["project_id"]) |
| 81 | + |
| 82 | + yield wrapper |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def oidc_credentials(service_account_file, http_request): |
| 87 | + result = service_account.IDTokenCredentials.from_service_account_file( |
| 88 | + service_account_file, target_audience=_AUDIENCE_OIDC |
| 89 | + ) |
| 90 | + result.refresh(http_request) |
| 91 | + yield result |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture |
| 95 | +def service_account_info(service_account_file): |
| 96 | + with open(service_account_file) as f: |
| 97 | + yield json.load(f) |
| 98 | + |
| 99 | + |
| 100 | +# Our external accounts tests involve setting up some preconditions, setting a |
| 101 | +# credential file, and then making sure that our client libraries can work with |
| 102 | +# the set credentials. |
| 103 | +def get_project_dns(dns_access, credential_data): |
| 104 | + with NamedTemporaryFile() as credfile: |
| 105 | + credfile.write(json.dumps(credential_data).encode("utf-8")) |
| 106 | + credfile.flush() |
| 107 | + old_credentials = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") |
| 108 | + |
| 109 | + with patch.dict(os.environ, {"GOOGLE_APPLICATION_CREDENTIALS": credfile.name}): |
| 110 | + # If our setup and credential file are correct, |
| 111 | + # discovery.build should be able to establish these as the default credentials. |
| 112 | + return dns_access() |
| 113 | + |
| 114 | + |
| 115 | +# This test makes sure that setting an accesible credential file |
| 116 | +# works to allow access to Google resources. |
| 117 | +def test_file_based_external_account( |
| 118 | + oidc_credentials, service_account_info, dns_access |
| 119 | +): |
| 120 | + with NamedTemporaryFile() as tmpfile: |
| 121 | + tmpfile.write(oidc_credentials.token.encode("utf-8")) |
| 122 | + tmpfile.flush() |
| 123 | + |
| 124 | + assert get_project_dns( |
| 125 | + dns_access, |
| 126 | + { |
| 127 | + "type": "external_account", |
| 128 | + "audience": _AUDIENCE_OIDC, |
| 129 | + "subject_token_type": "urn:ietf:params:oauth:token-type:jwt", |
| 130 | + "token_url": "https://sts.googleapis.com/v1/token", |
| 131 | + "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{}:generateAccessToken".format( |
| 132 | + oidc_credentials.service_account_email |
| 133 | + ), |
| 134 | + "credential_source": { |
| 135 | + "file": tmpfile.name, |
| 136 | + }, |
| 137 | + }, |
| 138 | + ) |
0 commit comments