Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 11 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@ func (e *APIError) Unwrap() error {
return e.e
}

type InvalidCredentialsError struct{}

func (e InvalidCredentialsError) Error() string {
return "invalid credentials"
}
var ErrInvalidCredentials = fmt.Errorf("invalid credentials")
var ErrInvalidCode = fmt.Errorf("invalid enrollment code")

type EnrollMeta struct {
OrganizationID string
Expand Down Expand Up @@ -163,6 +160,13 @@ func (c *Client) Enroll(ctx context.Context, logger logrus.FieldLogger, code str
return nil, nil, nil, nil, &APIError{e: fmt.Errorf("error decoding JSON response: %s\nbody: %s", err, b), ReqID: reqID}
}

// Check for *only* an "invalid code" error returned by the API
if len(r.Errors) == 1 {
if err := r.Errors[0]; err.Path == "code" && err.Code == "ERR_INVALID_VALUE" {
return nil, nil, nil, nil, &APIError{e: ErrInvalidCode, ReqID: reqID}
}
}

// Check for any errors returned by the API
if err := r.Errors.ToError(); err != nil {
return nil, nil, nil, nil, &APIError{e: fmt.Errorf("unexpected error during enrollment: %v", err), ReqID: reqID}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're no longer capturing the request ID. We log this in mobile_nebula. I think we either need to come up with a solution to continue returning it in APIErrors, or maybe add it to every element in the []APIError (and add it to APIError), .... or just take the easier approach of returning a specific code here (ErrInvalidCode), which will also mean that callers don't all have to parse this themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that lose information if the api server were to return an error other than "invalid code"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you think we might be able to avoid that, and still return an explicit error for invalid codes here? How would the caller do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the server ever returns invalid code along with an additional error. If it did, the caller might want to know that information. So I think what I would do is only return ErrInvalidCode if that's the only error. If there are multiple errors, leave the return as it was before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

LGTM

Expand Down Expand Up @@ -404,7 +408,7 @@ func (c *Client) streamingPostDNClient(ctx context.Context, reqType string, valu
case http.StatusOK:
sc.respBytes = respBody
case http.StatusUnauthorized:
sc.err.Store(InvalidCredentialsError{})
sc.err.Store(ErrInvalidCredentials)
default:
var errors struct {
Errors message.APIErrors
Expand Down Expand Up @@ -451,7 +455,7 @@ func (c *Client) postDNClient(ctx context.Context, reqType string, value []byte,
case http.StatusOK:
return respBody, nil
case http.StatusUnauthorized:
return nil, InvalidCredentialsError{}
return nil, ErrInvalidCredentials
default:
var errors struct {
Errors message.APIErrors
Expand Down
6 changes: 2 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ func TestDoUpdate(t *testing.T) {
defer cancel()
_, err = c.CheckForUpdate(ctx, invalidCreds)
assert.Error(t, err)
invalidCredsErrorType := InvalidCredentialsError{}
assert.ErrorAs(t, err, &invalidCredsErrorType)
assert.ErrorIs(t, err, ErrInvalidCredentials)
serverErrs := ts.Errors() // This consumes/resets the server errors
require.Len(t, serverErrs, 1)

Expand Down Expand Up @@ -427,8 +426,7 @@ func TestDoUpdate_P256(t *testing.T) {
defer cancel()
_, err = c.CheckForUpdate(ctx, invalidCreds)
assert.Error(t, err)
invalidCredsErrorType := InvalidCredentialsError{}
assert.ErrorAs(t, err, &invalidCredsErrorType)
assert.ErrorIs(t, err, ErrInvalidCredentials)
serverErrs := ts.Errors() // This consumes/resets the server errors
require.Len(t, serverErrs, 1)

Expand Down
Loading