def testCreateDefaultNoCreate(self):
     self.ClearAllConfigurations()
     active = named_configs.ActiveConfig(force_create=False)
     self.assertEqual('default', active.name)
     self.assertEqual(True, active.is_active)
     self.assertEqual(os.path.join(self.named_config_dir, 'config_default'),
                      active.file_path)
     self.assertEqual({}, active.GetProperties())
     self.assertFalse(os.path.exists(active.file_path))
Exemplo n.º 2
0
def _Load(account, scopes, prevent_refresh):
    """Helper for Load()."""
    # If a credential file is set, just use that and ignore the active account
    # and whatever is in the credential store.
    cred_file_override = properties.VALUES.auth.credential_file_override.Get()
    if cred_file_override:
        log.info('Using alternate credentials from file: [%s]',
                 cred_file_override)
        try:
            cred = client.GoogleCredentials.from_stream(cred_file_override)
        except client.Error as e:
            raise InvalidCredentialFileException(cred_file_override, e)

        if cred.create_scoped_required():
            if scopes is None:
                scopes = config.CLOUDSDK_SCOPES
            cred = cred.create_scoped(scopes)

        # Set token_uri after scopes since token_uri needs to be explicitly
        # preserved when scopes are applied.
        token_uri_override = properties.VALUES.auth.token_host.Get()
        if token_uri_override:
            cred_type = creds.CredentialType.FromCredentials(cred)
            if cred_type in (creds.CredentialType.SERVICE_ACCOUNT,
                             creds.CredentialType.P12_SERVICE_ACCOUNT):
                cred.token_uri = token_uri_override
        # The credential override is not stored in credential store, but we still
        # want to cache access tokens between invocations.
        return creds.MaybeAttachAccessTokenCacheStore(cred)

    if not account:
        account = properties.VALUES.core.account.Get()

    if not account:
        raise NoActiveAccountException(
            named_configs.ActiveConfig(False).file_path)

    cred = STATIC_CREDENTIAL_PROVIDERS.GetCredentials(account)
    if cred is not None:
        return cred

    store = creds.GetCredentialStore()
    cred = store.Load(account)
    if not cred:
        raise NoCredentialsForAccountException(account)

    # cred.token_expiry is in UTC time.
    if (not prevent_refresh
            and (not cred.token_expiry
                 or cred.token_expiry < cred.token_expiry.utcnow())):
        Refresh(cred)

    return cred
 def testCreateDefaultWithAutoMigrateNoForce(self):
     self.ClearAllConfigurations()
     with open(config.Paths().user_properties_path, 'w') as f:
         f.write('[core]\naccount = foo\n')
     active = named_configs.ActiveConfig(force_create=False)
     self.assertEqual('default', active.name)
     self.assertEqual(True, active.is_active)
     self.assertEqual(os.path.join(self.named_config_dir, 'config_default'),
                      active.file_path)
     self.assertEqual({'core': {'account': 'foo'}}, active.GetProperties())
     # File exists even though we said not to force create since there were
     # legacy properties.
     self.assertTrue(os.path.exists(active.file_path))