Skip to content

Commit 6048998

Browse files
committed
fix: Do not treat invalid credentials as an internal error
1 parent 86eeb03 commit 6048998

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pkg/webhook/preflight/nutanix/credentials.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package nutanix
66
import (
77
"context"
88
"fmt"
9+
"strings"
910

1011
corev1 "k8s.io/api/core/v1"
1112
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -181,21 +182,30 @@ func newCredentialsCheck(
181182

182183
// Validate the credentials using an API call.
183184
_, err = nclient.GetCurrentLoggedInUser(ctx)
184-
if err != nil {
185+
if err == nil {
186+
// We initialized both clients, and verified the credentials using the v3 client.
187+
cd.nclient = nclient
188+
return credentialsCheck
189+
}
190+
191+
if strings.Contains(err.Error(), "invalid Nutanix credentials") {
185192
credentialsCheck.result.Allowed = false
186-
credentialsCheck.result.InternalError = true
187193
credentialsCheck.result.Causes = append(credentialsCheck.result.Causes,
188194
preflight.Cause{
189-
Message: fmt.Sprintf("Failed to validate credentials using the v3 API client. "+
190-
"The URL and/or credentials may be incorrect. (Error: %q)", err),
191-
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix.prismCentralEndpoint",
195+
Message: fmt.Sprintf("Failed to validate credentials using the v3 API client: %s", err),
196+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix.prismCentralEndpoint.credentials.secretRef",
192197
},
193198
)
194199
return credentialsCheck
195200
}
196201

197-
// We initialized both clients, and verified the credentials using the v3 client.
198-
cd.nclient = nclient
199-
202+
credentialsCheck.result.Allowed = false
203+
credentialsCheck.result.InternalError = true
204+
credentialsCheck.result.Causes = append(credentialsCheck.result.Causes,
205+
preflight.Cause{
206+
Message: fmt.Sprintf("Failed to validate credentials using the v3 API client: %s", err),
207+
Field: "$.spec.topology.variables[[email protected]==\"clusterConfig\"].value.nutanix.prismCentralEndpoint",
208+
},
209+
)
200210
return credentialsCheck
201211
}

pkg/webhook/preflight/nutanix/credentials_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,21 @@ func TestNewCredentialsCheck_FailedToGetCurrentLoggedInUser(t *testing.T) {
212212
result := check.Run(context.Background())
213213
assert.False(t, result.Allowed)
214214
assert.True(t, result.InternalError)
215-
assert.Contains(t, result.Causes[0].Message, "Failed to validate credentials using the v3 API client.")
215+
assert.Contains(t, result.Causes[0].Message, "Failed to validate credentials using the v3 API client: "+
216+
assert.AnError.Error())
217+
}
218+
219+
func TestNewCredentialsCheck_GetCurrentLoggedInUserInvalidCredentials(t *testing.T) {
220+
nclientFactory := func(_ prismgoclient.Credentials) (client, error) {
221+
return &mocknclient{err: fmt.Errorf("invalid Nutanix credentials")}, nil
222+
}
223+
cd := validCheckDependencies()
224+
check := newCredentialsCheck(context.Background(), nclientFactory, cd)
225+
result := check.Run(context.Background())
226+
assert.False(t, result.Allowed)
227+
assert.False(t, result.InternalError)
228+
assert.Contains(t, result.Causes[0].Message, "Failed to validate credentials using the v3 API client: "+
229+
"invalid Nutanix credentials")
216230
}
217231

218232
func validCheckDependencies() *checkDependencies {

0 commit comments

Comments
 (0)