Skip to content

Commit

Permalink
Prevented unnecessary writes to the main kubeconfig file (#912)
Browse files Browse the repository at this point in the history
* Prevented unnecessary writes to the main kubeconfig file

* Updated changelog
  • Loading branch information
vvondruska committed Oct 13, 2022
1 parent d0ffd89 commit 0eb52af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

- Added read header timeout to http server

### Changed

- Adjusted `kubectl gs login` command to ensure that it writes to the main kubeconfig file only in case there are actual changes in the content of the file.

## [2.24.1] - 2022-10-12

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions cmd/login/clientcert.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ func printWCClientCertCredentials(k8sConfigAccess clientcmd.ConfigAccess, fs afe
if err != nil {
return "", false, microerror.Mask(err)
}
// Because we are still in the MC context we need to switch back to the origin context after creating the WC kubeconfig file
if c.loginOptions.originContext != "" {
// Change back to the origin context if needed
if c.loginOptions.originContext != "" && config.CurrentContext != "" && c.loginOptions.originContext != config.CurrentContext {
// Because we are still in the MC context we need to switch back to the origin context after creating the WC kubeconfig file
config.CurrentContext = c.loginOptions.originContext
err = clientcmd.ModifyConfig(k8sConfigAccess, *config, false)
if err != nil {
Expand Down
28 changes: 18 additions & 10 deletions pkg/middleware/renewtoken/renewtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"github.com/giantswarm/kubectl-gs/pkg/oidc"
)

const (
refreshTokenKey = "refresh-token"
idTokenKey = "id-token"
idpIssuerUrlKey = "idp-issuer-url"
clientIdKey = "client-id"
)

// Middleware will attempt to renew the current context's auth info token.
// If the renewal fails, this middleware will not fail.
func Middleware(config genericclioptions.RESTClientGetter) middleware.Middleware {
Expand All @@ -30,25 +37,26 @@ func Middleware(config genericclioptions.RESTClientGetter) middleware.Middleware
var auther *oidc.Authenticator
{
oidcConfig := oidc.Config{
Issuer: authProvider.Config["idp-issuer-url"],
ClientID: authProvider.Config["client-id"],
Issuer: authProvider.Config[idpIssuerUrlKey],
ClientID: authProvider.Config[clientIdKey],
}
auther, err = oidc.New(ctx, oidcConfig)
if err != nil {
return nil
}
}

{
idToken, rToken, err := auther.RenewToken(ctx, authProvider.Config["refresh-token"])
if err != nil {
return nil
}
authProvider.Config["refresh-token"] = rToken
authProvider.Config["id-token"] = idToken
idToken, rToken, err := auther.RenewToken(ctx, authProvider.Config[refreshTokenKey])
if err != nil {
return nil
}

_ = clientcmd.ModifyConfig(k8sConfigAccess, *config, true)
// Update the config only in case there are actual changes
if authProvider.Config[refreshTokenKey] != rToken || authProvider.Config[idTokenKey] != idToken {
authProvider.Config[refreshTokenKey] = rToken
authProvider.Config[idTokenKey] = idToken
_ = clientcmd.ModifyConfig(k8sConfigAccess, *config, true)
}

return nil
}
Expand Down

0 comments on commit 0eb52af

Please sign in to comment.