Skip to content

Commit

Permalink
add token path
Browse files Browse the repository at this point in the history
  • Loading branch information
luphaz committed Sep 27, 2023
1 parent d591ed7 commit c50b4c7
Show file tree
Hide file tree
Showing 19 changed files with 5,508 additions and 10 deletions.
3 changes: 3 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ github.com/stretchr/testify,github.com/stretchr/testify/assert,MIT
github.com/stretchr/testify,github.com/stretchr/testify/mock,MIT
github.com/stretchr/testify,github.com/stretchr/testify/require,MIT
github.com/subosito/gotenv,github.com/subosito/gotenv,MIT
github.com/tidwall/gjson,github.com/tidwall/gjson,MIT
github.com/tidwall/match,github.com/tidwall/match,MIT
github.com/tidwall/pretty,github.com/tidwall/pretty,MIT
github.com/tinylib/msgp,github.com/tinylib/msgp/msgp,MIT
github.com/vishvananda/netlink,github.com/vishvananda/netlink,Apache-2.0
github.com/vishvananda/netlink,github.com/vishvananda/netlink/nl,Apache-2.0
Expand Down
1 change: 1 addition & 0 deletions chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data:
filteredReasons: {{ .Values.controller.notifiers.http.filteredReasons | toJson }}
authURL: {{ .Values.controller.notifiers.http.authURL | quote }}
authHeaders: {{ .Values.controller.notifiers.http.authHeaders | toJson }}
authTokenPath: {{ .Values.controller.notifiers.http.authTokenPath | quote }}
datadog:
enabled: {{ .Values.controller.notifiers.datadog.enabled }}
cloudProviders:
Expand Down
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ func New(logger *zap.SugaredLogger, osArgs []string) (config, error) {
return cfg, err
}

// TODO: add newly declared HTTP notifier flags...

mainFS.StringToStringVar(&cfg.Injector.Annotations, "injector-annotations", map[string]string{}, "Annotations added to the generated injector pods")

if err := viper.BindPFlag("injector.annotations", mainFS.Lookup("injector-annotations")); err != nil {
Expand Down
21 changes: 15 additions & 6 deletions eventnotifier/http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"net/http"

"go.uber.org/zap"

"github.com/tidwall/gjson"
)

type BearerAuthTokenProvider interface {
Expand All @@ -22,18 +24,20 @@ type BearerAuthTokenProvider interface {
var _ BearerAuthTokenProvider = bearerAuthTokenProvider{}

type bearerAuthTokenProvider struct {
Logger *zap.SugaredLogger
URL string
Client *http.Client
Headers map[string]string
Logger *zap.SugaredLogger
URL string
Client *http.Client
Headers map[string]string
TokenPath string
}

func NewBearerAuthTokenProvider(logger *zap.SugaredLogger, client *http.Client, url string, headers map[string]string) BearerAuthTokenProvider {
func NewBearerAuthTokenProvider(logger *zap.SugaredLogger, client *http.Client, url string, headers map[string]string, tokenPath string) BearerAuthTokenProvider {
return bearerAuthTokenProvider{
logger,
url,
client,
headers,
tokenPath,
}
}

Expand Down Expand Up @@ -67,5 +71,10 @@ func (b bearerAuthTokenProvider) AuthToken(ctx context.Context) (string, error)
return "", fmt.Errorf("error when reading token: %w", err)
}

return string(tokenBytes), nil
value := gjson.Get(string(tokenBytes), b.TokenPath)
if value.Exists() {
return value.String(), nil
}

return "", fmt.Errorf("auth response body does not contains expected token path %s", b.TokenPath)
}
13 changes: 11 additions & 2 deletions eventnotifier/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
chaostypes "github.com/DataDog/chaos-controller/types"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
coretypes "k8s.io/apimachinery/pkg/types"
)

Expand All @@ -36,6 +37,7 @@ type NotifierHTTPConfig struct {
FilteredReasons []string
AuthURL string
AuthHeaders []string
AuthTokenPath string
}

// Notifier describes a HTTP notifier
Expand Down Expand Up @@ -127,7 +129,7 @@ func New(commonConfig types.NotifiersCommonConfig, httpConfig NotifierHTTPConfig
return nil, fmt.Errorf("notifier http: invalid headers for auth: %w", err)
}

authTokenProvider = NewBearerAuthTokenProvider(logger, client, httpConfig.AuthURL, authHeaders)
authTokenProvider = NewBearerAuthTokenProvider(logger, client, httpConfig.AuthURL, authHeaders, httpConfig.AuthTokenPath)
}

return &Notifier{
Expand Down Expand Up @@ -192,17 +194,24 @@ func (n *Notifier) Notify(dis v1beta1.Disruption, event corev1.Event, notifType
DisruptionPodName: targetInjection.InjectorPodName,
}

// TODO: should we store label before????
if dis.Spec.Level == chaostypes.DisruptionLevelNode {
node := corev1.Node{}
if err := n.common.Client.Get(context.Background(), coretypes.NamespacedName{Namespace: dis.Namespace, Name: targetName}, &node); err != nil {
if apierrors.IsNotFound(err) {
continue
}

return err
}

target.Labels = node.Labels
} else {
pod := corev1.Pod{}
if err := n.common.Client.Get(context.Background(), coretypes.NamespacedName{Namespace: dis.Namespace, Name: targetName}, &pod); err != nil {
if apierrors.IsNotFound(err) {
continue
}

return err
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
github.com/tidwall/gjson v1.17.0
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230420174744-55c8b9515a01
github.com/vishvananda/netns v0.0.5-0.20230405050519-16c2fa0b2f57
go.opentelemetry.io/otel v1.16.0
Expand Down Expand Up @@ -138,6 +139,8 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/tidwall/gjson/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c50b4c7

Please sign in to comment.