Skip to content

Commit

Permalink
Add helpful hint when receiving kind error
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlisa committed Oct 9, 2024
1 parent c6ecf62 commit ec9b6a8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/services/access_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const (
// requestTTL is the TTL for an access request, i.e. the amount of time that
// the access request can be reviewed. Defaults to 1 week.
requestTTL = 7 * day

InvalidKubernetesKindAccessRequest = "Not allowed to request Kubernetes resource kind"
)

// ValidateAccessRequest validates the AccessRequest and sets default values
Expand Down Expand Up @@ -1297,12 +1299,12 @@ func enforceKubernetesRequestModes(requestedResourceIDs []types.ResourceID, requ

for _, id := range requestedResourceIDs {
if id.Kind == types.KindKubernetesCluster {
return trace.BadParameter("Not allowed to request Kubernetes resource kind %q. Allowed kinds: %v.", types.KindKubernetesCluster, slices.Collect(maps.Keys(allowedKindsLookup)))
return trace.BadParameter("%s %q. Allowed kinds: %v.", InvalidKubernetesKindAccessRequest, types.KindKubernetesCluster, slices.Collect(maps.Keys(allowedKindsLookup)))
}
// Filter for kube resources.
if slices.Contains(types.KubernetesResourcesKinds, id.Kind) {
if _, found := allowedKindsLookup[id.Kind]; !found {
return trace.BadParameter("Not allowed to request Kubernetes resource kind %q. Allowed kinds: %v.", id.Kind, slices.Collect(maps.Keys(allowedKindsLookup)))
return trace.BadParameter("%s %q. Allowed kinds: %v.", InvalidKubernetesKindAccessRequest, id.Kind, slices.Collect(maps.Keys(allowedKindsLookup)))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/services/access_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ func TestValidate_WithKubernetesRequestMode(t *testing.T) {
"request-mode-namespace": {
Options: types.RoleOptions{
RequestMode: &types.AccessRequestMode{
KubernetesResources: []types.KubernetesResource{
KubernetesResources: []types.RequestModeKubernetesResource{
{Kind: types.KindKubeNamespace},
},
},
Expand All @@ -2581,7 +2581,7 @@ func TestValidate_WithKubernetesRequestMode(t *testing.T) {
"request-mode-wildcard": {
Options: types.RoleOptions{
RequestMode: &types.AccessRequestMode{
KubernetesResources: []types.KubernetesResource{
KubernetesResources: []types.RequestModeKubernetesResource{
{Kind: types.Wildcard},
},
},
Expand All @@ -2591,7 +2591,7 @@ func TestValidate_WithKubernetesRequestMode(t *testing.T) {
"request-mode-pods": {
Options: types.RoleOptions{
RequestMode: &types.AccessRequestMode{
KubernetesResources: []types.KubernetesResource{
KubernetesResources: []types.RequestModeKubernetesResource{
{Kind: types.KindKubePod},
},
},
Expand Down Expand Up @@ -2720,7 +2720,7 @@ func TestValidate_WithKubernetesRequestMode(t *testing.T) {
err = validator.Validate(context.Background(), req, identity)
if tc.wantErr {
require.Error(t, err)
require.Contains(t, err.Error(), "Not allowed to request Kubernetes resource")
require.Contains(t, err.Error(), InvalidKubernetesKindAccessRequest)
} else {
require.NoError(t, err)
}
Expand Down
4 changes: 4 additions & 0 deletions tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,10 @@ func executeAccessRequest(cf *CLIConf, tc *client.TeleportClient) error {
req, err = clt.CreateAccessRequestV2(cf.Context, req)
return trace.Wrap(err)
}); err != nil {
if strings.Contains(err.Error(), services.InvalidKubernetesKindAccessRequest) {
friendlyMsg := fmt.Sprintf("%s\nTry searching for specific kinds with:\n> tsh request search --kube-cluster=KUBE_CLUSTER_NAME --kind=KIND", err.Error())
return trace.BadParameter(friendlyMsg)
}
return trace.Wrap(err)
}
cf.RequestID = req.GetName()
Expand Down

0 comments on commit ec9b6a8

Please sign in to comment.