Service account keys, even when stored securely, provide persistent access, which becomes a security risk if the keys are exposed. They are valid for extended periods, requiring manual intervention for rotation or revocation.
from google.cloud import storage
import os
# Long-lived service account key loaded securely
service_account_key =
os.getenv('GCP_SERVICE_ACCOUNT_KEY_PATH') # Loaded from
secure path
client =
storage.Client.from_service_account_json(service_account_key)
# List buckets in GCP
buckets = list(client.list_buckets())
print("Buckets:", buckets)
Workload Identity Federation eliminates the need for long-lived service account keys, using short-lived tokens that are automatically managed.
By using Workload Identity Federation, the credentials are ephemeral, automatically rotated, and valid only for short durations. This eliminates the need for long-lived service account keys and significantly reduces the risk of long-term exposure.
To convert a service account key to a role-based authentication in Google Cloud Platform (GCP), you begin by creating a new IAM role with the necessary permissions that match the original service account's access level.
Next, update the applications or services that rely on the key to use Workload Identity Federation or the Application Default Credentials to assume the role and obtain temporary credentials without using long-term keys.
Once the applications are updated to use these secure methods for authentication, you can disable and delete the old service account key, improving security by leveraging short-lived, dynamically generated credentials instead of static keys.
from google.cloud import storage
# Use ephemeral credentials via Workload Identity Federation
client = storage.Client() # Credentials automatically
picked up from environment
# List buckets in GCP
buckets = list(client.list_buckets())
print("Buckets:", buckets)
When an Azure resource (e.g., a VM or Azure Function) with a managed identity needs to access another Azure service (such as a Key Vault, Storage Account, or Azure SQL Database), it authenticates using Azure AD. This process does not require any manual management of credentials, as Azure manages the lifecycle of the identity and authentication behind the scenes.
GET https://storage.googleapis.com/storage/v1/b/my-bucket
Authorization: Bearer <OAuth-Token>
This token-based authentication mechanism ensures that the service account is authenticated securely without needing long-lived credentials
IAM Policy: Resources in GCP (such as Cloud Storage buckets, BigQuery datasets, or Compute Engine instances) have associated IAM policies that specify who can access the resource and what actions they can perform. These policies define which identities (service accounts, users, or groups) can access the resource and what roles they have.
Example of an IAM policy:
json
{
"bindings": [
{
"role": "roles/storage.objectViewer",
"members": [
"serviceAccount:my-service-account@my-project.iam.gserviceaccount.com"
]
}
]
}
This policy grants the service account the ability to view objects in a specific Cloud Storage bucket.
You can just assign a service account to a resource (In this case a Compute Engine instance) instead of using the creds within it.