forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Palesandro
committed
Nov 9, 2023
1 parent
e41a28b
commit a333e96
Showing
16 changed files
with
957 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package config | ||
|
||
type WebhookConfig struct { | ||
URL string `json:"url"` | ||
InsecureSkipVerify bool `json:"insecureSkipVerify"` | ||
TLSRootCAFile string `json:"tlsRootCAFile"` | ||
ClientAuthentication *ClientAuthentication `json:"clientAuthentication"` | ||
} | ||
|
||
type ClientAuthentication struct { | ||
ClientCertificateFile string `json:"clientCertificateFile"` | ||
ClientKeyFile string `json:"clientKeyFile"` | ||
ClientCAFile string `json:"clientCAFile"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package config | ||
|
||
// HookRequestScope is the context of the request | ||
type HookRequestScope struct { | ||
// Headers is the headers of the request | ||
Headers []string `json:"headers"` | ||
// Params is the params of the request | ||
Params []string `json:"params"` | ||
} | ||
|
||
type ConnectorFilterHook struct { | ||
// Name is the name of the webhook | ||
Name string `json:"name"` | ||
// To be modified to enum? | ||
Type HookType `json:"type"` | ||
Check failure on line 15 in pkg/webhook/config/connectortypes.go GitHub Actions / Test
Check failure on line 15 in pkg/webhook/config/connectortypes.go GitHub Actions / Lint
Check failure on line 15 in pkg/webhook/config/connectortypes.go GitHub Actions / Lint
Check failure on line 15 in pkg/webhook/config/connectortypes.go GitHub Actions / Lint
|
||
// RequestScope is the context of the request | ||
RequestScope *HookRequestScope `json:"requestContext"` | ||
// Config is the configuration of the webhook | ||
Config *WebhookConfig `json:"config"` | ||
} | ||
|
||
type ConnectorFilterHooks struct { | ||
FilterHooks []*ConnectorFilterHook `json:"filterHooks"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//go:generate go run -mod mod go.uber.org/mock/mockgen -destination=./mocks/mock_caller.go -package=connectors --source=types.go FilterCaller | ||
package connectors | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/dexidp/dex/pkg/webhook/config" | ||
"github.com/dexidp/dex/pkg/webhook/helpers" | ||
"github.com/dexidp/dex/storage" | ||
) | ||
|
||
func NewConnectorFilter(hook *config.ConnectorFilterHook) (*ConnectorFilterHook, error) { | ||
var hookInvoker FilterCaller | ||
switch hook.Type { | ||
case config.External: | ||
h, err := helpers.NewWebhookHTTPHelpers(hook.Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create webhook http helpers: %w", err) | ||
} | ||
hookInvoker = NewFilterCaller(h, hook.RequestScope) | ||
default: | ||
return nil, fmt.Errorf("unknown type: %s", hook.Type) | ||
} | ||
return &ConnectorFilterHook{ | ||
Name: hook.Name, | ||
FilterInvoker: hookInvoker, | ||
}, nil | ||
} | ||
|
||
func (f WebhookFilterCaller) callHook(connectors []ConnectorContext, req RequestContext) ([]ConnectorContext, error) { | ||
toMarshal := FilterWebhookPayload{ | ||
Connectors: connectors, | ||
Request: req, | ||
} | ||
|
||
payload, err := json.Marshal(toMarshal) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not serialize claims: %w", err) | ||
} | ||
|
||
body, err := f.transportHelper.CallWebhook(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not call webhook: %w", err) | ||
} | ||
var res []ConnectorContext | ||
|
||
if err := json.Unmarshal(body, &res); err != nil { | ||
return nil, fmt.Errorf("could not unmarshal response: %w", err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func NewFilterCaller(h helpers.WebhookHTTPHelpers, | ||
context *config.HookRequestScope, | ||
) *WebhookFilterCaller { | ||
return &WebhookFilterCaller{ | ||
RequestScope: context, | ||
transportHelper: h, | ||
} | ||
} | ||
|
||
func (f WebhookFilterCaller) CallHook(connectors []storage.Connector, | ||
r *http.Request, | ||
) ([]storage.Connector, error) { | ||
payload := createConnectorWebhookPayload(f.RequestScope, connectors, r) | ||
filteredConnectors, err := f.callHook(payload.Connectors, payload.Request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return unwrapConnectorWebhookPayload(filteredConnectors, connectors), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package connectors | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
|
||
"github.com/dexidp/dex/pkg/webhook/config" | ||
"github.com/dexidp/dex/pkg/webhook/helpers" | ||
"github.com/dexidp/dex/storage" | ||
) | ||
|
||
func TestNewConnectorFilter(t *testing.T) { | ||
d, err := NewConnectorFilter(&config.ConnectorFilterHook{ | ||
Name: "test", | ||
Type: config.External, | ||
RequestScope: &config.HookRequestScope{ | ||
Headers: []string{"header1", "header2"}, | ||
Params: []string{"param1", "param2"}, | ||
}, | ||
Config: &config.WebhookConfig{ | ||
URL: "http://test.com", | ||
InsecureSkipVerify: true, | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, d) | ||
assert.Equal(t, d.Name, "test") | ||
assert.IsType(t, d.FilterInvoker, &WebhookFilterCaller{}) | ||
} | ||
|
||
func TestNewConnectorFilter_UnknownType(t *testing.T) { | ||
d, err := NewConnectorFilter(&config.ConnectorFilterHook{ | ||
Name: "test", | ||
Type: "unknown", | ||
RequestScope: &config.HookRequestScope{ | ||
Headers: []string{"header1", "header2"}, | ||
Params: []string{"param1", "param2"}, | ||
}, | ||
Config: &config.WebhookConfig{ | ||
URL: "http://test.com", | ||
InsecureSkipVerify: true, | ||
}, | ||
}) | ||
assert.Error(t, err) | ||
assert.Nil(t, d) | ||
} | ||
|
||
func TestNewFilterCaller(t *testing.T) { | ||
h, err := helpers.NewWebhookHTTPHelpers(&config.WebhookConfig{ | ||
URL: "http://test.com", | ||
InsecureSkipVerify: true, | ||
}) | ||
assert.NoError(t, err) | ||
d := NewFilterCaller(h, &config.HookRequestScope{ | ||
Headers: []string{"header1", "header2"}, | ||
Params: []string{"param1", "param2"}, | ||
}) | ||
assert.NotNil(t, d) | ||
assert.Equal(t, h, d.transportHelper) | ||
assert.Equal(t, d.RequestScope.Headers, []string{"header1", "header2"}) | ||
assert.Equal(t, d.RequestScope.Params, []string{"param1", "param2"}) | ||
} | ||
|
||
func TestCallHook_Logic(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
h := helpers.NewMockWebhookHTTPHelpers(ctrl) | ||
h.EXPECT().CallWebhook(gomock.Any()).Return([]byte(`[{"id": "test", "type": "test", "name": "test"}]`), nil) | ||
d := NewFilterCaller(h, &config.HookRequestScope{ | ||
Headers: []string{"header1", "header2"}, | ||
Params: []string{"param1", "param2"}, | ||
}) | ||
connectorList, err := d.CallHook([]storage.Connector{ | ||
{ | ||
ID: "test", | ||
Type: "test", | ||
Name: "test", | ||
}, | ||
}, &http.Request{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, connectorList, []storage.Connector{ | ||
{ | ||
ID: "test", | ||
Type: "test", | ||
Name: "test", | ||
}, | ||
}) | ||
} | ||
|
||
func TestCallHook_Logic_Error(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
h := helpers.NewMockWebhookHTTPHelpers(ctrl) | ||
h.EXPECT().CallWebhook(gomock.Any()).Return(nil, assert.AnError) | ||
d := NewFilterCaller(h, &config.HookRequestScope{ | ||
Headers: []string{"header1", "header2"}, | ||
Params: []string{"param1", "param2"}, | ||
}) | ||
connectorList, err := d.CallHook([]storage.Connector{ | ||
{ | ||
ID: "test", | ||
Type: "test", | ||
Name: "test", | ||
}, | ||
}, &http.Request{}) | ||
assert.Error(t, err) | ||
assert.Nil(t, connectorList) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package connectors | ||
|
||
import ( | ||
"net/http" | ||
|
||
"golang.org/x/exp/slices" | ||
|
||
"github.com/dexidp/dex/pkg/webhook/config" | ||
"github.com/dexidp/dex/storage" | ||
) | ||
|
||
func createConnectorWebhookPayload(requestScope *config.HookRequestScope, connectors []storage.Connector, | ||
r *http.Request, | ||
) *FilterWebhookPayload { | ||
payload := &FilterWebhookPayload{ | ||
Connectors: []ConnectorContext{}, | ||
Request: RequestContext{}, | ||
} | ||
for _, c := range connectors { | ||
payload.Connectors = append(payload.Connectors, ConnectorContext{ | ||
ID: c.ID, | ||
Type: c.Type, | ||
Name: c.Name, | ||
}) | ||
} | ||
payload.Request.Params = make(map[string][]string) | ||
if r != nil && r.URL != nil { | ||
for k, v := range r.URL.Query() { | ||
if slices.Contains(requestScope.Params, k) { | ||
payload.Request.Params[k] = v | ||
} | ||
} | ||
} | ||
payload.Request.Headers = make(map[string][]string) | ||
for k, v := range r.Header { | ||
if slices.Contains(requestScope.Headers, k) { | ||
payload.Request.Headers[k] = v | ||
} | ||
} | ||
return payload | ||
} | ||
|
||
func unwrapConnectorWebhookPayload(filteredConnectors []ConnectorContext, | ||
initialConnectors []storage.Connector, | ||
) []storage.Connector { | ||
mappedConnectors := make([]storage.Connector, 0) | ||
for _, filteredConnector := range filteredConnectors { | ||
for _, initialConnector := range initialConnectors { | ||
if filteredConnector.ID == initialConnector.ID { | ||
mappedConnectors = append(mappedConnectors, initialConnector) | ||
} | ||
} | ||
} | ||
return mappedConnectors | ||
} |
Oops, something went wrong.