Skip to content

Commit

Permalink
Merge pull request #135 from raptorsun/feature/trailing_wildcards
Browse files Browse the repository at this point in the history
allow path patterns in --allow-paths and --ignore-paths
  • Loading branch information
s-urbaniak authored Jul 30, 2021
2 parents 12dbeae + dd334c6 commit 8f1fd65
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
33 changes: 27 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -220,14 +221,31 @@ func main() {
klog.Fatal("Cannot use --allow-paths and --ignore-paths together.")
}

for _, pathAllowed := range cfg.allowPaths {
_, err := path.Match(pathAllowed, "")
if err != nil {
klog.Fatalf("Failed to verify allow path: %s", pathAllowed)
}
}

for _, pathIgnored := range cfg.ignorePaths {
_, err := path.Match(pathIgnored, "")
if err != nil {
klog.Fatalf("Failed to verify ignored path: %s", pathIgnored)
}
}

proxy := httputil.NewSingleHostReverseProxy(upstreamURL)
proxy.Transport = upstreamTransport
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
found := len(cfg.allowPaths) == 0
for _, path := range cfg.allowPaths {
if req.URL.Path == path {
found = true
for _, pathAllowed := range cfg.allowPaths {
found, err = path.Match(pathAllowed, req.URL.Path)
if err != nil {
return
}
if found {
break
}
}
Expand All @@ -237,9 +255,12 @@ func main() {
}

ignorePathFound := false
for _, path := range cfg.ignorePaths {
if req.URL.Path == path {
ignorePathFound = true
for _, pathIgnored := range cfg.ignorePaths {
ignorePathFound, err = path.Match(pathIgnored, req.URL.Path)
if err != nil {
return
}
if ignorePathFound {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/allowpaths/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8081/"
- "--allow-paths=/metrics"
- "--allow-paths=/metrics,/api/v1/label/*/values"
- "--logtostderr=true"
- "--v=10"
ports:
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/basics.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ func testAllowPathsRegexp(s *kubetest.Suite) kubetest.TestSuite {
fmt.Sprintf(command, "/", 404, 404),
nil,
),
ClientSucceeds(
s.KubeClient,
fmt.Sprintf(command, "/api/v1/label/name", 404, 404),
nil,
),
),
}.Run(t)

Expand Down Expand Up @@ -394,6 +399,11 @@ func testAllowPathsRegexp(s *kubetest.Suite) kubetest.TestSuite {
fmt.Sprintf(command, "/metrics", 200, 200),
nil,
),
ClientSucceeds(
s.KubeClient,
fmt.Sprintf(command, "/api/v1/label/job/values", 200, 200),
nil,
),
),
}.Run(t)
}
Expand Down Expand Up @@ -439,6 +449,11 @@ func testIgnorePaths(s *kubetest.Suite) kubetest.TestSuite {
fmt.Sprintf(commandWithoutAuth, "/metrics", 200, 200),
nil,
),
ClientSucceeds(
s.KubeClient,
fmt.Sprintf(commandWithoutAuth, "/api/v1/labels", 200, 200),
nil,
),
),
}.Run(t)

Expand Down Expand Up @@ -478,6 +493,11 @@ func testIgnorePaths(s *kubetest.Suite) kubetest.TestSuite {
fmt.Sprintf(commandWithoutAuth, "/", 401, 401),
nil,
),
ClientSucceeds(
s.KubeClient,
fmt.Sprintf(commandWithoutAuth, "/api/v1/label/job/values", 401, 401),
nil,
),
),
}.Run(t)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ignorepaths/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8081/"
- "--ignore-paths=/metrics"
- "--ignore-paths=/metrics,/api/v1/*"
- "--logtostderr=true"
- "--v=10"
ports:
Expand Down

0 comments on commit 8f1fd65

Please sign in to comment.