Skip to content

Commit

Permalink
Merge pull request #803 from Pix4D/pci-3852-policy-checker-add-logging
Browse files Browse the repository at this point in the history
pci-3852: policy checker handler: add logging
  • Loading branch information
aliculPix4D authored Oct 9, 2024
2 parents 955cdea + fd77cae commit 2e0352b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion atc/api/policychecker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ type policyCheckingHandler struct {
func (h policyCheckingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
acc := accessor.GetAccessor(r)

logger := h.logger.Session("policy-checker")
result, err := h.policyChecker.Check(h.action, acc, r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "policy check error: %s", err.Error())
userMsg := "policy-checker: unreachable or misconfigured"
// error should be logged and meaningful to the Concourse operator/maintainer
logger.Error("policy-checker", err, lager.Data{"user-msg": userMsg})
// actual error is not useful and can be confusing to Concourse user
fmt.Fprint(w, userMsg)
return
}

Expand Down
2 changes: 1 addition & 1 deletion atc/api/policychecker/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("Handler", func() {

msg, err := io.ReadAll(responseWriter.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(msg)).To(Equal("policy check error: some-error"))
Expect(string(msg)).To(Equal("policy-checker: unreachable or misconfigured"))
})

It("not call the inner handler", func() {
Expand Down
8 changes: 4 additions & 4 deletions atc/policy/opa/opa.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ func (c opa) Check(input policy.PolicyCheckInput) (policy.PolicyCheckResult, err
client.Timeout = c.config.Timeout
resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("OPA server: connecting: %w", err)
}
defer resp.Body.Close()

statusCode := resp.StatusCode
if statusCode != http.StatusOK {
return nil, fmt.Errorf("opa returned status: %d", statusCode)
return nil, fmt.Errorf("OPA server returned status: %d", statusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("opa returned no response: %s", err.Error())
return nil, fmt.Errorf("OPA server: reading response body: %s", err.Error())
}

result, err := ParseOpaResult(body, c.config)
if err != nil {
return nil, fmt.Errorf("parsing opa results: %w", err)
return nil, fmt.Errorf("parsing OPA results: %w", err)
}

return result, nil
Expand Down
6 changes: 3 additions & 3 deletions atc/policy/opa/opa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ var _ = Describe("OPA Policy Checker", func() {
It("should return error", func() {
result, err := agent.Check(policy.PolicyCheckInput{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp("connection refused"))
Expect(err.Error()).To(MatchRegexp("OPA server: connecting: .* connection refused"))
Expect(result).To(BeNil())
})
})
Expand All @@ -190,7 +190,7 @@ var _ = Describe("OPA Policy Checker", func() {
It("should return error", func() {
result, err := agent.Check(policy.PolicyCheckInput{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("opa returned status: 404"))
Expect(err.Error()).To(Equal("OPA server returned status: 404"))
Expect(result).To(BeNil())
})
})
Expand All @@ -205,7 +205,7 @@ var _ = Describe("OPA Policy Checker", func() {
It("should return error", func() {
result, err := agent.Check(policy.PolicyCheckInput{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("invalid character 'h' looking for beginning of value"))
Expect(err.Error()).To(ContainSubstring("parsing JSON: invalid character 'h' looking for beginning of value"))
Expect(result).To(BeNil())
})
})
Expand Down
2 changes: 1 addition & 1 deletion atc/policy/opa/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ParseOpaResult(bytesResult []byte, opaConfig OpaConfig) (opaResult, error)
var results vars.StaticVariables
err := json.Unmarshal(bytesResult, &results)
if err != nil {
return opaResult{}, err
return opaResult{}, fmt.Errorf("parsing JSON: %w", err)
}

var allowed, shouldBlock, ok bool
Expand Down

0 comments on commit 2e0352b

Please sign in to comment.