ivanzlenko commented on code in PR #7268: URL: https://github.com/apache/ozone/pull/7268#discussion_r1800525735
########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); + + private OzoneS3ConfigUtils() { } + + /** + * Get the list of S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator users + * + * If ozone.s3.administrators value is empty string or unset, + * defaults to ozone.administrators value. + */ + public static Collection<String> getS3AdminsFromConfig(OzoneConfiguration conf) throws IOException { + Collection<String> ozAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); + + if (ozAdmins == null || ozAdmins.isEmpty()) { + ozAdmins = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS); + } + String omSPN = UserGroupInformation.getCurrentUser().getShortUserName(); + if (!ozAdmins.contains(omSPN)) { + ozAdmins.add(omSPN); + } + + return ozAdmins; + } + + /** + * Get the list of the groups that are a part S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator groups + * + * If ozone.s3.administrators.groups value is empty or unset, + * defaults to the ozone.administrators.groups value + */ + public static Collection<String> getS3AdminsGroupsFromConfig(OzoneConfiguration conf) { + Collection<String> s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS_GROUPS); + + if (s3AdminsGroup.isEmpty() && conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS).isEmpty()) { + s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS_GROUPS); + } + + return s3AdminsGroup; + } + + /** + * Get the users and groups that are a part of S3 administrators. + * @param conf Stores an instance of {@link OzoneConfiguration} being used + * @return an instance of {@link OzoneAdmins} containing the S3 admin users and groups + */ + public static OzoneAdmins getS3Admins(OzoneConfiguration conf) { + Collection<String> s3Admins; + try { + s3Admins = getS3AdminsFromConfig(conf); + } catch (IOException ie) { + s3Admins = null; + } + Collection<String> s3AdminGroups = getS3AdminsGroupsFromConfig(conf); + + if (LOG.isDebugEnabled()) { + if (null == s3Admins) { + LOG.debug("S3 Admins are not set in configuration"); + } + if (null == s3AdminGroups) { + LOG.debug("S3 Admin Groups are not set in configuration"); + } + } + return new OzoneAdmins(s3Admins, s3AdminGroups); + } + + /** + * Check if the provided user is an S3 administrator. + * @param user An instance of {@link UserGroupInformation} with information about the user to verify + * @param s3Admins An instance of {@link OzoneAdmins} containing information + * of the S3 administrator users and groups in the system + * @return {@code true} if the provided user is an S3 administrator else {@code false} + */ + public static boolean isS3Admin(@Nullable UserGroupInformation user, OzoneAdmins s3Admins) { Review Comment: Not sure if there is any point to have this method. ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); + + private OzoneS3ConfigUtils() { } + + /** + * Get the list of S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator users + * + * If ozone.s3.administrators value is empty string or unset, + * defaults to ozone.administrators value. + */ + public static Collection<String> getS3AdminsFromConfig(OzoneConfiguration conf) throws IOException { + Collection<String> ozAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); + + if (ozAdmins == null || ozAdmins.isEmpty()) { + ozAdmins = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS); + } + String omSPN = UserGroupInformation.getCurrentUser().getShortUserName(); Review Comment: Why are we adding current user as an admin? ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); + + private OzoneS3ConfigUtils() { } + + /** + * Get the list of S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator users + * + * If ozone.s3.administrators value is empty string or unset, + * defaults to ozone.administrators value. + */ + public static Collection<String> getS3AdminsFromConfig(OzoneConfiguration conf) throws IOException { + Collection<String> ozAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); Review Comment: ```suggestion Collection<String> ozoneAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); ``` ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); Review Comment: ```suggestion private static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); ``` ########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/conf/TestOzoneS3ConfigUtils.java: ########## @@ -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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * This class is to test S3 configuration based utils. + */ +public class TestOzoneS3ConfigUtils { Review Comment: ```suggestion class TestOzoneS3ConfigUtils { ``` ########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/conf/TestOzoneS3ConfigUtils.java: ########## @@ -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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * This class is to test S3 configuration based utils. + */ +public class TestOzoneS3ConfigUtils { + + @Test + public void testS3AdminExtraction() throws IOException { Review Comment: ```suggestion void testS3AdminExtraction() throws IOException { ``` ########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/conf/TestOzoneS3ConfigUtils.java: ########## @@ -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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * This class is to test S3 configuration based utils. + */ +public class TestOzoneS3ConfigUtils { + + @Test + public void testS3AdminExtraction() throws IOException { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_S3_ADMINISTRATORS, "alice,bob"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsFromConfig(configuration)) + .containsAll(Arrays.asList("alice", "bob")); + } + + @Test + public void testS3AdminExtractionWithFallback() throws IOException { Review Comment: ```suggestion void testS3AdminExtractionWithFallback() throws IOException { ``` ########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/conf/TestOzoneS3ConfigUtils.java: ########## @@ -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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * This class is to test S3 configuration based utils. + */ +public class TestOzoneS3ConfigUtils { + + @Test + public void testS3AdminExtraction() throws IOException { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_S3_ADMINISTRATORS, "alice,bob"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsFromConfig(configuration)) + .containsAll(Arrays.asList("alice", "bob")); + } + + @Test + public void testS3AdminExtractionWithFallback() throws IOException { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_ADMINISTRATORS, "alice,bob"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsFromConfig(configuration)) + .containsAll(Arrays.asList("alice", "bob")); + } + + @Test + public void testS3AdminGroupExtraction() { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS, + "test1, test2"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsGroupsFromConfig(configuration)) + .containsAll(Arrays.asList("test1", "test2")); + } + + @Test + public void testS3AdminGroupExtractionWithFallback() { Review Comment: ```suggestion void testS3AdminGroupExtractionWithFallback() { ``` ########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/conf/TestOzoneS3ConfigUtils.java: ########## @@ -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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * This class is to test S3 configuration based utils. + */ +public class TestOzoneS3ConfigUtils { + + @Test + public void testS3AdminExtraction() throws IOException { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_S3_ADMINISTRATORS, "alice,bob"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsFromConfig(configuration)) + .containsAll(Arrays.asList("alice", "bob")); + } + + @Test + public void testS3AdminExtractionWithFallback() throws IOException { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OzoneConfigKeys.OZONE_ADMINISTRATORS, "alice,bob"); + + assertThat(OzoneS3ConfigUtils.getS3AdminsFromConfig(configuration)) + .containsAll(Arrays.asList("alice", "bob")); + } + + @Test + public void testS3AdminGroupExtraction() { Review Comment: ```suggestion void testS3AdminGroupExtraction() { ``` ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); + + private OzoneS3ConfigUtils() { } + + /** + * Get the list of S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator users + * + * If ozone.s3.administrators value is empty string or unset, + * defaults to ozone.administrators value. + */ + public static Collection<String> getS3AdminsFromConfig(OzoneConfiguration conf) throws IOException { + Collection<String> ozAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); + + if (ozAdmins == null || ozAdmins.isEmpty()) { + ozAdmins = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS); + } + String omSPN = UserGroupInformation.getCurrentUser().getShortUserName(); + if (!ozAdmins.contains(omSPN)) { + ozAdmins.add(omSPN); + } + + return ozAdmins; + } + + /** + * Get the list of the groups that are a part S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator groups + * + * If ozone.s3.administrators.groups value is empty or unset, + * defaults to the ozone.administrators.groups value + */ + public static Collection<String> getS3AdminsGroupsFromConfig(OzoneConfiguration conf) { + Collection<String> s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS_GROUPS); + + if (s3AdminsGroup.isEmpty() && conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS).isEmpty()) { + s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS_GROUPS); + } + + return s3AdminsGroup; + } + + /** + * Get the users and groups that are a part of S3 administrators. + * @param conf Stores an instance of {@link OzoneConfiguration} being used + * @return an instance of {@link OzoneAdmins} containing the S3 admin users and groups + */ + public static OzoneAdmins getS3Admins(OzoneConfiguration conf) { Review Comment: Please cover with tests ########## hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3secret/TestSecretGenerate.java: ########## @@ -51,6 +52,7 @@ */ @ExtendWith(MockitoExtension.class) class TestSecretGenerate { Review Comment: Tests are failing locally. ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/conf/OzoneS3ConfigUtils.java: ########## @@ -0,0 +1,133 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.conf; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.server.OzoneAdmins; +import org.apache.hadoop.security.UserGroupInformation; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_GROUPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_S3_ADMINISTRATORS_GROUPS; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.annotation.Nullable; + +import java.io.IOException; +import java.util.Collection; + +/** + * Config based utilities for Ozone S3. + */ +public final class OzoneS3ConfigUtils { + static final Logger LOG = LoggerFactory.getLogger(OzoneS3ConfigUtils.class); + + private OzoneS3ConfigUtils() { } + + /** + * Get the list of S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator users + * + * If ozone.s3.administrators value is empty string or unset, + * defaults to ozone.administrators value. + */ + public static Collection<String> getS3AdminsFromConfig(OzoneConfiguration conf) throws IOException { + Collection<String> ozAdmins = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS); + + if (ozAdmins == null || ozAdmins.isEmpty()) { + ozAdmins = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS); + } + String omSPN = UserGroupInformation.getCurrentUser().getShortUserName(); + if (!ozAdmins.contains(omSPN)) { + ozAdmins.add(omSPN); + } + + return ozAdmins; + } + + /** + * Get the list of the groups that are a part S3 administrators from Ozone config. + * + * @param conf An instance of {@link OzoneConfiguration} being used + * @return A {@link Collection} of the S3 administrator groups + * + * If ozone.s3.administrators.groups value is empty or unset, + * defaults to the ozone.administrators.groups value + */ + public static Collection<String> getS3AdminsGroupsFromConfig(OzoneConfiguration conf) { + Collection<String> s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS_GROUPS); + + if (s3AdminsGroup.isEmpty() && conf.getTrimmedStringCollection(OZONE_S3_ADMINISTRATORS).isEmpty()) { + s3AdminsGroup = conf.getTrimmedStringCollection(OZONE_ADMINISTRATORS_GROUPS); + } + + return s3AdminsGroup; + } + + /** + * Get the users and groups that are a part of S3 administrators. + * @param conf Stores an instance of {@link OzoneConfiguration} being used + * @return an instance of {@link OzoneAdmins} containing the S3 admin users and groups + */ + public static OzoneAdmins getS3Admins(OzoneConfiguration conf) { + Collection<String> s3Admins; + try { + s3Admins = getS3AdminsFromConfig(conf); + } catch (IOException ie) { + s3Admins = null; + } + Collection<String> s3AdminGroups = getS3AdminsGroupsFromConfig(conf); + + if (LOG.isDebugEnabled()) { + if (null == s3Admins) { + LOG.debug("S3 Admins are not set in configuration"); + } + if (null == s3AdminGroups) { + LOG.debug("S3 Admin Groups are not set in configuration"); + } + } + return new OzoneAdmins(s3Admins, s3AdminGroups); + } + + /** + * Check if the provided user is an S3 administrator. + * @param user An instance of {@link UserGroupInformation} with information about the user to verify + * @param s3Admins An instance of {@link OzoneAdmins} containing information + * of the S3 administrator users and groups in the system + * @return {@code true} if the provided user is an S3 administrator else {@code false} + */ + public static boolean isS3Admin(@Nullable UserGroupInformation user, OzoneAdmins s3Admins) { + return null != user && s3Admins.isAdmin(user); + } + + /** + * Check if the provided user is an S3 administrator. + * @param user An instance of {@link UserGroupInformation} with information about the user to verify + * @param conf An instance of {@link OzoneConfiguration} being used + * @return {@code true} if the provided user is an S3 administrator else {@code false} + */ + public static boolean isS3Admin(@Nullable UserGroupInformation user, OzoneConfiguration conf) { Review Comment: Please cover with tests ########## hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3secret/TestSecretRevoke.java: ########## @@ -26,42 +26,43 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.hadoop.ozone.client.ObjectStoreStub; import org.apache.hadoop.ozone.client.OzoneClient; import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.client.protocol.ClientProtocol; import org.apache.hadoop.ozone.om.exceptions.OMException; -import org.apache.ozone.test.tag.Unhealthy; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.OK; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS; import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.ACCESS_DENIED; import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.S3_SECRET_NOT_FOUND; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** * Test for S3 secret revoke endpoint. */ @ExtendWith(MockitoExtension.class) public class TestSecretRevoke { Review Comment: Tests are failing locally. -- 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]
