Skip to main content

Running a Broker on Google Cloud

If your Broker runs on Google Cloud, it signs in to the Wi-DAS Certificate API as a Google Cloud service account. It asks Google for an ID token (a short-lived, Google-signed OpenID Connect token) for our audience, and sends it as the bearer token on POST /broker/certificates.

This page covers only what's different on Google Cloud: setting up the service account, registering it with us, and getting a token. Everything else is the same as for any Broker, so read Brokered issuance first. Its request body, Idempotency-Key rules and refusals all apply here unchanged.

What you need​

  • A service account that your Broker runs as.
  • A way for your Broker to get an ID token for that service account.
  • A registration with us for the service account.

You don't need a client secret, a certificate or a service account key file. Google signs the token and we only check it, so neither of us holds secret material, as long as your Broker runs on Google Cloud as that service account.

1. Create a service account for your Broker​

Create a service account that does nothing but act as your Broker, and run your Broker as it:

  • Cloud Run: set the service's service account to this one. If you don't, it runs as the project's Compute Engine default service account.
  • GKE (Google Kubernetes Engine): use Workload Identity Federation for GKE, and bind the Broker's Kubernetes service account to this service account. Without that, the metadata server answers for the node's service account instead.
  • Compute Engine: attach this service account to the instance.

2. Find its Unique ID​

We register your service account by its numeric Unique ID, not its email address. The Unique ID is the sub claim in the tokens it mints: digits only, usually 21 of them, and never starting with a zero.

  • In the Google Cloud console: go to IAM & Admin → Service accounts, select the service account, and copy Unique ID from its Details tab.

  • With gcloud:

    gcloud iam service-accounts describe {broker-sa-email} --format='value(uniqueId)'

The ID belongs to that one service account, not to its name. If you delete the service account and create it again with the same email address, it gets a new Unique ID. We won't recognise it, and your Broker gets brokerNotAuthorised until you send us the new ID and we register it.

3. Register it with Wi-DAS​

Registration goes through us; there's no self-service. Send us:

  • the service account's Unique ID;
  • which of your apps (Clients) the Broker may request certificates for.

We register the ID against your organisation, give you your app's clientId, and send you the audience to request tokens for. The audience is different in each environment, so use the one we give you for the environment you're calling.

Before we switch the registration on, we confirm the ID with one real call from your Broker:

  1. When we tell you we're ready, make one certificate request from your Broker, as in step 5.
  2. It will be refused with 403 brokerNotAuthorised. That's expected.
  3. Tell us you've made the call. We check the Unique ID it carried against the one you sent us.
  4. We enable the registration, and your Broker can issue from then on.

This proves the ID belongs to a service account you control, so nobody can get a registration made in your name by quoting an ID in an email. Until the registration is enabled, every request gets brokerNotAuthorised.

4. Get an ID token​

The token must be minted for the audience we gave you. There are two ways to get one.

From the metadata server​

On Cloud Run, GKE and Compute Engine, ask the metadata server for an ID token for the service account your Broker runs as:

curl -s -H "Metadata-Flavor: Google" \
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience={audience}"

The response body is the token itself. URL-encode the audience in the query string.

With the IAM Credentials API​

If your Broker's code runs as a different identity, it can mint a token for the Broker's service account with the IAM Credentials generateIdToken method. The caller needs the Service Account OpenID Connect Identity Token Creator role (roles/iam.serviceAccountOpenIdTokenCreator) on the Broker's service account. The IAM Service Account Credentials API (iamcredentials.googleapis.com) must also be enabled in your Google Cloud project, or the call is refused.

curl -s -X POST \
-H "Authorization: Bearer {caller-access-token}" \
-H "Content-Type: application/json" \
-d '{"audience": "{audience}"}' \
"https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{broker-sa-email}:generateIdToken"

The response is {"token": "..."}. You don't need to ask for the email claim: we identify your Broker by its Unique ID only.

In .NET​

The Google.Apis.Auth library does either of these for you. Its OidcToken caches the ID token and gets a new one when it's due, every time you call GetAccessTokenAsync, so you don't write that part yourself. Create the token source once and keep it, because the cache lives on that instance. Register a class like this one as a singleton:

public sealed class WidasTokenSource(string widasAudience)
{
private readonly SemaphoreSlim _gate = new(1, 1);
private volatile OidcToken? _token;

public async Task<string> GetIdTokenAsync(CancellationToken cancellationToken)
{
var token = _token ?? await CreateAsync(cancellationToken);
return await token.GetAccessTokenAsync(cancellationToken); // the ID token, cached and refreshed
}

// Builds the credential and the token source once. If that fails, for example because the metadata
// server isn't ready yet at start-up, nothing is kept and the next call tries again.
private async Task<OidcToken> CreateAsync(CancellationToken cancellationToken)
{
await _gate.WaitAsync(cancellationToken);
try
{
if (_token is null)
{
var credential = await GoogleCredential.GetApplicationDefaultAsync();
_token = await credential.GetOidcTokenAsync(
OidcTokenOptions.FromTargetAudience(widasAudience), cancellationToken);
}

return _token;
}
finally
{
_gate.Release();
}
}
}

Don't cache the Task from that first call in a Lazy<Task<OidcToken>>. If it fails, the Lazy keeps the failed task, and every later call fails the same way until the Broker restarts.

On Cloud Run, GKE and Compute Engine, the default credential is the service account the workload runs as, and the library uses the metadata server. If your code runs as a different service account, call Impersonate on the credential with the Broker's service account, and the library uses generateIdToken instead. A credential for your own Google user account can't mint ID tokens this way, so test from a workload, or impersonate the service account.

Cache it​

An ID token lasts an hour. Cache it and reuse it across requests, and get a new one a few minutes before it expires (its exp claim says when). Don't mint one per certificate request: that adds a call to Google to every enrolment, and a large rollout could run into Google's own quotas.

5. Request a certificate​

Send the token as the bearer token:

POST https://partner-api.wi-das.com/broker/certificates
Authorization: Bearer {id-token}
Content-Type: application/json
Idempotency-Key: {idempotency-key}

The body, the response and the Idempotency-Key header are described in Brokered issuance. The header is required, and a retry must send the same key as the attempt it's retrying; see Retrying safely and Request a certificate for how to make one.

Refusals that are specific to Google Cloud​

The refusals table applies, with these differences in what the sign-in failures mean on this route:

Status and titleWhat it means on Google CloudWhat to do
401The token is missing or expired, was minted for a different audience, or wasn't issued by Google. Also every request while the Google Cloud route is off in that environmentCheck the audience against the one we gave you for this environment, and get a new token. If it still fails, ask us whether the route is enabled there
403 with any title other than brokerNotAuthorisedGoogle signed the token, but its sub is missing or isn't shaped like a Unique ID: digits only, no leading zero, and at most 32 digits. A token minted for a service account or a Google user account never looks like thisGet the token as the Broker's service account, as in step 4. If it still fails, contact us
403 brokerNotAuthorisedThe Unique ID isn't registered, the registration or the app is disabled, or the Broker isn't allowed to issue for this app. A service account that was deleted and recreated lands here, and so does the confirmation call you make while we register you. So does a token for a Google user account: its sub is numeric too, so to us it looks exactly like an unregistered service accountDuring registration, tell us you've made the call. Otherwise, check the token was minted as the Broker's service account, not your own Google account, and that its Unique ID matches what you registered. If it's changed, or the rest looks right, contact us

Keeping the service account safe​

On this route the service account is your Broker's identity. Anything that can mint an ID token for it can request a certificate for any user of the apps it's registered for, so guard it as you would a code-signing key.

  • Keep it dedicated to the Broker. Don't run other workloads as it, and don't reuse an existing service account such as the Compute Engine default one.
  • Grant the Token Creator role narrowly. Give roles/iam.serviceAccountOpenIdTokenCreator on this service account only to the identity that actually mints its tokens, never at project level. The broader Service Account Token Creator role (roles/iam.serviceAccountTokenCreator) and the Workload Identity User role (roles/iam.workloadIdentityUser) let their holders mint its tokens too.
  • Know who can run workloads as it. Anyone with the Service Account User role (roles/iam.serviceAccountUser) on the service account can run a Cloud Run service, GKE workload or VM as it, read an ID token from that workload's metadata server, and so issue certificates for every user of your registered apps. So can anyone who can deploy or change a workload that already runs as it, such as by pushing a new revision of the Broker's Cloud Run service.
  • Check who holds those permissions. Roles are granted on the service account, its project, folder and organisation, and custom and basic roles can carry the same permissions. Check which principals hold iam.serviceAccounts.getOpenIdToken or iam.serviceAccounts.actAs on the account, for example with Policy Analyzer, not only who holds the roles named here.
  • Avoid key files. Run the Broker as the service account on Google Cloud rather than downloading a key for it. A key file is a long-lived credential that can mint tokens from anywhere.
  • Disable it promptly if something goes wrong. If you think the service account or a workload running as it is compromised, disable the service account in Google Cloud, which stops it minting new tokens, and tell us. We can't delete a registration, but we disable it straight away, and a disabled registration gets brokerNotAuthorised on every request. Disabling the service account doesn't stop tokens it has already minted: we accept each one for about 65 minutes after it was minted, its one-hour lifetime plus the 5 minutes we allow for clock differences. Disabling the registration is what stops those, so tell us as well.