Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -43,6 +43,7 @@
import org.apache.hadoop.ozone.om.OMConfigKeys;
import org.apache.hadoop.ozone.om.OMMetadataManager;
import org.apache.hadoop.ozone.om.OMMetrics;
import org.apache.hadoop.ozone.om.OmConfig;
import org.apache.hadoop.ozone.om.OzoneManager;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes;
Expand Down Expand Up @@ -93,9 +94,17 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException {
CreateBucketRequest createBucketRequest =
getOmRequest().getCreateBucketRequest();
BucketInfo bucketInfo = createBucketRequest.getBucketInfo();
// Verify resource name
OmUtils.validateBucketName(bucketInfo.getBucketName(),
ozoneManager.isStrictS3());

BucketLayout bucketLayout = BucketLayout.fromProto(bucketInfo.getBucketLayout());
OmConfig omConfig = ozoneManager.getConfig();
boolean fsPathEnabled = false;
if (omConfig != null) {
fsPathEnabled = omConfig.isFileSystemPathEnabled();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding this logic, please mock ozoneManager.getConfig() in TestOMClientRequestWithUserInfo:

when(ozoneManager.getConfiguration()).thenReturn(ozoneConfiguration);

like:

when(ozoneManager.getConfiguration()).thenReturn(ozoneConfiguration);
when(ozoneManager.getConfig()).thenReturn(ozoneConfiguration.getObject(OmConfig.class));


boolean strict = ozoneManager.isStrictS3()
|| bucketLayout.isObjectStore(fsPathEnabled);
OmUtils.validateBucketName(bucketInfo.getBucketName(), strict);

// ACL check during preExecute
if (ozoneManager.getAclsEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void testAcceptNonS3CompliantBucketNameCreationWithStrictS3False()
{"bucket_underscore", "_bucket___multi_underscore_", "bucket_"};
when(ozoneManager.isStrictS3()).thenReturn(false);
for (String bucketName : nonS3CompliantBucketName) {
acceptBucketCreationHelper(volumeName, bucketName);
acceptFSOBucketCreationHelper(volumeName, bucketName);
}
}

Expand Down Expand Up @@ -478,4 +478,28 @@ public static void addCreateVolumeToTable(String volumeName,
.setOwnerName(UUID.randomUUID().toString()).build();
OMRequestTestUtils.addVolumeToOM(omMetadataManager, omVolumeArgs);
}

protected OMBucketCreateRequest doPreExecute(String volumeName,
String bucketName,
OzoneManagerProtocolProtos.BucketLayoutProto layout) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Please do not format method signature like this. Whenever visibility / return type / method name / other modifiers are changed, we would have to reindent all parameters.


OzoneManagerProtocolProtos.BucketInfo.Builder bucketInfo =
newBucketInfoBuilder(bucketName, volumeName)
.setBucketLayout(layout);

if (layout == OzoneManagerProtocolProtos.BucketLayoutProto.FILE_SYSTEM_OPTIMIZED) {
bucketInfo.addMetadata(OMRequestTestUtils.fsoMetadata());
}

return doPreExecute(bucketInfo);
}

private void acceptFSOBucketCreationHelper(String volumeName, String bucketName)
throws Exception {
OMBucketCreateRequest omBucketCreateRequest =
doPreExecute(volumeName, bucketName,
OzoneManagerProtocolProtos.BucketLayoutProto.FILE_SYSTEM_OPTIMIZED);
doValidateAndUpdateCache(volumeName, bucketName,
omBucketCreateRequest.getOmRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newBucketInfoBuilder;
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newCreateBucketRequest;
import static org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketLayoutProto.FILE_SYSTEM_OPTIMIZED;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -182,4 +183,36 @@ protected void doValidateAndUpdateCache(String volumeName, String bucketName,
verifySuccessCreateBucketResponse(omClientResponse.getOMResponse());

}

@Test
public void testNonS3BucketNameAllowedForFSOWhenStrictDisabled() throws Exception {
// Arrange
ozoneManager.getConfiguration().setBoolean(
OMConfigKeys.OZONE_OM_NAMESPACE_STRICT_S3, false); // 如果常數名稱不同,改成實際的 key

when(ozoneManager.getOMDefaultBucketLayout()).thenReturn(
BucketLayout.FILE_SYSTEM_OPTIMIZED);

String volumeName = UUID.randomUUID().toString();
String bucketName = "bucket_with_underscore"; // non-S3-compliant name
addCreateVolumeToTable(volumeName, omMetadataManager);

OzoneManagerProtocolProtos.BucketInfo.Builder bucketInfo =
newBucketInfoBuilder(bucketName, volumeName)
.setBucketLayout(FILE_SYSTEM_OPTIMIZED)
.addMetadata(OMRequestTestUtils.fsoMetadata());

OMRequest originalRequest = newCreateBucketRequest(bucketInfo).build();
OMBucketCreateRequest req = new OMBucketCreateRequest(originalRequest);

// Act
OMRequest modifiedRequest = req.preExecute(ozoneManager);

// Assert: validateAndUpdateCache should succeed
assertDoesNotThrow(() -> {
OMBucketCreateRequest omReq = new OMBucketCreateRequest(modifiedRequest);
omReq.setUGI(UserGroupInformation.getCurrentUser());
omReq.validateAndUpdateCache(ozoneManager, 1);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.hadoop.ozone.om.request.bucket;

import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newBucketInfoBuilder;
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newCreateBucketRequest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.UUID;
import org.apache.hadoop.ozone.om.OMConfigKeys;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Tests bucket creation behavior for ObjectStore / Legacy bucket layouts.
*/
public class TestOMBucketCreateRequestWithObjectStore extends TestOMBucketCreateRequest {

@BeforeEach
public void setupWithObjectStore() {
ozoneManager.getConfiguration().set(
OMConfigKeys.OZONE_DEFAULT_BUCKET_LAYOUT,
OMConfigKeys.OZONE_BUCKET_LAYOUT_OBJECT_STORE);
}

@Test
public void testNonS3BucketNameRejectedForObjectStoreWhenStrictDisabled()
throws Exception {

// strict mode disabled
ozoneManager.getConfiguration().setBoolean(
OMConfigKeys.OZONE_OM_NAMESPACE_STRICT_S3, false);

String volumeName = UUID.randomUUID().toString();
String bucketName = "bucket_with_underscore"; // non-S3-compliant
addCreateVolumeToTable(volumeName, omMetadataManager);

// Explicitly set bucket layout to OBJECT_STORE so the test doesn't depend on
// defaults or mocked OM behavior.
OzoneManagerProtocolProtos.BucketInfo.Builder bucketInfo =
newBucketInfoBuilder(bucketName, volumeName)
.setBucketLayout(
OzoneManagerProtocolProtos.BucketLayoutProto.OBJECT_STORE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Then we don't need this new test class, test case can be added in TestOMBucketCreateRequest.


OMRequest originalRequest = newCreateBucketRequest(bucketInfo).build();
OMBucketCreateRequest req = new OMBucketCreateRequest(originalRequest);

OMException ex = assertThrows(OMException.class,
() -> req.preExecute(ozoneManager));

assertEquals(OMException.ResultCodes.INVALID_BUCKET_NAME, ex.getResult());
}
}