forked from gkatzioura/CloudStorageMaven
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - gkatzioura#38 - Deprecated the PublicReadProperty in favour…
… of the CannedAccessControlListProperty (cannedAcl), which offers a superset of functionality to the PublicReadProperty. For backwards compatibility, CannedAccessControlListProperty is preferred, but falls back to PublicReadProperty if unset.
- Loading branch information
1 parent
490837c
commit 319f035
Showing
5 changed files
with
178 additions
and
31 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...ageWagon/src/main/java/com/gkatzioura/maven/cloud/s3/CannedAccessControlListProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2018 Emmanouil Gkatziouras | ||
* | ||
* 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 com.gkatzioura.maven.cloud.s3; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
|
||
public class CannedAccessControlListProperty { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CannedAccessControlListProperty.class.getName()); | ||
|
||
private static final String CANNED_ACL_PROP_TAG = "cannedAcl"; | ||
private static final String CANNED_ACL_ENV_TAG = "CANNED_ACL"; | ||
|
||
private static final CannedAccessControlList DEFAULT_CANNED_ACL = CannedAccessControlList.Private; | ||
|
||
private final CannedAccessControlList cannedAccessControlList; | ||
@Deprecated | ||
private final PublicReadProperty publicReadProperty; | ||
|
||
public CannedAccessControlListProperty() { | ||
this((CannedAccessControlList) null, new PublicReadProperty(null)); | ||
} | ||
|
||
/** | ||
* @param cannedAccessControlList may be null | ||
* @param publicReadProperty may not be null | ||
*/ | ||
public CannedAccessControlListProperty(String cannedAccessControlList, PublicReadProperty publicReadProperty) { | ||
this(lenientValueOf(cannedAccessControlList), publicReadProperty); | ||
} | ||
|
||
private CannedAccessControlListProperty(CannedAccessControlList cannedAccessControlList, PublicReadProperty publicReadProperty) { | ||
this.cannedAccessControlList = cannedAccessControlList; | ||
this.publicReadProperty = publicReadProperty; | ||
} | ||
|
||
/** | ||
* return the cannedAccessControlList set in the constructor or the cannedAcl set using the system property or environment variable, else the now deprecated {@link PublicReadProperty}, or finally the default value | ||
*/ | ||
public CannedAccessControlList get() { | ||
if (this.cannedAccessControlList != null) { | ||
return this.cannedAccessControlList; | ||
} | ||
|
||
String cannedAccessControlListProp = System.getProperty(CANNED_ACL_PROP_TAG); | ||
if (cannedAccessControlListProp != null) { | ||
return lenientValueOf(cannedAccessControlListProp); | ||
} | ||
|
||
String cannedAccessControlListEnv = System.getenv(CANNED_ACL_ENV_TAG); | ||
if (cannedAccessControlListEnv != null) { | ||
return lenientValueOf(cannedAccessControlListEnv); | ||
} | ||
|
||
if (publicReadProperty.get()) { | ||
LOGGER.warning("Public read was set to true, so setting cannedAcl to PublicRead. This feature will be removed in future in favour of the cannedAcl property"); | ||
return CannedAccessControlList.PublicRead; | ||
} | ||
|
||
return DEFAULT_CANNED_ACL; | ||
} | ||
|
||
private static CannedAccessControlList lenientValueOf(String stringValue) { | ||
if (stringValue == null) { | ||
return null; | ||
} else { | ||
for (CannedAccessControlList cannedAcl : CannedAccessControlList.values()) { | ||
if (cannedAcl.name().equalsIgnoreCase(stringValue) || cannedAcl.toString().equalsIgnoreCase(stringValue)) { | ||
return cannedAcl; | ||
} | ||
} | ||
} | ||
LOGGER.warning("Could not find a cannedAcl value with name: " + stringValue + " so returning the default value of: " + DEFAULT_CANNED_ACL); | ||
return DEFAULT_CANNED_ACL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...agon/src/test/java/com/gkatzioura/maven/cloud/s3/CannedAccessControlListPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.gkatzioura.maven.cloud.s3; | ||
|
||
import static com.amazonaws.services.s3.model.CannedAccessControlList.AuthenticatedRead; | ||
import static com.amazonaws.services.s3.model.CannedAccessControlList.AwsExecRead; | ||
import static com.amazonaws.services.s3.model.CannedAccessControlList.BucketOwnerFullControl; | ||
import static com.amazonaws.services.s3.model.CannedAccessControlList.BucketOwnerRead; | ||
import static com.amazonaws.services.s3.model.CannedAccessControlList.Private; | ||
import static com.amazonaws.services.s3.model.CannedAccessControlList.PublicRead; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
public class CannedAccessControlListPropertyTest { | ||
|
||
private static final PublicReadProperty PUBLIC_READ_TRUE = new PublicReadProperty(true); | ||
|
||
@Test | ||
public void defaultConstructorReturnsDefaultValue() { | ||
assertEquals(Private, new CannedAccessControlListProperty().get()); | ||
} | ||
|
||
@Test | ||
public void cannedAclNameValue() { | ||
assertEquals(AuthenticatedRead, new CannedAccessControlListProperty("AuthenticatedRead", PUBLIC_READ_TRUE).get()); | ||
} | ||
|
||
@Test | ||
public void cannedAclHeaderValue() { | ||
assertEquals(AwsExecRead, new CannedAccessControlListProperty("aws-exec-read", PUBLIC_READ_TRUE).get()); | ||
} | ||
|
||
@Test | ||
public void nullCannedAclReturnsSystemPropertyValue() { | ||
System.setProperty("cannedAcl", "BucketOwnerRead"); | ||
assertEquals(BucketOwnerRead, new CannedAccessControlListProperty().get()); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void nullCannedAclReturnsEnvironmentVariableValue() { | ||
// TODO: work out how to test environment variables for test | ||
// System.setenv("CANNED_ACL", "BucketOwnerFullControl"); | ||
assertEquals(BucketOwnerFullControl, new CannedAccessControlListProperty().get()); | ||
} | ||
|
||
@Test | ||
public void nullCannedAclFallsBackToPublicReadProperty() { | ||
assertEquals(PublicRead, new CannedAccessControlListProperty(null, PUBLIC_READ_TRUE).get()); | ||
} | ||
|
||
@Test | ||
public void nonsenseValueSetsToDefault() { | ||
assertEquals(Private, new CannedAccessControlListProperty("nonsense", PUBLIC_READ_TRUE).get()); | ||
} | ||
} |