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
e5897a8
commit 0770dbb
Showing
12 changed files
with
538 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,7 @@ | ||
package config | ||
|
||
type HookType string | ||
|
||
const ( | ||
External HookType = "external" | ||
) |
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.