Class ComputeEngineCredentials
- All Implemented Interfaces:
IdTokenProvider,QuotaProjectIdProvider,ServiceAccountSigner,Serializable
Fetches access tokens from the Google Compute Engine metadata server.
These credentials use the IAM API to sign data. See sign(byte[]) for more details.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumExperimental Feature.static classstatic enumExperimental Feature.Nested classes/interfaces inherited from class com.google.auth.oauth2.OAuth2Credentials
OAuth2Credentials.CredentialsChangedListenerNested classes/interfaces inherited from interface com.google.auth.oauth2.IdTokenProvider
IdTokenProvider.OptionNested classes/interfaces inherited from interface com.google.auth.ServiceAccountSigner
ServiceAccountSigner.SigningException -
Field Summary
Fields inherited from class com.google.auth.oauth2.GoogleCredentials
quotaProjectIdFields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE -
Method Summary
Modifier and TypeMethodDescriptionstatic ComputeEngineCredentialscreate()Create a new ComputeEngineCredentials instance with default behavior.createScoped(Collection<String> newScopes) Clones the compute engine account with the specified scopes.createScoped(Collection<String> newScopes, Collection<String> newDefaultScopes) Clones the compute engine account with the specified scopes and default scopes.booleanReturns the email address associated with the GCE default service account.static Stringstatic Stringstatic StringgetMetadataServerUrl(com.google.auth.oauth2.DefaultCredentialsProvider provider) Retrieves the Google Cloud project ID from the Compute Engine (GCE) metadata server.static Stringfinal Collection<String>static Stringstatic Stringstatic StringgetTokenServerEncodedUrl(com.google.auth.oauth2.DefaultCredentialsProvider provider) Gets the universe domain from the GCE metadata server.static StringinthashCode()idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) Returns a Google ID Token from the metadata server on ComputeEngineRefresh the access token by getting it from the GCE metadata serverbyte[]sign(byte[] toSign) Signs the provided bytes using the private key associated with the service account.protected com.google.common.base.MoreObjects.ToStringHelperA helper for overriding the toString() method.Methods inherited from class com.google.auth.oauth2.GoogleCredentials
create, create, createDelegated, createScoped, createScopedRequired, createWithCustomRetryStrategy, createWithQuotaProject, fromStream, fromStream, getAdditionalHeaders, getApplicationDefault, getApplicationDefault, getCredentialInfo, getQuotaProjectId, isExplicitUniverseDomain, toStringMethods inherited from class com.google.auth.oauth2.OAuth2Credentials
addChangeListener, getAccessToken, getAuthenticationType, getFromServiceLoader, getRequestMetadata, getRequestMetadata, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListenerMethods inherited from class com.google.auth.Credentials
blockingGetToCallback, getRequestMetadata
-
Method Details
-
getMetricsCredentialType
- Overrides:
getMetricsCredentialTypein classCredentials
-
createScoped
Clones the compute engine account with the specified scopes.- Overrides:
createScopedin classGoogleCredentials- Parameters:
newScopes- Collection of scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createScoped
public GoogleCredentials createScoped(Collection<String> newScopes, Collection<String> newDefaultScopes) Clones the compute engine account with the specified scopes and default scopes.- Overrides:
createScopedin classGoogleCredentials- Parameters:
newScopes- Collection of scopes to request.newDefaultScopes- Collection of default scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
create
Create a new ComputeEngineCredentials instance with default behavior.- Returns:
- new ComputeEngineCredentials
-
getScopes
-
getUniverseDomain
Gets the universe domain from the GCE metadata server.Returns an explicit universe domain if it was provided during credential initialization.
Returns the
Credentials.GOOGLE_DEFAULT_UNIVERSEif universe domain endpoint is not found (404) or returns an empty string.Otherwise, returns universe domain from GCE metadata service.
Any above value is cached for the credential lifetime.
- Overrides:
getUniverseDomainin classGoogleCredentials- Returns:
- string representing a universe domain in the format some-domain.xyz
- Throws:
IOException- if a call to GCE metadata service was unsuccessful. Check if exception implements theRetryableandisRetryable()will return true if the operation may be retried.
-
getProjectId
Retrieves the Google Cloud project ID from the Compute Engine (GCE) metadata server.On its first successful execution, it fetches the project ID and caches it for the lifetime of the object. Subsequent calls will return the cached value without making additional network requests.
If the request to the metadata server fails (e.g., due to network issues, or if the VM lacks the required service account permissions), the method will attempt to fall back to a default project ID provider which could be
null.- Overrides:
getProjectIdin classGoogleCredentials- Returns:
- the GCP project ID string, or
nullif the metadata server is inaccessible and no fallback project ID can be determined.
-
refreshAccessToken
Refresh the access token by getting it from the GCE metadata server- Overrides:
refreshAccessTokenin classOAuth2Credentials- Returns:
- never
- Throws:
IOException
-
idTokenWithAudience
public IdToken idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) throws IOException Returns a Google ID Token from the metadata server on ComputeEngine- Specified by:
idTokenWithAudiencein interfaceIdTokenProvider- Parameters:
targetAudience- the aud: field the IdToken should includeoptions- list of Credential specific options for the token. For example, an IDToken for a ComputeEngineCredential could have the full formatted claims returned if IdTokenProvider.Option.FORMAT_FULL) is provided as a list option. Valid option values are:
IdTokenProvider.Option.FORMAT_FULL
IdTokenProvider.Option.LICENSES_TRUE
If no options are set, the defaults are "&format=standard&licenses=false"- Returns:
- IdToken object which includes the raw id_token, JsonWebSignature
- Throws:
IOException- if the attempt to get an IdToken failed
-
getMetadataServerUrl
public static String getMetadataServerUrl(com.google.auth.oauth2.DefaultCredentialsProvider provider) -
getMetadataServerUrl
-
getTokenServerEncodedUrl
public static String getTokenServerEncodedUrl(com.google.auth.oauth2.DefaultCredentialsProvider provider) -
getTokenServerEncodedUrl
-
getUniverseDomainUrl
-
getServiceAccountsUrl
-
getIdentityDocumentUrl
-
getProjectIdUrl
-
hashCode
public int hashCode()- Overrides:
hashCodein classGoogleCredentials
-
toStringHelper
protected com.google.common.base.MoreObjects.ToStringHelper toStringHelper()Description copied from class:GoogleCredentialsA helper for overriding the toString() method. This allows inheritance of super class fields. Extending classes can override this implementation and call super implementation and add more fields. Same cannot be done with overriding the toString() directly.- Overrides:
toStringHelperin classGoogleCredentials- Returns:
- an instance of the ToStringHelper that has public fields added
-
equals
- Overrides:
equalsin classGoogleCredentials
-
toBuilder
- Overrides:
toBuilderin classGoogleCredentials
-
newBuilder
-
getAccount
Returns the email address associated with the GCE default service account.- Specified by:
getAccountin interfaceServiceAccountSigner- Throws:
RuntimeException- if the default service account cannot be read
-
sign
public byte[] sign(byte[] toSign) Signs the provided bytes using the private key associated with the service account.The Compute Engine's project must enable the Identity and Access Management (IAM) API and the instance's service account must have the iam.serviceAccounts.signBlob permission.
- Specified by:
signin interfaceServiceAccountSigner- Parameters:
toSign- bytes to sign- Returns:
- signed bytes
- Throws:
ServiceAccountSigner.SigningException- if the attempt to sign the provided bytes failed- See Also:
-