google.cloud.gcp_storage_signed_url module – Generates a signed URL for a GCP Storage object

Note

This module is part of the google.cloud collection (version 1.16.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install google.cloud. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: google.cloud.gcp_storage_signed_url.

Synopsis

  • Generate a V4 signed URL for a GCS object, so that a party without any IAM access to the bucket can perform a single HTTP request (GET, PUT, DELETE or HEAD) against that object until the URL expires.

  • Two signing methods are supported, and exactly one must be configured. With signer_service_account, the URL is signed by a service account other than the one running the module, via the IAM Credentials API signBlob - no service account private key is ever read from disk, but the identity running the module needs roles/iam.serviceAccountTokenCreator on the signer, and generating the URL costs one IAM API call. With hmac_key_access_id / hmac_key_secret, the URL is signed locally from an HMAC key secret - no IAM API call and no serviceAccountTokenCreator grant are needed at all, at the cost of managing an HMAC key credential (created with gcloud storage hmac create or the storage.projects.hmacKeys.create API) instead of an IAM binding.

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.6

  • requests >= 2.18.4 (only when signing with signer_service_account)

  • google-auth >= 1.3.0 (only when signing with signer_service_account)

  • google-cloud-storage >= 1.2.0 (only when signing with signer_service_account)

Parameters

Parameter

Comments

access_token

string

An OAuth2 access token if credential type is accesstoken.

auth_kind

string / required

The type of credential used.

Still required when signing with hmac_key_access_id, even though it is not used to build the signature itself.

Choices:

  • "application"

  • "machineaccount"

  • "serviceaccount"

  • "accesstoken"

bucket

string / required

The name of the bucket containing the object.

env_type

string

Specifies which Ansible environment you’re running this module within.

This should not be set unless you know what you’re doing.

This only alters the User Agent string for any API requests.

expiration_seconds

integer

How long the signed URL stays valid, in seconds.

Must be between 1 and 604800 (7 days), the limit of the V4 signing scheme.

Default: 900

hmac_key_access_id

string

Access ID of an HMAC key belonging to the service account (or user) that signs the URL.

The signing is performed locally, using the V4 HMAC-SHA256 algorithm - no call to the IAM Credentials API is made, and no serviceAccountTokenCreator grant is required.

The HMAC key only authenticates the request; the service account (or user) it belongs to still needs the usual Cloud Storage IAM role on the bucket or object (for example roles/storage.objectViewer for a GET URL) for the resulting URL to actually work.

Requires hmac_key_secret. Mutually exclusive with signer_service_account.

hmac_key_secret

string

Secret of the HMAC key identified by hmac_key_access_id.

Requires hmac_key_access_id.

http_method

string

HTTP method the signed URL is valid for.

Choices:

  • "GET" ← (default)

  • "PUT"

  • "DELETE"

  • "HEAD"

object_name

string / required

The full path of the object inside the bucket.

Any leading / is removed, so /agents/a.rpm and agents/a.rpm both sign the object agents/a.rpm. An object whose name really starts with / cannot be signed with this module.

project

string

The Google Cloud Platform project to use.

region

string

Region used in the credential scope of the V4 signature when signing with an HMAC key. Any value is accepted by Cloud Storage; it does not need to match the bucket’s actual location.

Only used with hmac_key_access_id.

Default: "auto"

scopes

list / elements=string

Array of scopes to be used

service_account_contents

jsonarg

The contents of a Service Account JSON file, either in a dictionary or as a JSON string that represents it.

service_account_email

string

An optional service account email address if machineaccount is selected and the user does not wish to use the default email.

service_account_file

path

The path of a Service Account JSON file if serviceaccount is selected as type.

signer_service_account

string

Email of the service account that signs the URL, via the IAM Credentials API.

It must have read (or write, for http_method=PUT) access to the object; the identity running this module must hold roles/iam.serviceAccountTokenCreator on it.

Mutually exclusive with hmac_key_access_id / hmac_key_secret.

Examples

- name: Generate a signed URL via IAM Credentials API (signBlob), then download it
  block:
    - name: Generate signed URL, signed by a dedicated service account
      google.cloud.gcp_storage_signed_url:
        bucket: my-software-bucket
        object_name: agents/agent-1.2.3.rpm
        signer_service_account: bucket-reader-sa@my-project.iam.gserviceaccount.com
        auth_kind: application
      delegate_to: localhost
      register: package_url

    - name: Download package
      ansible.builtin.get_url:
        url: "{{ package_url.signed_url }}"
        dest: /tmp/agent.rpm

- name: Generate a signed URL locally from an HMAC key, then download it
  block:
    - name: Generate signed URL, signed with an HMAC key - no IAM API call involved
      google.cloud.gcp_storage_signed_url:
        bucket: my-software-bucket
        object_name: agents/agent-1.2.3.rpm
        hmac_key_access_id: GOOG1EZ...
        hmac_key_secret: "{{ vaulted_hmac_secret }}"
        auth_kind: application
      delegate_to: localhost
      register: package_url

    - name: Download package
      ansible.builtin.get_url:
        url: "{{ package_url.signed_url }}"
        dest: /tmp/agent.rpm

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

expires_at

string

When the signed URL stops being valid, in ISO 8601 UTC.

Returned: success

signed_url

string

The generated signed URL.

Returned: success

Authors

  • Google Inc. (@googlecloudplatform)