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

OIDC: allow providing whole Kubeconfig #2123

Merged
merged 2 commits into from
May 29, 2024
Merged
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
24 changes: 20 additions & 4 deletions internal/cmd/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type oidcConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

output string
caCertificate string
Expand All @@ -33,7 +34,8 @@ type TokenData struct {

func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := oidcConfig{
KymaConfig: kymaConfig,
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
}

cmd := &cobra.Command{
Expand All @@ -49,6 +51,8 @@ func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
},
}

cfg.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&cfg.output, "output", "", "Path to the output kubeconfig file")
cmd.Flags().StringVar(&cfg.caCertificate, "ca-certificate", "", "Path to the CA certificate file")
cmd.Flags().StringVar(&cfg.clusterServer, "cluster-server", "", "URL of the cluster server")
Expand All @@ -57,8 +61,9 @@ func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cmd.Flags().StringVar(&cfg.audience, "audience", "", "Audience of the token")
cmd.Flags().StringVar(&cfg.idTokenRequestURL, "id-token-request-url", "", "URL to request the ID token, defaults to ACTIONS_ID_TOKEN_REQUEST_URL env variable")

_ = cmd.MarkFlagRequired("ca-certificate")
_ = cmd.MarkFlagRequired("cluster-server")
cmd.MarkFlagsOneRequired("kubeconfig", "ca-certificate")
cmd.MarkFlagsRequiredTogether("ca-certificate", "cluster-server")
cmd.MarkFlagsMutuallyExclusive("kubeconfig", "ca-certificate")

cmd.MarkFlagsMutuallyExclusive("token", "id-token-request-url")
cmd.MarkFlagsMutuallyExclusive("token", "audience")
Expand All @@ -71,6 +76,10 @@ func (cfg *oidcConfig) complete() clierror.Error {
cfg.idTokenRequestURL = os.Getenv("ACTIONS_ID_TOKEN_REQUEST_URL")
}
cfg.idTokenRequestToken = os.Getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")

if cfg.KubeClientConfig.Kubeconfig != "" {
return cfg.KubeClientConfig.Complete()
}
return nil
}

Expand Down Expand Up @@ -107,8 +116,15 @@ func runOIDC(cfg *oidcConfig) clierror.Error {
return clierror.Wrap(err, clierror.New("failed to get token"))
}
}
caCertificate := cfg.caCertificate
clusterServer := cfg.clusterServer
if cfg.KubeClientConfig.Kubeconfig != "" {
currentServer := cfg.KubeClient.ApiConfig().Clusters[cfg.KubeClient.ApiConfig().CurrentContext]
caCertificate = string(currentServer.CertificateAuthorityData)
clusterServer = currentServer.Server
}

enrichedKubeconfig, err := createKubeconfig(cfg.caCertificate, cfg.clusterServer, token)
enrichedKubeconfig, err := createKubeconfig(caCertificate, clusterServer, token)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to create kubeconfig"))
}
Expand Down
Loading