ChenSammi commented on code in PR #11461:
URL: https://github.com/apache/ozone/pull/11461#discussion_r4255947907


##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestS3LifecycleConfigurationPut.java:
##########
@@ -917,6 +922,183 @@ public void 
testPutLifecycleMatchingOwnerStillValidatesXml() throws Exception {
     verify(proxy, never()).setLifecycleConfiguration(any());
   }
 
+  @ParameterizedTest
+  @MethodSource("versioningActions")
+  public void testRejectVersioningActions(String action, String status, 
boolean namespace) throws Exception {
+    ClientProtocol proxy = mock(ClientProtocol.class);
+    BucketEndpoint endpoint = newProtocolEndpoint(proxy, headers, 
BucketLayout.OBJECT_STORE);
+    String xml = "<LifecycleConfiguration" + (namespace ? " xmlns=\"" + 
S3Consts.S3_XML_NAMESPACE + "\"" : "")
+        + "><Rule><ID>versioning</ID><Prefix>prefix/</Prefix><Status>" + 
status + "</Status>"
+        + 
"<AbortIncompleteMultipartUpload><DaysAfterInitiation>1</DaysAfterInitiation>"
+        + "</AbortIncompleteMultipartUpload>" + action + 
"</Rule></LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    verify(proxy, never()).setLifecycleConfiguration(any());
+  }
+
+  private static Stream<Arguments> versioningActions() {
+    String transition = 
"<NoncurrentVersionTransition><NoncurrentDays>1</NoncurrentDays>"
+        + "<StorageClass>GLACIER</StorageClass></NoncurrentVersionTransition>";
+    return Stream.of(
+        
"<NoncurrentVersionExpiration><NoncurrentDays>1</NoncurrentDays></NoncurrentVersionExpiration>",
+        "<NoncurrentVersionExpiration/>",
+        "<NoncurrentVersionExpiration 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; xsi:nil=\"true\"/>",
+        "<NoncurrentVersionTransition/>",
+        "<NoncurrentVersionTransition 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; xsi:nil=\"true\"/>",
+        transition, transition + transition,
+        
"<Expiration><Days>30</Days><ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker></Expiration>",
+        
"<Expiration><Days>30</Days><ExpiredObjectDeleteMarker>false</ExpiredObjectDeleteMarker></Expiration>",
+        "<Expiration><Days>30</Days><ExpiredObjectDeleteMarker/></Expiration>",
+        "<Expiration><Days>30</Days><ExpiredObjectDeleteMarker "
+            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; 
xsi:nil=\"true\"/></Expiration>",
+        
"<Expiration><ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker></Expiration>")
+        .flatMap(action -> Stream.of("Enabled", "Disabled")
+            .flatMap(status -> Stream.of(true, false).map(namespace -> 
Arguments.of(action, status, namespace))));
+  }
+
+  @Test
+  public void testRejectedVersioningConfigurationPreservesExistingRules() 
throws Exception {
+    bucketEndpoint.put("bucket1", onePrefix());
+    String xml = 
"<LifecycleConfiguration><Rule><ID>replacement</ID><Prefix>replacement/</Prefix>"
+        + "<Status>Enabled</Status>"
+        + 
"<Expiration><Days>1</Days></Expiration></Rule><Rule><ID>unsupported</ID><Prefix>prefix/</Prefix>"
+        + "<Status>Enabled</Status>"
+        + 
"<NoncurrentVersionExpiration><NoncurrentDays>1</NoncurrentDays></NoncurrentVersionExpiration>"
+        + "</Rule></LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> bucketEndpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    try (Response response = bucketEndpoint.get("bucket1")) {
+      S3LifecycleConfiguration configuration = (S3LifecycleConfiguration) 
response.getEntity();
+      assertThat(configuration.getRules()).hasSize(1);
+      S3LifecycleConfiguration.Rule rule = configuration.getRules().get(0);
+      assertThat(rule.getId()).isEqualTo("remove logs after 30 days");
+      assertThat(rule.getPrefix()).isEqualTo("prefix/");
+      assertThat(rule.getExpiration().getDays()).isEqualTo(30);
+      StringWriter xmlOutput = new StringWriter();
+      
JAXBContext.newInstance(S3LifecycleConfiguration.class).createMarshaller().marshal(configuration,
 xmlOutput);
+      assertThat(xmlOutput.toString()).doesNotContain("NoncurrentVersion", 
"ExpiredObjectDeleteMarker");
+    }
+  }
+
+  @Test
+  public void testVersioningNamesInTextRemainSupported() throws Exception {
+    String xml = 
"<LifecycleConfiguration><Rule><ID>NoncurrentVersionExpiration</ID>"
+        + 
"<Prefix>NoncurrentVersionTransition/ExpiredObjectDeleteMarker/</Prefix><Status>Enabled</Status>"
+        + "<Expiration><Days>30</Days></Expiration>"
+        + "</Rule></LifecycleConfiguration>";
+    try (Response response = bucketEndpoint.put("bucket1",
+        new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))) {
+      assertThat(response.getStatus()).isEqualTo(HTTP_OK);
+    }
+  }
+
+  @Test
+  public void testVersioningRuleStillChecksStatusAndOwner() throws Exception {
+    ClientProtocol proxy = mock(ClientProtocol.class);
+    BucketEndpoint endpoint = newProtocolEndpoint(proxy, headers, 
BucketLayout.OBJECT_STORE);
+    String xml = "<LifecycleConfiguration><Rule><Status>invalid</Status>"
+        + "<NoncurrentVersionExpiration/></Rule></LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    
when(headers.getHeaderString(EXPECTED_BUCKET_OWNER_HEADER)).thenReturn("other-owner");
+    assertErrorResponse(ACCESS_DENIED,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    verify(proxy, never()).setLifecycleConfiguration(any());
+  }
+
+  @ParameterizedTest
+  @MethodSource("unsupportedLifecycleElements")
+  public void testRejectUnsupportedLifecycleElements(String elements, boolean 
namespace) throws Exception {
+    ClientProtocol proxy = mock(ClientProtocol.class);
+    BucketEndpoint endpoint = newProtocolEndpoint(proxy, headers, 
BucketLayout.OBJECT_STORE);
+    String xml = "<LifecycleConfiguration" + (namespace ? " xmlns=\"" + 
S3Consts.S3_XML_NAMESPACE + "\"" : "")
+        + "><Rule><ID>unsupported</ID><Status>Enabled</Status>"
+        + elements + "</Rule></LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    verify(proxy, never()).setLifecycleConfiguration(any());
+  }
+
+  private static Stream<Arguments> unsupportedLifecycleElements() {
+    String prefix = "<Prefix>prefix/</Prefix>";
+    String expiration = "<Expiration><Days>30</Days></Expiration>";
+    return Stream.of(
+        prefix + expiration + "<UnknownElement/>",
+        prefix + "<Expiration><Days>30</Days><UnknownElement/></Expiration>",
+        prefix + "<Expiration><Days>30<UnknownElement/></Days></Expiration>",
+        prefix + 
"<AbortIncompleteMultipartUpload><DaysAfterInitiation>1</DaysAfterInitiation>"
+            + "<UnknownElement/></AbortIncompleteMultipartUpload>",
+        expiration + 
"<Filter><Prefix>prefix/</Prefix><ObjectSizeGreaterThan>1</ObjectSizeGreaterThan></Filter>",
+        expiration + 
"<Filter><Prefix>prefix/</Prefix><ObjectSizeLessThan>1000</ObjectSizeLessThan></Filter>",
+        expiration + 
"<Filter><And><Prefix>prefix/</Prefix><Tag><Key>key</Key><Value>value</Value></Tag>"
+            + 
"<ObjectSizeGreaterThan>1</ObjectSizeGreaterThan></And></Filter>",
+        expiration + 
"<Filter><Tag><Key>key</Key><Value>value</Value><UnknownElement/></Tag></Filter>",
+        prefix + 
"<Expiration><Days>30</Days><ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker>"
+            + "</Expiration>" + expiration,
+        prefix + 
"<Expiration><Days>30</Days><Tag><Key>key</Key><Value>value</Value></Tag></Expiration>",
+        prefix + 
"<Transition><Days>30</Days><StorageClass>GLACIER</StorageClass><UnknownElement/></Transition>")
+        .flatMap(elements -> Stream.of(Arguments.of(elements, false), 
Arguments.of(elements, true)));
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"<UnknownElement/>", "<Prefix>prefix/</Prefix>"})
+  public void testRejectUnsupportedRootElements(String element) throws 
Exception {
+    ClientProtocol proxy = mock(ClientProtocol.class);
+    BucketEndpoint endpoint = newProtocolEndpoint(proxy, headers, 
BucketLayout.OBJECT_STORE);
+    String xml = "<LifecycleConfiguration>" + element
+        + 
"<Rule><Prefix>prefix/</Prefix><Status>Enabled</Status><Expiration><Days>30</Days></Expiration>"
+        + "</Rule></LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    verify(proxy, never()).setLifecycleConfiguration(any());
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {
+      "<Rule xsi:nil=\"true\"><Prefix>prefix/</Prefix><Status>Enabled</Status>"
+          + "<Expiration><Days>30</Days></Expiration><UnknownElement/></Rule>",
+      "<Rule><Prefix>prefix/</Prefix><Status>Enabled</Status>"
+          + "<Expiration 
xsi:nil=\"true\"><Days>30</Days><UnknownElement/></Expiration></Rule>",
+      "<Rule><Prefix>prefix/</Prefix><Status>Enabled</Status>"
+          + "<Expiration 
xsi:type=\"xs:string\"><Days>30</Days><UnknownElement/></Expiration></Rule>"
+  })
+  public void testRejectUnsupportedElementsWithXmlTypeOverrides(String rule) 
throws Exception {
+    ClientProtocol proxy = mock(ClientProtocol.class);
+    BucketEndpoint endpoint = newProtocolEndpoint(proxy, headers, 
BucketLayout.OBJECT_STORE);
+    String xml = "<LifecycleConfiguration 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
+        + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\";>" + rule + 
"</LifecycleConfiguration>";
+    assertErrorResponse(MALFORMED_XML,
+        () -> endpoint.put("bucket1", new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
+    verify(proxy, never()).setLifecycleConfiguration(any());
+  }
+
+  @ParameterizedTest
+  @ValueSource(booleans = {false, true})
+  public void testSupportedLifecycleElementPaths(boolean namespace) throws 
Exception {
+    String xml = "<LifecycleConfiguration" + (namespace ? " xmlns=\"" + 
S3Consts.S3_XML_NAMESPACE + "\"" : "")

Review Comment:
   @rich7420 , could you add one more unit test, which tests that the 
configuration doesn't start with "LifecycleConfiguration" ? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to