feat(rt): project grants (#10951)

This pull request introduces a new feature that allows adding, updating,
and querying project grants The changes are primarily in the backend and
include new database tables, repositories, and domain logic to support
project grants.

## Changes

* New `project_grants` table: A new table `zitadel.project_grants` is
introduced to store project grants It includes columns for
`instance_id`, `id`, `state`, `project_id`, `granted_org_id` and
`granting_org_id`.
* New `ProjectGrantRepository`: A new repository
`ProjectGrantRepository` is created to handle all database operations
for project grants. It provides methods to `Get`, `List`, `Create`,
`Update`, and `Delete`.
* New `project_grant_roles` table: A new table
`zitadel.project_grant_roles` is introduced to store project grant roles
It includes columns for `instance_id`, `grant_id`, `key`, `project_id`
and `project_org_id`.
* New `ProjectGrantRoleRepository`: A new repository
`ProjectGrantRoleRepository` is created to handle all database
operations for project grant roles. It provides methods to `Get`,
`List`, `Add`, and `Delete`.
* New `project_grant_relational_projection`: A new projection
`project_grant_relational_projection` is added to update the
`zitadel.project_grants` and `zitadel.project_grant_roles` tables based
on events. It handles all project grant events.
* Updated `ProjectRepository`: The `ProjectRepository` is updated to
list granted projects.

# Additional Context

Closes https://github.com/zitadel/zitadel/issues/10766

---------

Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: Marco A. <[email protected]>
Co-authored-by: Silvan <[email protected]>
This commit is contained in:
Tim Möhlmann
2025-11-14 11:04:16 +00:00
committed by GitHub
co-authored by Stefan Benz Marco A. Silvan
parent 6051ce591d
commit 9274e008fe
15 changed files with 1782 additions and 30 deletions
@@ -0,0 +1,154 @@
//go:build integration
package events_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/backend/v3/domain"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
"github.com/zitadel/zitadel/internal/integration"
org "github.com/zitadel/zitadel/pkg/grpc/org/v2beta"
v2beta_project "github.com/zitadel/zitadel/pkg/grpc/project/v2beta"
)
func TestServer_ProjectGrantReduces(t *testing.T) {
instanceID := Instance.ID()
orgID := Instance.DefaultOrg.Id
projectGrantRepo := repository.ProjectGrantRepository()
projectName := integration.ProjectName()
createProjectRes, err := ProjectClient.CreateProject(CTX, &v2beta_project.CreateProjectRequest{
OrganizationId: orgID,
Name: projectName,
ProjectRoleAssertion: true,
AuthorizationRequired: true,
ProjectAccessRequired: true,
})
require.NoError(t, err)
keyName := "key"
_, err = ProjectClient.AddProjectRole(CTX, &v2beta_project.AddProjectRoleRequest{
ProjectId: createProjectRes.GetId(),
RoleKey: keyName,
DisplayName: "display",
Group: nil,
})
require.NoError(t, err)
grantedOrgRes, err := OrgClient.CreateOrganization(CTX, &org.CreateOrganizationRequest{
Name: integration.OrganizationName(),
})
require.NoError(t, err)
_, err = ProjectClient.CreateProjectGrant(CTX, &v2beta_project.CreateProjectGrantRequest{
ProjectId: createProjectRes.GetId(),
GrantedOrganizationId: grantedOrgRes.GetId(),
})
require.NoError(t, err)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
t.Run("create project grant reduces", func(t *testing.T) {
require.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProjectGrant, err := projectGrantRepo.Get(CTX, pool, database.WithCondition(
database.And(
projectGrantRepo.InstanceIDCondition(instanceID),
projectGrantRepo.ProjectIDCondition(createProjectRes.GetId()),
projectGrantRepo.GrantedOrganizationIDCondition(grantedOrgRes.GetId()),
),
))
require.NoError(collect, err)
assert.Equal(collect, createProjectRes.GetId(), dbProjectGrant.ProjectID)
assert.Equal(collect, orgID, dbProjectGrant.GrantingOrganizationID)
assert.Equal(collect, grantedOrgRes.GetId(), dbProjectGrant.GrantedOrganizationID)
assert.Equal(collect, domain.ProjectGrantStateActive, dbProjectGrant.State)
assert.NotNil(collect, dbProjectGrant.CreatedAt)
assert.NotNil(collect, dbProjectGrant.UpdatedAt)
}, retryDuration, tick, "project grant not found within %v: %v", retryDuration, err)
})
t.Run("update project grant reduces", func(t *testing.T) {
_, err := ProjectClient.UpdateProjectGrant(CTX, &v2beta_project.UpdateProjectGrantRequest{
ProjectId: createProjectRes.GetId(),
GrantedOrganizationId: grantedOrgRes.GetId(),
RoleKeys: []string{keyName},
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProjectGrant, err := projectGrantRepo.Get(CTX, pool, database.WithCondition(
database.And(
projectGrantRepo.InstanceIDCondition(instanceID),
projectGrantRepo.ProjectIDCondition(createProjectRes.GetId()),
projectGrantRepo.GrantedOrganizationIDCondition(grantedOrgRes.GetId()),
),
))
require.NoError(collect, err)
if !assert.Len(collect, dbProjectGrant.RoleKeys, 1) {
return
}
assert.Equal(collect, dbProjectGrant.RoleKeys[0], keyName)
}, retryDuration, tick, "project grant not updated within %v: %v", retryDuration, err)
})
t.Run("(de)activate project grant reduces", func(t *testing.T) {
_, err := ProjectClient.DeactivateProjectGrant(CTX, &v2beta_project.DeactivateProjectGrantRequest{
ProjectId: createProjectRes.GetId(),
GrantedOrganizationId: grantedOrgRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectGrantRepo.Get(CTX, pool, database.WithCondition(
database.And(
projectGrantRepo.InstanceIDCondition(instanceID),
projectGrantRepo.ProjectIDCondition(createProjectRes.GetId()),
projectGrantRepo.GrantedOrganizationIDCondition(grantedOrgRes.GetId()),
),
))
require.NoError(collect, err)
assert.Equal(collect, domain.ProjectGrantStateInactive, dbProject.State)
}, retryDuration, tick, "project grant not deactivated within %v: %v", retryDuration, err)
_, err = ProjectClient.ActivateProjectGrant(CTX, &v2beta_project.ActivateProjectGrantRequest{
ProjectId: createProjectRes.GetId(),
GrantedOrganizationId: grantedOrgRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectGrantRepo.Get(CTX, pool, database.WithCondition(
database.And(
projectGrantRepo.InstanceIDCondition(instanceID),
projectGrantRepo.ProjectIDCondition(createProjectRes.GetId()),
projectGrantRepo.GrantedOrganizationIDCondition(grantedOrgRes.GetId()),
),
))
require.NoError(collect, err)
assert.Equal(collect, domain.ProjectGrantStateActive, dbProject.State)
}, retryDuration, tick, "project grant not activated within %v: %v", retryDuration, err)
})
t.Run("delete project grant reduces", func(t *testing.T) {
_, err := ProjectClient.DeleteProjectGrant(CTX, &v2beta_project.DeleteProjectGrantRequest{
ProjectId: createProjectRes.GetId(),
GrantedOrganizationId: grantedOrgRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
_, err := projectGrantRepo.Get(CTX, pool, database.WithCondition(
database.And(
projectGrantRepo.InstanceIDCondition(instanceID),
projectGrantRepo.ProjectIDCondition(createProjectRes.GetId()),
projectGrantRepo.GrantedOrganizationIDCondition(grantedOrgRes.GetId()),
),
))
require.ErrorIs(collect, err, database.NewNoRowFoundError(nil))
}, retryDuration, tick, "project grant not deleted within %v: %v", retryDuration, err)
})
}