Skip to content

Commit

Permalink
add arg builder to {set,get,delete}BucketPolicy APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed May 22, 2020
1 parent 9fd2c92 commit 9e830b2
Show file tree
Hide file tree
Showing 10 changed files with 6,302 additions and 60 deletions.
27 changes: 27 additions & 0 deletions api/src/main/java/io/minio/DeleteBucketPolicyArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* 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 io.minio;

/** Argument class of MinioClient.deleteBucketPolicy(). */
public class DeleteBucketPolicyArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link DeleteBucketPolicyArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, DeleteBucketPolicyArgs> {}
}
27 changes: 27 additions & 0 deletions api/src/main/java/io/minio/GetBucketPolicyArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* 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 io.minio;

/** Argument class of MinioClient.getBucketPolicy(). */
public class GetBucketPolicyArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link GetBucketPolicyArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, GetBucketPolicyArgs> {}
}
158 changes: 142 additions & 16 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4294,20 +4294,53 @@ public void putObject(
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public String getBucketPolicy(String bucketName)
throws BucketPolicyTooLargeException, ErrorResponseException, IllegalArgumentException,
InsufficientDataException, InternalException, InvalidBucketNameException,
InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
XmlParserException {
return getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
}

/**
* Gets bucket policy configuration of a bucket.
*
* <pre>Example:{@code
* String config =
* minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link GetBucketPolicyArgs} object.
* @return String - Bucket policy configuration as JSON string.
* @throws BucketPolicyTooLargeException thrown to indicate returned bucket policy is too large.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public String getBucketPolicy(GetBucketPolicyArgs args)
throws BucketPolicyTooLargeException, ErrorResponseException, IllegalArgumentException,
InsufficientDataException, InternalException, InvalidBucketNameException,
InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("policy", "");

Response response = null;
byte[] buf = new byte[MAX_BUCKET_POLICY_SIZE];
int bytesRead = 0;

try {
response = executeGet(bucketName, null, null, queryParamMap);
try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) {
byte[] buf = new byte[MAX_BUCKET_POLICY_SIZE];
int bytesRead = 0;
bytesRead = response.body().byteStream().read(buf, 0, MAX_BUCKET_POLICY_SIZE);
if (bytesRead < 0) {
throw new IOException("unexpected EOF when reading bucket policy");
Expand All @@ -4319,26 +4352,23 @@ public String getBucketPolicy(String bucketName)
while (byteRead == 0) {
byteRead = response.body().byteStream().read();
if (byteRead < 0) {
// reached EOF which is fine.
break;
break; // reached EOF which is fine.
}

if (byteRead > 0) {
throw new BucketPolicyTooLargeException(bucketName);
throw new BucketPolicyTooLargeException(args.bucket());
}
}
}

return new String(buf, 0, bytesRead, StandardCharsets.UTF_8);
} catch (ErrorResponseException e) {
if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_BUCKET_POLICY) {
throw e;
}
} finally {
if (response != null) {
response.body().close();
}
}

return new String(buf, 0, bytesRead, StandardCharsets.UTF_8);
return "";
}

/**
Expand Down Expand Up @@ -4384,18 +4414,114 @@ public String getBucketPolicy(String bucketName)
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public void setBucketPolicy(String bucketName, String policy)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
}

/**
* Sets bucket policy configuration to a bucket.
*
* <pre>Example:{@code
* // Assume policyJson contains below JSON string;
* // {
* // "Statement": [
* // {
* // "Action": [
* // "s3:GetBucketLocation",
* // "s3:ListBucket"
* // ],
* // "Effect": "Allow",
* // "Principal": "*",
* // "Resource": "arn:aws:s3:::my-bucketname"
* // },
* // {
* // "Action": "s3:GetObject",
* // "Effect": "Allow",
* // "Principal": "*",
* // "Resource": "arn:aws:s3:::my-bucketname/myobject*"
* // }
* // ],
* // "Version": "2012-10-17"
* // }
* //
* minioClient.setBucketPolicy(
* SetBucketPolicyArgs.builder().bucket("my-bucketname").config(policyJson).build());
* }</pre>
*
* @param args {@link SetBucketPolicyArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public void setBucketPolicy(SetBucketPolicyArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", "application/json");

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("policy", "");

Response response = executePut(bucketName, null, headerMap, queryParamMap, policy, 0);
response.body().close();
Response response = executePut(args.bucket(), null, headerMap, queryParamMap, args.config(), 0);
response.close();
}

/**
* Deletes bucket policy configuration to a bucket.
*
* <pre>Example:{@code
* minioClient.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket("my-bucketname"));
* }</pre>
*
* @param args {@link DeleteBucketPolicyArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public void deleteBucketPolicy(DeleteBucketPolicyArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("policy", "");

try {
Response response = executeDelete(args.bucket(), "", queryParamMap);
response.close();
} catch (ErrorResponseException e) {
if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_BUCKET_POLICY) {
throw e;
}
}
}

/**
Expand Down
Loading

0 comments on commit 9e830b2

Please sign in to comment.