-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix broken auth Access Request creation tests #49258
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8053,7 +8053,7 @@ func TestCreateAccessRequest(t *testing.T) { | |
clock := srv.Clock() | ||
alice, bob, admin := createSessionTestUsers(t, srv.Auth()) | ||
|
||
searchRole, err := types.NewRole("requestRole", types.RoleSpecV6{ | ||
searchRole, err := types.NewRole("searchRole", types.RoleSpecV6{ | ||
Allow: types.RoleConditions{ | ||
Request: &types.AccessRequestConditions{ | ||
Roles: []string{"requestRole"}, | ||
|
@@ -8063,11 +8063,32 @@ func TestCreateAccessRequest(t *testing.T) { | |
}) | ||
require.NoError(t, err) | ||
|
||
requestRole, err := types.NewRole("requestRole", types.RoleSpecV6{}) | ||
requestRole, err := types.NewRole("requestRole", types.RoleSpecV6{ | ||
Allow: types.RoleConditions{ | ||
GroupLabels: types.Labels{ | ||
types.Wildcard: []string{types.Wildcard}, | ||
}, | ||
NodeLabels: types.Labels{ | ||
types.Wildcard: []string{types.Wildcard}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
srv.Auth().CreateRole(ctx, searchRole) | ||
srv.Auth().CreateRole(ctx, requestRole) | ||
nodeAllowedByRequestRole, err := types.NewServerWithLabels( | ||
"test-node", | ||
types.KindNode, | ||
types.ServerSpecV2{}, | ||
map[string]string{"any-key": "any-val"}, | ||
) | ||
require.NoError(t, err) | ||
|
||
_, err = srv.Auth().UpsertNode(ctx, nodeAllowedByRequestRole) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we able to avoid doing upserts here? I know that due to how our cache works, it's safe in tests to create a resource and then not wait for the change to propagate. This isn't true when performing an update, so you run into a risk of having a flaky test. The rest of the code in the test might execute without observing the change to See the question I asked about this on Slack some time ago. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing out. AFAICT there is nothing like |
||
require.NoError(t, err) | ||
_, err = srv.Auth().CreateRole(ctx, requestRole) | ||
require.NoError(t, err) | ||
_, err = srv.Auth().CreateRole(ctx, searchRole) | ||
require.NoError(t, err) | ||
|
||
user, err := srv.Auth().GetUser(ctx, alice, true) | ||
require.NoError(t, err) | ||
|
@@ -8110,33 +8131,36 @@ func TestCreateAccessRequest(t *testing.T) { | |
user: alice, | ||
accessRequest: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), nodeAllowedByRequestRole.GetKind(), nodeAllowedByRequestRole.GetName()), | ||
}), | ||
errAssertionFunc: require.NoError, | ||
expected: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), nodeAllowedByRequestRole.GetKind(), nodeAllowedByRequestRole.GetName()), | ||
}), | ||
}, | ||
{ | ||
name: "admin creates a request for alice", | ||
user: admin, | ||
accessRequest: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup1.GetName()), | ||
}), | ||
errAssertionFunc: require.NoError, | ||
expected: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup1.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindApp, userGroup1.GetApplications()[0]), | ||
mustResourceID(srv.ClusterName(), types.KindApp, userGroup1.GetApplications()[1]), | ||
mustResourceID(srv.ClusterName(), types.KindApp, userGroup1.GetApplications()[2]), | ||
}), | ||
}, | ||
{ | ||
name: "bob fails to create a request for alice", | ||
user: bob, | ||
accessRequest: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup1.GetName()), | ||
}), | ||
errAssertionFunc: require.Error, | ||
}, | ||
|
@@ -8145,7 +8169,7 @@ func TestCreateAccessRequest(t *testing.T) { | |
user: alice, | ||
accessRequest: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), nodeAllowedByRequestRole.GetKind(), nodeAllowedByRequestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup1.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindApp, "app1"), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup2.GetName()), | ||
|
@@ -8154,7 +8178,7 @@ func TestCreateAccessRequest(t *testing.T) { | |
errAssertionFunc: require.NoError, | ||
expected: mustAccessRequest(t, alice, types.RequestState_PENDING, clock.Now(), clock.Now().Add(time.Hour), | ||
[]string{requestRole.GetName()}, []types.ResourceID{ | ||
mustResourceID(srv.ClusterName(), types.KindRole, requestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), nodeAllowedByRequestRole.GetKind(), nodeAllowedByRequestRole.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup1.GetName()), | ||
mustResourceID(srv.ClusterName(), types.KindApp, "app1"), | ||
mustResourceID(srv.ClusterName(), types.KindUserGroup, userGroup2.GetName()), | ||
|
@@ -8389,9 +8413,13 @@ func TestAccessRequestNonGreedyAnnotations(t *testing.T) { | |
require.NoError(t, err) | ||
paymentsServer.SetStaticLabels(map[string]string{"service": "payments"}) | ||
|
||
idServer, err := types.NewServer("server-identity", types.KindNode, types.ServerSpecV2{}) | ||
idServer, err := types.NewServerWithLabels( | ||
"server-identity", | ||
types.KindNode, | ||
types.ServerSpecV2{}, | ||
map[string]string{"service": "identity"}, | ||
) | ||
require.NoError(t, err) | ||
idServer.SetStaticLabels(map[string]string{"service": "payments"}) | ||
|
||
ctx := context.Background() | ||
srv := newTestTLSServer(t) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This role wasn't created before, because
searchRole
above had the same name ("requestRole"
) andsrv.Auth().CreateRole(ctx, requestRole)
call (below) wasn't checked for error.