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-16 15:48:19 -04:00
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
2020-03-20 18:00:44 -04:00
|
|
|
"context"
|
2018-10-16 15:48:19 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-04-07 22:30:59 -04:00
|
|
|
|
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
2018-10-16 15:48:19 -04:00
|
|
|
)
|
|
|
|
|
2019-09-26 14:23:13 -04:00
|
|
|
// InfoCannedPolicy - expand canned policy into JSON structure.
|
2020-04-07 22:30:59 -04:00
|
|
|
func (adm *AdminClient) InfoCannedPolicy(ctx context.Context, policyName string) (*iampolicy.Policy, error) {
|
2019-09-26 14:23:13 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("name", policyName)
|
|
|
|
|
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/info-canned-policy",
|
2019-09-26 14:23:13 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// Execute GET on /minio/admin/v3/info-canned-policy
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
|
2019-09-26 14:23:13 -04:00
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
return iampolicy.ParseConfig(resp.Body)
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
// ListCannedPolicies - list all configured canned policies.
|
2020-04-07 22:30:59 -04:00
|
|
|
func (adm *AdminClient) ListCannedPolicies(ctx context.Context) (map[string]*iampolicy.Policy, error) {
|
2018-10-16 15:48:19 -04:00
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/list-canned-policies",
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// Execute GET on /minio/admin/v3/list-canned-policies
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
var policies = make(map[string]*iampolicy.Policy)
|
2018-10-16 15:48:19 -04:00
|
|
|
if err = json.Unmarshal(respBytes, &policies); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return policies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCannedPolicy - remove a policy for a canned.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) RemoveCannedPolicy(ctx context.Context, policyName string) error {
|
2018-10-16 15:48:19 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("name", policyName)
|
|
|
|
|
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/remove-canned-policy",
|
2018-10-16 15:48:19 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// Execute DELETE on /minio/admin/v3/remove-canned-policy to remove policy.
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCannedPolicy - adds a policy for a canned.
|
2020-04-07 22:30:59 -04:00
|
|
|
func (adm *AdminClient) AddCannedPolicy(ctx context.Context, policyName string, policy *iampolicy.Policy) error {
|
|
|
|
if policy == nil {
|
|
|
|
return ErrInvalidArgument("policy input cannot be empty")
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
if err := policy.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
buf, err := json.Marshal(policy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("name", policyName)
|
|
|
|
|
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/add-canned-policy",
|
2018-10-16 15:48:19 -04:00
|
|
|
queryValues: queryValues,
|
2020-04-07 22:30:59 -04:00
|
|
|
content: buf,
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// Execute PUT on /minio/admin/v3/add-canned-policy to set policy.
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-13 16:41:06 -04:00
|
|
|
|
|
|
|
// SetPolicy - sets the policy for a user or a group.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) SetPolicy(ctx context.Context, policyName, entityName string, isGroup bool) error {
|
2019-08-13 16:41:06 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("policyName", policyName)
|
|
|
|
queryValues.Set("userOrGroup", entityName)
|
|
|
|
groupStr := "false"
|
|
|
|
if isGroup {
|
|
|
|
groupStr = "true"
|
|
|
|
}
|
|
|
|
queryValues.Set("isGroup", groupStr)
|
|
|
|
|
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/set-user-or-group-policy",
|
2019-08-13 16:41:06 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// Execute PUT on /minio/admin/v3/set-user-or-group-policy to set policy.
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
|
2019-08-13 16:41:06 -04:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|