Skip to content

Commit

Permalink
c/k/app,pkg/{authn,server}: use upstream oidc opts
Browse files Browse the repository at this point in the history
  • Loading branch information
ibihim committed Jul 23, 2024
1 parent c7e1aff commit 5e866f5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 113 deletions.
13 changes: 7 additions & 6 deletions cmd/kube-rbac-proxy/app/kube-rbac-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import (
utilerrors "k8s.io/apimachinery/pkg/util/errors"
waitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/apiserver/pkg/authorization/union"
kubefilters "k8s.io/apiserver/pkg/endpoints/filters"
"k8s.io/apiserver/pkg/endpoints/request"
serverconfig "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/plugin/pkg/authenticator/token/oidc"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
k8sapiflag "k8s.io/component-base/cli/flag"
Expand All @@ -47,7 +49,6 @@ import (
"k8s.io/klog/v2"

"github.com/brancz/kube-rbac-proxy/cmd/kube-rbac-proxy/app/options"
"github.com/brancz/kube-rbac-proxy/pkg/authn"
"github.com/brancz/kube-rbac-proxy/pkg/authn/identityheaders"
"github.com/brancz/kube-rbac-proxy/pkg/authorization/path"
"github.com/brancz/kube-rbac-proxy/pkg/authorization/rewrite"
Expand Down Expand Up @@ -195,14 +196,14 @@ func Run(cfg *server.KubeRBACProxyConfig) error {

var authenticator authenticator.Request
// If OIDC configuration provided, use oidc authenticator
if cfg.KubeRBACProxyInfo.OIDC.IssuerURL != "" {
oidcAuthenticator, err := authn.NewOIDCAuthenticator(ctx, cfg.KubeRBACProxyInfo.OIDC)
if cfg.KubeRBACProxyInfo.OIDC.JWTAuthenticator.Issuer.URL != "" {
tokenAuthenticator, err := oidc.New(ctx, *cfg.KubeRBACProxyInfo.OIDC)
if err != nil {
return fmt.Errorf("failed to instantiate OIDC authenticator: %w", err)
return fmt.Errorf("setting up oidc failed: %w", err)
}

go oidcAuthenticator.Run(ctx)
authenticator = oidcAuthenticator
go cfg.KubeRBACProxyInfo.OIDCDynamicCAContent.Run(ctx, 1)
authenticator = bearertoken.New(tokenAuthenticator)
} else {
authenticator = cfg.DelegatingAuthentication.Authenticator
}
Expand Down
38 changes: 29 additions & 9 deletions cmd/kube-rbac-proxy/app/options/oidcoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,30 @@ limitations under the License.
package options

import (
"github.com/brancz/kube-rbac-proxy/pkg/authn"
"fmt"

"k8s.io/apiserver/pkg/server/dynamiccertificates"
"k8s.io/apiserver/plugin/pkg/authenticator/token/oidc"

"github.com/brancz/kube-rbac-proxy/pkg/server"

"github.com/spf13/pflag"
)

type OIDCOptions struct {
*authn.OIDCConfig
oidc.Options

CAFile string
}

func (o *OIDCOptions) AddFlags(flagset *pflag.FlagSet) {
//Authn OIDC flags
flagset.StringVar(&o.IssuerURL, "oidc-issuer", "", "The URL of the OpenID issuer, only HTTPS scheme will be accepted. If set, it will be used to verify the OIDC JSON Web Token (JWT).")
flagset.StringVar(&o.ClientID, "oidc-clientID", "", "The client ID for the OpenID Connect client, must be set if oidc-issuer-url is set.")
flagset.StringVar(&o.UsernameClaim, "oidc-username-claim", "email", "Identifier of the user in JWT claim, by default set to 'email'")
flagset.StringVar(&o.GroupsClaim, "oidc-groups-claim", "groups", "Identifier of groups in JWT claim, by default set to 'groups'")
flagset.StringVar(&o.UsernamePrefix, "oidc-username-prefix", "", "If provided, the username will be prefixed with this value to prevent conflicts with other authentication strategies.")
flagset.StringVar(&o.GroupsPrefix, "oidc-groups-prefix", "", "If provided, all groups will be prefixed with this value to prevent conflicts with other authentication strategies.")
flagset.StringVar(&o.JWTAuthenticator.Issuer.URL, "oidc-issuer", "", "The URL of the OpenID issuer, only HTTPS scheme will be accepted. If set, it will be used to verify the OIDC JSON Web Token (JWT).")
flagset.StringSliceVar(&o.JWTAuthenticator.Issuer.Audiences, "oidc-clientID", []string{}, "The client ID for the OpenID Connect client, must be set if oidc-issuer-url is set.")
flagset.StringVar(&o.JWTAuthenticator.ClaimMappings.Username.Claim, "oidc-username-claim", "email", "Identifier of the user in JWT claim, by default set to 'email'")
flagset.StringVar(o.JWTAuthenticator.ClaimMappings.Username.Prefix, "oidc-username-prefix", "", "If provided, the username will be prefixed with this value to prevent conflicts with other authentication strategies.")
flagset.StringVar(&o.JWTAuthenticator.ClaimMappings.Groups.Claim, "oidc-groups-claim", "groups", "Identifier of groups in JWT claim, by default set to 'groups'")
flagset.StringVar(o.JWTAuthenticator.ClaimMappings.Groups.Prefix, "oidc-groups-prefix", "", "If provided, all groups will be prefixed with this value to prevent conflicts with other authentication strategies.")
flagset.StringArrayVar(&o.SupportedSigningAlgs, "oidc-sign-alg", []string{"RS256"}, "Supported signing algorithms, default RS256")
flagset.StringVar(&o.CAFile, "oidc-ca-file", "", "If set, the OpenID server's certificate will be verified by one of the authorities in the oidc-ca-file, otherwise the host's root CA set will be used.")

Expand All @@ -45,6 +52,19 @@ func (o *OIDCOptions) Validate() []error {
}

func (o *OIDCOptions) ApplyTo(c *server.KubeRBACProxyInfo) error {
c.OIDC = o.OIDCConfig
if o.JWTAuthenticator.Issuer.URL == "" {
return nil
}

dyCA, err := dynamiccertificates.NewDynamicCAContentFromFile("oidc-ca", o.CAFile)

if err != nil {
return fmt.Errorf("failed to create dynamic CA content: %w", err)
}

o.CAContentProvider = dyCA
c.OIDC = &o.Options
c.OIDCDynamicCAContent = dyCA

return nil
}
5 changes: 1 addition & 4 deletions cmd/kube-rbac-proxy/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package options
import (
"fmt"

"github.com/brancz/kube-rbac-proxy/pkg/authn"
"github.com/brancz/kube-rbac-proxy/pkg/authn/identityheaders"
genericoptions "k8s.io/apiserver/pkg/server/options"
kubeflags "k8s.io/component-base/cli/flag"
Expand Down Expand Up @@ -53,9 +52,7 @@ func NewProxyRunOptions() *ProxyRunOptions {
ProxyOptions: &ProxyOptions{
UpstreamHeader: &identityheaders.AuthnHeaderConfig{},
},
OIDCOptions: &OIDCOptions{
OIDCConfig: &authn.OIDCConfig{},
},
OIDCOptions: &OIDCOptions{},
}
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/authn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package authn

import "k8s.io/apiserver/plugin/pkg/authenticator/token/oidc"

// X509Config holds public client certificate used for authentication requests if specified
type X509Config struct {
ClientCAFile string
Expand All @@ -30,12 +32,7 @@ type TokenConfig struct {

// OIDCConfig represents configuration used for JWT request authentication
type OIDCConfig struct {
IssuerURL string
ClientID string
CAFile string
UsernameClaim string
UsernamePrefix string
GroupsClaim string
GroupsPrefix string
SupportedSigningAlgs []string
CAFile string

oidc.Options
}
84 changes: 0 additions & 84 deletions pkg/authn/oidc.go

This file was deleted.

6 changes: 4 additions & 2 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
"os"

serverconfig "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/dynamiccertificates"
"k8s.io/apiserver/plugin/pkg/authenticator/token/oidc"

"github.com/brancz/kube-rbac-proxy/pkg/authn"
"github.com/brancz/kube-rbac-proxy/pkg/authn/identityheaders"
authz "github.com/brancz/kube-rbac-proxy/pkg/authorization"
"github.com/brancz/kube-rbac-proxy/pkg/authorization/rewrite"
Expand Down Expand Up @@ -55,7 +56,8 @@ type KubeRBACProxyInfo struct {

Authorization *authz.AuthzConfig

OIDC *authn.OIDCConfig
OIDC *oidc.Options
OIDCDynamicCAContent *dynamiccertificates.DynamicFileCAContent

AllowPaths []string
IgnorePaths []string
Expand Down

0 comments on commit 5e866f5

Please sign in to comment.