Modifying Identity and Access Management (IAM) policies across Google Cloud services requires a read-modify-write cycle guarded by optimistic concurrency control (OCC). This document shows you how to update an IAM policy using an OCC retry loop in the Google Cloud client libraries for Swift.
Before you begin
This document uses the Secret Manager API and an existing secret as an
example, though the same pattern applies to any Google Cloud service that
exposes getIamPolicy and setIamPolicy. To prepare your environment, complete
the following steps:
- Set up the Swift client libraries and enable the Secret Manager API by following Quickstart: Get started with Swift.
- If you don't have a secret, create one by following the Secret Manager quickstart.
- Verify that your principal has permission to read and update
IAM policies on the secret, such as the Secret Manager Admin
role (
roles/secretmanager.admin).
Add required dependencies
In addition to the swift-google-cloud-secretmanager-v1 client library, you
need types from the swift-google-iam-v1 and
swift-google-gax packages on GitHub to update
IAM policies and handle OCC conflicts. To add these packages and
their GoogleIAMV1 and GoogleGax modules, follow these steps:
Add the packages to your
Package.swiftdependencies:swift package add-dependency \ https://github.com/googleapis/swift-google-iam-v1.git --from 0.6.0 swift package add-dependency \ https://github.com/googleapis/swift-google-gax.git --from 0.6.0Add the
GoogleIAMV1andGoogleGaxmodules to your target dependencies:swift package add-target-dependency \ GoogleIAMV1 TARGET_NAME \ --package swift-google-iam-v1 swift package add-target-dependency \ GoogleGax TARGET_NAME \ --package swift-google-gaxReplace
TARGET_NAMEwith the name of your target.
Why IAM policy updates require an OCC loop
Google Cloud IAM policies update as a whole document rather than by patching individual role bindings on the server. To prevent concurrent writers from overwriting each other's changes, IAM uses OCC across three stages:
- Read: your app calls
getIamPolicyto fetch the currentPolicyobject, which includes anetagfield identifying the exact version of the policy stored on the server. - Modify: your app updates the
bindingsarray on the returnedPolicyobject in memory while preserving itsetagfield. - Write: your app calls
setIamPolicywith the modifiedPolicyobject, and the server compares theetagvalue in your request against the current policy version.
If another process modifies the policy between your getIamPolicy and
setIamPolicy calls, the etag value no longer matches. The server rejects the
write with RequestError.service(let serviceError), where serviceError.code ==
.aborted (HTTP 409 Conflict).
The client library's built-in retry policy can't
resolve this conflict because repeating the same setIamPolicy request with a
stale etag value fails on every attempt. Instead, your app must catch
.aborted, wait using exponential backoff with jitter, and repeat the
read-modify-write cycle to apply your changes to the updated policy.
If you add, modify, or remove bindings on a policy that uses conditional role
bindings, set policy.version = 3 before calling setIamPolicy.
Write an OCC read-modify-write loop
To add an IAM role binding using an OCC read-modify-write loop, follow these steps:
Add the imports needed for the service client,
GoogleGax, andGoogleIAMV1:Define a function that accepts the target resource identifiers, the IAM
role, such as"roles/secretmanager.secretAccessor", and thememberprincipal, such as"user:dana@example.com":Initialize the client and configure an
ExponentialBackoffpolicy to compute jittered delays between retry attempts:Run a bounded loop that reads the current policy, applies the binding change, and writes the updated policy. If
setIamPolicyfails with.aborted, wait for the backoff delay and retry the cycle fromgetIamPolicy:
When setIamPolicy succeeds, it returns the updated Policy with the new role
binding. You can verify the updated policy by running
gcloud secrets get-iam-policy:
gcloud secrets get-iam-policy SECRET_ID
Replace SECRET_ID with your secret ID.
Complete code
The following example shows the complete code for this document:
What's next
- Learn how to override the default retry policies.
- Learn how to update a resource using a field mask.
- Learn how to call paginated operations.
- Learn how to call long-running operations.
- Browse the Google Cloud client libraries for Swift reference documentation.