Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add arg builder to {set,get,delete}BucketPolicy APIs #933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {}
}
178 changes: 144 additions & 34 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4373,21 +4373,53 @@ public void putObject(
* @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.
* @deprecated use {@link #getBucketPolicy(GetBucketPolicyArgs)}
*/
@Deprecated
anjalshireesh marked this conversation as resolved.
Show resolved Hide resolved
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 {
checkArgs(args);

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 @@ -4399,26 +4431,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 @@ -4463,19 +4492,112 @@ public String getBucketPolicy(String bucketName)
* @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.
* @deprecated use {@link #setBucketPolicy(SetBucketPolicyArgs)}
*/
@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 {
checkArgs(args);

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 {
checkArgs(args);

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 Expand Up @@ -4557,9 +4679,7 @@ public void setBucketLifeCycle(SetBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
Expand Down Expand Up @@ -4619,9 +4739,7 @@ public void deleteBucketLifeCycle(DeleteBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
Expand Down Expand Up @@ -4685,9 +4803,7 @@ public String getBucketLifeCycle(GetBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
Expand Down Expand Up @@ -5353,9 +5469,7 @@ public void setBucketEncryption(SetBucketEncryptionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("encryption", "");
Expand Down Expand Up @@ -5390,9 +5504,7 @@ public SseConfiguration getBucketEncryption(GetBucketEncryptionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("encryption", "");
Expand Down Expand Up @@ -5433,9 +5545,7 @@ public void deleteBucketEncryption(DeleteBucketEncryptionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("encryption", "");
Expand Down
51 changes: 51 additions & 0 deletions api/src/main/java/io/minio/SetBucketPolicyArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.setBucketPolicy(). */
public class SetBucketPolicyArgs extends BucketArgs {
private String config;

public String config() {
return config;
}

public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link SetBucketPolicyArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, SetBucketPolicyArgs> {
private void validateConfig(String config) {
if (config == null) {
throw new IllegalArgumentException("null policy configuration");
}
}

@Override
protected void validate(SetBucketPolicyArgs args) {
anjalshireesh marked this conversation as resolved.
Show resolved Hide resolved
super.validate(args);
validateConfig(args.config);
}

public Builder config(String config) {
validateConfig(config);
operations.add(args -> args.config = config);
return this;
}
}
}
Loading