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

Praserx custom http method #143

Closed
Closed
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
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ func (r *Config) isValid() error {
}
}
}

// check: ensure each of the resource are valid
for _, resource := range r.Resources {
if err := resource.valid(); err != nil {
if err := resource.valid(r.CustomHTTPMethods); err != nil {
return err
}
}
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ type Config struct {
RequestIDHeader string `json:"request-id-header" yaml:"request-id-header" usage:"the http header name for request id" env:"REQUEST_ID_HEADER"`
// ResponseHeader is a map of response headers to add to the response
ResponseHeaders map[string]string `json:"response-headers" yaml:"response-headers" usage:"custom headers to added to the http response key=value"`
// CustomHTTPMethods is a list of additional non-standard http methods. If additional method is required it has to explicitly allowed at resource allowed method definition.
CustomHTTPMethods []string `json:"custom-http-methods" yaml:"custom-http-methods" usage:"list of additional non-standard http methods" env:"CUSTOM_HTTP_METHODS"`

// EnableSelfSignedTLS indicates we should create a self-signed ceritificate for the service
EnabledSelfSignedTLS bool `json:"enable-self-signed-tls" yaml:"enable-self-signed-tls" usage:"create self signed certificates for the proxy" env:"ENABLE_SELF_SIGNED_TLS"`
Expand Down
4 changes: 2 additions & 2 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *Resource) parse(resource string) (*Resource, error) {
}

// valid ensure the resource is valid
func (r *Resource) valid() error {
func (r *Resource) valid(customHTTPMethods []string) error {
if r.Methods == nil {
r.Methods = make([]string, 0)
}
Expand All @@ -96,7 +96,7 @@ func (r *Resource) valid() error {
}
// step: check the method is valid
for _, m := range r.Methods {
if !isValidHTTPMethod(m) {
if !isValidHTTPMethod(m) && !isValidCustomHTTPMethod(m, customHTTPMethods) {
return fmt.Errorf("invalid method %s", m)
}
}
Expand Down
16 changes: 13 additions & 3 deletions resource_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !e2e
// +build !e2e

/*
Expand Down Expand Up @@ -96,8 +97,9 @@ func TestResourceParseOk(t *testing.T) {

func TestIsValid(t *testing.T) {
testCases := []struct {
Resource *Resource
Ok bool
Resource *Resource
CustomHTTPMethods []string
Ok bool
}{
{
Resource: &Resource{URL: "/test"},
Expand Down Expand Up @@ -125,10 +127,18 @@ func TestIsValid(t *testing.T) {
Methods: []string{"NO_SUCH_METHOD"},
},
},
{
Resource: &Resource{
URL: "/test",
Methods: []string{"PROPFIND"},
},
CustomHTTPMethods: []string{"PROPFIND"},
Ok: true,
},
}

for i, c := range testCases {
err := c.Resource.valid()
err := c.Resource.valid(c.CustomHTTPMethods)
if err != nil && c.Ok {
t.Errorf("case %d should not have failed, error: %s", i, err)
}
Expand Down
8 changes: 8 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ func (r *oauthProxy) createReverseProxy() error {
if err := r.createTemplates(); err != nil {
return err
}

// step: add custom http methods
if r.config.CustomHTTPMethods != nil {
for _, customHTTPMethod := range r.config.CustomHTTPMethods {
chi.RegisterMethod(customHTTPMethod)
}
}

// step: provision in the protected resources
enableDefaultDeny := r.config.EnableDefaultDeny
for _, x := range r.config.Resources {
Expand Down
52 changes: 52 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !e2e
// +build !e2e

/*
Expand Down Expand Up @@ -1032,3 +1033,54 @@ func TestTLS(t *testing.T) {
)
}
}

func TestCustomHTTPMethod(t *testing.T) {
config := newFakeKeycloakConfig()
config.EnableDefaultDeny = true
config.CustomHTTPMethods = []string{"PROPFIND"} // WebDav method
config.Resources = []*Resource{
{
URL: "/public/*",
Methods: allHTTPMethods,
WhiteListed: true,
},
{
URL: "/api/*",
Methods: []string{http.MethodGet, http.MethodPost, http.MethodPut},
},
{
URL: "/webdav/*",
Methods: []string{"PROPFIND"},
},
}
requests := []fakeRequest{
{
URI: "/public/allowed",
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
{
Method: "PROPFIND",
URI: "/public/allowed",
// FIXME: It should return 405, see https://github.com/gogatekeeper/gatekeeper/issues/142
ExpectedCode: http.StatusUnauthorized,
},
{
Method: "PROPFIND",
URI: "/api/test",
HasLogin: true,
Redirects: true,
// FIXME: It should return 405, see https://github.com/gogatekeeper/gatekeeper/issues/142
ExpectedCode: http.StatusUnauthorized,
},
{
Method: "PROPFIND",
URI: "/webdav/test",
HasLogin: true,
Redirects: true,
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
}
newFakeProxy(config, &fakeAuthConfig{}).RunTests(t, requests)
}
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ func isValidHTTPMethod(method string) bool {
return false
}

// isValidCustomHTTPMethod ensure this is a valid custom http method type
func isValidCustomHTTPMethod(method string, customHTTPMethods []string) bool {
for _, x := range customHTTPMethods {
if method == x {
return true
}
}

return false
}

// defaultTo returns the value of the default
func defaultTo(v, d string) string {
if v != "" {
Expand Down