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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -110,7 +110,8 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
public enum PublicAccessPrevention {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
ENFORCED("enforced"),
/** Default value for Public Access Prevention */
UNSPECIFIED("unspecified");
UNSPECIFIED("unspecified"),
UNKNOWN(null);
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved

private final String value;

Expand All @@ -121,6 +122,15 @@ public enum PublicAccessPrevention {
public String getValue() {
return value;
}

public static PublicAccessPrevention parse(String value) {
String upper = value.toUpperCase();
try {
return valueOf(upper);
} catch (IllegalArgumentException ignore) {
tritone marked this conversation as resolved.
Show resolved Hide resolved
return UNKNOWN;
}
}
}

/**
Expand Down Expand Up @@ -224,15 +234,8 @@ static IamConfiguration fromPb(Bucket.IamConfiguration iamConfiguration) {
String publicAccessPrevention = iamConfiguration.getPublicAccessPrevention();

PublicAccessPrevention publicAccessPreventionValue = null;

try {
if (publicAccessPrevention != null) {
publicAccessPreventionValue =
PublicAccessPrevention.valueOf(publicAccessPrevention.toUpperCase());
}
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(
"IamConfiguration: Received an unexpected value of " + publicAccessPrevention);
if (publicAccessPrevention != null) {
publicAccessPreventionValue = PublicAccessPrevention.parse(publicAccessPrevention);
}

return newBuilder()
Expand Down
Expand Up @@ -20,7 +20,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.api.client.util.DateTime;
import com.google.api.services.storage.model.Bucket;
Expand All @@ -32,11 +31,13 @@
import com.google.cloud.storage.BucketInfo.CreatedBeforeDeleteRule;
import com.google.cloud.storage.BucketInfo.DeleteRule;
import com.google.cloud.storage.BucketInfo.DeleteRule.Type;
import com.google.cloud.storage.BucketInfo.IamConfiguration;
import com.google.cloud.storage.BucketInfo.IsLiveDeleteRule;
import com.google.cloud.storage.BucketInfo.LifecycleRule;
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction;
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition;
import com.google.cloud.storage.BucketInfo.NumNewerVersionsDeleteRule;
import com.google.cloud.storage.BucketInfo.PublicAccessPrevention;
import com.google.cloud.storage.BucketInfo.RawDeleteRule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -373,18 +374,14 @@ public void testIamConfiguration() {

@Test
public void testPapValueOfIamConfiguration() {
try {
Bucket.IamConfiguration iamConfiguration = new Bucket.IamConfiguration();
Bucket.IamConfiguration.UniformBucketLevelAccess uniformBucketLevelAccess =
new Bucket.IamConfiguration.UniformBucketLevelAccess();
iamConfiguration.setUniformBucketLevelAccess(uniformBucketLevelAccess);
iamConfiguration.setPublicAccessPrevention("random-string");
BucketInfo.IamConfiguration.fromPb(iamConfiguration);
fail("Expected an IllegalArgumentException when passing in a bad");
} catch (IllegalArgumentException ex) {
// expected IllegalArgumentException because random-string does not map to an enum value
assertTrue(ex.getMessage().contains("random-string"));
}
Bucket.IamConfiguration iamConfiguration = new Bucket.IamConfiguration();
Bucket.IamConfiguration.UniformBucketLevelAccess uniformBucketLevelAccess =
new Bucket.IamConfiguration.UniformBucketLevelAccess();
iamConfiguration.setUniformBucketLevelAccess(uniformBucketLevelAccess);
iamConfiguration.setPublicAccessPrevention("random-string");
IamConfiguration fromPb = IamConfiguration.fromPb(iamConfiguration);

assertEquals(PublicAccessPrevention.UNKNOWN, fromPb.getPublicAccessPrevention());
}

@Test
Expand Down