Skip to content

Commit

Permalink
Fix no auth
Browse files Browse the repository at this point in the history
Related to #8220
  • Loading branch information
jrouzierinverse committed Jul 29, 2024
1 parent 8b3a1e8 commit 04ef891
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
22 changes: 11 additions & 11 deletions go/plugin/caddy2/api-aaa/api-aaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ type PrettyTokenInfo struct {
}

type ApiAAAHandler struct {
router *httprouter.Router
systemBackend *aaa.MemAuthenticationBackend
webservicesBackend *aaa.MemAuthenticationBackend
authentication *aaa.TokenAuthenticationMiddleware
authorization *aaa.TokenAuthorizationMiddleware
noAuthPaths map[string]bool
tokenBackend []string
router *httprouter.Router `json:"-"`
systemBackend *aaa.MemAuthenticationBackend `json:"-"`
webservicesBackend *aaa.MemAuthenticationBackend `json:"-"`
authentication *aaa.TokenAuthenticationMiddleware `json:"-"`
authorization *aaa.TokenAuthorizationMiddleware `json:"-"`
NoAuthPaths map[string]bool `json:"no_auth_paths"`
TokenBackend []string `json:"token_backend"`
}

// Setup the api-aaa middleware
Expand Down Expand Up @@ -94,8 +94,8 @@ func (s *ApiAAAHandler) UnmarshalCaddyfile(c *caddyfile.Dispenser) error {
}
}

s.noAuthPaths = noAuthPaths
s.tokenBackend = tokenBackendArgs
s.NoAuthPaths = noAuthPaths
s.TokenBackend = tokenBackendArgs
return nil
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func (h *ApiAAAHandler) buildApiAAAHandler(ctx context.Context) error {
u.AddStruct(ctx, "PfConfServicesURL", servicesURL)
})

tokenBackend := aaa.MakeTokenBackend(ctx, h.tokenBackend)
tokenBackend := aaa.MakeTokenBackend(ctx, h.TokenBackend)
h.authentication = aaa.NewTokenAuthenticationMiddleware(tokenBackend)

// Backend for the system Unified API user
Expand Down Expand Up @@ -384,7 +384,7 @@ func (h *ApiAAAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
// TODO change me and wrap actions into something that handles server errors
return nil
} else {
_, noauth := h.noAuthPaths[r.URL.Path]
_, noauth := h.NoAuthPaths[r.URL.Path]
if noauth || h.HandleAAA(w, r) {
return next.ServeHTTP(w, r)
}
Expand Down
26 changes: 26 additions & 0 deletions go/plugin/caddy2/api-aaa/api-aaa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"testing"

"github.com/caddyserver/caddy/v2/caddytest"
"github.com/inverse-inc/go-utils/log"
"github.com/inverse-inc/go-utils/sharedutils"
"github.com/inverse-inc/packetfence/go/pfconfigdriver"
Expand Down Expand Up @@ -144,3 +145,28 @@ func TestApiAAAContentType(t *testing.T) {
}

}

func TestRespond(t *testing.T) {
// arrange
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
http_port 9080
https_port 9443
grace_period 1ns
}
localhost:9080 {
route * {
api-aaa {
no_auth /api/v1/pfconnector/tunnel
}
}
}
`, "caddyfile")

// act and assert
tester.AssertGetResponse("http://localhost:9080/api/v1/pfconnector/tunnel", 200, "")

}

0 comments on commit 04ef891

Please sign in to comment.