This page shows you how to create, disable, and delete Hash-based Message Authentication Code (HMAC) keys associated with service accounts in your project.
Before you begin
Before using this feature in Cloud Storage, you must meet the following requirements:
Have sufficient permission to work with HMAC keys in the selected project:
If you own the project, you most likely have the necessary permissions.
You should have the IAM permissions that are prefixed with
storage.hmacKeys
for the project. See Using IAM Permissions for instructions on how to get a role, such as Storage HMAC Key Admin, that has these permissions.
Have a service account in your project that you intend to create HMAC keys for. See Creating a service account if you don't currently have one.
Have the
restrictAuthTypes
constraint disabled for HMAC key authentication. See Creating and managing organization policies for instructions on how to check and disable the constraint.
Create an HMAC key
To create an HMAC key for a service account:
Console
- In the Google Cloud console, go to the Cloud Storage Settings page.
Select the Interoperability tab.
Click add_box Create a key for a service account.
Select the service account you want the HMAC key to be associated with.
Click Create key.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, see Troubleshooting.
Command line
Use the hmac create
command:
gcloud storage hmac create SERVICE_ACCOUNT_EMAIL
Where SERVICE_ACCOUNT_EMAIL
is the email address
associated with your service account. For example,
service-7550275089395@my-pet-project.iam.gserviceaccount.com
.
If successful, the response contains an HMAC key resource,
including values for the accessId
and secret
.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Terraform
You can use a Terraform resource to create an HMAC key. This sample also includes a resource to create a service account.
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aPOST
hmacKeys request,:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/projects/PROJECT_IDENTIFIER/hmacKeys?serviceAccountEmail=SERVICE_ACCOUNT_EMAIL"
Where:
PROJECT_IDENTIFIER
is the ID or number for the project associated with the key you want to create. For example,my-pet-project
.SERVICE_ACCOUNT_EMAIL
is the email address associated with your service account. For example,service-7550275089395@my-pet-project.iam.gserviceaccount.com
.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aPOST
HMAC Key request:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/?Action=CreateAccessKey&UserName=SERVICE_ACCOUNT_EMAIL"
Where
SERVICE_ACCOUNT_EMAIL
is the email address associated with your service account. For example,service-7550275089395@my-pet-project.iam.gserviceaccount.com
.
Get HMAC key information
To list the HMAC keys for a project, and get information about the keys:
Console
- In the Google Cloud console, go to the Cloud Storage Settings page.
Select the Interoperability tab.
Service accounts that have HMAC keys associated with them appear in the Access keys for service accounts subsection of the Service account HMAC section.
Click the name of a specific service account to see the HMAC keys associated with it and the status of those keys.
Command line
Use the
hmac list
command to list hmac keys in your project:gcloud storage hmac list
If successful, the command returns a list of hmac key access IDs, along with the state of each key and the service account associated with each key.
Use the
hmac describe
command to retrieve metadata for a specific key:gcloud storage hmac describe KEY_ACCESS_ID
Where
KEY_ACCESS_ID
is the access ID for the desired key.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample retrieves a list of HMAC keys associated with a project: The following sample retrieves information for a specific HMAC key:C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aLIST
hmacKeys request:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/projects/PROJECT_IDENTIFIER/hmacKeys"
Where
PROJECT_IDENTIFIER
is the ID or number for the project associated with the keys you want to list. For example,my-pet-project
.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aGET
HMAC Key request:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/?Action=ListAccessKeys&UserName=SERVICE_ACCOUNT_EMAIL"
Where
SERVICE_ACCOUNT_EMAIL
is the email address associated with your service account. For example,service-7550275089395@my-pet-project.iam.gserviceaccount.com
.
Update the state of an HMAC key
To switch an HMAC key between being active and inactive:
Console
- In the Google Cloud console, go to the Cloud Storage Settings page.
Select the Interoperability tab.
In the Access keys for service accounts subsection, click the name of the service account associated with the HMAC key whose status you want to update.
Click the status of the key you want to update.
If you are changing the key's state from Inactive to Active, click Deactivate in the window that appears.
If you are changing the key's state from Active to Inactive, no additional steps are required.
Command line
Use the hmac update
command:
gcloud storage hmac update ACCESS_KEY_ID STATE
Where:
ACCESS_KEY_ID
is the access ID associated with the key you are updating.STATE
is either--activate
or--deactivate
.
If successful, the command returns the updated metadata of the HMAC key.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample deactivates an HMAC key: The following sample activates an HMAC key:C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Create a JSON file that contains the following information:
{"state": "STATE"}
Where
STATE
is the desired state for the key. For example,INACTIVE
.Use
cURL
to call the JSON API with aPUT
hmacKeys request:curl -X PUT --data-binary @JSON_FILE_NAME \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/projects/PROJECT_IDENTIFIER/hmacKeys/ACCESS_KEY_ID"
Where:
JSON_FILE_NAME
is the path for the file that you created in Step 2.PROJECT_IDENTIFIER
is the ID or number for the project associated with the key you want to update. For example,my-pet-project
.ACCESS_KEY_ID
is the access ID associated with the key you are updating.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aPOST
HMAC Key request:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/?Action=UpdateAccessKey&AccessKeyId=ACCESS_KEY_ID&Status=STATUS"
Where:
ACCESS_KEY_ID
is the access ID associated with the key you are updating.STATUS
is the desired status for the key. For example,Inactive
.
When you change the state of an HMAC key, it takes up to 3 minutes for the state change to propagate through the Cloud Storage system. For this reason, you should wait at least 3 minutes between making an HMAC key inactive and deleting the key.
Delete an HMAC key
An HMAC key must be in an inactive state in order to delete it. To delete an inactive HMAC key:
Console
- In the Google Cloud console, go to the Cloud Storage Settings page.
Select the Interoperability tab.
In the Access keys for service accounts subsection, click the name of the service account associated with the HMAC key you want to delete.
Click the Trash icon associated with the key you want to delete.
In the dialog that appears, enter the first 10 characters of the access key ID as they are given in the window.
Click Delete.
Command line
Use the hmac delete
command:
gcloud storage hmac delete ACCESS_KEY_ID
Where ACCESS_KEY_ID
is the access ID associated
with the key you are deleting.
If successful, the command does not return a response.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aDELETE
hmacKeys request:curl -X DELETE \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/projects/PROJECT_IDENTIFIER/hmacKeys/ACCESS_KEY_ID"
Where:
PROJECT_IDENTIFIER
is the ID or number for the project associated with the key you want to delete. For example,my-pet-project
.ACCESS_KEY_ID
is the access ID associated with the key you are deleting.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aPOST
HMAC Key request:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/?Action=DeleteAccessKey&AccessKeyId=ACCESS_KEY_ID"
Where
ACCESS_KEY_ID
is the access ID associated with the key you are deleting.
What's next
- Follow the guidelines for migrating from user account HMAC keys to service account HMAC keys.
- Use an HMAC key in an authenticated request.