|
|
@ -20,6 +20,7 @@ import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
@ -121,14 +122,36 @@ func (o *Opa) IsAllowed(args Args) bool { |
|
|
|
} |
|
|
|
} |
|
|
|
defer o.args.CloseRespFn(resp.Body) |
|
|
|
defer o.args.CloseRespFn(resp.Body) |
|
|
|
|
|
|
|
|
|
|
|
// Handle OPA response
|
|
|
|
// Read the body to be saved later.
|
|
|
|
type opaResponse struct { |
|
|
|
opaRespBytes, err := ioutil.ReadAll(resp.Body) |
|
|
|
Allow bool `json:"allow"` |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
|
|
|
|
var result opaResponse |
|
|
|
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&result); err != nil { |
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result.Allow |
|
|
|
// Handle large OPA responses when OPA URL is of
|
|
|
|
|
|
|
|
// form http://localhost:8181/v1/data/httpapi/authz
|
|
|
|
|
|
|
|
type opaResultAllow struct { |
|
|
|
|
|
|
|
Result struct { |
|
|
|
|
|
|
|
Allow bool `json:"allow"` |
|
|
|
|
|
|
|
} `json:"result"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle simpler OPA responses when OPA URL is of
|
|
|
|
|
|
|
|
// form http://localhost:8181/v1/data/httpapi/authz/allow
|
|
|
|
|
|
|
|
type opaResult struct { |
|
|
|
|
|
|
|
Result bool `json:"result"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
respBody := bytes.NewReader(opaRespBytes) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result opaResult |
|
|
|
|
|
|
|
if err = json.NewDecoder(respBody).Decode(&result); err != nil { |
|
|
|
|
|
|
|
respBody.Seek(0, 0) |
|
|
|
|
|
|
|
var resultAllow opaResultAllow |
|
|
|
|
|
|
|
if err = json.NewDecoder(respBody).Decode(&resultAllow); err != nil { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return resultAllow.Result.Allow |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result.Result |
|
|
|
} |
|
|
|
} |
|
|
|