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

Remove Acquire #266

Merged
merged 9 commits into from
Nov 21, 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
13 changes: 13 additions & 0 deletions chrysom/acquire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package chrysom

import (
"net/http"
)

// Acquirer adds an authorization header and value to a given http request.
type Acquirer interface {
AddAuth(*http.Request) error
}
19 changes: 19 additions & 0 deletions chrysom/acquire_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package chrysom

import (
"net/http"

"github.com/stretchr/testify/mock"
)

type MockAquirer struct {
mock.Mock
}

func (m *MockAquirer) AddAuth(req *http.Request) error {
args := m.Called(req)

return args.Error(0)
}
36 changes: 8 additions & 28 deletions chrysom/basicClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"

"github.com/xmidt-org/ancla/model"
"github.com/xmidt-org/bascule/acquire"
"github.com/xmidt-org/sallust"
"go.uber.org/zap"
)
Expand All @@ -25,6 +24,7 @@ var (
ErrItemDataEmpty = errors.New("data field in item is required")
ErrUndefinedIntervalTicker = errors.New("interval ticker is nil. Can't listen for updates")
ErrAuthAcquirerFailure = errors.New("failed acquiring auth token")
ErrAuthAcquirerNil = errors.New("auth aquirer is nil")
ErrBadRequest = errors.New("argus rejected the request as invalid")
)

Expand Down Expand Up @@ -58,23 +58,17 @@ type BasicClientConfig struct {

// Auth provides the mechanism to add auth headers to outgoing requests.
// (Optional) If not provided, no auth headers are added.
Auth Auth
Auth Acquirer
}

// BasicClient is the client used to make requests to Argus.
type BasicClient struct {
client *http.Client
auth acquire.Acquirer
auth Acquirer
storeBaseURL string
bucket string
}

// Auth contains authorization data for requests to Argus.
type Auth struct {
JWT acquire.RemoteBearerTokenAcquirerOptions
Basic string
}

type response struct {
Body []byte
ArgusErrorHeader string
Expand All @@ -99,13 +93,9 @@ func NewBasicClient(config BasicClientConfig) (*BasicClient, error) {
return nil, err
}

tokenAcquirer, err := buildTokenAcquirer(config.Auth)
if err != nil {
return nil, err
}
clientStore := &BasicClient{
client: config.HTTPClient,
auth: tokenAcquirer,
auth: config.Auth,
bucket: config.Bucket,
storeBaseURL: config.Address + storeAPIPath,
}
Expand Down Expand Up @@ -210,7 +200,10 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
if err != nil {
return response{}, fmt.Errorf(errWrappedFmt, errNewRequestFailure, err.Error())
}
err = acquire.AddAuth(r, c.auth)
if c.auth == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this fixes the failing test in listenerClient_test

return response{}, ErrAuthAcquirerNil
}
err = c.auth.AddAuth(r)
if err != nil {
return response{}, fmt.Errorf(errWrappedFmt, ErrAuthAcquirerFailure, err.Error())
}
Expand All @@ -234,10 +227,6 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
return sqResp, nil
}

func isEmpty(options acquire.RemoteBearerTokenAcquirerOptions) bool {
return len(options.AuthURL) < 1 || options.Buffer == 0 || options.Timeout == 0
}

// translateNonSuccessStatusCode returns as specific error
// for known Argus status codes.
func translateNonSuccessStatusCode(code int) error {
Expand All @@ -251,15 +240,6 @@ func translateNonSuccessStatusCode(code int) error {
}
}

func buildTokenAcquirer(auth Auth) (acquire.Acquirer, error) {
if !isEmpty(auth.JWT) {
return acquire.NewRemoteBearerTokenAcquirer(auth.JWT)
} else if len(auth.Basic) > 0 {
return acquire.NewFixedAuthAcquirer(auth.Basic)
}
return &acquire.DefaultAcquirer{}, nil
}

func validateBasicConfig(config *BasicClientConfig) error {
if config.Address == "" {
return ErrAddressEmpty
Expand Down
Loading
Loading