Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(api): compare the IAM policy etag under lock (T18b) (#21285)

Danny Xu069b82dc65d3
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yaml
index 363cce347a..812642750f 100644
--- a/backend/api/mcp/gen/openapi.yaml
+++ b/backend/api/mcp/gen/openapi.yaml
@@ -13138,9 +13138,10 @@ components:
type: string
title: etag
description: |-
- The current etag of the policy.
- If an etag is provided and does not match the current etag of the policy,
- the call will be blocked and an ABORTED error will be returned.
+ The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+ Sending it back on SetIamPolicy makes the write conditional: if the policy
+ changed since the read, the call returns ABORTED. Omitting it applies the
+ write unconditionally, overwriting any concurrent change (AIP-154).
title: IamPolicy
additionalProperties: false
description: IAM policy that binds members to roles.
@@ -19479,7 +19480,9 @@ components:
etag:
type: string
title: etag
- description: The current etag of the policy.
+ description: |-
+ The current etag of the policy. Equivalent to setting `policy.etag`;
+ supplying both with different values returns INVALID_ARGUMENT.
title: SetIamPolicyRequest
additionalProperties: false
description: Request message for setting an IAM policy.
diff --git a/backend/api/v1/iam_policy_etag_test.go b/backend/api/v1/iam_policy_etag_test.go
new file mode 100644
index 0000000000..3caa7bd911
--- /dev/null
+++ b/backend/api/v1/iam_policy_etag_test.go
@@ -0,0 +1,92 @@
+package v1
+
+import (
+ "testing"
+
+ "connectrpc.com/connect"
+ "github.com/stretchr/testify/require"
+
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+)
+
+// GetIamPolicy hands the etag back inside the policy, so the round-trip a
+// read-modify-write performs carries it in `policy.etag` and leaves the request
+// field empty. Reading only the request field discarded it, and two admins
+// editing permissions at once silently lost one edit.
+func TestRequestedIamPolicyEtag(t *testing.T) {
+ testCases := []struct {
+ name string
+ request *v1pb.SetIamPolicyRequest
+ want string
+ wantInvalid bool
+ }{
+ {
+ name: "round-tripped inside the policy",
+ request: &v1pb.SetIamPolicyRequest{Policy: &v1pb.IamPolicy{Etag: "1756000000000"}},
+ want: "1756000000000",
+ },
+ {
+ name: "set on the request",
+ request: &v1pb.SetIamPolicyRequest{Etag: "1756000000000", Policy: &v1pb.IamPolicy{}},
+ want: "1756000000000",
+ },
+ {
+ name: "both, agreeing",
+ request: &v1pb.SetIamPolicyRequest{Etag: "1756000000000", Policy: &v1pb.IamPolicy{Etag: "1756000000000"}},
+ want: "1756000000000",
+ },
+ {
+ name: "both, disagreeing",
+ request: &v1pb.SetIamPolicyRequest{Etag: "1756000000000", Policy: &v1pb.IamPolicy{Etag: "1756000000001"}},
+ wantInvalid: true,
+ },
+ {
+ name: "neither, so no check is asked for",
+ request: &v1pb.SetIamPolicyRequest{Policy: &v1pb.IamPolicy{}},
+ want: "",
+ },
+ {
+ name: "no policy at all",
+ request: &v1pb.SetIamPolicyRequest{},
+ want: "",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ a := require.New(t)
+ etag, err := requestedIamPolicyEtag(tc.request)
+ if tc.wantInvalid {
+ a.Error(err)
+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))
+ return
+ }
+ a.NoError(err)
+ a.Equal(tc.want, etag)
+ })
+ }
+}
+
+// The workspace a SetIamPolicy call writes comes from the token, but the audit
+// log records the resource the caller named. Accepting a name that points
+// somewhere else would attribute a real permission change to the wrong
+// workspace, so the two have to agree.
+func TestWorkspaceResourceMatches(t *testing.T) {
+ testCases := []struct {
+ name string
+ resource string
+ want bool
+ }{
+ {name: "the caller's own workspace", resource: "workspaces/ws-a", want: true},
+ {name: "unset", resource: "", want: true},
+ {name: "the console's placeholder", resource: "workspaces/-", want: true},
+ {name: "another workspace", resource: "workspaces/ws-b", want: false},
+ {name: "not a workspace at all", resource: "projects/ws-a", want: false},
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ require.Equal(t, tc.want, workspaceResourceMatches(tc.resource, "ws-a"))
+ })
+ }
+}
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 51e1e91901..976259ad97 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -598,8 +598,13 @@ func (s *ProjectService) SetIamPolicy(ctx context.Context, req *connect.Request[
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find project iam policy with error"))
}
- if req.Msg.Etag != "" && req.Msg.Etag != oldIamPolicyMsg.Etag {
- return nil, connect.NewError(connect.CodeAborted, errors.Errorf("there is concurrent update to the project iam policy, please refresh and try again"))
+
+ // The etag is compared only in the write below, against the locked row.
+ // Comparing it here too would add a second answer to the same question from
+ // a read the write does not hold, and only the locked one decides.
+ expectedEtag, err := requestedIamPolicyEtag(req.Msg)
+ if err != nil {
+ return nil, err
}
if err := validateIAMPolicy(ctx, s.store, !s.profile.SaaS, req.Msg, oldIamPolicyMsg); err != nil {
@@ -611,30 +616,22 @@ func (s *ProjectService) SetIamPolicy(ctx context.Context, req *connect.Request[
return nil, err
}
- policyPayload, err := protojson.Marshal(policy)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, err)
- }
- if _, err = s.store.CreatePolicy(ctx, &store.PolicyMessage{
- Workspace: workspaceID,
- Resource: common.FormatProject(project.ResourceID),
- ResourceType: storepb.Policy_PROJECT,
- Payload: string(policyPayload),
- Type: storepb.Policy_IAM,
- InheritFromParent: false,
- // Enforce cannot be false while creating a policy.
- Enforce: true,
- }); err != nil {
- return nil, connect.NewError(connect.CodeInternal, err)
- }
-
- iamPolicyMessage, err := s.store.GetProjectIamPolicy(ctx, workspaceID, project.ResourceID)
+ replaced, iamPolicyMessage, err := s.store.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: workspaceID,
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject(project.ResourceID),
+ Policy: policy,
+ ExpectedEtag: expectedEtag,
+ })
if err != nil {
+ if errors.Is(err, store.ErrIamPolicyEtagMismatch) {
+ return nil, connect.NewError(connect.CodeAborted, errors.Errorf("there is concurrent update to the project iam policy, please refresh and try again"))
+ }
return nil, connect.NewError(connect.CodeInternal, err)
}
if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok {
- deltas := findIamPolicyDeltas(oldIamPolicyMsg.Policy, iamPolicyMessage.Policy)
+ deltas := findIamPolicyDeltas(replaced.Policy, iamPolicyMessage.Policy)
p, err := convertToProtoAny(deltas)
if err != nil {
slog.Warn("audit: failed to convert to anypb.Any")
@@ -1126,6 +1123,22 @@ func getBindingIdentifier(role string, condition *expr.Expr) string {
return strings.Join(ids, ";")
}
+// requestedIamPolicyEtag reads the etag the caller presented. GetIamPolicy
+// returns the etag inside the policy, so a read-modify-write round-trips it
+// there; the request field carries the same value for a caller that sets it
+// explicitly. Either alone is honored, and an empty etag asks for no check at
+// all. Two that disagree name two different reads, which no caller can mean.
+func requestedIamPolicyEtag(msg *v1pb.SetIamPolicyRequest) (string, error) {
+ policyEtag := msg.GetPolicy().GetEtag()
+ if msg.Etag != "" && policyEtag != "" && msg.Etag != policyEtag {
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("request etag %q and policy etag %q disagree", msg.Etag, policyEtag))
+ }
+ if msg.Etag != "" {
+ return msg.Etag, nil
+ }
+ return policyEtag, nil
+}
+
func validateIAMPolicy(
ctx context.Context,
stores *store.Store,
diff --git a/backend/api/v1/workspace_service.go b/backend/api/v1/workspace_service.go
index df331148db..b4db475841 100644
--- a/backend/api/v1/workspace_service.go
+++ b/backend/api/v1/workspace_service.go
@@ -10,7 +10,6 @@ import (
"connectrpc.com/connect"
"github.com/google/uuid"
"github.com/pkg/errors"
- "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/bytebase/bytebase/backend/api/auth"
@@ -426,6 +425,16 @@ func (s *WorkspaceService) LeaveWorkspace(ctx context.Context, req *connect.Requ
return s.authService.switchWorkspaceInternal(ctx, user, nextWS.ResourceID, isWeb, req.Header())
}
+// workspaceResourceMatches reports whether the resource a caller named is the
+// workspace their token authenticates them to. GetIamPolicy ignores the field
+// entirely, and the console sends a placeholder before it has resolved the
+// name, so both an empty value and the wildcard mean "the current workspace".
+func workspaceResourceMatches(resource, workspaceID string) bool {
+ return resource == "" ||
+ resource == common.FormatWorkspace(workspaceID) ||
+ resource == common.FormatWorkspace("-")
+}
+
func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Request[v1pb.SetIamPolicyRequest]) (*connect.Response[v1pb.IamPolicy], error) {
request := req.Msg
@@ -434,8 +443,21 @@ func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Reques
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace iam policy"))
}
- if request.Etag != "" && request.Etag != policyMessage.Etag {
- return nil, connect.NewError(connect.CodeAborted, errors.New("there is concurrent update to the workspace iam policy, please refresh and try again"))
+ // The write targets the workspace the caller is authenticated to, so a
+ // request naming a different one is refused rather than silently applied to
+ // this one: getRequestResource audits the name the caller sent, and writing
+ // one workspace while recording another attributes a real permission change
+ // to the wrong place.
+ if !workspaceResourceMatches(request.Resource, workspaceID) {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("resource %q is not the workspace this request is authenticated to", request.Resource))
+ }
+
+ // The etag is compared only in the write below, against the locked row.
+ // Comparing it here too would add a second answer to the same question from
+ // a read the write does not hold, and only the locked one decides.
+ expectedEtag, err := requestedIamPolicyEtag(request)
+ if err != nil {
+ return nil, err
}
if err := validateIAMPolicy(ctx, s.store, !s.profile.SaaS, request, policyMessage); err != nil {
@@ -469,29 +491,42 @@ func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Reques
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to count users in IAM policy"))
}
- if newCount > userLimit {
- oldCount, err := countUsersInIamPolicy(ctx, s.store, workspaceID, policyMessage.Policy, s.profile.SaaS)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to count users in current IAM policy"))
- }
- if newCount > oldCount {
- return nil, connect.NewError(connect.CodeResourceExhausted, errors.Errorf("workspace has %d users, exceeding the limit of %d", newCount, userLimit))
- }
- }
- payloadBytes, err := protojson.Marshal(iamPolicy)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to marshal iam policy"))
- }
- patch := &store.UpdatePolicyMessage{
- ResourceType: storepb.Policy_WORKSPACE,
- Resource: request.Resource,
- Type: storepb.Policy_IAM,
+ replaced, policy, err := s.store.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
Workspace: workspaceID,
- Payload: new(string(payloadBytes)),
- }
-
- if _, err := s.store.UpdatePolicy(ctx, patch); err != nil {
+ ResourceType: storepb.Policy_WORKSPACE,
+ Resource: common.FormatWorkspace(workspaceID),
+ Policy: iamPolicy,
+ ExpectedEtag: expectedEtag,
+ // The seat count this change is measured against is the one it replaces,
+ // so it is taken under the same lock. Counting from the read above left
+ // two concurrent writes both passing the guard, and the later one growing
+ // an over-limit workspace -- for an etag-less caller, which the
+ // compare-and-swap does not cover.
+ ValidateReplaced: func(previous *storepb.IamPolicy) error {
+ if newCount <= userLimit {
+ return nil
+ }
+ oldCount, err := countUsersInIamPolicy(ctx, s.store, workspaceID, previous, s.profile.SaaS)
+ if err != nil {
+ return connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to count users in current IAM policy"))
+ }
+ if newCount > oldCount {
+ return connect.NewError(connect.CodeResourceExhausted, errors.Errorf("workspace has %d users, exceeding the limit of %d", newCount, userLimit))
+ }
+ return nil
+ },
+ })
+ if err != nil {
+ if errors.Is(err, store.ErrIamPolicyEtagMismatch) {
+ return nil, connect.NewError(connect.CodeAborted, errors.New("there is concurrent update to the workspace iam policy, please refresh and try again"))
+ }
+ // ValidateReplaced reports its own status; only an unclassified failure
+ // is this handler's to label.
+ var connectErr *connect.Error
+ if errors.As(err, &connectErr) {
+ return nil, connectErr
+ }
return nil, connect.NewError(connect.CodeInternal, err)
}
@@ -499,12 +534,7 @@ func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Reques
return nil, err
}
- policy, err := s.store.GetWorkspaceIamPolicy(ctx, workspaceID)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find iam policy"))
- }
-
- deltas := findIamPolicyDeltas(policyMessage.Policy, policy.Policy)
+ deltas := findIamPolicyDeltas(replaced.Policy, policy.Policy)
if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok {
p, err := convertToProtoAny(deltas)
@@ -515,7 +545,7 @@ func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Reques
}
// send invite emails to newly added members.
- go s.sendInviteEmails(context.WithoutCancel(ctx), workspaceID, policyMessage.Policy, deltas)
+ go s.sendInviteEmails(context.WithoutCancel(ctx), workspaceID, replaced.Policy, deltas)
v1Policy, err := convertToV1IamPolicy(policy)
if err != nil {
diff --git a/backend/generated-go/v1/iam_policy.pb.go b/backend/generated-go/v1/iam_policy.pb.go
index 7a99f3b9da..2d2aa1bd31 100644
--- a/backend/generated-go/v1/iam_policy.pb.go
+++ b/backend/generated-go/v1/iam_policy.pb.go
@@ -133,7 +133,8 @@ type SetIamPolicyRequest struct {
// Format: workspaces/{workspace}
Resource string `protobuf:"bytes,1,opt,name=resource,proto3" json:"resource,omitempty"`
Policy *IamPolicy `protobuf:"bytes,2,opt,name=policy,proto3" json:"policy,omitempty"`
- // The current etag of the policy.
+ // The current etag of the policy. Equivalent to setting `policy.etag`;
+ // supplying both with different values returns INVALID_ARGUMENT.
Etag string `protobuf:"bytes,3,opt,name=etag,proto3" json:"etag,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
@@ -196,9 +197,10 @@ type IamPolicy struct {
// Collection of binding.
// A binding binds one or more project members to a single project role.
Bindings []*Binding `protobuf:"bytes,1,rep,name=bindings,proto3" json:"bindings,omitempty"`
- // The current etag of the policy.
- // If an etag is provided and does not match the current etag of the policy,
- // the call will be blocked and an ABORTED error will be returned.
+ // The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+ // Sending it back on SetIamPolicy makes the write conditional: if the policy
+ // changed since the read, the call returns ABORTED. Omitting it applies the
+ // write unconditionally, overwriting any concurrent change (AIP-154).
Etag string `protobuf:"bytes,2,opt,name=etag,proto3" json:"etag,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
@@ -482,11 +484,11 @@ const file_v1_iam_policy_proto_rawDesc = "" +
"\n" +
"\x13v1/iam_policy.proto\x12\vbytebase.v1\x1a%google/api/expr/v1alpha1/syntax.proto\x1a\x1fgoogle/api/field_behavior.proto\x1a\x19google/api/resource.proto\x1a\x16google/type/expr.proto\"Q\n" +
"\x13GetIamPolicyRequest\x12:\n" +
- "\bresource\x18\x01 \x01(\tB\x1e\xe0A\x02\xfaA\x18\x12\x16bytebase.com/IAMPolicyR\bresource\"\x9a\x01\n" +
+ "\bresource\x18\x01 \x01(\tB\x1e\xe0A\x02\xfaA\x18\x12\x16bytebase.com/IAMPolicyR\bresource\"\x9f\x01\n" +
"\x13SetIamPolicyRequest\x12:\n" +
"\bresource\x18\x01 \x01(\tB\x1e\xe0A\x02\xfaA\x18\x12\x16bytebase.com/IAMPolicyR\bresource\x123\n" +
- "\x06policy\x18\x02 \x01(\v2\x16.bytebase.v1.IamPolicyB\x03\xe0A\x02R\x06policy\x12\x12\n" +
- "\x04etag\x18\x03 \x01(\tR\x04etag\"Q\n" +
+ "\x06policy\x18\x02 \x01(\v2\x16.bytebase.v1.IamPolicyB\x03\xe0A\x02R\x06policy\x12\x17\n" +
+ "\x04etag\x18\x03 \x01(\tB\x03\xe0A\x01R\x04etag\"Q\n" +
"\tIamPolicy\x120\n" +
"\bbindings\x18\x01 \x03(\v2\x14.bytebase.v1.BindingR\bbindings\x12\x12\n" +
"\x04etag\x18\x02 \x01(\tR\x04etag\"\xae\x01\n" +
diff --git a/backend/store/advisory_lock.go b/backend/store/advisory_lock.go
index 929d08fedd..c8fda98a4a 100644
--- a/backend/store/advisory_lock.go
+++ b/backend/store/advisory_lock.go
@@ -32,6 +32,10 @@ const (
// AdvisoryLockKeyInstancePurge serializes instance lifecycle operations with
// descendant creation and purge when the descendant row may not exist yet.
AdvisoryLockKeyInstancePurge AdvisoryLockKey = 1007
+ // AdvisoryLockKeyIamPolicy serializes compare-and-swap writes to one
+ // resource's IAM policy, including the first write, when there is no policy
+ // row to lock yet.
+ AdvisoryLockKeyIamPolicy AdvisoryLockKey = 1008
)
// AcquirePlanIssueRolloutAdvisoryLock serializes coordinated Plan, linked Issue,
diff --git a/backend/store/iam_policy_test.go b/backend/store/iam_policy_test.go
new file mode 100644
index 0000000000..86a0019b80
--- /dev/null
+++ b/backend/store/iam_policy_test.go
@@ -0,0 +1,422 @@
+package store_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/pkg/errors"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/store"
+)
+
+const ownerAndDeveloperPolicy = `{"bindings":[` +
+ `{"role":"roles/projectOwner","members":["users/[email protected]"]},` +
+ `{"role":"roles/projectDeveloper","members":["users/[email protected]"]}]}`
+
+func iamPolicy(t *testing.T, roleMembers map[string][]string) *storepb.IamPolicy {
+ t.Helper()
+ policy := &storepb.IamPolicy{}
+ for role, members := range roleMembers {
+ policy.Bindings = append(policy.Bindings, &storepb.Binding{Role: role, Members: members})
+ }
+ return policy
+}
+
+// projectPolicyMembers reads the members bound to one role of project-a's
+// stored policy, in the workspace named.
+func projectPolicyMembers(ctx context.Context, t *testing.T, s *store.Store, workspace, role string) []string {
+ t.Helper()
+ policy, err := s.GetProjectIamPolicy(ctx, workspace, "project-a")
+ require.NoError(t, err)
+ for _, binding := range policy.Policy.Bindings {
+ if binding.Role == role {
+ return binding.Members
+ }
+ }
+ return nil
+}
+
+// Two admins editing permissions at once used to lose one edit: the etag was
+// compared against a read taken before validation, outside the write
+// transaction, so both writers passed the compare and the later one clobbered
+// the other. If the lost edit was a revoke, access stayed granted.
+func TestSetIamPolicyCompareAndSwap(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx, s := fixture.ctx, fixture.store
+ resource := common.FormatProject("project-a")
+
+ current, err := s.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ require.NotEmpty(t, current.Etag)
+
+ // One admin revokes the developer, presenting the etag they read.
+ revoked := iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}})
+ replaced, updated, err := s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: resource,
+ Policy: revoked,
+ ExpectedEtag: current.Etag,
+ })
+ require.NoError(t, err)
+ require.Len(t, replaced.Policy.Bindings, 2, "the replaced policy is what the write actually overwrote")
+ require.NotEqual(t, current.Etag, updated.Etag, "the etag must move with the write")
+
+ // The second admin still holds the etag from before the revoke. Their write
+ // would put the developer back, so it must be rejected, not applied.
+ regranted := iamPolicy(t, map[string][]string{
+ "roles/projectOwner": {"users/[email protected]"},
+ "roles/projectDeveloper": {"users/[email protected]", "users/[email protected]"},
+ })
+ _, _, err = s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: resource,
+ Policy: regranted,
+ ExpectedEtag: current.Etag,
+ })
+ require.ErrorIs(t, err, store.ErrIamPolicyEtagMismatch)
+ require.Nil(t, projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"),
+ "the revoke must survive the rejected write")
+
+ // Refetching and reapplying is what the caller does next, and it lands.
+ current, err = s.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ _, _, err = s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: resource,
+ Policy: regranted,
+ ExpectedEtag: current.Etag,
+ })
+ require.NoError(t, err)
+ require.ElementsMatch(t, []string{"users/[email protected]", "users/[email protected]"},
+ projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"))
+
+ // An empty etag is a caller that never read the policy, and asks for no
+ // check -- the behavior every etag-less client has always had.
+ _, _, err = s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: resource,
+ Policy: revoked,
+ ExpectedEtag: "",
+ })
+ require.NoError(t, err)
+ require.Nil(t, projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"))
+}
+
+// The compare has to read the row it is about to write, not a cached copy.
+// Nothing in a server deployment invalidates another process's cache, and the
+// recovery CLI opens its own store against the same database, so a cache can
+// disagree with the row -- and comparing against it would accept exactly the
+// stale write the etag exists to reject.
+func TestSetIamPolicyComparesAgainstTheRowNotTheCache(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx := fixture.ctx
+
+ cached, err := store.New(ctx, fixture.pgURL, true)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, cached.Close()) })
+
+ // Warm the cache, then write through a store that does not share it.
+ stale, err := cached.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ require.NotEmpty(t, stale.Etag)
+
+ _, _, err = fixture.store.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ ExpectedEtag: stale.Etag,
+ })
+ require.NoError(t, err)
+
+ // The cached read still reports the pre-write etag, which is the whole
+ // hazard; the write must reject it anyway.
+ cachedAfter, err := cached.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ require.Equal(t, stale.Etag, cachedAfter.Etag, "the cache is expected to be stale here")
+
+ _, _, err = cached.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectDeveloper": {"users/[email protected]"}}),
+ ExpectedEtag: stale.Etag,
+ })
+ require.ErrorIs(t, err, store.ErrIamPolicyEtagMismatch)
+
+ // Rejecting is only half of it. The caller refetches to pick up the etag it
+ // lost to, and being served the same stale one from here would conflict
+ // again forever -- so the entry the write proved stale is gone.
+ recovered, err := cached.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ require.NotEqual(t, stale.Etag, recovered.Etag, "the stale cache entry must not survive the conflict")
+ require.Nil(t, projectPolicyMembers(ctx, t, cached, "default", "roles/projectDeveloper"))
+
+ // And the retry that etag carries now lands.
+ _, _, err = cached.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectDeveloper": {"users/[email protected]"}}),
+ ExpectedEtag: recovered.Etag,
+ })
+ require.NoError(t, err)
+}
+
+// A check that compares the new policy against the old one -- the workspace
+// seat guard -- has to see the policy the write replaces. Reading it in the
+// caller leaves a gap another request can land in, so two writes both pass a
+// guard neither would have passed against what the other stored.
+func TestSetIamPolicyValidatesAgainstTheReplacedPolicy(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx, s := fixture.ctx, fixture.store
+
+ var seen []string
+ rejected := errors.New("too many members")
+ _, _, err := s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ ValidateReplaced: func(previous *storepb.IamPolicy) error {
+ for _, binding := range previous.Bindings {
+ seen = append(seen, binding.Role)
+ }
+ return rejected
+ },
+ })
+ require.ErrorIs(t, err, rejected)
+ require.ElementsMatch(t, []string{"roles/projectOwner", "roles/projectDeveloper"}, seen,
+ "the check must see the policy stored, not the one being written")
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"),
+ "a rejected check must leave the policy untouched")
+}
+
+// policy is keyed by (workspace, resource_type, resource, type), and two
+// workspaces name their projects independently -- so the same resource string
+// exists in both. A write must touch only the workspace it named.
+func TestSetIamPolicyIsScopedToItsWorkspace(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO workspace (resource_id) VALUES ('other');
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE),
+ ('other', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx, s := fixture.ctx, fixture.store
+
+ _, _, err := s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ })
+ require.NoError(t, err)
+
+ require.Nil(t, projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"))
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, s, "other", "roles/projectDeveloper"),
+ "the other workspace's identically named project must be untouched")
+}
+
+// A resource whose IAM policy was never written has no row to lock, so the
+// first write creates one rather than failing the compare.
+func TestSetIamPolicyCreatesTheFirstPolicy(t *testing.T) {
+ fixture := newProjectDeletionLockOrderFixture(t, "")
+ ctx, s := fixture.ctx, fixture.store
+
+ absent, err := s.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+ require.Empty(t, absent.Policy.Bindings)
+
+ replaced, updated, err := s.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ ExpectedEtag: absent.Etag,
+ })
+ require.NoError(t, err)
+ require.Empty(t, replaced.Policy.Bindings)
+ require.NotEmpty(t, updated.Etag)
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, s, "default", "roles/projectOwner"))
+}
+
+// PatchWorkspaceIamPolicy merges one member's roles rather than replacing the
+// policy, and now does it inside the locked transaction. Its merge semantics
+// must be unchanged by that move.
+func TestPatchWorkspaceIamPolicyMerges(t *testing.T) {
+ fixture := newProjectDeletionLockOrderFixture(t, "")
+ ctx, s := fixture.ctx, fixture.store
+
+ _, err := s.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{
+ Workspace: "default",
+ Member: common.FormatUserEmail("[email protected]"),
+ Roles: []string{"roles/workspaceAdmin"},
+ })
+ require.NoError(t, err)
+
+ // A second member joining must not displace the first.
+ policy, err := s.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{
+ Workspace: "default",
+ Member: common.FormatUserEmail("[email protected]"),
+ Roles: []string{"roles/workspaceMember"},
+ })
+ require.NoError(t, err)
+
+ members := map[string][]string{}
+ for _, binding := range policy.Policy.Bindings {
+ members[binding.Role] = binding.Members
+ }
+ require.Equal(t, []string{"users/[email protected]"}, members["roles/workspaceAdmin"])
+ require.Equal(t, []string{"users/[email protected]"}, members["roles/workspaceMember"])
+
+ // Setting a member's roles to a different set removes the ones dropped.
+ _, err = s.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{
+ Workspace: "default",
+ Member: common.FormatUserEmail("[email protected]"),
+ Roles: []string{"roles/workspaceMember"},
+ })
+ require.NoError(t, err)
+
+ stored, err := s.GetWorkspaceIamPolicy(ctx, "default")
+ require.NoError(t, err)
+ members = map[string][]string{}
+ for _, binding := range stored.Policy.Bindings {
+ members[binding.Role] = binding.Members
+ }
+ require.Empty(t, members["roles/workspaceAdmin"])
+ require.ElementsMatch(t, []string{"users/[email protected]", "users/[email protected]"},
+ members["roles/workspaceMember"])
+}
+
+// A merge that reads the policy before the write -- through a cache another
+// node never invalidates -- writes back a policy missing whatever that node
+// changed. Approving a role grant while an admin revokes someone in the same
+// project put the revoked member back.
+func TestPatchIamPolicyMergesIntoTheStoredPolicy(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx := fixture.ctx
+ resource := common.FormatProject("project-a")
+
+ merging, err := store.New(ctx, fixture.pgURL, true)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, merging.Close()) })
+
+ // This node has read the policy and cached it. Another node then revokes the
+ // developer.
+ _, err = merging.GetProjectIamPolicy(ctx, "default", "project-a")
+ require.NoError(t, err)
+
+ _, _, err = fixture.store.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: resource,
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ })
+ require.NoError(t, err)
+
+ // The merge must land on the revoked policy, not on the cached one.
+ _, err = merging.PatchIamPolicy(ctx, "default", storepb.Policy_PROJECT, resource,
+ func(policy *storepb.IamPolicy) error {
+ policy.Bindings = append(policy.Bindings, &storepb.Binding{
+ Role: "roles/projectReleaser",
+ Members: []string{"users/[email protected]"},
+ })
+ return nil
+ })
+ require.NoError(t, err)
+
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, fixture.store, "default", "roles/projectReleaser"))
+ require.Nil(t, projectPolicyMembers(ctx, t, fixture.store, "default", "roles/projectDeveloper"),
+ "the merge must not resurrect the revoked member")
+}
+
+// A mutation that fails leaves the policy alone rather than committing a
+// half-applied merge.
+func TestPatchIamPolicyRollsBackAFailedMutation(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx, s := fixture.ctx, fixture.store
+
+ boom := errors.New("boom")
+ _, err := s.PatchIamPolicy(ctx, "default", storepb.Policy_PROJECT, common.FormatProject("project-a"),
+ func(policy *storepb.IamPolicy) error {
+ policy.Bindings = nil
+ return boom
+ })
+ require.ErrorIs(t, err, boom)
+
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, s, "default", "roles/projectDeveloper"))
+}
+
+// A write must not publish what it just wrote into the cache. The advisory lock
+// ends with the commit, so two writers can commit in one order and reach the
+// cache in the other, and the loser's snapshot would then sit in a cache with no
+// expiry -- which is what permission checks read. Observed here without racing
+// goroutines: after a write, the row is changed out of band, and a read that
+// still saw the written value would prove the write had published it.
+func TestSetIamPolicyDoesNotPublishItsOwnWrite(t *testing.T) {
+ const seedSQL = `
+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent)
+ VALUES ('default', 'PROJECT', 'projects/project-a', 'IAM', '` + ownerAndDeveloperPolicy + `', FALSE);
+ `
+ fixture := newProjectDeletionLockOrderFixture(t, seedSQL)
+ ctx := fixture.ctx
+
+ cached, err := store.New(ctx, fixture.pgURL, true)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, cached.Close()) })
+
+ _, _, err = cached.SetIamPolicy(ctx, &store.SetIamPolicyMessage{
+ Workspace: "default",
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject("project-a"),
+ Policy: iamPolicy(t, map[string][]string{"roles/projectOwner": {"users/[email protected]"}}),
+ })
+ require.NoError(t, err)
+
+ // Stand in for the writer that committed second: the row moves on without
+ // this process's cache being told.
+ _, err = fixture.db.ExecContext(ctx, `
+ UPDATE policy SET payload = $1, updated_at = now()
+ WHERE workspace = 'default' AND resource_type = 'PROJECT'
+ AND resource = 'projects/project-a' AND type = 'IAM'`,
+ `{"bindings":[{"role":"roles/projectReleaser","members":["users/[email protected]"]}]}`)
+ require.NoError(t, err)
+
+ require.Equal(t, []string{"users/[email protected]"},
+ projectPolicyMembers(ctx, t, cached, "default", "roles/projectReleaser"),
+ "the write must leave nothing cached, so this read reaches the row")
+ require.Nil(t, projectPolicyMembers(ctx, t, cached, "default", "roles/projectOwner"))
+}
diff --git a/backend/store/lock_order_test.go b/backend/store/lock_order_test.go
index 81d9373883..b20c15b37e 100644
--- a/backend/store/lock_order_test.go
+++ b/backend/store/lock_order_test.go
@@ -18,6 +18,9 @@ type projectDeletionLockOrderFixture struct {
ctx context.Context
db *sql.DB
store *store.Store
+ // pgURL lets a test open a second store against the same database, e.g. one
+ // with the caches enabled.
+ pgURL string
}
func newProjectDeletionLockOrderFixture(t *testing.T, seedSQL string) *projectDeletionLockOrderFixture {
@@ -44,7 +47,7 @@ func newProjectDeletionLockOrderFixture(t *testing.T, seedSQL string) *projectDe
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, s.Close()) })
- return &projectDeletionLockOrderFixture{ctx: ctx, db: db, store: s}
+ return &projectDeletionLockOrderFixture{ctx: ctx, db: db, store: s, pgURL: pgURL}
}
func runWithConcurrentProjectDeletion(
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 77d4ac5d9c..6122be3fd8 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -32,6 +32,173 @@ func generateEtag(t time.Time) string {
return fmt.Sprintf("%d", t.UnixMilli())
}
+// ErrIamPolicyEtagMismatch reports that the IAM policy moved under a
+// compare-and-swap write; the caller refetches and reapplies.
+var ErrIamPolicyEtagMismatch = errors.New("iam policy etag mismatch")
+
+// SetIamPolicyMessage replaces the IAM policy of one resource.
+type SetIamPolicyMessage struct {
+ Workspace string
+ ResourceType storepb.Policy_Resource
+ Resource string
+ Policy *storepb.IamPolicy
+ // ExpectedEtag is the etag the caller read the policy at. Empty skips the
+ // compare, which is all a caller that never read the policy can offer.
+ ExpectedEtag string
+ // ValidateReplaced, when set, runs inside the write transaction against the
+ // policy being replaced. A check that compares the new policy to the old one
+ // belongs here rather than in the caller, where reading the old one and
+ // writing are two steps another request can land between. Returning an error
+ // aborts the write, and the error reaches the caller unwrapped.
+ ValidateReplaced func(previous *storepb.IamPolicy) error
+}
+
+// SetIamPolicy replaces an IAM policy under compare-and-swap. The policy row is
+// locked and its etag compared inside the write transaction, so a
+// full-replacement write can never silently undo a concurrent revocation.
+// Comparing against a read taken earlier cannot promise that: two requests
+// reading the same policy would both pass, and the later write would win --
+// which is the defect this exists to close, one layer up.
+//
+// Returns the policy the write replaced, so the caller reports the delta it
+// actually applied rather than a snapshot that may have moved, and
+// ErrIamPolicyEtagMismatch when the caller's etag is stale.
+func (s *Store) SetIamPolicy(ctx context.Context, set *SetIamPolicyMessage) (*IamPolicyMessage, *IamPolicyMessage, error) {
+ tx, err := s.GetDB().BeginTx(ctx, nil)
+ if err != nil {
+ return nil, nil, errors.Wrap(err, "failed to begin transaction")
+ }
+ defer tx.Rollback()
+
+ if err := acquireIamPolicyLock(ctx, tx, set.Workspace, set.ResourceType, set.Resource); err != nil {
+ return nil, nil, err
+ }
+ previous, err := lockIamPolicyImpl(ctx, tx, set.Workspace, set.ResourceType, set.Resource)
+ if err != nil {
+ return nil, nil, err
+ }
+ if set.ExpectedEtag != "" && set.ExpectedEtag != previous.Etag {
+ // The locked row just disagreed with what this process has cached, which
+ // only an out-of-band writer can cause -- the recovery CLI opens its own
+ // store against the same database. Drop it so the refetch the caller
+ // makes next is not served the etag it already lost with.
+ s.evictIamPolicyCache(set.Workspace, set.ResourceType, set.Resource)
+ return nil, nil, ErrIamPolicyEtagMismatch
+ }
+ if set.ValidateReplaced != nil {
+ if err := set.ValidateReplaced(previous.Policy); err != nil {
+ return nil, nil, err
+ }
+ }
+
+ policy, err := writeIamPolicyImpl(ctx, tx, set.Workspace, set.ResourceType, set.Resource, set.Policy)
+ if err != nil {
+ return nil, nil, err
+ }
+ if err := tx.Commit(); err != nil {
+ return nil, nil, errors.Wrap(err, "failed to commit transaction")
+ }
+ s.evictIamPolicyCache(set.Workspace, set.ResourceType, set.Resource)
+
+ return previous, &IamPolicyMessage{Policy: set.Policy, Etag: generateEtag(policy.UpdatedAt)}, nil
+}
+
+// PatchIamPolicy applies mutate to one resource's IAM policy inside the locked
+// transaction, so a merge neither loses nor is lost by a concurrent
+// full-replacement write. mutate sees the policy as stored, not a snapshot read
+// before the write began, which is the only thing a caller holding no etag can
+// be given.
+func (s *Store) PatchIamPolicy(ctx context.Context, workspace string, resourceType storepb.Policy_Resource, resource string, mutate func(*storepb.IamPolicy) error) (*IamPolicyMessage, error) {
+ tx, err := s.GetDB().BeginTx(ctx, nil)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to begin transaction")
+ }
+ defer tx.Rollback()
+
+ if err := acquireIamPolicyLock(ctx, tx, workspace, resourceType, resource); err != nil {
+ return nil, err
+ }
+ current, err := lockIamPolicyImpl(ctx, tx, workspace, resourceType, resource)
+ if err != nil {
+ return nil, err
+ }
+ if err := mutate(current.Policy); err != nil {
+ return nil, err
+ }
+
+ policy, err := writeIamPolicyImpl(ctx, tx, workspace, resourceType, resource, current.Policy)
+ if err != nil {
+ return nil, err
+ }
+ if err := tx.Commit(); err != nil {
+ return nil, errors.Wrap(err, "failed to commit transaction")
+ }
+ s.evictIamPolicyCache(workspace, resourceType, resource)
+
+ return &IamPolicyMessage{Policy: current.Policy, Etag: generateEtag(policy.UpdatedAt)}, nil
+}
+
+// acquireIamPolicyLock serializes writers of one resource's IAM policy before
+// any row lock, so two first writes to a resource that has no policy row yet --
+// which have no row to lock -- still order against each other.
+func acquireIamPolicyLock(ctx context.Context, tx *sql.Tx, workspace string, resourceType storepb.Policy_Resource, resource string) error {
+ return AcquireAdvisoryXactLockWithStringKey(ctx, tx, AdvisoryLockKeyIamPolicy, fmt.Sprintf("%s/%s/%s", workspace, resourceType, resource))
+}
+
+// lockIamPolicyImpl locks one IAM policy row and returns the policy it holds,
+// reading past the policy cache so the etag reflects the row being replaced. A
+// resource whose policy was never written has no row, and reads as the empty
+// policy.
+func lockIamPolicyImpl(ctx context.Context, tx *sql.Tx, workspace string, resourceType storepb.Policy_Resource, resource string) (*IamPolicyMessage, error) {
+ var payload string
+ var updatedAt time.Time
+ if err := tx.QueryRowContext(ctx, `
+ SELECT payload, updated_at FROM policy
+ WHERE workspace = $1 AND resource_type = $2 AND resource = $3 AND type = $4
+ FOR UPDATE`,
+ workspace, resourceType.String(), resource, storepb.Policy_IAM.String(),
+ ).Scan(&payload, &updatedAt); err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return &IamPolicyMessage{Policy: &storepb.IamPolicy{}}, nil
+ }
+ return nil, errors.Wrapf(err, "failed to lock iam policy for %s", resource)
+ }
+
+ p := &storepb.IamPolicy{}
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(payload), p); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal iam policy for %s", resource)
+ }
+ return &IamPolicyMessage{Policy: p, Etag: generateEtag(updatedAt)}, nil
+}
+
+func writeIamPolicyImpl(ctx context.Context, tx *sql.Tx, workspace string, resourceType storepb.Policy_Resource, resource string, policy *storepb.IamPolicy) (*PolicyMessage, error) {
+ payload, err := protojson.Marshal(policy)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to marshal iam policy")
+ }
+ return upsertPolicyImpl(ctx, tx, &PolicyMessage{
+ Workspace: workspace,
+ ResourceType: resourceType,
+ Resource: resource,
+ Type: storepb.Policy_IAM,
+ Payload: string(payload),
+ InheritFromParent: false,
+ // Enforce cannot be false while creating a policy.
+ Enforce: true,
+ })
+}
+
+// evictIamPolicyCache drops this process's cached copies of one IAM policy. A
+// write evicts rather than publishing what it just wrote: the advisory lock ends
+// with the commit, so two writers can commit in one order and reach the cache in
+// the other, leaving the older policy cached with no expiry -- and permission
+// checks read this cache. Eviction is safe under every interleaving, because it
+// never installs a value; the next read repopulates it from the row.
+func (s *Store) evictIamPolicyCache(workspace string, resourceType storepb.Policy_Resource, resource string) {
+ s.policyCache.Remove(getPolicyCacheKey(workspace, resourceType, resource, storepb.Policy_IAM))
+ s.iamPolicyCache.Remove(getIamPolicyCacheKey(workspace, resourceType, resource))
+}
+
func (s *Store) GetWorkspaceIamPolicy(ctx context.Context, workspaceID string) (*IamPolicyMessage, error) {
return s.getIamPolicy(ctx, &FindPolicyMessage{
Workspace: workspaceID,
@@ -47,28 +214,33 @@ type PatchIamPolicyMessage struct {
}
// PatchWorkspaceIamPolicy will set or remove the member for the workspace role.
+// The read-modify-write runs inside the locked transaction, so it neither loses
+// a concurrent SetIamPolicy nor has its own membership lost by one.
func (s *Store) PatchWorkspaceIamPolicy(ctx context.Context, patch *PatchIamPolicyMessage) (*IamPolicyMessage, error) {
- workspaceResource := common.FormatWorkspace(patch.Workspace)
-
- workspaceIamPolicy, err := s.GetWorkspaceIamPolicy(ctx, patch.Workspace)
- if err != nil {
- return nil, err
- }
+ return s.PatchIamPolicy(ctx, patch.Workspace, storepb.Policy_WORKSPACE, common.FormatWorkspace(patch.Workspace),
+ func(policy *storepb.IamPolicy) error {
+ applyIamPolicyMemberPatch(policy, patch.Member, patch.Roles)
+ return nil
+ })
+}
+// applyIamPolicyMemberPatch sets the member's roles to exactly the listed ones,
+// adding and removing bindings in place.
+func applyIamPolicyMemberPatch(policy *storepb.IamPolicy, member string, roles []string) {
roleMap := map[string]bool{}
- for _, role := range patch.Roles {
+ for _, role := range roles {
roleMap[role] = true
}
- for _, binding := range workspaceIamPolicy.Policy.Bindings {
- index := slices.Index(binding.Members, patch.Member)
+ for _, binding := range policy.Bindings {
+ index := slices.Index(binding.Members, member)
if !roleMap[binding.Role] {
if index >= 0 {
binding.Members = slices.Delete(binding.Members, index, index+1)
}
} else {
if index < 0 {
- binding.Members = append(binding.Members, patch.Member)
+ binding.Members = append(binding.Members, member)
}
}
@@ -76,36 +248,13 @@ func (s *Store) PatchWorkspaceIamPolicy(ctx context.Context, patch *PatchIamPoli
}
for role := range roleMap {
- workspaceIamPolicy.Policy.Bindings = append(workspaceIamPolicy.Policy.Bindings, &storepb.Binding{
+ policy.Bindings = append(policy.Bindings, &storepb.Binding{
Role: role,
Members: []string{
- patch.Member,
+ member,
},
})
}
-
- policyPayload, err := protojson.Marshal(workspaceIamPolicy.Policy)
- if err != nil {
- return nil, err
- }
-
- if _, err := s.CreatePolicy(ctx, &PolicyMessage{
- Workspace: patch.Workspace,
- ResourceType: storepb.Policy_WORKSPACE,
- Resource: workspaceResource,
- Payload: string(policyPayload),
- Type: storepb.Policy_IAM,
- InheritFromParent: false,
- // Enforce cannot be false while creating a policy.
- Enforce: true,
- }); err != nil {
- return nil, err
- }
-
- // Invalidate caches after mutation.
- s.iamPolicyCache.Remove(getIamPolicyCacheKey(patch.Workspace, storepb.Policy_WORKSPACE, workspaceResource))
-
- return s.GetWorkspaceIamPolicy(ctx, patch.Workspace)
}
func (s *Store) GetProjectIamPolicy(ctx context.Context, workspaceID string, projectID string) (*IamPolicyMessage, error) {
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index aa920920c5..5336a3b40b 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -13,7 +13,6 @@ import (
"connectrpc.com/connect"
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/type/expr"
- "google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
@@ -100,17 +99,14 @@ func CheckIssueApprovedForPlan(issue *store.IssueMessage, plan *store.PlanMessag
}
// UpdateProjectPolicyFromRoleGrantIssue updates the project policy from a role grant issue.
+// The grant is merged into the policy as stored, under lock, so approving a
+// grant while an admin edits the same project's permissions cannot drop either
+// change -- including a revoke.
func UpdateProjectPolicyFromRoleGrantIssue(ctx context.Context, stores *store.Store, workspaceID string, issue *store.IssueMessage, roleGrant *storepb.RoleGrant) error {
- policyMessage, err := stores.GetProjectIamPolicy(ctx, workspaceID, issue.ProjectID)
- if err != nil {
- return errors.Wrapf(err, "failed to get project policy for project %s", issue.ProjectID)
- }
-
var newConditionExpr string
if roleGrant.Condition != nil {
newConditionExpr = roleGrant.Condition.Expression
}
- updated := false
email, err := extractEmailFromUserIdentifier(roleGrant.User)
if err != nil {
@@ -124,50 +120,37 @@ func UpdateProjectPolicyFromRoleGrantIssue(ctx context.Context, stores *store.St
return connect.NewError(connect.CodeInternal, errors.Errorf("user %s not found", email))
}
memberName := formatMemberNameByType(newUser)
- for _, binding := range policyMessage.Policy.Bindings {
- if binding.Role != roleGrant.Role {
- continue
- }
- var oldConditionExpr string
- if binding.Condition != nil {
- oldConditionExpr = binding.Condition.Expression
- }
- if oldConditionExpr != newConditionExpr {
- continue
- }
- // Append
- binding.Members = append(binding.Members, memberName)
- updated = true
- break
- }
- if !updated {
- condition := roleGrant.Condition
- if condition == nil {
- condition = &expr.Expr{}
- }
- condition.Description = fmt.Sprintf("#%d", issue.UID)
- policyMessage.Policy.Bindings = append(policyMessage.Policy.Bindings, &storepb.Binding{
- Role: roleGrant.Role,
- Members: []string{memberName},
- Condition: condition,
- })
- }
- policyPayload, err := protojson.Marshal(policyMessage.Policy)
- if err != nil {
- return err
- }
- if _, err := stores.CreatePolicy(ctx, &store.PolicyMessage{
- Workspace: workspaceID,
- Resource: common.FormatProject(issue.ProjectID),
- ResourceType: storepb.Policy_PROJECT,
- Payload: string(policyPayload),
- Type: storepb.Policy_IAM,
- InheritFromParent: false,
- // Enforce cannot be false while creating a policy.
- Enforce: true,
- }); err != nil {
- return err
+ if _, err := stores.PatchIamPolicy(ctx, workspaceID, storepb.Policy_PROJECT, common.FormatProject(issue.ProjectID),
+ func(policy *storepb.IamPolicy) error {
+ for _, binding := range policy.Bindings {
+ if binding.Role != roleGrant.Role {
+ continue
+ }
+ var oldConditionExpr string
+ if binding.Condition != nil {
+ oldConditionExpr = binding.Condition.Expression
+ }
+ if oldConditionExpr != newConditionExpr {
+ continue
+ }
+ // Append
+ binding.Members = append(binding.Members, memberName)
+ return nil
+ }
+ condition := roleGrant.Condition
+ if condition == nil {
+ condition = &expr.Expr{}
+ }
+ condition.Description = fmt.Sprintf("#%d", issue.UID)
+ policy.Bindings = append(policy.Bindings, &storepb.Binding{
+ Role: roleGrant.Role,
+ Members: []string{memberName},
+ Condition: condition,
+ })
+ return nil
+ }); err != nil {
+ return errors.Wrapf(err, "failed to grant role in project %s", issue.ProjectID)
}
return nil
diff --git a/docs/design/v1-api-audit-2026-08.md b/docs/design/v1-api-audit-2026-08.md
index e67d1ed9c7..73ca117a99 100644
--- a/docs/design/v1-api-audit-2026-08.md
+++ b/docs/design/v1-api-audit-2026-08.md
@@ -12,7 +12,6 @@ noted. Fixed findings are removed and listed at the end.
|---|---|---|
| T12 | Anonymous callers can enumerate emails, relay mail, and read your LDAP config | MED |
| T16 | Four `BatchGet` RPCs behave four different ways when an item is missing | MED |
-| T18b | Concurrent permission edits silently lose one side, including a revoke | MED |
| T11 | A create-permission check is skipped on `allow_missing` updates | MED |
| T17 | Typing `"` in a search box breaks search | MED |
| T18c | Three smaller ones: broken filter, unpaginated list, half-finished delete | MED |
@@ -56,16 +55,6 @@ enforced. It's decorative, and it reads as protection. `acl.go:251-253`, `group_
## The API says OK and does nothing
-### T18b · Concurrent permission edits silently lose one side — MED
-
-`IamPolicy` has an etag for exactly this, and the docs promise it triggers ABORTED — but the
-handlers only check the etag on the *request wrapper*, never the one inside the policy. A normal
-read-modify-write round-trips the policy etag and has it silently discarded, so two admins editing
-permissions at once means one edit vanishes. If the lost edit was a revoke, access stays granted.
-
-The saved-query path already implements this correctly, so the pattern is in-tree.
-`project_service.go:601`, `workspace_service.go:437`
-
### T18c-i · Deleting several projects can half-finish — MED
`BatchDeleteProjects` purges in a loop with no transaction. A failure partway leaves some projects
@@ -186,9 +175,10 @@ constraints.
| T18 worksheet write/delete | [#21169](https://github.com/bytebase/bytebase/pull/21169) + [#21181](https://github.com/bytebase/bytebase/pull/21181) — per-verb permissions; SQL Editor Read User can no longer rewrite or delete |
| T18a | [#21265](https://github.com/bytebase/bytebase/pull/21265) — `BatchCancelTaskRuns` confines its lookup to the plan and environment just authorized; the stage in a task run name is caller-supplied, so cancel rights in one environment reached a RUNNING row in another. An out-of-scope ID is rejected, not dropped — the cancel is driven by the requested IDs |
| T9 | [#21189](https://github.com/bytebase/bytebase/pull/21189) (self-hosted audit rows) + [#21234](https://github.com/bytebase/bytebase/pull/21234) — one `login_attempt` table bounds password, email-code, and MFA guessing per identity on both deployments, replacing the audit-log counter and the per-code attempt column (which bypassed the resend cooldown). Accepted with it: no per-tenant failed-login record on Cloud, and lockout-as-denial-of-service — see [`login-attempt-lockout.md`](login-attempt-lockout.md) |
-| T15 | `update_mask` now decides what each of the four Update methods writes. **`UpdateDataSource`** dispatches the IAM credential on the mask path instead of the request's `authentication_type`, which was unset on any request that masked only the credential — the AIP-134 shape — so rotating a leaked key returned 200 and wrote nothing. The same branch, given a body type that disagreed with the mask, wrote the *other* credential and moved `authentication_type` with it though the mask named neither; the effective type is now resolved from the mask before the loop and a mismatched path is `InvalidArgument`. **`UpdateSetting(APP_IM)`** splices only the masked providers into the stored value rather than assigning the request wholesale, so saving Slack no longer erases the Feishu, WeCom, Lark, DingTalk and Teams secrets; the mask is now required, and a masked provider the payload omits is removed, which is how the console deletes one (it no longer merges the list client-side). **`UpdateIssue`** rejects paths it does not implement instead of returning 200 and dropping them — `status` belongs to `BatchUpdateIssuesStatus` and approvals to `ApproveIssue`/`RejectIssue`, so the supported set was right and only the silence was wrong. **`UpdateDatabaseCatalog`** keeps full-replace semantics and now says so in the proto; its `allow_missing` is removed and the number reserved, because a catalog is not independently creatable — it exists once its database is synced — so the flag had nothing to create, and the ACL's create-permission check on it denied every caller. Both merges were extracted into pure functions (`applyDataSourceUpdateMask`, `mergeAppIMSetting`), so `instance_service_test.go` and `setting_service_test.go` pin the behavior in milliseconds without a server or a metadata database. Accepted with it: the APP_IM mask paths still name no real proto field (`AppIMSetting` holds one repeated `settings`, and `value.app_im_setting_value.teams` is not a field path at all), so they are matched as a fixed vocabulary — making them true paths means restructuring the message one-field-per-provider, which needs a JSONB migration. Also left open, and the reason `UpdateDatabaseCatalog`'s hazard is documented rather than removed: a mask cannot express "this one table" while `schemas` is repeated, and read-modify-write on the catalog and on any IAM policy still loses one side of a concurrent edit, which needs an etag (AIP-154, and T18b) |
+| T15 | `update_mask` now decides what each of the four Update methods writes. **`UpdateDataSource`** dispatches the IAM credential on the mask path instead of the request's `authentication_type`, which was unset on any request that masked only the credential — the AIP-134 shape — so rotating a leaked key returned 200 and wrote nothing. The same branch, given a body type that disagreed with the mask, wrote the *other* credential and moved `authentication_type` with it though the mask named neither; the effective type is now resolved from the mask before the loop and a mismatched path is `InvalidArgument`. **`UpdateSetting(APP_IM)`** splices only the masked providers into the stored value rather than assigning the request wholesale, so saving Slack no longer erases the Feishu, WeCom, Lark, DingTalk and Teams secrets; the mask is now required, and a masked provider the payload omits is removed, which is how the console deletes one (it no longer merges the list client-side). **`UpdateIssue`** rejects paths it does not implement instead of returning 200 and dropping them — `status` belongs to `BatchUpdateIssuesStatus` and approvals to `ApproveIssue`/`RejectIssue`, so the supported set was right and only the silence was wrong. **`UpdateDatabaseCatalog`** keeps full-replace semantics and now says so in the proto; its `allow_missing` is removed and the number reserved, because a catalog is not independently creatable — it exists once its database is synced — so the flag had nothing to create, and the ACL's create-permission check on it denied every caller. Both merges were extracted into pure functions (`applyDataSourceUpdateMask`, `mergeAppIMSetting`), so `instance_service_test.go` and `setting_service_test.go` pin the behavior in milliseconds without a server or a metadata database. Accepted with it: the APP_IM mask paths still name no real proto field (`AppIMSetting` holds one repeated `settings`, and `value.app_im_setting_value.teams` is not a field path at all), so they are matched as a fixed vocabulary — making them true paths means restructuring the message one-field-per-provider, which needs a JSONB migration. Also left open, and the reason `UpdateDatabaseCatalog`'s hazard is documented rather than removed: a mask cannot express "this one table" while `schemas` is repeated, and read-modify-write on the catalog still loses one side of a concurrent edit, which needs an etag (AIP-154); the IAM-policy half of it is fixed, see T18b below |
| T14 | [#21267](https://github.com/bytebase/bytebase/pull/21267) — filed as issue paging; the sweep it prompted found the same defect across the class, so it was fixed as a class. Counting endpoints, **17 of the 22** offset-paginated v1 list RPCs were affected; counting the store functions behind them, **14 of 17**. The three already sorting on a total order were `ListQueryHistories` and `ListSavedQueries` (both from one commit, [#21203](https://github.com/bytebase/bytebase/pull/21203), that was never swept across the other lists) and `ListPlans`, which is total only because a mandatory `WHERE plan.project = ?` pins its scope column. Every store list now names tiebreak columns that are unique under its own scope — written into the SQL, and appended after the caller's keys at the six affected lists that accept an `order_by`. The three that were already total are left untouched. Nothing enforces this statically: an earlier revision routed all seventeen through a `buildStableOrderBy` helper so an AST test could require it, but review judged that ceremony and both were removed, so a new paginated list added without a tiebreak will not fail CI. Enforcement is step 4 of `docs/pre-pr-checklist.md` plus review, with `TestPaginationStabilityAcrossProjects` and `TestIssueCommentBatchKeepsInsertionOrder` covering the behavior against a real PostgreSQL. The rules, and the five traps that produced the class — `id` alone in a `(project, id)` table, `created_at` (the *transaction* timestamp, identical across a batch insert), a nullable column, a partial unique index, and an `order_by` that replaces the default ordering rather than adding to it — are in [`backend/store/AGENTS.md`](../../backend/store/AGENTS.md#pagination-ordering). One adjacent bug fixed with it: `ListDatabases`, `ListInstances` and `ListProjects` each threw away the whole clause, tiebreak included, the moment a caller passed `order_by` — which is what broke their paging. Accepted with them: offset paging still drifts under concurrent inserts and deletes (that needs keyset pagination and a page-token change). `ListIssueComment` needed a second fix: its `resource_id` tiebreak is a random UUID, which would have scrambled the activity feed of a multi-field `UpdateIssue`, so `CreateIssueComments` now offsets each row of a batch by its ordinal to keep `created_at` unique and in insertion order. Deliberately out of scope, and worth its own change: `issue.id` is a per-project counter, so ordering a cross-project list by it is not recency — seeded with a three-year-old project and one created today, the newest issue in the new project ranked 4962nd on the default list. Making `create_time` the cross-project default is a product decision. Also left alone: issue search still discards an explicit `order_by` outright when the caller supplied query text |
| T10 | [#21252](https://github.com/bytebase/bytebase/pull/21252) + [#21258](https://github.com/bytebase/bytebase/pull/21258) — password change and MFA lifecycle move off `UpdateUser` onto their own methods, each requiring a `CredentialProof`: current password, live OTP, recovery code, or a Cloud-only emailed re-auth code. Every proof claims a T9 login-attempt slot, so no proof channel is an unbounded guessing oracle, and a factor-touching method refuses the password while a live factor exists. Accepted with it: **the stolen access token still answers until it expires (≤1h)** — credential generation and the fenced transaction were cut or deferred, password change revokes only the account's web refresh tokens (best-effort, OAuth grants untouched), and an MFA change revokes nothing. What this closes is the credential being *spent* on its own replacement, not the session. Design, and the shipped-vs-designed delta: [`reauthenticate-credential-changes.md`](reauthenticate-credential-changes.md) |
+| T18b | The etag is now read wherever the caller presents it: `policy.etag` — the only place `GetIamPolicy` returns one, and so the only place a read-modify-write round-trips it — as well as the request field, with two that disagree rejected. Both handlers write through `store.SetIamPolicy`, which compares under `SELECT ... FOR UPDATE` inside the write transaction. Comparing against the handler's own earlier read would not have been enough: that is a check-then-act, and two requests reading the same policy both pass it before either writes. (An earlier revision justified this by replica cache skew, which does not exist: `store.New(ctx, pgURL, !profile.HA)` disables read caching in the only mode that runs more than one process.) The same lock closed two merges that present no etag at all and so could never be compared — `PatchWorkspaceIamPolicy` (signup, SSO provisioning, workspace-leave) and the role-grant issue approval in `backend/utils` — each of which read the policy, merged one member in, and wrote the whole thing back, so either could resurrect a member an admin had just revoked; both now merge inside the locked transaction through `store.PatchIamPolicy`. Fixed with it: workspace `SetIamPolicy` wrote to `request.Resource` while every read and check used the workspace from the token, so a request naming anything else matched no row and returned 200 having written nothing — the write now targets the workspace the reads did, which is what `GetIamPolicy` already does by ignoring the field. The audit delta and the invite emails are now derived from the policy the write actually replaced rather than the pre-validation snapshot, so they describe what happened even for an etag-less caller, and the workspace seat guard counts against that same locked policy through a `ValidateReplaced` hook rather than against a read the write does not hold. Accepted with it: an etag-less caller still gets last-write-wins, which is what AIP-154 optional-etag semantics mean and what existing API clients rely on; and the etag is still `updated_at` in milliseconds, so a write landing in the same millisecond as the one before it leaves the etag unmoved and a stale write racing that pair is accepted — too narrow to justify the wire change to a content hash, unlike the saved-query policy, which has no timestamp to derive one from. Also left as is: `validateIAMPolicy` grandfathers bindings found in the policy the handler read before the write, so an etag-less caller racing a concurrent removal re-adds that binding without the `allUsers`, role-existence, CEL and maximum-expiration checks — pre-existing, and closing it means splitting the function into a fetch half and a pure half that runs under the lock |
**`AIService.Chat` — accepted, not fixed** (2026-08-18). `Chat` requires an authenticated workspace
principal and touches no database or sheet, so the only exposure is LLM spend. On Cloud the feature
diff --git a/frontend/src/stores/app/iam.ts b/frontend/src/stores/app/iam.ts
index 6475deeb2a..24be4a4f2b 100644
--- a/frontend/src/stores/app/iam.ts
+++ b/frontend/src/stores/app/iam.ts
@@ -1,5 +1,5 @@
import { create as createProto } from "@bufbuild/protobuf";
-import { createContextValues } from "@connectrpc/connect";
+import { Code, ConnectError, createContextValues } from "@connectrpc/connect";
import { cloneDeep } from "lodash-es";
import {
projectServiceClientConnect,
@@ -53,6 +53,20 @@ const mergeBinding = ({
return next;
};
+// The server rejects a write whose etag no longer matches with ABORTED rather
+// than letting it overwrite the edit that got there first. The policy this
+// store holds is then the stale one that lost, so a retry would replay the same
+// conflict forever -- refetch it so the next attempt carries the etag it lost
+// to. The error itself is reported by the notification interceptor, as every
+// other request failure is.
+const recoverFromIamPolicyConflict = async (
+ error: unknown,
+ refetch: () => Promise<unknown>
+) => {
+ if (!(error instanceof ConnectError) || error.code !== Code.Aborted) return;
+ await refetch().catch(() => undefined);
+};
+
export const createIamSlice: AppSliceCreator<IamSlice> = (set, get) => ({
projectPoliciesByName: {},
projectPolicyRequests: {},
@@ -211,13 +225,32 @@ export const createIamSlice: AppSliceCreator<IamSlice> = (set, get) => ({
binding.members = [...new Set(binding.members)];
}
}
- const updated = await projectServiceClientConnect.setIamPolicy(
- createProto(SetIamPolicyRequestSchema, {
- resource: project,
- policy: deduped,
- etag: deduped.etag,
- })
- );
+ // The etag rides in both fields: `policy.etag` is where GetIamPolicy
+ // returns it and so where the round-trip carries it, and the request field
+ // is what an older server reads.
+ const updated = await projectServiceClientConnect
+ .setIamPolicy(
+ createProto(SetIamPolicyRequestSchema, {
+ resource: project,
+ policy: deduped,
+ etag: deduped.etag,
+ })
+ )
+ .catch(async (error) => {
+ await recoverFromIamPolicyConflict(error, async () => {
+ const fresh = await projectServiceClientConnect.getIamPolicy(
+ createProto(GetIamPolicyRequestSchema, { resource: project }),
+ { contextValues: createContextValues().set(silentContextKey, true) }
+ );
+ set((state) => ({
+ projectPoliciesByName: {
+ ...state.projectPoliciesByName,
+ [project]: fresh,
+ },
+ }));
+ });
+ throw error;
+ });
set((state) => ({
projectPoliciesByName: {
...state.projectPoliciesByName,
@@ -284,13 +317,20 @@ export const createIamSlice: AppSliceCreator<IamSlice> = (set, get) => ({
get().workspace?.name ||
get().currentUser?.workspace ||
`${workspaceNamePrefix}-`;
- const updated = await workspaceServiceClientConnect.setIamPolicy(
- createProto(SetIamPolicyRequestSchema, {
- resource,
- policy,
- etag: policy.etag,
- })
- );
+ const updated = await workspaceServiceClientConnect
+ .setIamPolicy(
+ createProto(SetIamPolicyRequestSchema, {
+ resource,
+ policy,
+ etag: policy.etag,
+ })
+ )
+ .catch(async (error) => {
+ await recoverFromIamPolicyConflict(error, () =>
+ get().fetchWorkspaceIamPolicy(true)
+ );
+ throw error;
+ });
set({ workspacePolicy: updated });
},
diff --git a/frontend/src/stores/app/index.test.ts b/frontend/src/stores/app/index.test.ts
index f1eec55ade..85fcefa857 100644
--- a/frontend/src/stores/app/index.test.ts
+++ b/frontend/src/stores/app/index.test.ts
@@ -1,5 +1,6 @@
import { create as createProto } from "@bufbuild/protobuf";
import { TimestampSchema } from "@bufbuild/protobuf/wkt";
+import { Code, ConnectError } from "@connectrpc/connect";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { silentContextKey } from "@/api/context-key";
import { isValidDatabaseGroupName, UNKNOWN_PROJECT_NAME } from "@/types";
@@ -118,6 +119,7 @@ const mocks = vi.hoisted(() => ({
getSetting: vi.fn(),
getProject: vi.fn(),
getProjectIamPolicy: vi.fn(),
+ setProjectIamPolicy: vi.fn(),
batchGetProjects: vi.fn(),
searchProjects: vi.fn(),
createProject: vi.fn(),
@@ -202,6 +204,7 @@ vi.mock("@/api", () => ({
projectServiceClientConnect: {
getProject: mocks.getProject,
getIamPolicy: mocks.getProjectIamPolicy,
+ setIamPolicy: mocks.setProjectIamPolicy,
batchGetProjects: mocks.batchGetProjects,
searchProjects: mocks.searchProjects,
createProject: mocks.createProject,
@@ -1088,6 +1091,138 @@ describe("useAppStore", () => {
expect(store.getState().workspacePolicy).toBe(setPolicy);
});
+ test("patchWorkspaceIamPolicy round-trips the etag it read", async () => {
+ const existing = createProto(IamPolicySchema, {
+ etag: "1756000000000",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/workspaceMember",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ mocks.setIamPolicy.mockResolvedValue(existing);
+ const store = createAppStore();
+ store.setState({ workspacePolicy: existing });
+
+ await store
+ .getState()
+ .patchWorkspaceIamPolicy([
+ { member: "user:[email protected]", roles: ["roles/workspaceMember"] },
+ ]);
+
+ // The server reads the etag from either field; sending both keeps an older
+ // server, which only reads the request field, checking it too.
+ const request = mocks.setIamPolicy.mock.calls[0][0] as {
+ etag: string;
+ policy: { etag: string };
+ };
+ expect(request.etag).toBe("1756000000000");
+ expect(request.policy.etag).toBe("1756000000000");
+ });
+
+ test("patchWorkspaceIamPolicy refetches after a concurrent edit", async () => {
+ const stale = createProto(IamPolicySchema, {
+ etag: "1756000000000",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/workspaceMember",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ // What the other admin left behind: alice revoked, and a new etag.
+ const current = createProto(IamPolicySchema, {
+ etag: "1756000009999",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/workspaceAdmin",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ mocks.setIamPolicy.mockRejectedValue(
+ new ConnectError("concurrent update", Code.Aborted)
+ );
+ mocks.getIamPolicy.mockResolvedValue(current);
+ const store = createAppStore();
+ store.setState({ workspacePolicy: stale });
+
+ await expect(
+ store
+ .getState()
+ .patchWorkspaceIamPolicy([
+ { member: "user:[email protected]", roles: ["roles/workspaceMember"] },
+ ])
+ ).rejects.toThrow();
+
+ // Without the refetch the store would keep the losing policy, and every
+ // retry would replay the same stale etag.
+ expect(store.getState().workspacePolicy).toBe(current);
+ });
+
+ test("updateProjectIamPolicy refetches after a concurrent edit", async () => {
+ const stale = createProto(IamPolicySchema, {
+ etag: "1756000000000",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/projectDeveloper",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ const current = createProto(IamPolicySchema, {
+ etag: "1756000009999",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/projectOwner",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ mocks.setProjectIamPolicy.mockRejectedValue(
+ new ConnectError("concurrent update", Code.Aborted)
+ );
+ mocks.getProjectIamPolicy.mockResolvedValue(current);
+ const store = createAppStore();
+ store.setState({ projectPoliciesByName: { [projectA.name]: stale } });
+
+ await expect(
+ store.getState().updateProjectIamPolicy(projectA.name, stale)
+ ).rejects.toThrow();
+
+ expect(mocks.setProjectIamPolicy.mock.calls[0][0]).toMatchObject({
+ etag: "1756000000000",
+ });
+ expect(store.getState().projectPoliciesByName[projectA.name]).toBe(current);
+ });
+
+ test("updateProjectIamPolicy leaves the cached policy alone on other errors", async () => {
+ const stale = createProto(IamPolicySchema, {
+ etag: "1756000000000",
+ bindings: [
+ createProto(BindingSchema, {
+ role: "roles/projectDeveloper",
+ members: ["user:[email protected]"],
+ }),
+ ],
+ });
+ mocks.setProjectIamPolicy.mockRejectedValue(
+ new ConnectError("at least one owner", Code.InvalidArgument)
+ );
+ const store = createAppStore();
+ store.setState({ projectPoliciesByName: { [projectA.name]: stale } });
+
+ await expect(
+ store.getState().updateProjectIamPolicy(projectA.name, stale)
+ ).rejects.toThrow();
+
+ // A rejected write left the server's policy where it was, so the etag this
+ // store holds is still good and refetching would be churn.
+ expect(mocks.getProjectIamPolicy).not.toHaveBeenCalled();
+ expect(store.getState().projectPoliciesByName[projectA.name]).toBe(stale);
+ });
+
test("workspaceUserMapToRoles inverts policy bindings to member -> roles", () => {
const policy = createProto(IamPolicySchema, {
bindings: [
diff --git a/frontend/src/types/proto-es/v1/iam_policy_pb.d.ts b/frontend/src/types/proto-es/v1/iam_policy_pb.d.ts
index af9750e9ba..064a15ab4a 100644
--- a/frontend/src/types/proto-es/v1/iam_policy_pb.d.ts
+++ b/frontend/src/types/proto-es/v1/iam_policy_pb.d.ts
@@ -55,7 +55,8 @@ export declare type SetIamPolicyRequest = Message<"bytebase.v1.SetIamPolicyReque
policy?: IamPolicy | undefined;
/**
- * The current etag of the policy.
+ * The current etag of the policy. Equivalent to setting `policy.etag`;
+ * supplying both with different values returns INVALID_ARGUMENT.
*
* @generated from field: string etag = 3;
*/
@@ -83,9 +84,10 @@ export declare type IamPolicy = Message<"bytebase.v1.IamPolicy"> & {
bindings: Binding[];
/**
- * The current etag of the policy.
- * If an etag is provided and does not match the current etag of the policy,
- * the call will be blocked and an ABORTED error will be returned.
+ * The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+ * Sending it back on SetIamPolicy makes the write conditional: if the policy
+ * changed since the read, the call returns ABORTED. Omitting it applies the
+ * write unconditionally, overwriting any concurrent change (AIP-154).
*
* @generated from field: string etag = 2;
*/
diff --git a/frontend/src/types/proto-es/v1/iam_policy_pb.js b/frontend/src/types/proto-es/v1/iam_policy_pb.js
index 453ed8580f..b939af6e30 100644
--- a/frontend/src/types/proto-es/v1/iam_policy_pb.js
+++ b/frontend/src/types/proto-es/v1/iam_policy_pb.js
@@ -12,7 +12,7 @@ import { file_google_type_expr } from "../google/type/expr_pb";
* Describes the file v1/iam_policy.proto.
*/
export const file_v1_iam_policy = /*@__PURE__*/
- fileDesc("ChN2MS9pYW1fcG9saWN5LnByb3RvEgtieXRlYmFzZS52MSJHChNHZXRJYW1Qb2xpY3lSZXF1ZXN0EjAKCHJlc291cmNlGAEgASgJQh7gQQL6QRgSFmJ5dGViYXNlLmNvbS9JQU1Qb2xpY3kiggEKE1NldElhbVBvbGljeVJlcXVlc3QSMAoIcmVzb3VyY2UYASABKAlCHuBBAvpBGBIWYnl0ZWJhc2UuY29tL0lBTVBvbGljeRIrCgZwb2xpY3kYAiABKAsyFi5ieXRlYmFzZS52MS5JYW1Qb2xpY3lCA+BBAhIMCgRldGFnGAMgASgJIkEKCUlhbVBvbGljeRImCghiaW5kaW5ncxgBIAMoCzIULmJ5dGViYXNlLnYxLkJpbmRpbmcSDAoEZXRhZxgCIAEoCSKIAQoHQmluZGluZxIMCgRyb2xlGAEgASgJEg8KB21lbWJlcnMYAiADKAkSJAoJY29uZGl0aW9uGAMgASgLMhEuZ29vZ2xlLnR5cGUuRXhwchI4CgtwYXJzZWRfZXhwchgEIAEoCzIeLmdvb2dsZS5hcGkuZXhwci52MWFscGhhMS5FeHByQgPgQQMiQAoLUG9saWN5RGVsdGESMQoOYmluZGluZ19kZWx0YXMYASADKAsyGS5ieXRlYmFzZS52MS5CaW5kaW5nRGVsdGEiuwEKDEJpbmRpbmdEZWx0YRIwCgZhY3Rpb24YASABKA4yIC5ieXRlYmFzZS52MS5CaW5kaW5nRGVsdGEuQWN0aW9uEgwKBHJvbGUYAiABKAkSDgoGbWVtYmVyGAMgASgJEiQKCWNvbmRpdGlvbhgEIAEoCzIRLmdvb2dsZS50eXBlLkV4cHIiNQoGQWN0aW9uEhYKEkFDVElPTl9VTlNQRUNJRklFRBAAEgcKA0FERBABEgoKBlJFTU9WRRACQqQBCg9jb20uYnl0ZWJhc2UudjFCDklhbVBvbGljeVByb3RvUAFaNGdpdGh1Yi5jb20vYnl0ZWJhc2UvYnl0ZWJhc2UvYmFja2VuZC9nZW5lcmF0ZWQtZ28vdjGiAgNCWFiqAgtCeXRlYmFzZS5WMcoCC0J5dGViYXNlXFYx4gIXQnl0ZWJhc2VcVjFcR1BCTWV0YWRhdGHqAgxCeXRlYmFzZTo6VjFiBnByb3RvMw", [file_google_api_expr_v1alpha1_syntax, file_google_api_field_behavior, file_google_api_resource, file_google_type_expr]);
+ fileDesc("ChN2MS9pYW1fcG9saWN5LnByb3RvEgtieXRlYmFzZS52MSJHChNHZXRJYW1Qb2xpY3lSZXF1ZXN0EjAKCHJlc291cmNlGAEgASgJQh7gQQL6QRgSFmJ5dGViYXNlLmNvbS9JQU1Qb2xpY3kihwEKE1NldElhbVBvbGljeVJlcXVlc3QSMAoIcmVzb3VyY2UYASABKAlCHuBBAvpBGBIWYnl0ZWJhc2UuY29tL0lBTVBvbGljeRIrCgZwb2xpY3kYAiABKAsyFi5ieXRlYmFzZS52MS5JYW1Qb2xpY3lCA+BBAhIRCgRldGFnGAMgASgJQgPgQQEiQQoJSWFtUG9saWN5EiYKCGJpbmRpbmdzGAEgAygLMhQuYnl0ZWJhc2UudjEuQmluZGluZxIMCgRldGFnGAIgASgJIogBCgdCaW5kaW5nEgwKBHJvbGUYASABKAkSDwoHbWVtYmVycxgCIAMoCRIkCgljb25kaXRpb24YAyABKAsyES5nb29nbGUudHlwZS5FeHByEjgKC3BhcnNlZF9leHByGAQgASgLMh4uZ29vZ2xlLmFwaS5leHByLnYxYWxwaGExLkV4cHJCA+BBAyJACgtQb2xpY3lEZWx0YRIxCg5iaW5kaW5nX2RlbHRhcxgBIAMoCzIZLmJ5dGViYXNlLnYxLkJpbmRpbmdEZWx0YSK7AQoMQmluZGluZ0RlbHRhEjAKBmFjdGlvbhgBIAEoDjIgLmJ5dGViYXNlLnYxLkJpbmRpbmdEZWx0YS5BY3Rpb24SDAoEcm9sZRgCIAEoCRIOCgZtZW1iZXIYAyABKAkSJAoJY29uZGl0aW9uGAQgASgLMhEuZ29vZ2xlLnR5cGUuRXhwciI1CgZBY3Rpb24SFgoSQUNUSU9OX1VOU1BFQ0lGSUVEEAASBwoDQUREEAESCgoGUkVNT1ZFEAJCpAEKD2NvbS5ieXRlYmFzZS52MUIOSWFtUG9saWN5UHJvdG9QAVo0Z2l0aHViLmNvbS9ieXRlYmFzZS9ieXRlYmFzZS9iYWNrZW5kL2dlbmVyYXRlZC1nby92MaICA0JYWKoCC0J5dGViYXNlLlYxygILQnl0ZWJhc2VcVjHiAhdCeXRlYmFzZVxWMVxHUEJNZXRhZGF0YeoCDEJ5dGViYXNlOjpWMWIGcHJvdG8z", [file_google_api_expr_v1alpha1_syntax, file_google_api_field_behavior, file_google_api_resource, file_google_type_expr]);
/**
* Describes the message bytebase.v1.GetIamPolicyRequest.
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index c8b3251af4..24be63e57f 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -15322,7 +15322,9 @@ paths:
etag:
type: string
title: etag
- description: The current etag of the policy.
+ description: |-
+ (OPTIONAL) The current etag of the policy. Equivalent to setting `policy.etag`;
+ supplying both with different values returns INVALID_ARGUMENT.
title: SetIamPolicyRequest
required:
- policy
@@ -18999,7 +19001,9 @@ paths:
etag:
type: string
title: etag
- description: The current etag of the policy.
+ description: |-
+ (OPTIONAL) The current etag of the policy. Equivalent to setting `policy.etag`;
+ supplying both with different values returns INVALID_ARGUMENT.
title: SetIamPolicyRequest
required:
- policy
@@ -24219,9 +24223,10 @@ components:
type: string
title: etag
description: |-
- The current etag of the policy.
- If an etag is provided and does not match the current etag of the policy,
- the call will be blocked and an ABORTED error will be returned.
+ The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+ Sending it back on SetIamPolicy makes the write conditional: if the policy
+ changed since the read, the call returns ABORTED. Omitting it applies the
+ write unconditionally, overwriting any concurrent change (AIP-154).
title: IamPolicy
additionalProperties: false
description: IAM policy that binds members to roles.
@@ -30726,7 +30731,9 @@ components:
etag:
type: string
title: etag
- description: The current etag of the policy.
+ description: |-
+ (OPTIONAL) The current etag of the policy. Equivalent to setting `policy.etag`;
+ supplying both with different values returns INVALID_ARGUMENT.
title: SetIamPolicyRequest
required:
- resource
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 2c789f4494..4a16e89ebb 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -1736,7 +1736,7 @@ IAM policy that binds members to roles.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| bindings | [Binding](#bytebase-v1-Binding) | repeated | Collection of binding. A binding binds one or more project members to a single project role. |
-| etag | [string](#string) | | The current etag of the policy. If an etag is provided and does not match the current etag of the policy, the call will be blocked and an ABORTED error will be returned. |
+| etag | [string](#string) | | The current etag of the policy, returned by GetIamPolicy and SetIamPolicy. Sending it back on SetIamPolicy makes the write conditional: if the policy changed since the read, the call returns ABORTED. Omitting it applies the write unconditionally, overwriting any concurrent change (AIP-154). |
@@ -1768,7 +1768,7 @@ Request message for setting an IAM policy.
| ----- | ---- | ----- | ----------- |
| resource | [string](#string) | | The name of the resource to set the IAM policy. Format: projects/{project} Format: workspaces/{workspace} |
| policy | [IamPolicy](#bytebase-v1-IamPolicy) | | |
-| etag | [string](#string) | | The current etag of the policy. |
+| etag | [string](#string) | | The current etag of the policy. Equivalent to setting `policy.etag`; supplying both with different values returns INVALID_ARGUMENT. |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 14a352b7a0..dbc8f0fc5f 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -5822,9 +5822,10 @@ A binding binds one or more project members to a single project role. </p></td>
<td>etag</td>
<td><a href="#string">string</a></td>
<td></td>
- <td><p>The current etag of the policy.
-If an etag is provided and does not match the current etag of the policy,
-the call will be blocked and an ABORTED error will be returned. </p></td>
+ <td><p>The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+Sending it back on SetIamPolicy makes the write conditional: if the policy
+changed since the read, the call returns ABORTED. Omitting it applies the
+write unconditionally, overwriting any concurrent change (AIP-154). </p></td>
</tr>
</tbody>
@@ -5888,7 +5889,8 @@ Format: workspaces/{workspace} </p></td>
<td>etag</td>
<td><a href="#string">string</a></td>
<td></td>
- <td><p>The current etag of the policy. </p></td>
+ <td><p>The current etag of the policy. Equivalent to setting `policy.etag`;
+supplying both with different values returns INVALID_ARGUMENT. </p></td>
</tr>
</tbody>
diff --git a/proto/v1/v1/iam_policy.proto b/proto/v1/v1/iam_policy.proto
index 21a9301ed5..a5b9dae387 100644
--- a/proto/v1/v1/iam_policy.proto
+++ b/proto/v1/v1/iam_policy.proto
@@ -32,8 +32,9 @@ message SetIamPolicyRequest {
IamPolicy policy = 2 [(google.api.field_behavior) = REQUIRED];
- // The current etag of the policy.
- string etag = 3;
+ // The current etag of the policy. Equivalent to setting `policy.etag`;
+ // supplying both with different values returns INVALID_ARGUMENT.
+ string etag = 3 [(google.api.field_behavior) = OPTIONAL];
}
// IAM policy that binds members to roles.
@@ -42,9 +43,10 @@ message IamPolicy {
// A binding binds one or more project members to a single project role.
repeated Binding bindings = 1;
- // The current etag of the policy.
- // If an etag is provided and does not match the current etag of the policy,
- // the call will be blocked and an ABORTED error will be returned.
+ // The current etag of the policy, returned by GetIamPolicy and SetIamPolicy.
+ // Sending it back on SetIamPolicy makes the write conditional: if the policy
+ // changed since the read, the call returns ABORTED. Omitting it applies the
+ // write unconditionally, overwriting any concurrent change (AIP-154).
string etag = 2;
}