def _IsJsonFile(filename): """Check and validate if given filename is proper json file.""" content = files.GetFileOrStdinContents(filename, binary=True) try: return json.loads(content), True except ValueError as e: if filename.endswith('.json'): raise auth_service_account.BadCredentialFileException( 'Could not read json file {0}: {1}'.format(filename, e)) return content, False
def _LoadJsonFile(filename): """Checks and validates if given filename is a proper JSON file. Args: filename: str, path to JSON file. Returns: bytes, the content of the file. """ content = console_io.ReadFromFileOrStdin(filename, binary=True) try: json.loads(encoding.Decode(content)) return content except ValueError as e: if filename.endswith(".json"): raise service_account.BadCredentialFileException( "Could not read JSON file {0}: {1}".format(filename, e)) raise service_account.BadCredentialFileException( "Unsupported credential file: {0}".format(filename))
def Run(self, args): """Create service account credentials.""" try: cred = auth_service_account.CredentialsFromAdcFile(args.key_file) except auth_service_account.BadCredentialFileException: account = args.account if not account: raise c_exc.RequiredArgumentException( 'ACCOUNT', 'An account is required when using .p12 keys') password = None if args.password_file: try: with open(args.password_file) as f: password = f.read().strip() except IOError as e: raise c_exc.UnknownArgumentException('--password-file', e) elif args.prompt_for_password: password = getpass.getpass('Password: '******'--password-file', 'A .json service account key does not require a password.') account = cred.service_account_email if args.account and args.account != account: raise c_exc.InvalidArgumentException( 'ACCOUNT', 'The given account name does not match the account name in the key ' 'file. This argument can be omitted when using .json keys.' ) try: c_store.ActivateCredentials(account, cred) except c_store.TokenRefreshError as e: log.file_only_logger.exception(e) raise auth_service_account.BadCredentialFileException( 'Failed to activate the given service account. ' 'Please ensure provided key file is valid.') project = args.project if project: properties.PersistProperty(properties.VALUES.core.project, project) log.status.Print( 'Activated service account credentials for: [{0}]'.format(account))