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

feat: Token validity check #1103

Merged
merged 6 commits into from
Oct 1, 2021
Merged
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
6 changes: 6 additions & 0 deletions pkg/deploy/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ func getGatewayServerConfigSpec(deployContext *deploy.DeployContext) (corev1.Con

if util.IsNativeUserModeEnabled(deployContext.CheCluster) {
cfg.AddAuthHeaderRewrite(serverComponentName)
// native user mode is currently only available on OpenShift but let's be defensive here so that
// this doesn't break once we enable it on Kubernetes, too. Token check will have to work
// differently on Kuberentes.
if util.IsOpenShift {
cfg.AddOpenShiftTokenCheck(serverComponentName)
}
}

return GetConfigmapForGatewayConfig(deployContext, serverComponentName, cfg)
Expand Down
47 changes: 47 additions & 0 deletions pkg/deploy/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
orgv1 "github.com/eclipse-che/che-operator/api/v1"
"github.com/eclipse-che/che-operator/pkg/deploy"
"github.com/eclipse-che/che-operator/pkg/util"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/yaml"
)

func TestSyncAllToCluster(t *testing.T) {
Expand Down Expand Up @@ -241,3 +243,48 @@ func TestOauthProxyConfigUnauthorizedRegistries(t *testing.T) {
}
})
}

func TestTokenValidityCheckOnOpenShiftNativeUser(t *testing.T) {
onOpenShift4(func() {
orgv1.SchemeBuilder.AddToScheme(scheme.Scheme)
corev1.SchemeBuilder.AddToScheme(scheme.Scheme)

cm, err := getGatewayServerConfigSpec(&deploy.DeployContext{
CheCluster: &orgv1.CheCluster{
Spec: orgv1.CheClusterSpec{
Auth: orgv1.CheClusterSpecAuth{
NativeUserMode: util.NewBoolPointer(true),
},
},
},
ClusterAPI: deploy.ClusterAPI{
Scheme: scheme.Scheme,
},
})
assert.NoError(t, err)

cfg := &TraefikConfig{}

assert.NoError(t, yaml.Unmarshal([]byte(cm.Data["server.yml"]), cfg))

if assert.Contains(t, cfg.HTTP.Routers, "server") {
assert.Contains(t, cfg.HTTP.Routers["server"].Middlewares, "server-token-check")
}
if assert.Contains(t, cfg.HTTP.Middlewares, "server-token-check") && assert.NotNil(t, cfg.HTTP.Middlewares["server-token-check"].ForwardAuth) {
assert.Equal(t, "https://kubernetes.default.svc/apis/user.openshift.io/v1/users/~", cfg.HTTP.Middlewares["server-token-check"].ForwardAuth.Address)
}
})
}

func onOpenShift4(f func()) {
openshift := util.IsOpenShift
openshiftv4 := util.IsOpenShift4

util.IsOpenShift = true
util.IsOpenShift4 = true

f()

util.IsOpenShift = openshift
util.IsOpenShift4 = openshiftv4
}
8 changes: 7 additions & 1 deletion pkg/deploy/gateway/traefik_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ type TraefikConfigStripPrefix struct {
}

type TraefikConfigForwardAuth struct {
Address string `json:"address"`
Address string `json:"address"`
TrustForwardHeader bool `json:"trustForwardHeader"`
metlos marked this conversation as resolved.
Show resolved Hide resolved
TLS *TraefikConfigTLS `json:"tls,omitempty"`
}

type TraefikPlugin struct {
Expand All @@ -64,3 +66,7 @@ type TraefikPluginHeaderRewrite struct {
To string `json:"to"`
Prefix string `json:"prefix"`
}

type TraefikConfigTLS struct {
InsecureSkipVerify bool `json:"insecureSkipVerify"`
}
14 changes: 14 additions & 0 deletions pkg/deploy/gateway/traefik_config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ func (cfg *TraefikConfig) AddAuthHeaderRewrite(componentName string) {
},
}
}

func (cfg *TraefikConfig) AddOpenShiftTokenCheck(componentName string) {
middlewareName := componentName + "-token-check"
cfg.HTTP.Routers[componentName].Middlewares = append(cfg.HTTP.Routers[componentName].Middlewares, middlewareName)
cfg.HTTP.Middlewares[middlewareName] = &TraefikConfigMiddleware{
ForwardAuth: &TraefikConfigForwardAuth{
Address: "https://kubernetes.default.svc/apis/user.openshift.io/v1/users/~",
TrustForwardHeader: true,
TLS: &TraefikConfigTLS{
InsecureSkipVerify: true,
},
},
}
}
12 changes: 12 additions & 0 deletions pkg/deploy/gateway/traefik_config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func TestAddAuthHeaderRewrite(t *testing.T) {
assert.Contains(t, cfg.HTTP.Middlewares, cfg.HTTP.Routers[testComponentName].Middlewares[0], *cfg)
}

func TestAddOpenShiftTokenCheck(t *testing.T) {
cfg := CreateCommonTraefikConfig(testComponentName, testRule, 1, "http://svc:8080")
cfg.AddOpenShiftTokenCheck(testComponentName)

assert.Len(t, cfg.HTTP.Routers[testComponentName].Middlewares, 1, *cfg)
assert.Len(t, cfg.HTTP.Middlewares, 1, *cfg)
middlewareName := cfg.HTTP.Routers[testComponentName].Middlewares[0]
if assert.Contains(t, cfg.HTTP.Middlewares, middlewareName, *cfg) && assert.NotNil(t, cfg.HTTP.Middlewares[middlewareName].ForwardAuth) {
assert.Equal(t, "https://kubernetes.default.svc/apis/user.openshift.io/v1/users/~", cfg.HTTP.Middlewares[middlewareName].ForwardAuth.Address)
}
}

func TestMiddlewaresPreserveOrder(t *testing.T) {
t.Run("strip-header", func(t *testing.T) {
cfg := CreateCommonTraefikConfig(testComponentName, testRule, 1, "http://svc:8080")
Expand Down