2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
package opa
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-11-21 22:01:05 -05:00
|
|
|
"io"
|
2018-10-09 17:00:01 -04:00
|
|
|
"net/http"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/config"
|
2024-05-24 19:05:23 -04:00
|
|
|
"github.com/minio/pkg/v3/env"
|
|
|
|
xnet "github.com/minio/pkg/v3/net"
|
|
|
|
"github.com/minio/pkg/v3/policy"
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2019-10-07 01:50:24 -04:00
|
|
|
// Env IAM OPA URL
|
|
|
|
const (
|
2019-10-23 01:59:13 -04:00
|
|
|
URL = "url"
|
|
|
|
AuthToken = "auth_token"
|
|
|
|
|
|
|
|
EnvPolicyOpaURL = "MINIO_POLICY_OPA_URL"
|
|
|
|
EnvPolicyOpaAuthToken = "MINIO_POLICY_OPA_AUTH_TOKEN"
|
2019-10-07 01:50:24 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// DefaultKVS - default config for OPA config
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
2024-07-10 16:16:44 -04:00
|
|
|
Key: URL,
|
|
|
|
Value: "",
|
|
|
|
HiddenIfEmpty: true,
|
2019-11-20 18:10:24 -05:00
|
|
|
},
|
|
|
|
config.KV{
|
2024-07-10 16:16:44 -04:00
|
|
|
Key: AuthToken,
|
|
|
|
Value: "",
|
|
|
|
HiddenIfEmpty: true,
|
2019-11-20 18:10:24 -05:00
|
|
|
},
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Args opa general purpose policy engine configuration.
|
|
|
|
type Args 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.
|
2019-10-23 01:59:13 -04:00
|
|
|
func (a *Args) Validate() error {
|
|
|
|
req, err := http.NewRequest(http.MethodPost, a.URL.String(), bytes.NewReader([]byte("")))
|
2019-07-27 23:03:25 -04:00
|
|
|
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.
|
2019-10-23 01:59:13 -04:00
|
|
|
func (a *Args) UnmarshalJSON(data []byte) error {
|
2018-10-09 17:00:01 -04:00
|
|
|
// subtype to avoid recursive call to UnmarshalJSON()
|
2019-10-23 01:59:13 -04:00
|
|
|
type subArgs Args
|
|
|
|
var so subArgs
|
2018-10-09 17:00:01 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
oa := Args(so)
|
2018-10-09 17:00:01 -04:00
|
|
|
if oa.URL == nil || oa.URL.String() == "" {
|
|
|
|
*a = oa
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*a = oa
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opa - implements opa policy agent calls.
|
|
|
|
type Opa struct {
|
2019-10-23 01:59:13 -04:00
|
|
|
args Args
|
2018-11-21 22:01:05 -05:00
|
|
|
client *http.Client
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
// Enabled returns if opa is enabled.
|
|
|
|
func Enabled(kvs config.KVS) bool {
|
|
|
|
return kvs.Get(URL) != ""
|
|
|
|
}
|
|
|
|
|
2019-10-07 01:50:24 -04:00
|
|
|
// LookupConfig lookup Opa from config, override with any ENVs.
|
2019-10-23 01:59:13 -04:00
|
|
|
func LookupConfig(kv config.KVS, transport *http.Transport, closeRespFn func(io.ReadCloser)) (Args, error) {
|
|
|
|
args := Args{}
|
|
|
|
|
|
|
|
if err := config.CheckValidKeys(config.PolicyOPASubSys, kv, DefaultKVS); err != nil {
|
|
|
|
return args, err
|
2019-10-07 01:50:24 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
opaURL := env.Get(EnvIamOpaURL, "")
|
2019-10-07 01:50:24 -04:00
|
|
|
if opaURL == "" {
|
2019-10-23 01:59:13 -04:00
|
|
|
opaURL = env.Get(EnvPolicyOpaURL, kv.Get(URL))
|
|
|
|
if opaURL == "" {
|
|
|
|
return args, nil
|
|
|
|
}
|
2019-10-07 01:50:24 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
authToken := env.Get(EnvIamOpaAuthToken, "")
|
|
|
|
if authToken == "" {
|
|
|
|
authToken = env.Get(EnvPolicyOpaAuthToken, kv.Get(AuthToken))
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
u, err := xnet.ParseHTTPURL(opaURL)
|
2019-10-07 01:50:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return args, err
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
args = Args{
|
2019-10-07 01:50:24 -04:00
|
|
|
URL: u,
|
2019-10-23 01:59:13 -04:00
|
|
|
AuthToken: authToken,
|
2019-10-07 01:50:24 -04:00
|
|
|
Transport: transport,
|
|
|
|
CloseRespFn: closeRespFn,
|
|
|
|
}
|
|
|
|
if err = args.Validate(); err != nil {
|
|
|
|
return args, err
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// New - initializes opa policy engine connector.
|
|
|
|
func New(args Args) *Opa {
|
2018-10-09 17:00:01 -04:00
|
|
|
// No opa args.
|
2019-10-07 01:50:24 -04:00
|
|
|
if args.URL == nil || args.URL.Scheme == "" && args.AuthToken == "" {
|
2018-10-09 17:00:01 -04:00
|
|
|
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.
|
2023-09-14 17:50:16 -04:00
|
|
|
func (o *Opa) IsAllowed(args policy.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
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
req, err := http.NewRequest(http.MethodPost, o.args.URL.String(), bytes.NewReader(inputBytes))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-09-19 14:05:16 -04:00
|
|
|
opaRespBytes, err := io.ReadAll(resp.Body)
|
2019-06-10 20:06:32 -04:00
|
|
|
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
|
|
|
}
|
2020-02-04 21:20:39 -05:00
|
|
|
|
2019-07-27 23:03:25 -04:00
|
|
|
return result.Result, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|