Hanzo
PlatformHanzo KMSPlatformIdentities

GCP Auth

Learn how to authenticate with Hanzo KMS for services on Google Cloud Platform

GCP Auth is a GCP-native authentication method for GCP resources to access Hanzo KMS. It consists of two sub-methods/approaches:

Diagram

The following sequence diagram illustrates the GCP ID Token Auth workflow for authenticating GCP resources with Hanzo KMS.

sequenceDiagram
  participant GCE as GCP Service
  participant Infis as Hanzo KMS
  participant Google as OAuth2 API

  Note over GCE,Google: Step 1: Instance Identity Token Retrieval
  GCE->>Google: Request instance identity metadata token
  Google-->>GCE: Return JWT token with RS256 signature

  Note over GCE,Infis: Step 2: Identity Token Login Operation
  GCE->>Infis: Send JWT token to /api/v1/auth/gcp-auth/login
  Infis->>Google: Request OAuth2 certificates
  Google-->>Infis: Return certificates

  Note over Infis: Step 3: Identity Token Verification
  Note over Infis: Step 4: Identity Property Validation
  Infis->>GCE: Return short-lived access token

  Note over GCE,Infis: Step 4: Access Hanzo KMS API with Token
  GCE->>Infis: Make authenticated requests using the short-lived access token

Concept

At a high-level, Hanzo KMS authenticates a GCP resource by verifying its identity and checking that it meets specific requirements (e.g. it is an allowed GCE instance) at the /api/v1/auth/gcp-auth/login endpoint. If successful, then Hanzo KMS returns a short-lived access token that can be used to make authenticated requests to the Hanzo KMS API.

To be more specific:

  1. The client running on a GCP service obtains an ID token constituting the identity for a GCP resource such as a GCE instance or Cloud Function; this is a unique JWT token that includes details about the instance as well as Google's RS256 signature.
  2. The client sends the ID token to Hanzo KMS at the /api/v1/auth/gcp-auth/login endpoint.
  3. Hanzo KMS verifies the token against Google's public OAuth2 certificates.
  4. Hanzo KMS checks if the entity behind the ID token is allowed to authenticate with Hanzo KMS based on set criteria such as Allowed Service Account Emails.
  5. If all is well, Hanzo KMS returns a short-lived access token that the client can use to make authenticated requests to the Hanzo KMS API.

We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS using GCP ID Token Auth as they handle the authentication process including generating the instance ID token for you.

Also, note that Hanzo KMS needs network-level access to send requests to the Google Cloud API as part of the GCP Auth workflow.

Guide

In the following steps, we explore how to create and use identities for your workloads and applications on GCP to access the Hanzo KMS API using the GCP ID Token authentication method.

To create an identity, head to your Organization Settings > Access Control > Identities and press Create identity.

identities organization

When creating an identity, you specify an organization level role for it to assume; you can configure roles in Organization Settings > Access Control > Organization Roles.

identities organization create

Now input a few details for your new identity. Here's some guidance for each field:

  • Name (required): A friendly name for the identity.
  • Role (required): A role from the Organization Roles tab for the identity to assume. The organization role assigned will determine what organization level resources this identity can have access to.

Once you've created an identity, you'll be redirected to a page where you can manage the identity.

identities page

Since the identity has been configured with Universal Auth by default, you should re-configure it to use GCP Auth instead. To do this, press to edit the Authentication section, remove the existing Universal Auth configuration, and add a new GCP Auth configuration onto the identity; set the Type field to GCP ID Token Auth.

identities page remove default auth

identities create gcp auth method

Here's some more guidance on each field:

  • Allowed Service Account Emails: A comma-separated list of trusted service account emails corresponding to the GCE resource(s) allowed to authenticate with Hanzo KMS; this could be something like test@project.iam.gserviceaccount.com, 12345-compute@developer.gserviceaccount.com, etc.
  • Allowed Projects: A comma-separated list of trusted GCP projects that the GCE instance must belong to authenticate with Hanzo KMS. Note that this validation property will only work for GCE instances.
  • Allowed Zones: A comma-separated list of trusted zones that the GCE instances must belong to authenticate with Hanzo KMS; this should be the fully-qualified zone name in the format <region>-<zone>like us-central1-a, us-west1-b, etc. Note that this validation property will only work for GCE instances.
  • Access Token TTL (default is 2592000 equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time.
  • Access Token Max TTL (default is 2592000 equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time.
  • Access Token Max Number of Uses (default is 0): The maximum number of times that an access token can be used; a value of 0 implies infinite number of uses.
  • Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the 0.0.0.0/0, allowing usage from any network address.

To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project.

To do this, head over to the project you want to add the identity to and go to Project Settings > Access Control > Machine Identities and press Add identity.

Next, select the identity you want to add to the project and the project level role you want to allow it to assume. The project role assigned will determine what project level resources this identity can have access to.

identities project

identities project create

To access the Hanzo KMS API as the identity, you need to generate an ID token constituting the identity of the present GCE instance and make a request to the /api/v1/auth/gcp-auth/login endpoint containing the token in exchange for an access token.

We provide a few code examples below of how you can authenticate with Hanzo KMS to access the Hanzo KMS API.

Start by making a request from the GCE instance to obtain the ID token. For more examples of how to obtain the token in Java, Go, Node.js, etc. refer to the official documentation.

Sample request

curl -H "Metadata-Flavor: Google" \
'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=<identityId>&format=full'

Note that you should replace <identityId> with the ID of the identity you created in step 1.

Next use send the obtained JWT token along to authenticate with Hanzo KMS and obtain an access token.

Sample request

curl --location --request POST 'https://app.kms.hanzo.ai/api/v1/auth/gcp-auth/login' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'identityId=...' \
    --data-urlencode 'jwt=...'

Sample response

{
    "accessToken": "...",
    "expiresIn": 7200,
    "accessTokenMaxTTL": 43244
    "tokenType": "Bearer"
}

Next, you can use the access token to access the Hanzo KMS API

We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS using GCP IAM Auth as they handle the authentication process including generating the signed JWT token.

Each identity access token has a time-to-live (TTL) which you can infer from the response of the login operation; the default TTL is 7200 seconds which can be adjusted. If an identity access token expires, it can no longer authenticate with the Hanzo KMS API. In this case, a new access token should be obtained by performing another login operation.

Diagram

The following sequence diagram illustrates the GCP IAM Auth workflow for authenticating GCP IAM service accounts with Hanzo KMS.

sequenceDiagram
  participant GCE as Client
  participant Infis as Hanzo KMS
  participant Google as Cloud IAM

  Note over GCE,Google: Step 1: Signed JWT Token Generation
  GCE->>Google: Request to generate signed JWT token
  Google-->>GCE: Return signed JWT token

  Note over GCE,Infis: Step 2: JWT Token Login Operation
  GCE->>Infis: Send signed JWT token to /api/v1/auth/gcp-auth/login
  Infis->>Google: Request public key
  Google-->>Infis: Return public key

  Note over Infis: Step 3: JWT Token Verification
  Note over Infis: Step 4: JWT Property Validation
  Infis->>GCE: Return short-lived access token

  Note over GCE,Infis: Step 5: Access Hanzo KMS API with Token
  GCE->>Infis: Make authenticated requests using the short-lived access token

Concept

At a high-level, Hanzo KMS authenticates an IAM service account by verifying its identity and checking that it meets specific requirements (e.g. it is an allowed service account) at the /api/v1/auth/gcp-auth/login endpoint. If successful, then Hanzo KMS returns a short-lived access token that can be used to make authenticated requests to the Hanzo KMS API.

To be more specific:

  1. The client generates a signed JWT token using the projects.serviceAccounts.signJwt API method; this is done using the service account credentials associated with the client.
  2. The client sends the signed JWT token to Hanzo KMS at the /api/v1/auth/gcp-auth/login endpoint.
  3. Hanzo KMS verifies the signed JWT token.
  4. Hanzo KMS checks if the service account behind the JWT token is allowed to authenticate with Hanzo KMS based Allowed Service Account Emails.
  5. If all is well, Hanzo KMS returns a short-lived access token that the client can use to make authenticated requests to the Hanzo KMS API.

We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS using GCP IAM Auth as they handle the authentication process including generating the signed JWT token.

Also, note that Hanzo KMS needs network-level access to send requests to the Google Cloud API as part of the GCP Auth workflow.

Guide

In the following steps, we explore how to create and use identities for your workloads and applications on GCP to access the Hanzo KMS API using the GCP IAM authentication method.

To create an identity, head to your Organization Settings > Access Control > Identities and press Create identity.

identities organization

When creating an identity, you specify an organization level role for it to assume; you can configure roles in Organization Settings > Access Control > Organization Roles.

identities organization create

Now input a few details for your new identity. Here's some guidance for each field:

  • Name (required): A friendly name for the identity.
  • Role (required): A role from the Organization Roles tab for the identity to assume. The organization role assigned will determine what organization level resources this identity can have access to.

Once you've created an identity, you'll be redirected to a page where you can manage the identity.

identities page

Since the identity has been configured with Universal Auth by default, you should re-configure it to use GCP Auth instead. To do this, press to edit the Authentication section, remove the existing Universal Auth configuration, and add a new GCP Auth configuration onto the identity; set the Type field to GCP IAM Auth.

identities page remove default auth

identities organization create token auth method

Here's some more guidance on each field:

  • Allowed Service Account Emails: A comma-separated list of trusted IAM service account emails that are allowed to authenticate with Hanzo KMS; this could be something like test@project.iam.gserviceaccount.com, 12345-compute@developer.gserviceaccount.com, etc.
  • Access Token TTL (default is 2592000 equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time.
  • Access Token Max TTL (default is 2592000 equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time.
  • Access Token Max Number of Uses (default is 0): The maximum number of times that an access token can be used; a value of 0 implies infinite number of uses.
  • Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the 0.0.0.0/0, allowing usage from any network address.

To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project.

To do this, head over to the project you want to add the identity to and go to Project Settings > Access Control > Machine Identities and press Add identity.

Next, select the identity you want to add to the project and the project level role you want to allow it to assume. The project role assigned will determine what project level resources this identity can have access to.

identities project

identities project create

To access the Hanzo KMS API as the identity, you need to generate a signed JWT token using the projects.serviceAccounts.signJwt API method and make a request to the /api/v1/auth/gcp-auth/login endpoint containing the signed JWT token in exchange for an access token.

Make sure that the service account has the iam.serviceAccounts.signJwt permission or the roles/iam.serviceAccountTokenCreator role.

We provide a few code examples below of how you can authenticate with Hanzo KMS to access the Hanzo KMS API.

The following code provides a generic example of how you can generate a signed JWT token against the projects.serviceAccounts.signJwt API method.

The shown example uses Node.js and the official google-auth-library package but you can use any language you wish.

const { GoogleAuth } = require("google-auth-library");

const auth = new GoogleAuth({
    scopes: "https://www.googleapis.com/auth/cloud-platform",
});

const credentials = await auth.getCredentials();

const identityId = "<your-kms-identity-id>";

const jwtPayload = {
    sub: credentials.client_email,
    aud: identityId,
};

const { data } = await client.request({
    url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${credentials.client_email}:signJwt`,
    method: "POST",
    data: { payload: JSON.stringify(jwtPayload) },
});

const jwt = data.signedJwt // send this jwt to Hanzo KMS in the next step

Sample request

curl --location --request POST 'https://app.kms.hanzo.ai/api/v1/auth/gcp-auth/login' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'identityId=...' \
    --data-urlencode 'jwt=...'

Sample response

{
    "accessToken": "...",
    "expiresIn": 7200,
    "accessTokenMaxTTL": 43244
    "tokenType": "Bearer"
}

Next, you can use the access token to access the Hanzo KMS API

We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS using GCP IAM Auth as they handle the authentication process including generating the signed JWT token.

Each identity access token has a time-to-live (TTL) which you can infer from the response of the login operation; the default TTL is 7200 seconds which can be adjusted. If an identity access token expires, it can no longer authenticate with the Hanzo KMS API. In this case, a new access token should be obtained by performing another login operation.

How is this guide?

Last updated on

On this page