Skip to content

Commit 309e0e8

Browse files
fix(ske): prefer explicit login configuration
1 parent 445e7b2 commit 309e0e8

2 files changed

Lines changed: 54 additions & 20 deletions

File tree

internal/cmd/ske/kubeconfig/login/login.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"k8s.io/client-go/tools/clientcmd"
2525

2626
sdkAuth "github.com/stackitcloud/stackit-sdk-go/core/auth"
27-
"github.com/stackitcloud/stackit-sdk-go/core/clients"
2827
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
2928
ske "github.com/stackitcloud/stackit-sdk-go/services/ske/v2api"
3029

@@ -49,9 +48,8 @@ const (
4948
clusterNameFlag = "cluster-name"
5049
organizationFlag = "organization-id"
5150

52-
envAccessToken = "STACKIT_ACCESS_TOKEN"
53-
envServiceAccountEmail = "STACKIT_SERVICE_ACCOUNT_EMAIL"
54-
defaultFederatedTokenPath = "/var/run/secrets/stackit.cloud/serviceaccount/token" //nolint:gosec // Public path, not a credential.
51+
envAccessToken = "STACKIT_ACCESS_TOKEN"
52+
envServiceAccountEmail = "STACKIT_SERVICE_ACCOUNT_EMAIL"
5553
)
5654

5755
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -190,17 +188,17 @@ func parseClusterConfig(p *print.Printer, cmd *cobra.Command, idpMode, workloadI
190188
}
191189
}
192190

193-
if clusterConfig.ClusterName == "" {
194-
clusterConfig.ClusterName = flags.FlagToStringValue(p, cmd, clusterNameFlag)
191+
if clusterName := flags.FlagToStringValue(p, cmd, clusterNameFlag); clusterName != "" {
192+
clusterConfig.ClusterName = clusterName
195193
}
196-
if clusterConfig.OrganizationID == "" {
197-
clusterConfig.OrganizationID = flags.FlagToStringValue(p, cmd, organizationFlag)
194+
if organizationID := flags.FlagToStringValue(p, cmd, organizationFlag); organizationID != "" {
195+
clusterConfig.OrganizationID = organizationID
198196
}
199197
globalFlags := globalflags.Parse(p, cmd)
200-
if clusterConfig.STACKITProjectID == "" {
198+
if globalFlags.ProjectId != "" {
201199
clusterConfig.STACKITProjectID = globalFlags.ProjectId
202200
}
203-
if clusterConfig.Region == "" {
201+
if globalFlags.Region != "" {
204202
clusterConfig.Region = globalFlags.Region
205203
}
206204

@@ -444,11 +442,11 @@ func workloadIdentityConfigured() bool {
444442
if os.Getenv(envServiceAccountEmail) == "" {
445443
return false
446444
}
447-
if auth.IsOIDCEnabled() {
448-
return true
449-
}
450-
_, err := auth.OIDCTokenFunc()
451-
return err == nil
445+
if auth.IsOIDCEnabled() {
446+
return true
447+
}
448+
_, err := auth.OIDCTokenFunc()
449+
return err == nil
452450
}
453451

454452
func getWorkloadIdentityAccessToken() (string, error) {

internal/cmd/ske/kubeconfig/login/login_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ func TestParseClusterConfigWithoutExecClusterInfo(t *testing.T) {
112112
}
113113
}
114114

115-
func TestParseClusterConfigUsesExecClusterInfo(t *testing.T) {
115+
func TestParseClusterConfigPrefersExplicitConfiguration(t *testing.T) {
116116
viper.Reset()
117117
t.Cleanup(viper.Reset)
118-
viper.Set(config.ProjectIdKey, uuid.NewString())
119-
viper.Set(config.RegionKey, "conflicting-region")
118+
explicitProjectID := uuid.NewString()
119+
explicitRegion := "explicit-region"
120+
explicitClusterName := "explicit-cluster"
121+
explicitOrganizationID := uuid.NewString()
122+
viper.Set(config.ProjectIdKey, explicitProjectID)
123+
viper.Set(config.RegionKey, explicitRegion)
120124
t.Setenv(envServiceAccountEmail, "workload@sa.stackit.cloud")
121125

122126
configJSON, err := json.Marshal(fixtureClusterConfig())
@@ -131,16 +135,48 @@ func TestParseClusterConfigUsesExecClusterInfo(t *testing.T) {
131135
params := testparams.NewTestParams()
132136
cmd := &cobra.Command{}
133137
configureFlags(cmd)
134-
if err := cmd.Flags().Set(clusterNameFlag, "conflicting-cluster"); err != nil {
138+
if err := cmd.Flags().Set(clusterNameFlag, explicitClusterName); err != nil {
135139
t.Fatalf("set cluster name flag: %v", err)
136140
}
137-
if err := cmd.Flags().Set(organizationFlag, uuid.NewString()); err != nil {
141+
if err := cmd.Flags().Set(organizationFlag, explicitOrganizationID); err != nil {
138142
t.Fatalf("set organization flag: %v", err)
139143
}
140144
actual, err := parseClusterConfig(params.Printer, cmd, true, true, true)
141145
if err != nil {
142146
t.Fatalf("parse cluster config: %v", err)
143147
}
148+
expected := fixtureClusterConfig(func(config *clusterConfig) {
149+
config.STACKITProjectID = explicitProjectID
150+
config.Region = explicitRegion
151+
config.ClusterName = explicitClusterName
152+
config.OrganizationID = explicitOrganizationID
153+
})
154+
if diff := cmp.Diff(actual, expected, cmpopts.IgnoreFields(clusterConfig{}, "cacheKey")); diff != "" {
155+
t.Fatalf("Data does not match: %s", diff)
156+
}
157+
}
158+
159+
func TestParseClusterConfigUsesExecClusterInfoAsFallback(t *testing.T) {
160+
viper.Reset()
161+
t.Cleanup(viper.Reset)
162+
t.Setenv(envServiceAccountEmail, "workload@sa.stackit.cloud")
163+
164+
configJSON, err := json.Marshal(fixtureClusterConfig())
165+
if err != nil {
166+
t.Fatalf("marshal cluster config: %v", err)
167+
}
168+
setExecCredentialEnv(t, &clientauthenticationv1.Cluster{
169+
Server: "https://api.example.stackit.cloud",
170+
Config: runtime.RawExtension{Raw: configJSON},
171+
})
172+
173+
params := testparams.NewTestParams()
174+
cmd := &cobra.Command{}
175+
configureFlags(cmd)
176+
actual, err := parseClusterConfig(params.Printer, cmd, true, true, true)
177+
if err != nil {
178+
t.Fatalf("parse cluster config: %v", err)
179+
}
144180
expected := fixtureClusterConfig()
145181
if diff := cmp.Diff(actual, expected, cmpopts.IgnoreFields(clusterConfig{}, "cacheKey")); diff != "" {
146182
t.Fatalf("Data does not match: %s", diff)

0 commit comments

Comments
 (0)