Files

120 lines
3.5 KiB
Go

/*
* Teleport
* Copyright (C) 2025 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package utils
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
scopedaccessv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/scopes/access/v1"
"github.com/gravitational/teleport/api/types"
scopedaccess "github.com/gravitational/teleport/lib/scopes/access"
"github.com/gravitational/teleport/lib/scopes/cache/assignments"
"github.com/gravitational/teleport/lib/scopes/cache/roles"
)
func TestRangeScopedRoles(t *testing.T) {
const roleCount = 503
t.Parallel()
ctx := t.Context()
upstream := roles.NewRoleCache()
expectedRoleNames := make([]string, 0, roleCount)
for i := range roleCount {
name := fmt.Sprintf("role-%d", i)
err := upstream.Put(scopedaccessv1.ScopedRole_builder{
Kind: scopedaccess.KindScopedRole,
Metadata: headerv1.Metadata_builder{
Name: name,
}.Build(),
Scope: "/foo",
Spec: scopedaccessv1.ScopedRoleSpec_builder{
AssignableScopes: []string{"/foo"},
}.Build(),
Version: types.V1,
}.Build())
require.NoError(t, err)
expectedRoleNames = append(expectedRoleNames, name)
}
gotRoleNames := make([]string, 0, roleCount)
for role, err := range RangeScopedRoles(ctx, upstream, &scopedaccessv1.ListScopedRolesRequest{}) {
require.NoError(t, err)
require.NotNil(t, role)
gotRoleNames = append(gotRoleNames, role.GetMetadata().GetName())
}
require.ElementsMatch(t, expectedRoleNames, gotRoleNames)
}
func TestRangeScopedRoleAssignments(t *testing.T) {
const assignmentCount = 503
t.Parallel()
ctx := t.Context()
upstream := assignments.NewAssignmentCache(assignments.AssignmentCacheConfig{})
expectedAssignmentNames := make([]string, 0, assignmentCount)
for range assignmentCount {
name := uuid.New().String()
err := upstream.Put(scopedaccessv1.ScopedRoleAssignment_builder{
Kind: scopedaccess.KindScopedRoleAssignment,
SubKind: scopedaccess.SubKindDynamic,
Metadata: headerv1.Metadata_builder{
Name: name,
}.Build(),
Scope: "/",
Spec: scopedaccessv1.ScopedRoleAssignmentSpec_builder{
User: "alice",
Assignments: []*scopedaccessv1.Assignment{
scopedaccessv1.Assignment_builder{
Role: "/::some-role",
Scope: "/foo",
}.Build(),
},
}.Build(),
Version: types.V1,
}.Build())
require.NoError(t, err)
expectedAssignmentNames = append(expectedAssignmentNames, name)
}
gotAssignmentNames := make([]string, 0, assignmentCount)
for assignment, err := range RangeScopedRoleAssignments(ctx, upstream, &scopedaccessv1.ListScopedRoleAssignmentsRequest{}) {
require.NoError(t, err)
require.NotNil(t, assignment)
gotAssignmentNames = append(gotAssignmentNames, assignment.GetMetadata().GetName())
}
require.ElementsMatch(t, expectedAssignmentNames, gotAssignmentNames)
}