Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.client.OzoneLifecycleConfiguration;
import org.apache.hadoop.ozone.om.exceptions.OMException;
Expand All @@ -44,6 +45,9 @@
@XmlRootElement(name = "LifecycleConfiguration",
namespace = "http://s3.amazonaws.com/doc/2006-03-01/")
public class S3LifecycleConfiguration {
private static final String STATUS_ENABLED = "Enabled";
private static final String STATUS_DISABLED = "Disabled";

@XmlElement(name = "Rule")
private List<Rule> rules = new ArrayList<>();

Expand Down Expand Up @@ -312,13 +316,17 @@ public OmLifecycleConfiguration toOmLifecycleConfiguration(OzoneBucket ozoneBuck
* @return OmLCRule internal rule representation
*/
private OmLCRule convertToOmRule(Rule rule) throws OMException, OS3Exception {
if (rule.getStatus() == null || rule.getStatus().isEmpty()) {
String status = rule.getStatus();
if (StringUtils.isEmpty(status)) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_XML,
"The Status element is required in LifecycleConfiguration");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The second String argument here is actually resource, and OS3Exception serializes it as <Resource>. So this text does not replace <Message>; MALFORMED_XML already provides that. I think the one-argument overload is enough here.

This is in the existing missing-Status path, so I plan to follow it up separately with response coverage. If it makes more sense to include it here, I can update the patch.

}
if (!STATUS_ENABLED.equals(status) && !STATUS_DISABLED.equals(status)) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_XML);
}

OmLCRule.Builder builder = new OmLCRule.Builder()
.setEnabled("Enabled".equals(rule.getStatus()))
.setEnabled(STATUS_ENABLED.equals(status))
.setId(rule.getId())
.setPrefix(rule.getPrefix());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import org.apache.hadoop.ozone.s3.util.S3Consts;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;

/**
Expand Down Expand Up @@ -137,6 +139,16 @@ public void testPutLifecycleConfigurationWithoutStatus() throws Exception {
}
}

@ParameterizedTest
@ValueSource(strings = {"enabled", "disabled", "invalid"})
public void testPutLifecycleConfigurationWithInvalidStatus(String status)
throws Exception {
OS3Exception ex = assertThrows(OS3Exception.class,
() -> bucketEndpoint.put("bucket1", withStatus(status)));
assertEquals(HTTP_BAD_REQUEST, ex.getHttpCode());
assertEquals(MALFORMED_XML.getCode(), ex.getCode());
}

private void testInvalidLifecycleConfiguration(Supplier<InputStream> inputStream,
int expectedHttpCode, String expectedErrorCode) throws Exception {
try {
Expand Down Expand Up @@ -170,7 +182,8 @@ public void testPutInvalidExpirationDateLCC() throws Exception {

@Test
public void testPutValidLifecycleConfiguration() throws Exception {
assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", onePrefix()).getStatus());
assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", withStatus("Enabled")).getStatus());
assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", withStatus("Disabled")).getStatus());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verify valid input for both Enabled and Disabled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the Enabled test, thanks.

assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", emptyPrefix()).getStatus());
assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", oneTag()).getStatus());
assertEquals(HTTP_OK, bucketEndpoint.put("bucket1", twoTagsInAndOperator()).getStatus());
Expand Down Expand Up @@ -304,6 +317,19 @@ private static InputStream withoutStatus() {
return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
}

private static InputStream withStatus(String status) {
String xml =
"<LifecycleConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" +
"<Rule>" +
"<ID>remove logs after 30 days</ID>" +
"<Prefix>prefix/</Prefix>" +
"<Status>" + status + "</Status>" +
"<Expiration><Days>30</Days></Expiration>" +
"</Rule>" +
"</LifecycleConfiguration>";
return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
}

private static InputStream withoutFilter() {
String xml =
"<LifecycleConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" +
Expand Down