2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 22:50:48 +00:00
restic/vendor/github.com/minio/minio-go/v6/api-get-policy.go

79 lines
2.0 KiB
Go
Raw Normal View History

2017-07-23 12:24:45 +00:00
/*
2019-11-22 13:57:56 +00:00
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
* Copyright 2015-2017 MinIO, Inc.
2017-07-23 12:24:45 +00: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 minio
import (
2017-12-08 19:45:59 +00:00
"context"
2017-07-23 12:24:45 +00:00
"io/ioutil"
"net/http"
"net/url"
2019-11-22 13:57:56 +00:00
"github.com/minio/minio-go/v6/pkg/s3utils"
2017-07-23 12:24:45 +00:00
)
// GetBucketPolicy - get bucket policy at a given path.
func (c Client) GetBucketPolicy(bucketName string) (string, error) {
2017-07-23 12:24:45 +00:00
// Input validation.
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
bucketPolicy, err := c.getBucketPolicy(bucketName)
2017-07-23 12:24:45 +00:00
if err != nil {
errResponse := ToErrorResponse(err)
if errResponse.Code == "NoSuchBucketPolicy" {
return "", nil
2017-07-23 12:24:45 +00:00
}
return "", err
2017-07-23 12:24:45 +00:00
}
return bucketPolicy, nil
2017-07-23 12:24:45 +00:00
}
// Request server for current bucket policy.
func (c Client) getBucketPolicy(bucketName string) (string, error) {
2017-07-23 12:24:45 +00:00
// Get resources properly escaped and lined up before
// using them in http request.
urlValues := make(url.Values)
urlValues.Set("policy", "")
// Execute GET on bucket to list objects.
2017-12-08 19:45:59 +00:00
resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
bucketName: bucketName,
queryValues: urlValues,
contentSHA256Hex: emptySHA256Hex,
2017-07-23 12:24:45 +00:00
})
defer closeResponse(resp)
if err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
if resp != nil {
if resp.StatusCode != http.StatusOK {
return "", httpRespToErrorResponse(resp, bucketName, "")
2017-07-23 12:24:45 +00:00
}
}
bucketPolicyBuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
policy := string(bucketPolicyBuf)
2017-07-23 12:24:45 +00:00
return policy, err
}