Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Use public URIs in policy service response
Browse files Browse the repository at this point in the history
  • Loading branch information
birkland committed Apr 24, 2019
1 parent 796dbef commit 9b2631d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/pass-policy-service/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion web/policy_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
13 changes: 12 additions & 1 deletion web/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"net/http"
"strings"

"github.com/oa-pass/pass-policy-service/rule"
"github.com/pkg/errors"
Expand All @@ -10,14 +11,14 @@ 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) {

service = PolicyService{Fetcher: fetcher}
service.Rules, err = rule.Validate(rulesDoc)
if err != nil {

return service, errors.Wrapf(err, "could not validate rules dsl")
}

Expand All @@ -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
}

0 comments on commit 9b2631d

Please sign in to comment.