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

patch: fix failing caduceus tests #551

Merged
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
8 changes: 2 additions & 6 deletions caduceus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Caduceus(arguments []string, run bool) error {
goschtalt.UnmarshalFunc[sallust.Config]("logging"),
goschtalt.UnmarshalFunc[candlelight.Config]("tracing"),
goschtalt.UnmarshalFunc[touchstone.Config]("prometheus"),
goschtalt.UnmarshalFunc[handler.CapabilityConfig]("capabilityCheck"),
goschtalt.UnmarshalFunc[sink.Config]("sender"),
goschtalt.UnmarshalFunc[Service]("service"),
goschtalt.UnmarshalFunc[client.HttpClientTimeout]("argusClientTimeout"),
Expand Down Expand Up @@ -210,12 +211,7 @@ func provideCLIWithOpts(args cliArgs, testOpts bool) (*CLI, error) {

parser, err := kong.New(&cli,
kong.Name(applicationName),
kong.Description("The cpe agent for Xmidt service.\n"+
fmt.Sprintf("\tVersion: %s\n", version)+
fmt.Sprintf("\tDate: %s\n", date)+
fmt.Sprintf("\tCommit: %s\n", commit)+
fmt.Sprintf("\tBuilt By: %s\n", builtBy),
),
kong.Description("Xmidt Caudceus service.\n"),
kong.UsageOnError(),
opt,
)
Expand Down
112 changes: 56 additions & 56 deletions caduceus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,68 +272,68 @@ authHeader: ["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=", "dXNlcjpwYXNz"]
# GET. The accept all method is a catchall string that indicates the capability
# is approved for all methods.
# (Optional)
# capabilityCheck:
# # type provides the mode for capability checking.
# type: "enforce"
# # prefix provides the regex to match the capability before the endpoint.
# prefix: "prefix Here"
# # acceptAllMethod provides a way to have a capability that allows all
# # methods for a specific endpoint.
# acceptAllMethod: "all"
# # endpointBuckets provides regular expressions to use against the request
# # endpoint in order to group requests for a metric label.
# endpointBuckets:
# - "hook\\b"
# - "hooks\\b"
# - "notify\\b"
capability:
# type provides the mode for capability checking.
type: "enforce"
# prefix provides the regex to match the capability before the endpoint.
prefix: "prefix Here"
# acceptAllMethod provides a way to have a capability that allows all
# methods for a specific endpoint.
acceptAllMethod: "all"
# endpointBuckets provides regular expressions to use against the request
# endpoint in order to group requests for a metric label.
endpointBuckets:
- "hook\\b"
- "hooks\\b"
- "notify\\b"

##############################################################################
# Webhooks Related Configuration
##############################################################################
# webhook provides configuration for storing and obtaining webhook
# information using Argus.
# webhook:
# # JWTParserType establishes which parser type will be used by the JWT token
# # acquirer used by Argus. Options include 'simple' and 'raw'.
# # 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: "raw"
# basicClientConfig:
# # listen is the subsection that configures the listening feature of the argus client
# # (Optional)
# # listen:
# # # pullInterval provides how often the current webhooks list gets refreshed.
# # pullInterval: 5s

# # bucket is the partition name where webhooks will be stored.
# bucket: "webhooks"

# # address is Argus' network location.
# address: "http://localhost:6600"

# # auth the authentication method for argus.
# auth:
# # basic configures basic authentication for argus.
# # Must be of form: 'Basic xyz=='
# basic: "Basic dXNlcjpwYXNz"
#
# # jwt configures jwt style authentication for argus.
# JWT:
# # requestHeaders are added to the request for the token.
# # (Optional)
# # requestHeaders:
# # "": ""
#
# # authURL is the URL to access the token.
# authURL: ""
#
# # timeout is how long the request to get the token will take before
# # timing out.
# timeout: "1m"
#
# # buffer is the length of time before a token expires to get a new token.
# buffer: "2m"
webhook:
# JWTParserType establishes which parser type will be used by the JWT token
# acquirer used by Argus. Options include 'simple' and 'raw'.
# 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: "raw"
basicClientConfig:
# listen is the subsection that configures the listening feature of the argus client
# (Optional)
# listen:
# # pullInterval provides how often the current webhooks list gets refreshed.
# pullInterval: 5s

# bucket is the partition name where webhooks will be stored.
bucket: "webhooks"

# address is Argus' network location.
address: "http://localhost:6600"

# auth the authentication method for argus.
auth:
# basic configures basic authentication for argus.
# Must be of form: 'Basic xyz=='
basic: "Basic dXNlcjpwYXNz"

# jwt configures jwt style authentication for argus.
jwt:
# requestHeaders are added to the request for the token.
# (Optional)
# requestHeaders:
# "": ""
# authURL is the URL to access the token.
authUrl: ""
# timeout is how long the request to get the token will take before
# timing out.
timeout: "1m"
# buffer is the length of time before a token expires to get a new token.
buffer: "2m"

########################################
# Delivery Pipeline Related Configuration
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/xmidt-org/arrange/arrangepprof"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/caduceus/internal/client"
"github.com/xmidt-org/caduceus/internal/handler"
"github.com/xmidt-org/caduceus/internal/sink"
"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/clortho"
Expand All @@ -24,6 +25,7 @@ import (
// Config is the top level configuration for the caduceus service. Everything
// is contained in this structure or it will intentially cause a failure.
type Config struct {
Capability handler.CapabilityConfig
Logging sallust.Config
Tracing candlelight.Config
Prometheus touchstone.Config
Expand Down
55 changes: 13 additions & 42 deletions internal/handler/primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,19 @@
// SPDX-License-Identifier: Apache-2.0
package handler

// import (
// "bytes"
// "context"
// "encoding/base64"
// "fmt"
// "net/http"
// "os"
// "os/signal"
// "regexp"
// "syscall"

// "emperror.dev/emperror"

// "github.com/gorilla/mux"
// "github.com/justinas/alice"
// "github.com/spf13/viper"
// "github.com/xmidt-org/bascule"
// "github.com/xmidt-org/bascule/basculechecks"
// "github.com/xmidt-org/bascule/basculehelper"
// "github.com/xmidt-org/bascule/basculehttp"
// "github.com/xmidt-org/caduceus/internal/logging"
// "github.com/xmidt-org/clortho"
// "github.com/xmidt-org/clortho/clorthozap"
// "github.com/xmidt-org/sallust"
// "github.com/xmidt-org/touchstone"

// "go.uber.org/zap"
// )

// const (
// apiVersion = "v4"
// prevAPIVersion = "v3"
// apiBase = "api/" + apiVersion
// apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}"
// )

// type CapabilityConfig struct {
// Type string
// Prefix string
// AcceptAllMethod string
// EndpointBuckets []string
// }
const (
apiVersion = "v4"
prevAPIVersion = "v3"
apiBase = "api/" + apiVersion
apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}"
)

type CapabilityConfig struct {
Type string
Prefix string
AcceptAllMethod string
EndpointBuckets []string
}

// func NewPrimaryHandler(l *zap.Logger, v *viper.Viper, sw *ServerHandler, router *mux.Router, prevVersionSupport bool) (*mux.Router, error) {
// auth, err := authenticationMiddleware(v, l)
Expand Down
18 changes: 11 additions & 7 deletions internal/sink/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ type Matcher interface {
type MatcherV1 struct {
events []*regexp.Regexp
matcher []*regexp.Regexp
CommonMatcher
logger *zap.Logger

mutex sync.RWMutex
}

type MatcherV2 struct {
matcher map[string]*regexp.Regexp
CommonMatcher
}
logger *zap.Logger

type CommonMatcher struct {
logger *zap.Logger
mutex sync.RWMutex
mutex sync.RWMutex
}

// TODO: need to add matching logic for RegistryV2 & MatcherV2
Expand Down Expand Up @@ -69,8 +68,13 @@ func NewMatcher(l ancla.Register, logger *zap.Logger) (Matcher, error) {
// Update applies user configurable values for the outbound sender when a
// webhook is registered
func (m1 *MatcherV1) update(l ancla.RegistryV1) error {

m1.logger = m1.logger.With(zap.String("webhook.address", l.Registration.Address))
if l.Registration.FailureURL != "" {
_, err := url.ParseRequestURI(l.Registration.FailureURL)
if err != nil {
return err
}
}

var events []*regexp.Regexp
for _, event := range l.Registration.Events {
Expand Down
27 changes: 3 additions & 24 deletions internal/sink/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ package sink

import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
"github.com/xmidt-org/ancla"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/webhook-schema"
"github.com/xmidt-org/wrp-go/v3"
"go.uber.org/zap"
Expand All @@ -21,6 +20,7 @@ var (
matcher = &MatcherV1{
events: []*regexp.Regexp{regexp.MustCompile("iot")},
matcher: []*regexp.Regexp{regexp.MustCompile("mac:112233445566")},
logger: sallust.Default(),
}
)

Expand Down Expand Up @@ -139,6 +139,7 @@ func TestUpdate_MatcherV1(t *testing.T) {
registry: ancla.RegistryV1{
Registration: webhook.RegistrationV1{
FailureURL: "localhost.io",
Events: []string{"iot"},
},
},
expectedErr: fmt.Errorf("invalid URI for request"),
Expand Down Expand Up @@ -214,25 +215,3 @@ func TestNewMatcher(t *testing.T) {
})
}
}

func TestClientMock_Do(t *testing.T) {
// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

client := &ClientMock{}
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)

// Test case 1: Successful request
resp, err := client.Do(req)
assert.NoError(t, err)
assert.NotNil(t, resp)

// Test case 2: Error in request
req.URL.Scheme = "invalid"
resp, err = client.Do(req)
assert.Error(t, err)
assert.Nil(t, resp)
}
Loading