From 9b2631d79dca4c1fb3cd2397ac3fb099ba86eb66 Mon Sep 17 00:00:00 2001 From: Aaron Birkland Date: Tue, 23 Apr 2019 21:56:14 -0400 Subject: [PATCH] Use public URIs in policy service response --- cmd/pass-policy-service/serve.go | 4 ++++ web/policy_endpoint.go | 2 +- web/service.go | 13 ++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/pass-policy-service/serve.go b/cmd/pass-policy-service/serve.go index 17bf938..948fe59 100644 --- a/cmd/pass-policy-service/serve.go +++ b/cmd/pass-policy-service/serve.go @@ -97,6 +97,10 @@ func serveAction(opts serveOpts, args []string) error { return errors.Wrapf(err, "could not initialize policy service") } + policyService.Replace = map[string]string{ + opts.privateBaseURI: opts.publicBaseURI, + } + http.HandleFunc("/policies", policyService.RequestPolicies) listener, err := net.Listen("tcp", fmt.Sprintf(":%d", opts.port)) diff --git a/web/policy_endpoint.go b/web/policy_endpoint.go index df0ae04..ed1dcbb 100644 --- a/web/policy_endpoint.go +++ b/web/policy_endpoint.go @@ -43,7 +43,7 @@ func (p *policyEndpoint) sendPolicies(w http.ResponseWriter, r *http.Request, po var results []PolicyResult for _, policy := range policies { results = append(results, PolicyResult{ - ID: policy.ID, + ID: p.replace(policy.ID), Type: policy.Type, }) } diff --git a/web/service.go b/web/service.go index 0189787..7b6c1a7 100644 --- a/web/service.go +++ b/web/service.go @@ -2,6 +2,7 @@ package web import ( "net/http" + "strings" "github.com/oa-pass/pass-policy-service/rule" "github.com/pkg/errors" @@ -10,6 +11,7 @@ import ( type PolicyService struct { Rules rule.PolicyResolver Fetcher rule.PassEntityFetcher + Replace map[string]string // URI prefixes and their replacements } func NewPolicyService(rulesDoc []byte, fetcher rule.PassEntityFetcher) (service PolicyService, err error) { @@ -17,7 +19,6 @@ func NewPolicyService(rulesDoc []byte, fetcher rule.PassEntityFetcher) (service service = PolicyService{Fetcher: fetcher} service.Rules, err = rule.Validate(rulesDoc) if err != nil { - return service, errors.Wrapf(err, "could not validate rules dsl") } @@ -39,3 +40,13 @@ func (s *PolicyService) RequestPolicies(w http.ResponseWriter, r *http.Request) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } } + +func (s *PolicyService) replace(uri string) string { + for prefix, replacement := range s.Replace { + if strings.HasSuffix(uri, prefix) { + return strings.Replace(uri, prefix, replacement, 1) + } + } + + return uri +}