2018-10-16 15:48:19 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-10-16 15:48:19 -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 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"
|
|
|
|
)
|
|
|
|
|
2019-09-26 14:23:13 -04:00
|
|
|
// InfoCannedPolicy - expand canned policy into JSON structure.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) InfoCannedPolicy(ctx context.Context, policyName string) ([]byte, 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,
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute GET on /minio/admin/v2/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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
// ListCannedPolicies - list all configured canned policies.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) ListCannedPolicies(ctx context.Context) (map[string][]byte, 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
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute GET on /minio/admin/v2/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
|
|
|
|
}
|
|
|
|
|
|
|
|
var policies = make(map[string][]byte)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute DELETE on /minio/admin/v2/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-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) AddCannedPolicy(ctx context.Context, policyName, policy 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 + "/add-canned-policy",
|
2018-10-16 15:48:19 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
content: []byte(policy),
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute PUT on /minio/admin/v2/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,
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute PUT on /minio/admin/v2/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
|
|
|
|
}
|