-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdeprecated_restrictedadmin_test.go
143 lines (124 loc) · 5.13 KB
/
deprecated_restrictedadmin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//go:build (validation || infra.any || cluster.any || sanity) && !stress && !extended && (2.8 || 2.9 || 2.10)
package rbac
import (
"strings"
"testing"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/users"
"github.com/rancher/shepherd/pkg/config"
"github.com/rancher/tests/actions/projects"
"github.com/rancher/tests/actions/rbac"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func (rb *RBTestSuite) sequentialTestRBACRA(role rbac.Role, member string, user *management.User) {
standardClient, err := rb.client.AsUser(user)
require.NoError(rb.T(), err)
adminProject, err := rb.client.Management.Project.Create(projects.NewProjectConfig(rb.cluster.ID))
require.NoError(rb.T(), err)
if member == rbac.StandardUser.String() {
if strings.Contains(role.String(), "project") {
err := users.AddProjectMember(rb.client, adminProject, user, role.String(), nil)
require.NoError(rb.T(), err)
} else {
err := users.AddClusterRoleToUser(rb.client, rb.cluster, user, role.String(), nil)
require.NoError(rb.T(), err)
}
}
standardClient, err = standardClient.ReLogin()
require.NoError(rb.T(), err)
additionalUser, err := users.CreateUserWithRole(rb.client, users.UserConfig(), rbac.StandardUser.String())
require.NoError(rb.T(), err)
rb.Run("Validating Global Role Binding is created for "+role.String(), func() {
verifyRAGlobalRoleBindingsForUser(rb.T(), user, rb.client)
})
rb.Run("Validating corresponding role bindings for users", func() {
verifyRARoleBindingsForUser(rb.T(), user, rb.client, rb.cluster.ID)
})
rb.Run("Validating if "+role.String()+" can list any downstream clusters", func() {
verifyRAUserCanListCluster(rb.T(), rb.client, standardClient, rb.cluster.ID, role)
})
rb.Run("Validating if members with role "+role.String()+" are able to list all projects", func() {
verifyRAUserCanListProject(rb.T(), rb.client, standardClient, rb.cluster.ID)
})
rb.Run("Validating if members with role "+role.String()+" is able to create a project in the cluster", func() {
verifyRAUserCanCreateProjects(rb.T(), standardClient, rb.cluster.ID, role)
})
rb.Run("Validate namespaces checks for members with role "+role.String(), func() {
verifyRAUserCanCreateNamespace(rb.T(), standardClient, adminProject, role)
})
rb.Run("Validating if "+role.String()+" can lists all namespaces in a cluster.", func() {
verifyRAUserCanListNamespace(rb.T(), rb.client, standardClient, rb.cluster.ID, role)
})
rb.Run("Validating if "+role.String()+" can delete a namespace from a project they own.", func() {
verifyRAUserCanDeleteNamespace(rb.T(), rb.client, standardClient, adminProject, rb.cluster.ID, role)
})
rb.Run("Validating if member with role "+role.String()+" can add members to the cluster", func() {
verifyRAUserCanAddClusterRoles(rb.T(), rb.client, standardClient, rb.cluster)
})
rb.Run("Validating if member with role "+role.String()+" can add members to the project", func() {
if strings.Contains(role.String(), "project") {
verifyRAUserCanAddProjectRoles(rb.T(), standardClient, adminProject, additionalUser, rbac.ProjectOwner.String(), rb.cluster.ID)
}
})
rb.Run("Validating if member with role "+role.String()+" can delete a project they are not owner of ", func() {
rbac.VerifyUserCanDeleteProject(rb.T(), standardClient, adminProject, role)
})
rb.Run("Validating if member with role "+role.String()+" is removed from the cluster and returns nil clusters", func() {
if strings.Contains(role.String(), "cluster") {
verifyRAUserCanRemoveClusterRoles(rb.T(), rb.client, user)
}
})
}
func (rb *RBTestSuite) TestRBACRA() {
tests := []struct {
name string
role rbac.Role
member string
}{
{"Restricted Admin", restrictedAdmin, restrictedAdmin.String()},
}
for _, tt := range tests {
var newUser *management.User
rb.Run("Validate conditions for user with role "+tt.name, func() {
user, err := users.CreateUserWithRole(rb.client, users.UserConfig(), tt.member)
require.NoError(rb.T(), err)
newUser = user
rb.T().Logf("Created user: %v", newUser.Username)
})
if newUser != nil {
rb.sequentialTestRBACRA(tt.role, tt.member, newUser)
subSession := rb.session.NewSession()
defer subSession.Cleanup()
}
}
}
func (rb *RBTestSuite) TestRBACDynamicInputRA() {
roles := map[string]string{
"restricted-admin": restrictedAdmin.String(),
}
var member string
userConfig := new(rbac.Config)
config.LoadConfig(rbac.ConfigurationFileKey, userConfig)
username := userConfig.Username
userByName, err := users.GetUserIDByName(rb.client, username)
require.NoError(rb.T(), err)
user, err := rb.client.Management.User.ByID(userByName)
require.NoError(rb.T(), err)
user.Password = userConfig.Password
role := userConfig.Role
if userConfig.Role == "" {
rb.T().Skip()
} else {
val, ok := roles[role.String()]
if !ok {
rb.FailNow("Incorrect usage of roles. Please go through the readme for correct role configurations")
}
role = rbac.Role(val)
}
member = restrictedAdmin.String()
rb.sequentialTestRBACRA(role, member, user)
}
func TestRARBACTestSuite(t *testing.T) {
suite.Run(t, new(RBTestSuite))
}