Skip to content

Commit

Permalink
Experiment with CRD support in k8s resource mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
creack committed Dec 11, 2024
1 parent bc68383 commit 687b3b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions api/types/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,9 +1836,10 @@ func setDefaultKubernetesVerbs(spec *RoleSpecV6) {
// - Namespace is not empty
func validateKubeResources(roleVersion string, kubeResources []KubernetesResource) error {
for _, kubeResource := range kubeResources {
if !slices.Contains(KubernetesResourcesKinds, kubeResource.Kind) && kubeResource.Kind != Wildcard {
return trace.BadParameter("KubernetesResource kind %q is invalid or unsupported; Supported: %v", kubeResource.Kind, append([]string{Wildcard}, KubernetesResourcesKinds...))
}
// TODO(creack): Move the validation to the server side so we can lookup the list of valid CRDs.
// if !slices.Contains(KubernetesResourcesKinds, kubeResource.Kind) && kubeResource.Kind != Wildcard {
// return trace.BadParameter("KubernetesResource kind %q is invalid or unsupported; Supported: %v", kubeResource.Kind, append([]string{Wildcard}, KubernetesResourcesKinds...))
// }

for _, verb := range kubeResource.Verbs {
if !slices.Contains(KubernetesVerbs, verb) && verb != Wildcard && !strings.Contains(verb, "{{") {
Expand Down
4 changes: 4 additions & 0 deletions lib/kube/proxy/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"net"
"net/http"
"net/url"
"path"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -801,6 +802,9 @@ func (f *Forwarder) setupContext(
return nil, trace.Wrap(err)
}
}
if kubeResource != nil && kubeResource.Kind == "CustomResource" {
kubeResource.Kind = path.Join(apiResource.apiGroup, apiResource.apiGroupVersion, apiResource.resourceKind)
}

netConfig, err := f.cfg.CachingAuthClient.GetClusterNetworkingConfig(f.ctx)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions lib/kube/proxy/resource_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"io"
"net/http"
"path"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -59,6 +60,9 @@ func (f *Forwarder) listResources(sess *clusterSession, w http.ResponseWriter, r
if isLocalKubeCluster {
resourceKind, supportsType = sess.rbacSupportedResources.getTeleportResourceKindFromAPIResource(sess.apiResource)
}
if resourceKind == "CustomResource" {
resourceKind = path.Join(sess.apiResource.apiGroup, sess.apiResource.apiGroupVersion, sess.apiResource.resourceKind)
}

// status holds the returned response code.
var status int
Expand Down Expand Up @@ -119,6 +123,9 @@ func (f *Forwarder) listResourcesList(req *http.Request, w http.ResponseWriter,
if !ok {
return http.StatusBadRequest, trace.BadParameter("unknown resource kind %q", sess.apiResource.resourceKind)
}
if resourceKind == "CustomResource" {
resourceKind = path.Join(sess.apiResource.apiGroup, sess.apiResource.apiGroupVersion, sess.apiResource.resourceKind)
}
verb := sess.requestVerb
// filterBuffer filters the response to exclude resources the user doesn't have access to.
// The filtered payload will be written into memBuffer again.
Expand Down

0 comments on commit 687b3b1

Please sign in to comment.