Skip to content

Commit

Permalink
catch unauthed error and imporve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aliotta committed Jun 22, 2021
1 parent defbc3e commit 4df8dca
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func prepareDefaultAirflowImageTag(airflowVersion string, httpClient *airflowver
defaultImageTag = fmt.Sprintf("%s-buster-onbuild", airflowVersion)
}
} else if airflowVersion != "" {
return "", err
switch t := err; t {
default:
return "", err
case houston.PermissionsErrorVerbose:
return "", errors.New("The --airflow-version flag is not supported if you're not authenticated to Astronomer. Please authenticate and try again.")
}
}

if len(defaultImageTag) == 0 {
Expand Down
74 changes: 74 additions & 0 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,77 @@ func Test_prepareDefaultAirflowImageTagHoustonBadRequest(t *testing.T) {
assert.Equal(t, err.Error(), fmt.Sprintf("API error (400): %s", mockErrorResponse))
assert.Equal(t, "", defaultTag)
}


func Test_prepareDefaultAirflowImageTagHoustonUnauthedRequest(t *testing.T) {
testUtil.InitTestConfig()
mockErrorResponse := `{
"errors": [
{
"message": "You do not have the appropriate permissions for that",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"deploymentConfig"
],
"extensions": {
"code": "FORBIDDEN",
"exception": {
"message": "You do not have the appropriate permissions for that"
}
}
}
],
"data": {
"deploymentConfig": null
}
}`
// prepare fake response from updates.astronomer.io
okResponse := `{
"version": "1.0",
"available_releases": [
{
"version": "1.10.5",
"level": "new_feature",
"url": "https://github.com/astronomer/airflow/releases/tag/1.10.5-11",
"release_date": "2020-10-05T20:03:00+00:00",
"tags": [
"1.10.5-alpine3.10-onbuild",
"1.10.5-buster-onbuild",
"1.10.5-alpine3.10",
"1.10.5-buster"
],
"channel": "stable"
}
]
}`

client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(okResponse)),
Header: make(http.Header),
}
})
httpClient := airflowversions.NewClient(client)

// prepare fake response from houston
houstonClient := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(mockErrorResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(houstonClient)

output := new(bytes.Buffer)

defaultTag, err := prepareDefaultAirflowImageTag("2.0.2", httpClient, api, output)
assert.Equal(t, "The --airflow-version flag is not supported if you're not authenticated to Astronomer. Please authenticate and try again.", err.Error())
assert.Equal(t, "", defaultTag)
}
4 changes: 2 additions & 2 deletions houston/houston.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var PermissionsError = errors.New("You do not have the appropriate permissions for that")
var PermissionsErrorVerbose = errors.New("You do not have the appropriate permissions for that: Your token has expired. Please log in again.")

// Client containers the logger and HTTPClient used to communicate with the HoustonAPI
type Client struct {
Expand Down Expand Up @@ -85,12 +86,11 @@ func (c *Client) Do(doOpts httputil.DoOptions) (*Response, error) {
if err != nil {
return nil, errors.Wrap(err, "Failed to JSON decode Houston response")
}

// Houston Specific Errors
if decode.Errors != nil {
err = errors.New(decode.Errors[0].Message)
if err.Error() == PermissionsError.Error() {
return nil, errors.Wrap(errors.New("Your token has expired. Please log in again."), err.Error())
return nil, PermissionsErrorVerbose
}
return nil, err
}
Expand Down

0 comments on commit 4df8dca

Please sign in to comment.