Skip to content

Commit

Permalink
Merge pull request #45 from permitio/roe/fix-http-response-error-hand…
Browse files Browse the repository at this point in the history
…ling

Fix having no response data in http response errors
  • Loading branch information
roekatz authored Aug 23, 2024
2 parents 4b8ad56 + 93ba333 commit 581774c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/enforcement/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,36 @@ func (e *PermitEnforcer) getCheckEndpoint() string {

func (e *PermitEnforcer) parseResponse(res *http.Response) (*CheckResponse, error) {
var result CheckResponse
err := errors.HttpErrorHandle(nil, res)
if err != nil {
responseBodyZap := zap.String("response_body", "")
if permitErr, ok := err.(errors.PermitError); ok {
responseBodyZap = zap.String("response_body", permitErr.ResponseBody)
}
e.logger.Error("erroneous http response from PDP for Permit.Check()", zap.Error(err), responseBodyZap)
return nil, err
}

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
permitError := errors.NewPermitUnexpectedError(err, res)
permitError := errors.NewPermitUnexpectedError(err, nil)
e.logger.Error("error reading Permit.Check() response from PDP", zap.Error(permitError))
return nil, permitError
}
err = errors.HttpErrorHandle(err, res)
if err != nil {
e.logger.Error(string(bodyBytes), zap.Error(err))
return nil, err
}

if e.config.GetOpaUrl() != "" {
opaStruct := &struct {
Result *CheckResponse `json:"result"`
}{&result}

if err := json.Unmarshal(bodyBytes, opaStruct); err != nil {
permitError := errors.NewPermitUnexpectedError(err, res)
permitError := errors.NewPermitUnexpectedError(err, nil)
e.logger.Error("error unmarshalling Permit.Check() response from OPA", zap.Error(permitError))
return nil, err
}
} else {
if err := json.Unmarshal(bodyBytes, &result); err != nil {
permitError := errors.NewPermitUnexpectedError(err, res)
permitError := errors.NewPermitUnexpectedError(err, nil)
e.logger.Error("error unmarshalling Permit.Check response from PDP", zap.Error(permitError))
return nil, permitError
}
Expand Down

0 comments on commit 581774c

Please sign in to comment.