Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.crypto.key.kms.server;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache;
import org.apache.curator.test.TestingServer;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -48,7 +49,6 @@
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.security.token.delegation.web.DelegationTokenIdentifier;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.Whitebox;
import org.apache.hadoop.util.Time;
import org.apache.http.client.utils.URIBuilder;
import org.junit.After;
Expand All @@ -58,6 +58,7 @@
import org.junit.Test;
import org.junit.rules.Timeout;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.FieldReader;
import org.slf4j.event.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -929,6 +930,7 @@ public Void call() throws Exception {
}

@Test
@SuppressWarnings("unchecked")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I add @SuppressWarnings("unchecked") in this position, it will not bring additional type conversion risk.

public void testKMSProviderCaching() throws Exception {
Configuration conf = new Configuration();
File confDir = getTestDir();
Expand All @@ -946,11 +948,12 @@ public Void call() throws Exception {
KMSClientProvider kmscp = createKMSClientProvider(uri, conf);

// get the reference to the internal cache, to test invalidation.
ValueQueue vq =
(ValueQueue) Whitebox.getInternalState(kmscp, "encKeyVersionQueue");
ValueQueue vq = (ValueQueue) new FieldReader(kmscp,
FieldUtils.getField(KMSClientProvider.class, "encKeyVersionQueue", true)).read();
LoadingCache<String, LinkedBlockingQueue<EncryptedKeyVersion>> kq =
((LoadingCache<String, LinkedBlockingQueue<EncryptedKeyVersion>>)
Whitebox.getInternalState(vq, "keyQueues"));
(LoadingCache<String, LinkedBlockingQueue<EncryptedKeyVersion>>)
new FieldReader(vq, FieldUtils.getField(ValueQueue.class,
"keyQueues", true)).read();
EncryptedKeyVersion mockEKV = Mockito.mock(EncryptedKeyVersion.class);
when(mockEKV.getEncryptionKeyName()).thenReturn(keyName);
when(mockEKV.getEncryptionKeyVersionName()).thenReturn(mockVersionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.crypto.key.kms.server.KMS.KMSOp;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.Whitebox;
import org.apache.hadoop.util.ThreadUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
Expand All @@ -40,6 +41,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.mockito.internal.util.reflection.FieldReader;

public class TestKMSAudit {

Expand All @@ -63,7 +65,7 @@ public void setOutputStream(OutputStream out) {
}

@Rule
public final Timeout testTimeout = new Timeout(180000);
public final Timeout testTimeout = new Timeout(180000L, TimeUnit.MILLISECONDS);

@Before
public void setUp() throws IOException {
Expand Down Expand Up @@ -207,8 +209,9 @@ public void testAuditLogFormat() throws Exception {
@Test
public void testInitAuditLoggers() throws Exception {
// Default should be the simple logger
List<KMSAuditLogger> loggers = (List<KMSAuditLogger>) Whitebox
.getInternalState(kmsAudit, "auditLoggers");
List<KMSAuditLogger> loggers = (List<KMSAuditLogger>) new FieldReader(kmsAudit,
FieldUtils.getField(KMSAudit.class, "auditLoggers", true)).read();

Assert.assertEquals(1, loggers.size());
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());

Expand All @@ -218,8 +221,8 @@ public void testInitAuditLoggers() throws Exception {
SimpleKMSAuditLogger.class.getName() + ", "
+ SimpleKMSAuditLogger.class.getName());
final KMSAudit audit = new KMSAudit(conf);
loggers =
(List<KMSAuditLogger>) Whitebox.getInternalState(audit, "auditLoggers");
loggers = (List<KMSAuditLogger>) new FieldReader(audit,
FieldUtils.getField(KMSAudit.class, "auditLoggers", true)).read();
Assert.assertEquals(1, loggers.size());
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());

Expand Down