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

feat: add support of public access prevention #636

Merged
merged 22 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
private final String locationType;
private final Logging logging;

public enum PublicAccessPrevention {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
ENFORCED("enforced"),
UNSPECIFIED("unspecified");
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved

private final String value;

PublicAccessPrevention(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

/**
* The Bucket's IAM Configuration.
*
Expand All @@ -112,6 +127,7 @@ public static class IamConfiguration implements Serializable {

private Boolean isUniformBucketLevelAccessEnabled;
private Long uniformBucketLevelAccessLockedTime;
private PublicAccessPrevention publicAccessPrevention;
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean equals(Object o) {
Expand All @@ -125,12 +141,16 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(isUniformBucketLevelAccessEnabled, uniformBucketLevelAccessLockedTime);
return Objects.hash(
isUniformBucketLevelAccessEnabled,
uniformBucketLevelAccessLockedTime,
publicAccessPrevention);
}

private IamConfiguration(Builder builder) {
this.isUniformBucketLevelAccessEnabled = builder.isUniformBucketLevelAccessEnabled;
this.uniformBucketLevelAccessLockedTime = builder.uniformBucketLevelAccessLockedTime;
this.publicAccessPrevention = builder.publicAccessPrevention;
}

public static Builder newBuilder() {
Expand All @@ -141,6 +161,7 @@ public Builder toBuilder() {
Builder builder = new Builder();
builder.isUniformBucketLevelAccessEnabled = isUniformBucketLevelAccessEnabled;
builder.uniformBucketLevelAccessLockedTime = uniformBucketLevelAccessLockedTime;
builder.publicAccessPrevention = publicAccessPrevention;
return builder;
}

Expand All @@ -164,6 +185,11 @@ public Long getUniformBucketLevelAccessLockedTime() {
return uniformBucketLevelAccessLockedTime;
}

/** Returns the Public Access Prevention. * */
public PublicAccessPrevention getPublicAccessPrevention() {
return publicAccessPrevention;
}

Bucket.IamConfiguration toPb() {
Bucket.IamConfiguration iamConfiguration = new Bucket.IamConfiguration();

Expand All @@ -176,6 +202,8 @@ Bucket.IamConfiguration toPb() {
: new DateTime(uniformBucketLevelAccessLockedTime));

iamConfiguration.setUniformBucketLevelAccess(uniformBucketLevelAccess);
iamConfiguration.setPublicAccessPrevention(
publicAccessPrevention == null ? null : publicAccessPrevention.getValue());
shaffeeullah marked this conversation as resolved.
Show resolved Hide resolved

return iamConfiguration;
}
Expand All @@ -184,17 +212,23 @@ static IamConfiguration fromPb(Bucket.IamConfiguration iamConfiguration) {
Bucket.IamConfiguration.UniformBucketLevelAccess uniformBucketLevelAccess =
iamConfiguration.getUniformBucketLevelAccess();
DateTime lockedTime = uniformBucketLevelAccess.getLockedTime();
String publicAccessPrevention = iamConfiguration.getPublicAccessPrevention();

return newBuilder()
.setIsUniformBucketLevelAccessEnabled(uniformBucketLevelAccess.getEnabled())
.setUniformBucketLevelAccessLockedTime(lockedTime == null ? null : lockedTime.getValue())
.setPublicAccessPrevention(
publicAccessPrevention == null
shaffeeullah marked this conversation as resolved.
Show resolved Hide resolved
? null
: PublicAccessPrevention.valueOf(publicAccessPrevention.toUpperCase()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valueOf isn't very safe for direct use without some pre validation as it will throw an IllegalArgumentException if the value passed in doesn't match.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, missed this.

This would definitely suck. Do you have a suggestion?

.build();
}

/** Builder for {@code IamConfiguration} */
public static class Builder {
private Boolean isUniformBucketLevelAccessEnabled;
private Long uniformBucketLevelAccessLockedTime;
private PublicAccessPrevention publicAccessPrevention;

/** Deprecated in favor of setIsUniformBucketLevelAccessEnabled(). */
@Deprecated
Expand Down Expand Up @@ -235,6 +269,21 @@ Builder setUniformBucketLevelAccessLockedTime(Long uniformBucketLevelAccessLocke
return this;
}

/**
* Sets the bucket's Public Access Prevention configuration. Currently supported options are
* 'unspecified' (default) or 'enforced'.
*
* @see <a
* href="https://cloud.google.com/storage/docs/public-access-prevention">public-access-prevention</a>
*/
public Builder setPublicAccessPrevention(PublicAccessPrevention publicAccessPrevention) {
this.publicAccessPrevention =
publicAccessPrevention == null
shaffeeullah marked this conversation as resolved.
Show resolved Hide resolved
? publicAccessPrevention.UNSPECIFIED
: publicAccessPrevention;
return this;
}

/** Builds an {@code IamConfiguration} object */
public IamConfiguration build() {
return new IamConfiguration(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ public class BucketInfoTest {
LifecycleAction.newDeleteAction(),
LifecycleCondition.newBuilder().setAge(5).build()));
private static final String INDEX_PAGE = "index.html";
private static final String PUBLIC_ACCESS_PREVENTION_ENFORCED = "enforced";
shaffeeullah marked this conversation as resolved.
Show resolved Hide resolved
private static final BucketInfo.IamConfiguration IAM_CONFIGURATION =
BucketInfo.IamConfiguration.newBuilder()
.setIsUniformBucketLevelAccessEnabled(true)
.setUniformBucketLevelAccessLockedTime(System.currentTimeMillis())
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build();
private static final BucketInfo.Logging LOGGING =
BucketInfo.Logging.newBuilder()
Expand Down Expand Up @@ -358,11 +360,15 @@ public void testIamConfiguration() {
BucketInfo.IamConfiguration.newBuilder()
.setIsUniformBucketLevelAccessEnabled(true)
.setUniformBucketLevelAccessLockedTime(System.currentTimeMillis())
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build()
.toPb();

assertEquals(Boolean.TRUE, iamConfiguration.getUniformBucketLevelAccess().getEnabled());
assertNotNull(iamConfiguration.getUniformBucketLevelAccess().getLockedTime());
assertEquals(
BucketInfo.PublicAccessPrevention.ENFORCED.getValue(),
iamConfiguration.getPublicAccessPrevention());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,55 @@ public void testEnableAndDisableUniformBucketLevelAccessOnExistingBucket() throw
}
}

@Test
public void testUnspecifiedAndEnforcedPublicAccessPreventionOnBucket() throws Exception {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
String papBucket = RemoteStorageHelper.generateBucketName();
try {
BucketInfo.IamConfiguration iamConfiguration =
BucketInfo.IamConfiguration.newBuilder()
.setIsUniformBucketLevelAccessEnabled(true)
.build();
Bucket bucket =
storage.create(
Bucket.newBuilder(papBucket).setIamConfiguration(iamConfiguration).build());
assertEquals(
BucketInfo.PublicAccessPrevention.UNSPECIFIED.getValue(),
bucket.getIamConfiguration().getPublicAccessPrevention().getValue());
assertTrue(bucket.getIamConfiguration().isUniformBucketLevelAccessEnabled());

bucket
.toBuilder()
.setIamConfiguration(
iamConfiguration
.toBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build())
.build()
.update();
Bucket remoteBucket =
storage.get(papBucket, Storage.BucketGetOption.fields(BucketField.IAMCONFIGURATION));
assertEquals(
BucketInfo.PublicAccessPrevention.ENFORCED.getValue(),
remoteBucket.getIamConfiguration().getPublicAccessPrevention().getValue());

remoteBucket
.toBuilder()
.setIamConfiguration(
iamConfiguration.toBuilder().setIsUniformBucketLevelAccessEnabled(false).build())
.build()
.update();
remoteBucket =
storage.get(papBucket, Storage.BucketGetOption.fields(BucketField.IAMCONFIGURATION));
assertEquals(
BucketInfo.PublicAccessPrevention.ENFORCED.getValue(),
remoteBucket.getIamConfiguration().getPublicAccessPrevention().getValue());
assertFalse(remoteBucket.getIamConfiguration().isUniformBucketLevelAccessEnabled());

} finally {
RemoteStorageHelper.forceDelete(storage, papBucket, 1, TimeUnit.MINUTES);
}
}

@Test
public void testUploadUsingSignedURL() throws Exception {
String blobName = "test-signed-url-upload";
Expand Down