Skip to content
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

Include extra attributes in SubjectAccessReview #13170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/k8s/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ func ResourceAuthz(
func ResourceAuthzForUser(
ctx context.Context,
client kubernetes.Interface,
namespace, verb, group, version, resource, subresource, name, user string, userGroups []string) error {
namespace, verb, group, version, resource, subresource, name, user string, userGroups []string, extra map[string]authV1.ExtraValue) error {
sar := &authV1.SubjectAccessReview{
Spec: authV1.SubjectAccessReviewSpec{
User: user,
Groups: userGroups,
Extra: extra,

ResourceAttributes: &authV1.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Expand Down
25 changes: 20 additions & 5 deletions viz/tap/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/go-openapi/spec"
Expand All @@ -17,17 +18,19 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
authV1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
)

type handler struct {
k8sAPI *k8s.API
usernameHeader string
groupHeader string
grpcTapServer pb.TapServer
log *logrus.Entry
k8sAPI *k8s.API
usernameHeader string
groupHeader string
extraHeaderPrefix string
grpcTapServer pb.TapServer
log *logrus.Entry
}

// TODO: share with api_handlers.go
Expand Down Expand Up @@ -123,6 +126,17 @@ func (h *handler) handleTap(w http.ResponseWriter, req *http.Request, p httprout
namespace, resource, name, h.usernameHeader, h.groupHeader,
)

extra := make(map[string]authV1.ExtraValue)
for key, values := range req.Header {
if strings.HasPrefix(key, h.extraHeaderPrefix) {
key, err := url.QueryUnescape(strings.TrimPrefix(key, h.extraHeaderPrefix))

if err != nil {
extra[key] = values
}
}
}

// TODO: it's possible this SubjectAccessReview is redundant, consider
// removing, more info at https://github.com/linkerd/linkerd2/issues/3182
err := pkgK8s.ResourceAuthzForUser(
Expand All @@ -137,6 +151,7 @@ func (h *handler) handleTap(w http.ResponseWriter, req *http.Request, p httprout
name,
req.Header.Get(h.usernameHeader),
req.Header.Values(h.groupHeader),
extra,
)
if err != nil {
err = fmt.Errorf("tap authorization failed (%w), visit %s for more information", err, pkg.TapRbacURL)
Expand Down
36 changes: 23 additions & 13 deletions viz/tap/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewServer(
}
}()

clientCAPem, allowedNames, usernameHeader, groupHeader, err := serverAuth(ctx, k8sAPI)
clientCAPem, allowedNames, usernameHeader, groupHeader, extraHeaderPrefix, err := serverAuth(ctx, k8sAPI)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,11 +80,12 @@ func NewServer(

var emptyCert atomic.Value
h := &handler{
k8sAPI: k8sAPI,
usernameHeader: usernameHeader,
groupHeader: groupHeader,
grpcTapServer: grpcTapServer,
log: log,
k8sAPI: k8sAPI,
usernameHeader: usernameHeader,
groupHeader: groupHeader,
extraHeaderPrefix: extraHeaderPrefix,
grpcTapServer: grpcTapServer,
log: log,
}

lis, err := net.Listen("tcp", addr)
Expand Down Expand Up @@ -164,28 +165,28 @@ func (a *Server) validate(req *http.Request) error {
// authentication.
// kubectl -n kube-system get cm/extension-apiserver-authentication
// accessible via the extension-apiserver-authentication-reader role
func serverAuth(ctx context.Context, k8sAPI *k8s.API) (string, []string, string, string, error) {
func serverAuth(ctx context.Context, k8sAPI *k8s.API) (string, []string, string, string, string, error) {

cm, err := k8sAPI.Client.CoreV1().
ConfigMaps(metav1.NamespaceSystem).
Get(ctx, pkgk8s.ExtensionAPIServerAuthenticationConfigMapName, metav1.GetOptions{})
if err != nil {
return "", nil, "", "", fmt.Errorf("failed to load [%s] config: %w", pkgk8s.ExtensionAPIServerAuthenticationConfigMapName, err)
return "", nil, "", "", "", fmt.Errorf("failed to load [%s] config: %w", pkgk8s.ExtensionAPIServerAuthenticationConfigMapName, err)
}

clientCAPem, ok := cm.Data[pkgk8s.ExtensionAPIServerAuthenticationRequestHeaderClientCAFileKey]
if !ok {
return "", nil, "", "", fmt.Errorf("no client CA cert available for apiextension-server")
return "", nil, "", "", "", fmt.Errorf("no client CA cert available for apiextension-server")
}

allowedNames, err := deserializeStrings(cm.Data["requestheader-allowed-names"])
if err != nil {
return "", nil, "", "", err
return "", nil, "", "", "", err
}

usernameHeaders, err := deserializeStrings(cm.Data["requestheader-username-headers"])
if err != nil {
return "", nil, "", "", err
return "", nil, "", "", "", err
}
usernameHeader := ""
if len(usernameHeaders) > 0 {
Expand All @@ -194,14 +195,23 @@ func serverAuth(ctx context.Context, k8sAPI *k8s.API) (string, []string, string,

groupHeaders, err := deserializeStrings(cm.Data["requestheader-group-headers"])
if err != nil {
return "", nil, "", "", err
return "", nil, "", "", "", err
}
groupHeader := ""
if len(groupHeaders) > 0 {
groupHeader = groupHeaders[0]
}

return clientCAPem, allowedNames, usernameHeader, groupHeader, nil
extraHeaderPrefixes, err := deserializeStrings(cm.Data["requestheader-group-headers"])
if err != nil {
return "", nil, "", "", "", err
}
extraHeaderPrefix := ""
if len(extraHeaderPrefixes) > 0 {
extraHeaderPrefix = extraHeaderPrefixes[0]
}

return clientCAPem, allowedNames, usernameHeader, groupHeader, extraHeaderPrefix, nil
}

// copied from https://github.com/kubernetes/apiserver/blob/781c3cd1b3dc5b6f79c68ab0d16fe544600421ef/pkg/server/options/authentication.go#L360
Expand Down
29 changes: 17 additions & 12 deletions viz/tap/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (

func TestAPIServerAuth(t *testing.T) {
expectations := []struct {
k8sRes []string
clientCAPem string
allowedNames []string
usernameHeader string
groupHeader string
err error
k8sRes []string
clientCAPem string
allowedNames []string
usernameHeader string
groupHeader string
extraHeaderPrefix string
err error
}{
{
err: fmt.Errorf("failed to load [%s] config: configmaps %q not found", k8sutils.ExtensionAPIServerAuthenticationConfigMapName, k8sutils.ExtensionAPIServerAuthenticationConfigMapName),
Expand All @@ -44,11 +45,12 @@ data:
requestheader-username-headers: '["X-Remote-User"]'
`,
},
clientCAPem: "requestheader-client-ca-file",
allowedNames: []string{"name1", "name2"},
usernameHeader: "X-Remote-User",
groupHeader: "X-Remote-Group",
err: nil,
clientCAPem: "requestheader-client-ca-file",
allowedNames: []string{"name1", "name2"},
usernameHeader: "X-Remote-User",
groupHeader: "X-Remote-Group",
extraHeaderPrefix: "X-Remote-Extra-",
err: nil,
},
}

Expand All @@ -62,7 +64,7 @@ data:
t.Fatalf("NewFakeAPI returned an error: %s", err)
}

clientCAPem, allowedNames, usernameHeader, groupHeader, err := serverAuth(ctx, k8sAPI)
clientCAPem, allowedNames, usernameHeader, groupHeader, extraHeaderPrefix, err := serverAuth(ctx, k8sAPI)

if err != nil && exp.err != nil {
if err.Error() != exp.err.Error() {
Expand All @@ -86,6 +88,9 @@ data:
if groupHeader != exp.groupHeader {
t.Errorf("apiServerAuth returned unexpected groupHeader: %q, expected: %q", groupHeader, exp.groupHeader)
}
if extraHeaderPrefix != exp.extraHeaderPrefix {
t.Errorf("apiServerAuth returned unexpected extraHeaderPrefix: %q, expected: %q", extraHeaderPrefix, exp.extraHeaderPrefix)
}
})
}
}
Expand Down
Loading