Skip to content

Commit 999f5f0

Browse files
authored
Merge branch 'main' into renovate/etc-modules
2 parents 728f805 + 0eb52af commit 999f5f0

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed a bug in `login` command where the `issuer` URL was used instead of the `server` address in login retry attempt.
13+
1014
### Added
1115

1216
- Added read header timeout to http server
1317

18+
### Changed
19+
20+
- 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.
21+
1422
## [2.24.1] - 2022-10-12
1523

1624
### Fixed

cmd/login/clientcert.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ func printWCClientCertCredentials(k8sConfigAccess clientcmd.ConfigAccess, fs afe
392392
if err != nil {
393393
return "", false, microerror.Mask(err)
394394
}
395-
// Because we are still in the MC context we need to switch back to the origin context after creating the WC kubeconfig file
396-
if c.loginOptions.originContext != "" {
395+
// Change back to the origin context if needed
396+
if c.loginOptions.originContext != "" && config.CurrentContext != "" && c.loginOptions.originContext != config.CurrentContext {
397+
// Because we are still in the MC context we need to switch back to the origin context after creating the WC kubeconfig file
397398
config.CurrentContext = c.loginOptions.originContext
398399
err = clientcmd.ModifyConfig(k8sConfigAccess, *config, false)
399400
if err != nil {

cmd/login/login.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ func (r *runner) loginWithKubeContextName(ctx context.Context, contextName strin
4747
authType := kubeconfig.GetAuthType(config, contextName)
4848
if authType == kubeconfig.AuthTypeAuthProvider {
4949
// If we get here, we are sure that the kubeconfig context exists.
50-
authProvider, _ := kubeconfig.GetAuthProvider(config, contextName)
51-
issuer := authProvider.Config[Issuer]
50+
server, _ := kubeconfig.GetClusterServer(config, contextName)
5251

53-
err = r.loginWithURL(ctx, issuer, false, "")
52+
err = r.loginWithURL(ctx, server, false, "")
5453
if err != nil {
5554
return microerror.Mask(err)
5655
}

pkg/middleware/renewtoken/renewtoken.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
"github.com/giantswarm/kubectl-gs/pkg/oidc"
1111
)
1212

13+
const (
14+
refreshTokenKey = "refresh-token"
15+
idTokenKey = "id-token"
16+
idpIssuerUrlKey = "idp-issuer-url"
17+
clientIdKey = "client-id"
18+
)
19+
1320
// Middleware will attempt to renew the current context's auth info token.
1421
// If the renewal fails, this middleware will not fail.
1522
func Middleware(config genericclioptions.RESTClientGetter) middleware.Middleware {
@@ -30,25 +37,26 @@ func Middleware(config genericclioptions.RESTClientGetter) middleware.Middleware
3037
var auther *oidc.Authenticator
3138
{
3239
oidcConfig := oidc.Config{
33-
Issuer: authProvider.Config["idp-issuer-url"],
34-
ClientID: authProvider.Config["client-id"],
40+
Issuer: authProvider.Config[idpIssuerUrlKey],
41+
ClientID: authProvider.Config[clientIdKey],
3542
}
3643
auther, err = oidc.New(ctx, oidcConfig)
3744
if err != nil {
3845
return nil
3946
}
4047
}
4148

42-
{
43-
idToken, rToken, err := auther.RenewToken(ctx, authProvider.Config["refresh-token"])
44-
if err != nil {
45-
return nil
46-
}
47-
authProvider.Config["refresh-token"] = rToken
48-
authProvider.Config["id-token"] = idToken
49+
idToken, rToken, err := auther.RenewToken(ctx, authProvider.Config[refreshTokenKey])
50+
if err != nil {
51+
return nil
4952
}
5053

51-
_ = clientcmd.ModifyConfig(k8sConfigAccess, *config, true)
54+
// Update the config only in case there are actual changes
55+
if authProvider.Config[refreshTokenKey] != rToken || authProvider.Config[idTokenKey] != idToken {
56+
authProvider.Config[refreshTokenKey] = rToken
57+
authProvider.Config[idTokenKey] = idToken
58+
_ = clientcmd.ModifyConfig(k8sConfigAccess, *config, true)
59+
}
5260

5361
return nil
5462
}

0 commit comments

Comments
 (0)