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 3 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
16 changes: 16 additions & 0 deletions acquire/acquire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package acquire

import (
"net/http"
"time"
)

type Acquirer interface {
AddAuth(*http.Request) error
Acquire() (string, error)
ParseToken([]byte) (string, error)
ParseExpiration([]byte) (time.Time, error)
}
32 changes: 4 additions & 28 deletions chrysom/basicClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"io"
"net/http"

"github.com/xmidt-org/ancla/acquire"
"github.com/xmidt-org/ancla/model"
"github.com/xmidt-org/bascule/acquire"
"github.com/xmidt-org/sallust"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -55,10 +55,6 @@ type BasicClientConfig struct {
// HTTPClient refers to the client that will be used to send requests.
// (Optional) Defaults to http.DefaultClient.
HTTPClient *http.Client

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

// BasicClient is the client used to make requests to Argus.
Expand All @@ -69,12 +65,6 @@ type BasicClient struct {
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 @@ -93,19 +83,18 @@ type Items []model.Item

// NewBasicClient creates a new BasicClient that can be used to
// make requests to Argus.
func NewBasicClient(config BasicClientConfig) (*BasicClient, error) {
func NewBasicClient(config BasicClientConfig, auth acquire.Acquirer) (*BasicClient, error) {
err := validateBasicConfig(&config)
if err != nil {
return nil, err
}

tokenAcquirer, err := buildTokenAcquirer(config.Auth)
if err != nil {
return nil, err
}
clientStore := &BasicClient{
client: config.HTTPClient,
auth: tokenAcquirer,
auth: auth,
bucket: config.Bucket,
storeBaseURL: config.Address + storeAPIPath,
}
Expand Down Expand Up @@ -210,7 +199,7 @@ 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)
err = c.auth.AddAuth(r)
if err != nil {
return response{}, fmt.Errorf(errWrappedFmt, ErrAuthAcquirerFailure, err.Error())
}
Expand All @@ -234,10 +223,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 +236,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
1 change: 0 additions & 1 deletion chrysom/basicClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
allDefinedCaseConfig := &BasicClientConfig{
HTTPClient: myAmazingClient,
Address: "http://legit-argus-hostname.io",
Auth: Auth{},
Bucket: "amazing-bucket",
}

Expand Down Expand Up @@ -156,14 +155,14 @@
server := httptest.NewServer(echoHandler)
defer server.Close()

client, err := NewBasicClient(BasicClientConfig{

Check failure on line 158 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

not enough arguments in call to NewBasicClient
HTTPClient: server.Client(),
Address: "http://argus-hostname.io",
Bucket: "bucket-name",
})

if tc.AcquirerFails {
client.auth = acquirerFunc(failAcquirer)

Check failure on line 165 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

cannot use acquirerFunc(failAcquirer) (value of type acquirerFunc) as acquire.Acquirer value in assignment: acquirerFunc does not implement acquire.Acquirer (missing method AddAuth)
}

var URL = server.URL
Expand Down Expand Up @@ -254,7 +253,7 @@
rw.Write(tc.ResponsePayload)
}))

client, err := NewBasicClient(BasicClientConfig{

Check failure on line 256 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

not enough arguments in call to NewBasicClient
HTTPClient: server.Client(),
Address: server.URL,
Bucket: bucket,
Expand All @@ -263,7 +262,7 @@
require.Nil(err)

if tc.ShouldMakeRequestFail {
client.auth = acquirerFunc(failAcquirer)

Check failure on line 265 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

cannot use acquirerFunc(failAcquirer) (value of type acquirerFunc) as acquire.Acquirer value in assignment: acquirerFunc does not implement acquire.Acquirer (missing method AddAuth)
}

if tc.ShouldDoRequestFail {
Expand Down Expand Up @@ -389,14 +388,14 @@
}
}))

client, err := NewBasicClient(BasicClientConfig{

Check failure on line 391 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

not enough arguments in call to NewBasicClient
HTTPClient: server.Client(),
Address: server.URL,
Bucket: bucket,
})

if tc.ShouldMakeRequestFail {
client.auth = acquirerFunc(failAcquirer)

Check failure on line 398 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

cannot use acquirerFunc(failAcquirer) (value of type acquirerFunc) as acquire.Acquirer value in assignment: acquirerFunc does not implement acquire.Acquirer (missing method AddAuth)
}

if tc.ShouldDoRequestFail {
Expand Down Expand Up @@ -488,14 +487,14 @@
rw.Write(tc.ResponsePayload)
}))

client, err := NewBasicClient(BasicClientConfig{

Check failure on line 490 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

not enough arguments in call to NewBasicClient
HTTPClient: server.Client(),
Address: server.URL,
Bucket: bucket,
})

if tc.ShouldMakeRequestFail {
client.auth = acquirerFunc(failAcquirer)

Check failure on line 497 in chrysom/basicClient_test.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

cannot use acquirerFunc(failAcquirer) (value of type acquirerFunc) as acquire.Acquirer value in assignment: acquirerFunc does not implement acquire.Acquirer (missing method AddAuth)
}

if tc.ShouldDoRequestFail {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ toolchain go1.22.9
require (
github.com/aws/aws-sdk-go v1.54.19
github.com/go-kit/kit v0.13.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/lestrrat-go/jwx/v2 v2.1.2
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_model v0.6.1
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.9.0
github.com/xmidt-org/bascule v0.11.6
github.com/xmidt-org/httpaux v0.4.0
github.com/xmidt-org/sallust v0.2.2
github.com/xmidt-org/touchstone v0.1.5
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand Down Expand Up @@ -65,8 +63,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xmidt-org/bascule v0.11.6 h1:i46FAI97XPMt3OKraiNyKa+mt36AhLO8iuInAypXKNM=
github.com/xmidt-org/bascule v0.11.6/go.mod h1:BXb5PEm/tjqdiEGsd+phm+fItMJx+Huv6LTCEU/zTzg=
github.com/xmidt-org/httpaux v0.4.0 h1:cAL/MzIBpSsv4xZZeq/Eu1J5M3vfNe49xr41mP3COKU=
github.com/xmidt-org/httpaux v0.4.0/go.mod h1:UypqZwuZV1nn8D6+K1JDb+im9IZrLNg/2oO/Bgiybxc=
github.com/xmidt-org/sallust v0.2.2 h1:MrINLEr7cMj6ENx/O76fvpfd5LNGYnk7OipZAGXPYA0=
Expand Down
76 changes: 0 additions & 76 deletions jwtAcquireParser.go

This file was deleted.

94 changes: 0 additions & 94 deletions jwtAcquireParser_test.go

This file was deleted.

22 changes: 7 additions & 15 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"time"

"github.com/xmidt-org/ancla/acquire"
"github.com/xmidt-org/ancla/chrysom"
"github.com/xmidt-org/sallust"
"go.uber.org/fx"
Expand Down Expand Up @@ -47,7 +48,7 @@ type Config struct {
// Simple: parser assumes token payloads have the following structure: https://github.com/xmidt-org/bascule/blob/c011b128d6b95fa8358228535c63d1945347adaa/acquire/bearer.go#L77
// Raw: parser assumes all of the token payload == JWT token
// (Optional). Defaults to 'simple'
JWTParserType jwtAcquireParserType
JWTParserType string

// DisablePartnerIDs, if true, will allow webhooks to register without
// checking the validity of the partnerIDs in the request
Expand All @@ -68,9 +69,8 @@ type ClientService struct {
}

// NewService builds the Argus client service from the given configuration.
func NewService(cfg Config) (*ClientService, error) {
prepArgusBasicClientConfig(&cfg)
basic, err := chrysom.NewBasicClient(cfg.BasicClientConfig)
func NewService(cfg Config, auth acquire.Acquirer) (*ClientService, error) {
basic, err := chrysom.NewBasicClient(cfg.BasicClientConfig, auth)
if err != nil {
return nil, fmt.Errorf("failed to create chrysom basic client: %v", err)
}
Expand Down Expand Up @@ -133,16 +133,6 @@ func (s *ClientService) GetAll(ctx context.Context) ([]Register, error) {
return iws, nil
}

func prepArgusBasicClientConfig(cfg *Config) error {
p, err := newJWTAcquireParser(cfg.JWTParserType)
if err != nil {
return err
}
cfg.BasicClientConfig.Auth.JWT.GetToken = p.token
cfg.BasicClientConfig.Auth.JWT.GetExpiration = p.expiration
return nil
}

func prepArgusListenerConfig(cfg *chrysom.ListenerConfig, metrics chrysom.Measures, watches ...Watch) {
watches = append(watches, webhookListSizeWatch(metrics.WebhookListSizeGauge))
cfg.Listener = chrysom.ListenerFunc(func(ctx context.Context, items chrysom.Items) {
Expand All @@ -160,14 +150,16 @@ func prepArgusListenerConfig(cfg *chrysom.ListenerConfig, metrics chrysom.Measur

type ServiceIn struct {
fx.In

Config Config
Client *http.Client
Auth acquire.Acquirer
}

func ProvideService() fx.Option {
return fx.Provide(
func(in ServiceIn) (*ClientService, error) {
svc, err := NewService(in.Config)
svc, err := NewService(in.Config, in.Auth)
if err != nil {
return nil, errors.Join(errFailedConfig, err)
}
Expand Down
Loading