Skip to content

Commit

Permalink
Merge pull request #266 from xmidt-org/acquire
Browse files Browse the repository at this point in the history
Remove Acquire
  • Loading branch information
denopink authored Nov 21, 2024
2 parents 710f1a8 + 44b0ca9 commit 3980858
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 298 deletions.
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 {
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

0 comments on commit 3980858

Please sign in to comment.