Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw error when an api token is bad or invalid #1602

Merged
merged 1 commit into from
Mar 16, 2024
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
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/astronomer/astro-cli/cmd/registry"
Expand Down Expand Up @@ -83,6 +85,9 @@
if isCloudCtx {
err = cloudCmd.Setup(cmd, platformCoreClient, astroCoreClient)
if err != nil {
if strings.Contains(err.Error(), "token is invalid or malformed") {
return errors.New("API Token is invalid or malformed") //nolint

Check warning on line 89 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}
softwareCmd.InitDebugLogs = append(softwareCmd.InitDebugLogs, "Error during cmd setup: "+err.Error())
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func ParseAPIToken(astroAPIToken string) (*CustomClaims, error) {
// Parse the token to peek at the custom claims
jwtParser := jwt.NewParser()
parsedToken, _, err := jwtParser.ParseUnverified(astroAPIToken, &CustomClaims{})
if err != nil {
return nil, errors.Wrap(err, "token is invalid or malformed")
}
claims, ok := parsedToken.Claims.(*CustomClaims)
if !ok {
return nil, errors.Wrap(err, "failed to parse auth token")
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ func TestCheckEnvBool(t *testing.T) {
}
}

func TestParseAPIToken(t *testing.T) {
t.Run("throw error is token is invalid", func(t *testing.T) {
token := "invalid-token"
_, err := ParseAPIToken(token)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "token is invalid or malformed")
})

t.Run("returns token claims if token is valid", func(t *testing.T) {
// dummy token
token := "eyJhbGciOiAibm9uZSIsICJ0eXAiOiAiSldUIn0K.eyJ1c2VybmFtZSI6ImFkbWluaW5pc3RyYXRvciIsImlzX2FkbWluIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTUxNjI0MjYyMn0."
claims, err := ParseAPIToken(token)
assert.NotNil(t, claims)
assert.Nil(t, err)
})
}

func TestIsM1(t *testing.T) {
t.Run("returns true if running on arm architecture", func(t *testing.T) {
assert.True(t, IsM1("darwin", "arm64"))
Expand Down