Repository navigation
fix(iam): handle undefined policy bindings in Cloud Run and Storage helpers - #335
reginaldalfret wants to merge 2 commits into
Conversation
…elpers Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request improves robustness when retrieving and updating IAM policies in Cloud Run and Cloud Storage helpers by safely handling missing policies, bindings, or members. Corresponding unit tests have been added to verify these scenarios. Feedback on these changes suggests simplifying the conditional logic for initializing invokerBinding.members in run.js and utilizing optional chaining (?.) to streamline the member check in storage.js.
| if (!invokerBinding) { | ||
| invokerBinding = { role: INVOKER_ROLE, members: [] }; | ||
| policy.bindings.push(invokerBinding); | ||
| } else if (!invokerBinding.members) { | ||
| invokerBinding.members = []; | ||
| } |
There was a problem hiding this comment.
The conditional structure can be simplified. By moving the initialization of invokerBinding.members outside of the if block, we can eliminate the else if branch and make the code more concise and readable.
if (!invokerBinding) {
invokerBinding = { role: INVOKER_ROLE, members: [] };
policy.bindings.push(invokerBinding);
}
invokerBinding.members = invokerBinding.members || [];| // Check if member already has the role | ||
| const binding = policy.bindings.find((b) => b.role === role); | ||
| if (binding && binding.members.includes(member)) { | ||
| if (binding && binding.members && binding.members.includes(member)) { |
…l chaining Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
|
Applied the review suggestions:
|
Summary
Safely handle undefined
bindingsandmemberswhen retrieving IAM policies in Cloud Run (setServicePublicAccess) and Cloud Storage (grantBucketAccess) helpers.Root cause
In Google Cloud client libraries,
getIamPolicyandbucket.iam.getPolicyreturn a policy object where thebindingsarray is omitted (undefined) when a newly created Cloud Run service or Cloud Storage bucket has no custom IAM bindings.Previously,
setServicePublicAccessinlib/cloud-api/run.jsandgrantBucketAccessinlib/cloud-api/storage.jsdirectly called:without first verifying that
policyorpolicy.bindingsis defined. This causes an unhandledTypeError: Cannot read properties of undefined (reading 'find'). In addition, if a binding exists without initializedmembers, calling.includes()directly raisesTypeError: Cannot read properties of undefined (reading 'includes').This failure directly interrupts Cloud Run deployments whenever public access (
skipIamCheck) is requested or when setting bucket permissions for deployments. In contrast,lib/cloud-api/secrets.jsalready guardedpolicy.bindings = policy.bindings || [].Implementation
lib/cloud-api/run.js:policy = res || {}andpolicy.bindings = policy.bindings || [].invokerBinding.members = invokerBinding.members || []before calling.includes().lib/cloud-api/storage.js:policy = res || {}andpolicy.bindings = policy.bindings || [].binding.membersbefore calling.includes()and initializebinding.members = binding.members || []when updating an existing role binding.test/local/cloud-api/run.test.js: added test assertingsetServicePublicAccesssuccessfully handles an empty IAM policy[{}]without bindings.test/local/cloud-api/storage.test.js: added test assertinggrantBucketAccesssuccessfully handles an empty IAM policy[{}]without bindings.Tests
node --test test/local/cloud-api/run.test.js(3 passed, 0 failed).node --test test/local/cloud-api/storage.test.js(14 passed, 0 failed).npx prettier --check lib/cloud-api/run.js lib/cloud-api/storage.js test/local/cloud-api/run.test.js test/local/cloud-api/storage.test.js(clean).