Skip to content

Commit

Permalink
Test coverage for access request deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
espadolini committed Jan 3, 2022
1 parent 6cbcba7 commit d8a482c
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions lib/auth/auth_with_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
libdefaults "github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/tlsca"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -1233,3 +1234,91 @@ func TestKindClusterConfig(t *testing.T) {
}
})
}

func TestNoElevatedAccessRequestDeletion(t *testing.T) {
t.Parallel()
ctx := context.Background()

srv, err := NewTestAuthServer(TestAuthServerConfig{Dir: t.TempDir()})
require.NoError(t, err)
t.Cleanup(func() { srv.Close() })

deleterRole, err := types.NewRole("deleter", types.RoleSpecV4{
Allow: types.RoleConditions{
Rules: []types.Rule{{
Resources: []string{"access_request"},
Verbs: []string{"delete"},
}},
},
})
require.NoError(t, err)
deleterUser, err := CreateUser(srv.AuthServer, "deletey", deleterRole)
require.NoError(t, err)

requesterRole, err := types.NewRole("requester", types.RoleSpecV4{
Allow: types.RoleConditions{
Request: &types.AccessRequestConditions{
Roles: []string{deleterRole.GetName()},
},
},
})
require.NoError(t, err)
requesterUser, err := CreateUser(srv.AuthServer, "requesty", requesterRole)
require.NoError(t, err)

request, err := services.NewAccessRequest(requesterUser.GetName(), deleterRole.GetName())
require.NoError(t, err)
// the request must be for an allowed user/role combination or it will get rejected
err = srv.AuthServer.CreateAccessRequest(ctx, request)
require.NoError(t, err)

// requesty has used some other unspecified access request to get the
// deleter role in this identity
requesterAuthContext, err := srv.Authorizer.Authorize(context.WithValue(ctx,
ContextUser,
LocalUser{
Username: requesterUser.GetName(),
Identity: tlsca.Identity{
Username: requesterUser.GetName(),
Groups: []string{requesterRole.GetName(), deleterRole.GetName()},
// a tlsca.Identity must have a nonempty Traits field or the
// roles will be reloaded from the backend during Authorize
Traits: map[string][]string{"nonempty": {}},
},
},
))
require.NoError(t, err)
requesterAuth := &ServerWithRoles{
authServer: srv.AuthServer,
sessions: srv.SessionServer,
alog: srv.AuditLog,
context: *requesterAuthContext,
}

err = requesterAuth.DeleteAccessRequest(ctx, request.GetName())
require.True(t, trace.IsAccessDenied(err))
// matches the message in lib/auth/auth_with_roles.go:(*ServerWithRoles).DeleteAccessRequest()
require.Contains(t, err.Error(), "deletion through elevated roles")

deleterAuthContext, err := srv.Authorizer.Authorize(context.WithValue(ctx,
ContextUser,
LocalUser{
Username: deleterUser.GetName(),
Identity: tlsca.Identity{
Username: deleterUser.GetName(),
Groups: []string{deleterRole.GetName()},
Traits: map[string][]string{"nonempty": {}},
},
},
))
require.NoError(t, err)
deleterAuth := &ServerWithRoles{
authServer: srv.AuthServer,
sessions: srv.SessionServer,
alog: srv.AuditLog,
context: *deleterAuthContext,
}

err = deleterAuth.DeleteAccessRequest(ctx, request.GetName())
require.NoError(t, err)
}

0 comments on commit d8a482c

Please sign in to comment.