Skip to content

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

fix: respect condition expired time in IAM binding (#12498)

ecmadaob7f81487ac1c
diff --git a/backend/utils/member.go b/backend/utils/member.go
index 33784912a1..31c88d4130 100644
--- a/backend/utils/member.go
+++ b/backend/utils/member.go
@@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"strings"
+ "time"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
@@ -12,8 +13,16 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
+func validateIAMBinding(binding *storepb.Binding) bool {
+ ok, err := common.EvalBindingCondition(binding.Condition.GetExpression(), time.Now())
+ if err != nil {
+ slog.Error("failed to eval binding condition", slog.String("expression", binding.Condition.GetExpression()), log.BBError(err))
+ return false
+ }
+ return ok
+}
+
// GetUsersByRoleInIAMPolicy gets users in the iam policy.
-// TODO(p0ny): renovate this function to respect CEL.
func GetUsersByRoleInIAMPolicy(ctx context.Context, stores *store.Store, role api.Role, policy *storepb.ProjectIamPolicy) []*store.UserMessage {
roleFullName := common.FormatRole(role.String())
var users []*store.UserMessage
@@ -23,6 +32,10 @@ func GetUsersByRoleInIAMPolicy(ctx context.Context, stores *store.Store, role ap
continue
}
+ if !validateIAMBinding(binding) {
+ continue
+ }
+
for _, member := range binding.Members {
if member == api.AllUsers {
// TODO(d): make it more efficient.
@@ -92,3 +105,101 @@ func getUserByIdentifier(ctx context.Context, stores *store.Store, identifier st
}
return user
}
+
+// GetUserIAMPolicyBindings return the valid bindings for the user.
+func GetUserIAMPolicyBindings(ctx context.Context, stores *store.Store, user *store.UserMessage, policy *storepb.ProjectIamPolicy) []*storepb.Binding {
+ userIDFullName := common.FormatUserUID(user.ID)
+
+ var bindings []*storepb.Binding
+ for _, binding := range policy.Bindings {
+ if !validateIAMBinding(binding) {
+ continue
+ }
+
+ hasUser := false
+ for _, member := range binding.Members {
+ if member == api.AllUsers {
+ hasUser = true
+ break
+ }
+ if userIDFullName == member {
+ hasUser = true
+ break
+ }
+ if strings.HasPrefix(member, common.UserGroupPrefix) {
+ groupEmail, err := common.GetUserGroupEmail(member)
+ if err != nil {
+ slog.Error("failed to parse group email", slog.String("group", member), log.BBError(err))
+ continue
+ }
+ group, err := stores.GetUserGroup(ctx, groupEmail)
+ if err != nil {
+ slog.Error("failed to get group", slog.String("group", member), log.BBError(err))
+ continue
+ }
+ if group == nil {
+ slog.Error("cannot found group", slog.String("group", member))
+ continue
+ }
+ for _, member := range group.Payload.Members {
+ if userIDFullName == member.Member {
+ hasUser = true
+ break
+ }
+ }
+ }
+ }
+ if hasUser {
+ bindings = append(bindings, binding)
+ }
+ }
+ return bindings
+}
+
+// getUserRoles returns the `uniq`ed roles of a user, including workspace roles and the roles in the projects.
+// the condition of role binding is respected and evaluated with request.time=time.Now().
+// the returned role name should in the roles/{id} format.
+func getUserRoles(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) ([]string, error) {
+ var roles []string
+ for _, userRole := range user.Roles {
+ roles = append(roles, common.FormatRole(userRole.String()))
+ }
+
+ for _, projectPolicy := range projectPolicies {
+ bindings := GetUserIAMPolicyBindings(ctx, stores, user, projectPolicy)
+ for _, binding := range bindings {
+ roles = append(roles, binding.Role)
+ }
+ }
+ roles = uniq(roles)
+
+ return roles, nil
+}
+
+// See GetUserRoles.
+func GetUserRolesMap(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) (map[api.Role]bool, error) {
+ roles, err := getUserRoles(ctx, stores, user, projectPolicies...)
+ if err != nil {
+ return nil, err
+ }
+
+ rolesMap := make(map[api.Role]bool)
+ for _, role := range roles {
+ rolesMap[api.Role(strings.TrimPrefix(role, "roles/"))] = true
+ }
+ return rolesMap, nil
+}
+
+// See GetUserRoles. The returned map key format is roles/{role}.
+func GetUserFormattedRolesMap(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) (map[string]bool, error) {
+ roles, err := getUserRoles(ctx, stores, user, projectPolicies...)
+ if err != nil {
+ return nil, err
+ }
+
+ rolesMap := make(map[string]bool)
+ for _, role := range roles {
+ rolesMap[role] = true
+ }
+ return rolesMap, nil
+}
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index 21014c2634..ead1fa6071 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -692,110 +692,6 @@ func GetMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx context.Context, databas
return matches, unmatches, nil
}
-// GetUserIAMPolicyBindings return the valid bindings for the user.
-func GetUserIAMPolicyBindings(ctx context.Context, stores *store.Store, user *store.UserMessage, policy *storepb.ProjectIamPolicy) []*storepb.Binding {
- userIDFullName := common.FormatUserUID(user.ID)
- currentTime := time.Now()
-
- var bindings []*storepb.Binding
- for _, binding := range policy.Bindings {
- ok, err := common.EvalBindingCondition(binding.Condition.GetExpression(), currentTime)
- if err != nil {
- slog.Error("failed to eval binding condition", slog.String("expression", binding.Condition.GetExpression()), log.BBError(err))
- continue
- }
- if !ok {
- continue
- }
-
- hasUser := false
- for _, member := range binding.Members {
- if member == api.AllUsers {
- hasUser = true
- break
- }
- if userIDFullName == member {
- hasUser = true
- break
- }
- if strings.HasPrefix(member, common.UserGroupPrefix) {
- groupEmail, err := common.GetUserGroupEmail(member)
- if err != nil {
- slog.Error("failed to parse group email", slog.String("group", member), log.BBError(err))
- continue
- }
- group, err := stores.GetUserGroup(ctx, groupEmail)
- if err != nil {
- slog.Error("failed to get group", slog.String("group", member), log.BBError(err))
- continue
- }
- if group == nil {
- slog.Error("cannot found group", slog.String("group", member))
- continue
- }
- for _, member := range group.Payload.Members {
- if userIDFullName == member.Member {
- hasUser = true
- break
- }
- }
- }
- }
- if hasUser {
- bindings = append(bindings, binding)
- }
- }
- return bindings
-}
-
-// getUserRoles returns the `uniq`ed roles of a user, including workspace roles and the roles in the projects.
-// the condition of role binding is respected and evaluated with request.time=time.Now().
-// the returned role name should in the roles/{id} format.
-func getUserRoles(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) ([]string, error) {
- var roles []string
- for _, userRole := range user.Roles {
- roles = append(roles, common.FormatRole(userRole.String()))
- }
-
- for _, projectPolicy := range projectPolicies {
- bindings := GetUserIAMPolicyBindings(ctx, stores, user, projectPolicy)
- for _, binding := range bindings {
- roles = append(roles, binding.Role)
- }
- }
- roles = uniq(roles)
-
- return roles, nil
-}
-
-// See GetUserRoles.
-func GetUserRolesMap(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) (map[api.Role]bool, error) {
- roles, err := getUserRoles(ctx, stores, user, projectPolicies...)
- if err != nil {
- return nil, err
- }
-
- rolesMap := make(map[api.Role]bool)
- for _, role := range roles {
- rolesMap[api.Role(strings.TrimPrefix(role, "roles/"))] = true
- }
- return rolesMap, nil
-}
-
-// See GetUserRoles. The returned map key format is roles/{role}.
-func GetUserFormattedRolesMap(ctx context.Context, stores *store.Store, user *store.UserMessage, projectPolicies ...*storepb.ProjectIamPolicy) (map[string]bool, error) {
- roles, err := getUserRoles(ctx, stores, user, projectPolicies...)
- if err != nil {
- return nil, err
- }
-
- rolesMap := make(map[string]bool)
- for _, role := range roles {
- rolesMap[role] = true
- }
- return rolesMap, nil
-}
-
func uniq[T comparable](array []T) []T {
res := make([]T, 0, len(array))
seen := make(map[T]struct{}, len(array))
diff --git a/frontend/src/components/ProjectMember/ProjectMemberDataTable/cells/UserRolesCell.vue b/frontend/src/components/ProjectMember/ProjectMemberDataTable/cells/UserRolesCell.vue
index 103f59615d..0377dc98e9 100644
--- a/frontend/src/components/ProjectMember/ProjectMemberDataTable/cells/UserRolesCell.vue
+++ b/frontend/src/components/ProjectMember/ProjectMemberDataTable/cells/UserRolesCell.vue
@@ -11,7 +11,11 @@
</template>
{{ displayRoleTitle(role) }}
</NTag>
- <NTag v-for="binding in projectRoleBindings" :key="binding.role">
+ <NTag
+ v-for="binding in projectRoleBindings"
+ :key="binding.role"
+ :class="isBindingPolicyExpired(binding) ? 'line-through' : ''"
+ >
{{ displayRoleTitle(binding.role) }}
</NTag>
</div>
@@ -22,7 +26,7 @@ import { orderBy } from "lodash-es";
import { Building2Icon } from "lucide-vue-next";
import { NTag, NTooltip } from "naive-ui";
import { computed } from "vue";
-import { displayRoleTitle, sortRoles } from "@/utils";
+import { displayRoleTitle, sortRoles, isBindingPolicyExpired } from "@/utils";
import type { ProjectRole } from "../../types";
const props = defineProps<{
diff --git a/frontend/src/components/ProjectMember/ProjectMemberRolePanel/EditProjectRolePanel.vue b/frontend/src/components/ProjectMember/ProjectMemberRolePanel/EditProjectRolePanel.vue
index b160c1c8b9..55d997427b 100644
--- a/frontend/src/components/ProjectMember/ProjectMemberRolePanel/EditProjectRolePanel.vue
+++ b/frontend/src/components/ProjectMember/ProjectMemberRolePanel/EditProjectRolePanel.vue
@@ -123,8 +123,8 @@
<script lang="ts" setup>
import { cloneDeep, isEqual, uniq } from "lodash-es";
import { NButton, NDatePicker, NInput, NInputNumber } from "naive-ui";
-import { computed, reactive, ref } from "vue";
-import { onMounted } from "vue";
+import { computed, reactive, ref, onMounted } from "vue";
+import { useI18n } from "vue-i18n";
import QuerierDatabaseResourceForm from "@/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/index.vue";
import { Drawer, DrawerContent } from "@/components/v2";
import {
@@ -134,6 +134,7 @@ import {
useProjectIamPolicyStore,
useUserStore,
useUserGroupStore,
+ pushNotification,
} from "@/store";
import type { ComposedProject, DatabaseResource } from "@/types";
import {
@@ -171,6 +172,7 @@ interface LocalState {
databaseId?: string;
}
+const { t } = useI18n();
const databaseStore = useDatabaseV1Store();
const userStore = useUserStore();
const groupStore = useUserGroupStore();
@@ -342,6 +344,12 @@ const handleUpdateRole = async () => {
policy
);
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("common.updated"),
+ });
+
emit("close");
};
</script>
diff --git a/frontend/src/components/ProjectMember/ProjectMemberRolePanel/index.vue b/frontend/src/components/ProjectMember/ProjectMemberRolePanel/index.vue
index 2571106ac7..603edf898c 100644
--- a/frontend/src/components/ProjectMember/ProjectMemberRolePanel/index.vue
+++ b/frontend/src/components/ProjectMember/ProjectMemberRolePanel/index.vue
@@ -86,7 +86,10 @@
<template #item="{ item }: SingleBindingRow">
<div class="bb-grid-cell">
<span
- class="text-blue-600 cursor-pointer hover:opacity-80"
+ :class="[
+ 'text-blue-600 cursor-pointer hover:opacity-80',
+ checkRoleExpired(item) ? 'line-through' : '',
+ ]"
@click="editingBinding = item.rawBinding"
>
{{
@@ -119,8 +122,10 @@
{{ extractTableName(item.databaseResource) }}
</div>
</template>
- <div class="bb-grid-cell">
- {{ extractExpiration(item.expiration) }}
+ <div class="bb-grid-cell flex items-center space-x-1">
+ <span :class="checkRoleExpired(item) ? 'line-through' : ''">
+ {{ extractExpiration(item.expiration) }}
+ </span>
<RoleExpiredTip v-if="checkRoleExpired(item)" />
</div>
<div class="bb-grid-cell">
diff --git a/frontend/src/utils/v1/project.ts b/frontend/src/utils/v1/project.ts
index 09b0613aa8..5e8e50a9f5 100644
--- a/frontend/src/utils/v1/project.ts
+++ b/frontend/src/utils/v1/project.ts
@@ -14,6 +14,20 @@ import { User } from "@/types/proto/v1/auth_service";
import { State } from "@/types/proto/v1/common";
import type { IamPolicy, Binding } from "@/types/proto/v1/iam_policy";
import type { Project } from "@/types/proto/v1/project_service";
+import { convertFromExpr } from "@/utils/issue/cel";
+
+export const isBindingPolicyExpired = (binding: Binding): boolean => {
+ if (binding.parsedExpr?.expr) {
+ const conditionExpr = convertFromExpr(binding.parsedExpr.expr);
+ if (conditionExpr.expiredTime) {
+ const expiration = new Date(conditionExpr.expiredTime);
+ if (expiration < new Date()) {
+ return true;
+ }
+ }
+ }
+ return false;
+};
export const extractProjectResourceName = (name: string) => {
const pattern = /(?:^|\/)projects\/([^/]+)(?:$|\/)/;
@@ -31,6 +45,9 @@ export const roleListInProjectV1 = (iamPolicy: IamPolicy, user: User) => {
const projectBindingRoles = iamPolicy.bindings
.filter((binding) => {
+ if (isBindingPolicyExpired(binding)) {
+ return false;
+ }
for (const member of binding.members) {
if (member === ALL_USERS_USER_EMAIL) {
return true;
@@ -80,6 +97,10 @@ export const isViewerOfProjectV1 = (iamPolicy: IamPolicy, user: User) => {
};
export const getUserEmailListInBinding = (binding: Binding): string[] => {
+ if (isBindingPolicyExpired(binding)) {
+ return [];
+ }
+
const groupStore = useUserGroupStore();
const userStore = useUserStore();
const emailList = [];
@@ -111,6 +132,10 @@ export const memberListInProjectV1 = (iamPolicy: IamPolicy) => {
// rolesMapByEmail is Map<email, role list>
const rolesMapByEmail = new Map<string, Set<string>>();
for (const binding of iamPolicy.bindings) {
+ if (isBindingPolicyExpired(binding)) {
+ continue;
+ }
+
const emails = getUserEmailListInBinding(binding);
for (const email of emails) {