|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Demonstrates how to perform basic operations with Google Cloud IAM |
| 18 | +service account keys. |
| 19 | +
|
| 20 | +For more information, see the documentation at |
| 21 | +https://cloud.google.com/iam/docs/creating-managing-service-account-keys. |
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | +import os |
| 26 | + |
| 27 | +from google.oauth2 import service_account |
| 28 | +import googleapiclient.discovery |
| 29 | + |
| 30 | +credentials = service_account.Credentials.from_service_account_file( |
| 31 | + filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], |
| 32 | + scopes=['https://www.googleapis.com/auth/cloud-platform']) |
| 33 | +service = googleapiclient.discovery.build( |
| 34 | + 'iam', 'v1', credentials=credentials) |
| 35 | + |
| 36 | + |
| 37 | +# [START iam_create_key] |
| 38 | +def create_key(service_account_email): |
| 39 | + """Creates a key for a service account.""" |
| 40 | + |
| 41 | + # pylint: disable=no-member |
| 42 | + key = service.projects().serviceAccounts().keys().create( |
| 43 | + name='projects/-/serviceAccounts/' + service_account_email, body={} |
| 44 | + ).execute() |
| 45 | + |
| 46 | + print('Created key: ' + key['name']) |
| 47 | +# [END iam_create_key] |
| 48 | + |
| 49 | + |
| 50 | +# [START iam_list_keys] |
| 51 | +def list_keys(service_account_email): |
| 52 | + """Lists all keys for a service account.""" |
| 53 | + |
| 54 | + # pylint: disable=no-member |
| 55 | + keys = service.projects().serviceAccounts().keys().list( |
| 56 | + name='projects/-/serviceAccounts/' + service_account_email).execute() |
| 57 | + |
| 58 | + for key in keys['keys']: |
| 59 | + print('Key: ' + key['name']) |
| 60 | +# [END iam_list_keys] |
| 61 | + |
| 62 | + |
| 63 | +# [START iam_delete_key] |
| 64 | +def delete_key(full_key_name): |
| 65 | + """Deletes a service account key.""" |
| 66 | + |
| 67 | + # pylint: disable=no-member |
| 68 | + service.projects().serviceAccounts().keys().delete( |
| 69 | + name=full_key_name).execute() |
| 70 | + |
| 71 | + print('Deleted key: ' + full_key_name) |
| 72 | +# [END iam_delete_key] |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description=__doc__, |
| 78 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 79 | + |
| 80 | + subparsers = parser.add_subparsers(dest='command') |
| 81 | + |
| 82 | + create_key_parser = subparsers.add_parser( |
| 83 | + 'create', help=create_key.__doc__) |
| 84 | + create_key_parser.add_argument('service_account_email') |
| 85 | + |
| 86 | + list_keys_parser = subparsers.add_parser( |
| 87 | + 'list', help=list_keys.__doc__) |
| 88 | + list_keys_parser.add_argument('service_account_email') |
| 89 | + |
| 90 | + delete_key_parser = subparsers.add_parser( |
| 91 | + 'delete', help=delete_key.__doc__) |
| 92 | + delete_key_parser.add_argument('full_key_name') |
| 93 | + |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + if args.command == 'list': |
| 97 | + list_keys(args.service_account_email) |
| 98 | + elif args.command == 'create': |
| 99 | + create_key(args.service_account_email) |
| 100 | + elif args.command == 'delete': |
| 101 | + delete_key(args.full_key_name) |
0 commit comments