|
| 1 | +# Copyright 2018 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 | +# https://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 | +"""Get the client ID associated with a Cloud Composer environment.""" |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | + |
| 20 | +def get_client_id(project_id, location, composer_environment): |
| 21 | + # [START composer_get_environment_client_id] |
| 22 | + import google.auth |
| 23 | + import google.auth.transport.requests |
| 24 | + import requests |
| 25 | + import six.moves.urllib.parse |
| 26 | + |
| 27 | + # Authenticate with Google Cloud. |
| 28 | + # See: https://cloud.google.com/docs/authentication/getting-started |
| 29 | + credentials, _ = google.auth.default( |
| 30 | + scopes=['https://www.googleapis.com/auth/cloud-platform']) |
| 31 | + authed_session = google.auth.transport.requests.AuthorizedSession( |
| 32 | + credentials) |
| 33 | + |
| 34 | + # project_id = 'YOUR_PROJECT_ID' |
| 35 | + # location = 'us-central1' |
| 36 | + # composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME' |
| 37 | + |
| 38 | + environment_url = ( |
| 39 | + 'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}' |
| 40 | + '/environments/{}').format(project_id, location, composer_environment) |
| 41 | + composer_response = authed_session.request('GET', environment_url) |
| 42 | + environment_data = composer_response.json() |
| 43 | + airflow_uri = environment_data['config']['airflowUri'] |
| 44 | + |
| 45 | + # The Composer environment response does not include the IAP client ID. |
| 46 | + # Make a second, unauthenticated HTTP request to the web server to get the |
| 47 | + # redirect URI. |
| 48 | + redirect_response = requests.get(airflow_uri, allow_redirects=False) |
| 49 | + redirect_location = redirect_response.headers['location'] |
| 50 | + |
| 51 | + # Extract the client_id query parameter from the redirect. |
| 52 | + parsed = six.moves.urllib.parse.urlparse(redirect_location) |
| 53 | + query_string = six.moves.urllib.parse.parse_qs(parsed.query) |
| 54 | + print(query_string['client_id'][0]) |
| 55 | + # [END composer_get_environment_client_id] |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + parser = argparse.ArgumentParser( |
| 60 | + description=__doc__, |
| 61 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 62 | + parser.add_argument('project_id', help='Your Project ID.') |
| 63 | + parser.add_argument( |
| 64 | + 'location', help='Region of the Cloud Composer environent.') |
| 65 | + parser.add_argument( |
| 66 | + 'composer_environment', help='Name of the Cloud Composer environent.') |
| 67 | + |
| 68 | + args = parser.parse_args() |
| 69 | + get_client_id( |
| 70 | + args.project_id, args.location, args.composer_environment) |
0 commit comments