diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMDelegationTokenRequest.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMDelegationTokenRequest.java new file mode 100644 index 000000000000..08c95f6e8f2e --- /dev/null +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMDelegationTokenRequest.java @@ -0,0 +1,71 @@ +/* + * 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.security; + +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OMMetrics; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.OzoneManager; +import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerDoubleBufferHelper; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_DB_DIRS; +import org.junit.Rule; +import org.junit.Before; +import org.junit.After; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; +import static org.mockito.Mockito.when; + +/** + * Base class for testing OM delegation token request. + */ +@SuppressWarnings("visibilitymodifier") +public class TestOMDelegationTokenRequest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + protected OzoneManager ozoneManager; + protected OMMetrics omMetrics; + protected OMMetadataManager omMetadataManager; + protected ConfigurationSource conf; + + // Just setting OzoneManagerDoubleBuffer which does nothing. + protected OzoneManagerDoubleBufferHelper ozoneManagerDoubleBufferHelper = + ((response, transactionIndex) -> { + return null; + }); + + @Before + public void setup() throws Exception { + ozoneManager = Mockito.mock(OzoneManager.class); + + conf = new OzoneConfiguration(); + ((OzoneConfiguration) conf) + .set(OZONE_OM_DB_DIRS, folder.newFolder().getAbsolutePath()); + omMetadataManager = new OmMetadataManagerImpl((OzoneConfiguration) conf); + when(ozoneManager.getMetadataManager()).thenReturn(omMetadataManager); + } + + @After + public void stop() { + Mockito.framework().clearInlineMocks(); + } +} diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMGetDelegationTokenRequest.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMGetDelegationTokenRequest.java new file mode 100644 index 000000000000..df0fcb9fd19a --- /dev/null +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/security/TestOMGetDelegationTokenRequest.java @@ -0,0 +1,221 @@ +/* + * 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.security;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import com.google.common.base.Optional;
+import java.util.UUID;
+import org.apache.hadoop.ozone.om.response.OMClientResponse;
+import org.apache.hadoop.ozone.security.OzoneDelegationTokenSecretManager;
+import org.apache.hadoop.ozone.security.OzoneTokenIdentifier;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Status;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos
+ .OMRequest;
+import static org.apache.hadoop.ozone.security.OzoneTokenIdentifier.KIND_NAME;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.proto.SecurityProtos.GetDelegationTokenRequestProto;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.hadoop.io.Text;
+import org.mockito.Mockito;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.when;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * The class tests OMGetDelegationTokenRequest.
+ */
+public class TestOMGetDelegationTokenRequest extends
+ TestOMDelegationTokenRequest {
+
+ private OzoneDelegationTokenSecretManager secretManager;
+ private OzoneTokenIdentifier identifier;
+ private Token
+ * 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.response.security;
+
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.utils.db.BatchOperation;
+import org.apache.hadoop.ozone.om.OMConfigKeys;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.OmMetadataManagerImpl;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+import java.io.IOException;
+
+/** Base test class for delegation token response. */
+@SuppressWarnings("visibilitymodifier")
+public class TestOMDelegationTokenResponse {
+
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ protected ConfigurationSource conf;
+ protected OMMetadataManager omMetadataManager;
+ protected BatchOperation batchOperation;
+
+ @Before
+ public void setup() throws IOException {
+ conf = new OzoneConfiguration();
+ ((OzoneConfiguration) conf).set(OMConfigKeys.OZONE_OM_DB_DIRS,
+ folder.newFolder().getAbsolutePath());
+ omMetadataManager = new OmMetadataManagerImpl((OzoneConfiguration) conf);
+ batchOperation = omMetadataManager.getStore().initBatchOperation();
+ }
+}
diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/TestOMGetDelegationTokenResponse.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/TestOMGetDelegationTokenResponse.java
new file mode 100644
index 000000000000..df90d7ec8931
--- /dev/null
+++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/TestOMGetDelegationTokenResponse.java
@@ -0,0 +1,95 @@
+/*
+ * 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.response.security;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.ozone.om.request.security.OMGetDelegationTokenRequest;
+import org.apache.hadoop.ozone.security.OzoneTokenIdentifier;
+import org.apache.hadoop.security.proto.SecurityProtos.GetDelegationTokenRequestProto;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos
+ .OMRequest;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos
+ .OMResponse;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.UpdateGetDelegationTokenRequest;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Status;
+import java.io.IOException;
+import java.util.UUID;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/** The class tests OMGetDelegationTokenResponse. */
+public class TestOMGetDelegationTokenResponse extends
+ TestOMDelegationTokenResponse {
+
+ private OzoneTokenIdentifier identifier;
+ private UpdateGetDelegationTokenRequest updateGetDelegationTokenRequest;
+
+ @Before
+ public void setupGetDelegationToken() {
+ Text tester = new Text("tester");
+ identifier = new OzoneTokenIdentifier(tester, tester, tester);
+ identifier.setOmCertSerialId("certID");
+
+ GetDelegationTokenRequestProto getDelegationTokenRequestProto =
+ GetDelegationTokenRequestProto.newBuilder()
+ .setRenewer(identifier.getRenewer().toString())
+ .build();
+
+ OMRequest omRequest = OMRequest.newBuilder()
+ .setClientId(UUID.randomUUID().toString())
+ .setCmdType(Type.GetDelegationToken)
+ .setGetDelegationTokenRequest(getDelegationTokenRequestProto)
+ .build();
+
+ updateGetDelegationTokenRequest =
+ new OMGetDelegationTokenRequest(omRequest)
+ .getOmRequest()
+ .getUpdateGetDelegationTokenRequest();
+ }
+
+ @Test
+ public void testAddToDBBatch() throws IOException {
+ OMResponse omResponse = OMResponse.newBuilder()
+ .setCmdType(Type.GetDelegationToken)
+ .setStatus(Status.OK)
+ .setSuccess(true)
+ .setGetDelegationTokenResponse(
+ updateGetDelegationTokenRequest
+ .getGetDelegationTokenResponse())
+ .build();
+
+ long renewTime = 1000L;
+ OMGetDelegationTokenResponse getDelegationTokenResponse =
+ new OMGetDelegationTokenResponse(identifier, renewTime, omResponse);
+
+ getDelegationTokenResponse.addToDBBatch(omMetadataManager, batchOperation);
+ omMetadataManager.getStore().commitBatchOperation(batchOperation);
+
+ long rowNumInTable = 1;
+ long rowNumInTokenTable = omMetadataManager
+ .countRowsInTable(omMetadataManager.getDelegationTokenTable());
+ Assert.assertEquals(rowNumInTable, rowNumInTokenTable);
+
+ long renewTimeInTable = omMetadataManager.getDelegationTokenTable()
+ .get(identifier);
+ Assert.assertEquals(renewTime, renewTimeInTable);
+ }
+}
diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/package-info.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/package-info.java
new file mode 100644
index 000000000000..1c197c6f4dc9
--- /dev/null
+++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/response/security/package-info.java
@@ -0,0 +1,21 @@
+/**
+ * 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 contains test classes for delegation token responses.
+ */
+package org.apache.hadoop.ozone.om.response.security;