2018-10-09 17:00:01 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-10-09 17:00:01 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package iampolicy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-11-21 22:01:05 -05:00
|
|
|
"io"
|
2019-06-10 20:06:32 -04:00
|
|
|
"io/ioutil"
|
2018-10-09 17:00:01 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpaArgs opa general purpose policy engine configuration.
|
|
|
|
type OpaArgs struct {
|
2018-11-21 22:01:05 -05:00
|
|
|
URL *xnet.URL `json:"url"`
|
|
|
|
AuthToken string `json:"authToken"`
|
|
|
|
Transport http.RoundTripper `json:"-"`
|
|
|
|
CloseRespFn func(r io.ReadCloser) `json:"-"`
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate - validate opa configuration params.
|
|
|
|
func (a *OpaArgs) Validate() error {
|
2019-07-27 23:03:25 -04:00
|
|
|
req, err := http.NewRequest("POST", a.URL.String(), bytes.NewReader([]byte("")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if a.AuthToken != "" {
|
|
|
|
req.Header.Set("Authorization", a.AuthToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{Transport: a.Transport}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer a.CloseRespFn(resp.Body)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - decodes JSON data.
|
|
|
|
func (a *OpaArgs) UnmarshalJSON(data []byte) error {
|
|
|
|
// subtype to avoid recursive call to UnmarshalJSON()
|
|
|
|
type subOpaArgs OpaArgs
|
|
|
|
var so subOpaArgs
|
|
|
|
|
2019-07-29 18:58:25 -04:00
|
|
|
if err := json.Unmarshal(data, &so); err != nil {
|
|
|
|
return err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
oa := OpaArgs(so)
|
|
|
|
if oa.URL == nil || oa.URL.String() == "" {
|
|
|
|
*a = oa
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*a = oa
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opa - implements opa policy agent calls.
|
|
|
|
type Opa struct {
|
2018-11-21 22:01:05 -05:00
|
|
|
args OpaArgs
|
|
|
|
client *http.Client
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOpa - initializes opa policy engine connector.
|
|
|
|
func NewOpa(args OpaArgs) *Opa {
|
|
|
|
// No opa args.
|
|
|
|
if args.URL == nil && args.AuthToken == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Opa{
|
2018-11-21 22:01:05 -05:00
|
|
|
args: args,
|
|
|
|
client: &http.Client{Transport: args.Transport},
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAllowed - checks given policy args is allowed to continue the REST API.
|
2019-07-27 23:03:25 -04:00
|
|
|
func (o *Opa) IsAllowed(args Args) (bool, error) {
|
2018-10-09 17:00:01 -04:00
|
|
|
if o == nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OPA input
|
|
|
|
body := make(map[string]interface{})
|
|
|
|
body["input"] = args
|
|
|
|
|
|
|
|
inputBytes, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", o.args.URL.String(), bytes.NewReader(inputBytes))
|
|
|
|
if err != nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if o.args.AuthToken != "" {
|
|
|
|
req.Header.Set("Authorization", o.args.AuthToken)
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:01:05 -05:00
|
|
|
resp, err := o.client.Do(req)
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
2018-11-21 22:01:05 -05:00
|
|
|
defer o.args.CloseRespFn(resp.Body)
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-06-10 20:06:32 -04:00
|
|
|
// Read the body to be saved later.
|
|
|
|
opaRespBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:06:32 -04:00
|
|
|
// 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 {
|
2019-07-27 23:03:25 -04:00
|
|
|
return false, err
|
2019-06-10 20:06:32 -04:00
|
|
|
}
|
2019-07-27 23:03:25 -04:00
|
|
|
return resultAllow.Result.Allow, nil
|
2019-06-10 20:06:32 -04:00
|
|
|
}
|
2019-07-27 23:03:25 -04:00
|
|
|
return result.Result, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|