Beispiel #1
0
def _GetServiceAccountCreds(args):
    """Gets service account credentials from given file path or default if any.

  Args:
    args: Command arguments.

  Returns:
    str, service account credentials.
  """
    if args.json_key:
        file_content = _LoadJsonFile(args.json_key)
        return base64.b64encode(file_content).decode("utf-8")

    account = properties.VALUES.core.account.Get()
    if not account:
        raise store.NoActiveAccountException()
    cred = store.Load(account, prevent_refresh=True)
    if not cred:
        raise store.NoCredentialsForAccountException(account)

    account_type = creds.CredentialType.FromCredentials(cred)
    if account_type == creds.CredentialType.SERVICE_ACCOUNT:
        paths = config.Paths()
        json_content = files.ReadFileContents(
            paths.LegacyCredentialsAdcPath(account))
        return base64.b64encode(json_content.encode("utf-8")).decode("utf-8")
    return ""
Beispiel #2
0
def CheckCredOrExit(can_be_gce=False):
    try:
        cred = c_store.Load()
        if not can_be_gce and isinstance(cred, gce.AppAssertionCredentials):
            raise c_store.NoActiveAccountException()
    except (c_store.NoActiveAccountException,
            c_store.NoCredentialsForAccountException) as e:
        sys.stderr.write(str(e) + '\n\n')
        sys.exit(1)