MCPcopy Create free account
hub / github.com/bytebase/bytebase / validateIAMPolicy

Function validateIAMPolicy

backend/api/v1/project_service.go:1070–1129  ·  view source on GitHub ↗
(
	ctx context.Context,
	stores *store.Store,
	allowAllUsers bool,
	msg *v1pb.SetIamPolicyRequest,
	oldPolicyMessage *store.IamPolicyMessage,
)

Source from the content-addressed store, hash-verified

1068}
1069
1070func validateIAMPolicy(
1071 ctx context.Context,
1072 stores *store.Store,
1073 allowAllUsers bool,
1074 msg *v1pb.SetIamPolicyRequest,
1075 oldPolicyMessage *store.IamPolicyMessage,
1076) error {
1077 if msg.Policy == nil {
1078 return connect.NewError(connect.CodeInvalidArgument, errors.New("IAM Policy is required"))
1079 }
1080 if len(msg.Policy.Bindings) == 0 {
1081 return connect.NewError(connect.CodeInvalidArgument, errors.New("IAM Binding is empty"))
1082 }
1083
1084 workspaceProfileSetting, err := stores.GetWorkspaceProfileSetting(ctx, common.GetWorkspaceIDFromContext(ctx))
1085 if err != nil {
1086 return connect.NewError(connect.CodeInternal, errors.New("failed to get workspace profile setting"))
1087 }
1088 var maximumRequestExpiration *durationpb.Duration
1089 if workspaceProfileSetting != nil {
1090 maximumRequestExpiration = workspaceProfileSetting.MaximumRequestExpiration
1091 }
1092
1093 roleMessages, err := stores.ListRoles(ctx, &store.FindRoleMessage{Workspace: common.GetWorkspaceIDFromContext(ctx)})
1094 if err != nil {
1095 return connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list roles"))
1096 }
1097
1098 existingBindings := make(map[string]bool)
1099 for _, oldBinding := range oldPolicyMessage.Policy.Bindings {
1100 identifier := getBindingIdentifier(oldBinding.Role, oldBinding.Condition)
1101 existingBindings[identifier] = true
1102 }
1103
1104 bindings := []*v1pb.Binding{}
1105 for _, binding := range msg.Policy.Bindings {
1106 if len(binding.Members) == 0 {
1107 continue
1108 }
1109
1110 identifier := getBindingIdentifier(binding.Role, binding.Condition)
1111 if !existingBindings[identifier] {
1112 bindings = append(bindings, binding)
1113 }
1114 }
1115
1116 // If allUsers is not allowed in IAM policies, members must be explicitly added.
1117 if !allowAllUsers {
1118 for _, binding := range bindings {
1119 for _, member := range binding.Members {
1120 if member == common.AllUsers {
1121 return connect.NewError(connect.CodeInvalidArgument,
1122 errors.New("allUsers is not allowed in workspace IAM policy in SaaS mode, add members explicitly"))
1123 }
1124 }
1125 }
1126 }
1127

Callers 2

SetIamPolicyMethod · 0.85
SetIamPolicyMethod · 0.85

Calls 5

validateBindingsFunction · 0.85
getBindingIdentifierFunction · 0.70
ListRolesMethod · 0.65

Tested by

no test coverage detected