ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [13/32] ambari git commit: AMBARI-13214. Create a credentials resource used to securely set, update, and remove credentials used by Ambari (rlevas)
Date Fri, 02 Oct 2015 21:12:17 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/3b411744/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImplTest.java
new file mode 100644
index 0000000..daea963
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImplTest.java
@@ -0,0 +1,293 @@
+/*
+ * 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.ambari.server.security.encryption;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import junit.framework.Assert;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.security.SecurePasswordHelper;
+import org.apache.ambari.server.security.credential.PrincipalKeyCredential;
+import org.apache.ambari.server.state.stack.OsFamily;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import static org.easymock.EasyMock.createNiceMock;
+
+public class CredentialStoreServiceImplTest {
+
+  @Rule
+  public TemporaryFolder tmpFolder = new TemporaryFolder();
+
+  private CredentialStoreServiceImpl credentialStoreService;
+
+  private static final String CLUSTER_NAME = "C1";
+
+  @Before
+  public void setUp() throws Exception {
+    tmpFolder.create();
+    final File masterKeyFile = tmpFolder.newFile(Configuration.MASTER_KEY_FILENAME_DEFAULT);
+    Assert.assertTrue(MasterKeyServiceImpl.initializeMasterKeyFile(masterKeyFile, "secret"));
+
+    Injector injector = Guice.createInjector(new AbstractModule() {
+      @Override
+      protected void configure() {
+        Properties properties = new Properties();
+
+        properties.setProperty(Configuration.MASTER_KEY_LOCATION, tmpFolder.getRoot().getAbsolutePath());
+        properties.setProperty(Configuration.MASTER_KEYSTORE_LOCATION, tmpFolder.getRoot().getAbsolutePath());
+
+        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+        bind(SecurePasswordHelper.class).toInstance(new SecurePasswordHelper());
+        bind(Configuration.class).toInstance(new Configuration(properties));
+      }
+    });
+
+
+    // Test to make sure the MasterKey file is ok...
+    MasterKeyService masterKeyService = new MasterKeyServiceImpl(masterKeyFile);
+    Assert.assertTrue(masterKeyService.isMasterKeyInitialized());
+
+    credentialStoreService = injector.getInstance(CredentialStoreServiceImpl.class);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    credentialStoreService = null;
+    tmpFolder.delete();
+  }
+
+  @Test
+  public void testSetAndGetCredential_Temporary() throws Exception {
+    PrincipalKeyCredential credential = new PrincipalKeyCredential("username", "password");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential, CredentialStoreType.TEMPORARY);
+
+    Assert.assertEquals(credential, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.TEMPORARY));
+    Assert.assertEquals(credential, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED));
+  }
+
+  @Test
+  public void testSetAndGetCredential_Persisted() throws Exception {
+    PrincipalKeyCredential credential = new PrincipalKeyCredential("username", "password");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential, CredentialStoreType.PERSISTED);
+
+    Assert.assertEquals(credential, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.PERSISTED));
+    Assert.assertEquals(credential, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+  }
+
+  @Test
+  public void testRemoveCredential_Temporary() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.TEMPORARY);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.TEMPORARY);
+
+    // Nothing should happen if forcing remove from persistent CredentialStore
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED);
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.TEMPORARY));
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    // Remove should happen if forcing remove from temporary CredentialStore
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY);
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    // The other credentials should remain untouched
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test2"));
+  }
+
+  @Test
+  public void testRemoveCredential_Persisted() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.PERSISTED);
+
+    // Nothing should happen if forcing remove from temporary CredentialStore
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY);
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.PERSISTED));
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    // Remove should happen if forcing remove from persistent CredentialStore
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED);
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    // The other credentials should remain untouched
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test2"));
+  }
+
+  @Test
+  public void testRemoveCredential_Either() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    PrincipalKeyCredential credential3 = new PrincipalKeyCredential("username3", "password3");
+    PrincipalKeyCredential credential4 = new PrincipalKeyCredential("username4", "password4");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test3", credential3, CredentialStoreType.TEMPORARY);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test4", credential4, CredentialStoreType.TEMPORARY);
+
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test1");
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test2"));
+    Assert.assertEquals(credential3, credentialStoreService.getCredential(CLUSTER_NAME, "test3"));
+    Assert.assertEquals(credential4, credentialStoreService.getCredential(CLUSTER_NAME, "test4"));
+
+    credentialStoreService.removeCredential(CLUSTER_NAME, "test3");
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test2"));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test3"));
+    Assert.assertEquals(credential4, credentialStoreService.getCredential(CLUSTER_NAME, "test4"));
+  }
+
+
+  @Test
+  public void testUpdateCredential() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.PERSISTED));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.TEMPORARY);
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.TEMPORARY));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED));
+    Assert.assertEquals(credential1, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential2, CredentialStoreType.PERSISTED);
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test1",
CredentialStoreType.PERSISTED));
+    Assert.assertNull(credentialStoreService.getCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+    Assert.assertEquals(credential2, credentialStoreService.getCredential(CLUSTER_NAME, "test1"));
+  }
+
+
+  @Test
+  public void testContainsCredential() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    PrincipalKeyCredential credential3 = new PrincipalKeyCredential("username3", "password3");
+    PrincipalKeyCredential credential4 = new PrincipalKeyCredential("username4", "password4");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test3", credential3, CredentialStoreType.TEMPORARY);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test4", credential4, CredentialStoreType.TEMPORARY);
+
+
+    Assert.assertTrue(credentialStoreService.containsCredential(CLUSTER_NAME, "test1", CredentialStoreType.PERSISTED));
+    Assert.assertFalse(credentialStoreService.containsCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+    Assert.assertFalse(credentialStoreService.containsCredential(CLUSTER_NAME, "test3", CredentialStoreType.PERSISTED));
+
+    Assert.assertFalse(credentialStoreService.containsCredential(CLUSTER_NAME, "test1", CredentialStoreType.TEMPORARY));
+    Assert.assertFalse(credentialStoreService.containsCredential(CLUSTER_NAME, "test3", CredentialStoreType.PERSISTED));
+    Assert.assertTrue(credentialStoreService.containsCredential(CLUSTER_NAME, "test3", CredentialStoreType.TEMPORARY));
+
+    Assert.assertTrue(credentialStoreService.containsCredential(CLUSTER_NAME, "test1"));
+    Assert.assertTrue(credentialStoreService.containsCredential(CLUSTER_NAME, "test3"));
+  }
+
+
+  @Test
+  public void testIsCredentialPersisted() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    PrincipalKeyCredential credential3 = new PrincipalKeyCredential("username3", "password3");
+    PrincipalKeyCredential credential4 = new PrincipalKeyCredential("username4", "password4");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test3", credential3, CredentialStoreType.TEMPORARY);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test4", credential4, CredentialStoreType.TEMPORARY);
+
+    Assert.assertEquals(CredentialStoreType.PERSISTED, credentialStoreService.getCredentialStoreType(CLUSTER_NAME,
"test1"));
+    Assert.assertEquals(CredentialStoreType.PERSISTED, credentialStoreService.getCredentialStoreType(CLUSTER_NAME,
"test2"));
+    Assert.assertEquals(CredentialStoreType.TEMPORARY, credentialStoreService.getCredentialStoreType(CLUSTER_NAME,
"test3"));
+    Assert.assertEquals(CredentialStoreType.TEMPORARY, credentialStoreService.getCredentialStoreType(CLUSTER_NAME,
"test4"));
+
+    try {
+      credentialStoreService.getCredentialStoreType(CLUSTER_NAME, "test5");
+      Assert.fail("Expected AmbariException to be thrown");
+    } catch (AmbariException e) {
+      // expected
+    }
+  }
+
+  @Test
+  public void testListCredentials() throws Exception {
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+    PrincipalKeyCredential credential2 = new PrincipalKeyCredential("username2", "password2");
+    PrincipalKeyCredential credential3 = new PrincipalKeyCredential("username3", "password3");
+    PrincipalKeyCredential credential4 = new PrincipalKeyCredential("username4", "password4");
+    credentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test2", credential2, CredentialStoreType.PERSISTED);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test3", credential3, CredentialStoreType.TEMPORARY);
+    credentialStoreService.setCredential(CLUSTER_NAME, "test4", credential4, CredentialStoreType.TEMPORARY);
+
+    Map<String, CredentialStoreType> credentials = credentialStoreService.listCredentials(CLUSTER_NAME);
+    Assert.assertNotNull(credentials);
+    Assert.assertEquals(4, credentials.size());
+    Assert.assertEquals(CredentialStoreType.PERSISTED, credentials.get("test1"));
+    Assert.assertEquals(CredentialStoreType.PERSISTED, credentials.get("test2"));
+    Assert.assertEquals(CredentialStoreType.TEMPORARY, credentials.get("test3"));
+    Assert.assertEquals(CredentialStoreType.TEMPORARY, credentials.get("test4"));
+  }
+
+  @Test(expected = AmbariException.class)
+  public void testFailToReinitialize_Persisted() throws Exception {
+    // This should throw an exception not matter what the arguments are....
+    credentialStoreService.initializePersistedCredentialStore(null, null);
+  }
+
+  @Test(expected = AmbariException.class)
+  public void testFailToReinitialize_Temporary() throws Exception {
+    // This should throw an exception not matter what the arguments are....
+    credentialStoreService.initializeTemporaryCredentialStore(1, TimeUnit.MINUTES, false);
+  }
+
+  @Test
+  public void testFailNotInitialized() throws Exception {
+    Configuration configuration = new Configuration(new Properties());
+    CredentialStoreService uninitializedCredentialStoreService = new CredentialStoreServiceImpl(configuration,
new SecurePasswordHelper());
+    PrincipalKeyCredential credential1 = new PrincipalKeyCredential("username1", "password1");
+
+    // The temporary store should always be initialized.... this should succeed.
+    uninitializedCredentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1,
CredentialStoreType.TEMPORARY);
+
+    try {
+      uninitializedCredentialStoreService.setCredential(CLUSTER_NAME, "test1", credential1,
CredentialStoreType.PERSISTED);
+      Assert.fail("AmbariException should have been thrown");
+    } catch (AmbariException e) {
+      // This is expected...
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/3b411744/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
deleted file mode 100644
index 9725746..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
+++ /dev/null
@@ -1,239 +0,0 @@
-/**
- * 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.ambari.server.security.encryption;
-
-import junit.framework.Assert;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
-import java.util.concurrent.TimeUnit;
-
-public class CredentialStoreServiceTest {
-
-  @Rule
-  public TemporaryFolder tmpFolder = new TemporaryFolder();
-
-  @Before
-  public void setUp() throws Exception {
-    tmpFolder.create();
-  }
-
-  @After
-  public void cleanUp() throws Exception {
-    tmpFolder.delete();
-  }
-
-  @Test
-  public void testFileBasedCredentialStoreService_AddCredentialToStoreWithPersistMaster()
throws Exception {
-    addCredentialToStoreWithPersistMasterTest(new FileBasedCredentialStoreServiceFactory(),
new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testFileBasedCredentialStoreService_AddCredentialToStore() throws Exception
{
-    addCredentialToStoreTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testFileBasedCredentialStoreService_GetCredential() throws Exception {
-    getCredentialTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testFileBasedCredentialStoreService_RemoveCredential() throws Exception {
-    removeCredentialTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testInMemoryCredentialStoreService_AddCredentialToStoreWithPersistMaster()
throws Exception {
-    addCredentialToStoreWithPersistMasterTest(new InMemoryCredentialStoreServiceFactory(),
new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testInMemoryCredentialStoreService_AddCredentialToStore() throws Exception
{
-    addCredentialToStoreTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testInMemoryCredentialStoreService_GetCredential() throws Exception {
-    getCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testInMemoryCredentialStoreService_RemoveCredential() throws Exception {
-    removeCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  @Test
-  public void testInMemoryCredentialStoreService_CredentialExpired() throws Exception {
-    getExpiredCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
-  }
-
-  private void addCredentialToStoreWithPersistMasterTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
-                                                         MasterKeyServiceFactory masterKeyServiceFactory)
throws Exception {
-    File directory = tmpFolder.getRoot();
-
-    String masterKey = "ThisIsSomeSecretPassPhrase1234";
-    File masterKeyFile = new File(directory, "master");
-
-    MasterKeyService masterKeyService = masterKeyServiceFactory.createPersisted(masterKeyFile,
masterKey);
-    CredentialStoreService credentialStoreService = credentialStoreServiceFactory.create(directory,
masterKeyService);
-
-    String password = "mypassword";
-    credentialStoreService.addCredential("myalias", password.toCharArray());
-    char[] credential = credentialStoreService.getCredential("myalias");
-    Assert.assertEquals(password, new String(credential));
-
-    Assert.assertTrue(masterKeyFile.exists());
-  }
-
-  private void addCredentialToStoreTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
-                                        MasterKeyServiceFactory masterKeyServiceFactory)
throws Exception {
-    File directory = tmpFolder.getRoot();
-
-    String masterKey = "ThisIsSomeSecretPassPhrase1234";
-    File masterKeyFile = new File(directory, "master");
-
-    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
-    CredentialStoreService credentialStoreService = credentialStoreServiceFactory.create(directory,
masterKeyService);
-
-    String password = "mypassword";
-    credentialStoreService.addCredential("password", password.toCharArray());
-    char[] credential = credentialStoreService.getCredential("password");
-    Assert.assertEquals(password, new String(credential));
-
-    credentialStoreService.addCredential("null_password", null);
-    Assert.assertNull(credentialStoreService.getCredential("null_password"));
-
-    credentialStoreService.addCredential("empty_password", new char[0]);
-    Assert.assertNull(credentialStoreService.getCredential("empty_password"));
-
-    Assert.assertFalse(masterKeyFile.exists());
-  }
-
-  private void getCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
-                                 MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
-    File directory = tmpFolder.getRoot();
-
-    String masterKey = "ThisIsSomeSecretPassPhrase1234";
-
-    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
-    CredentialStoreService credentialStoreService = credentialStoreServiceFactory.create(directory,
masterKeyService);
-
-    Assert.assertNull(credentialStoreService.getCredential(""));
-    Assert.assertNull(credentialStoreService.getCredential(null));
-
-    String password = "mypassword";
-    credentialStoreService.addCredential("myalias", password.toCharArray());
-    char[] credential = credentialStoreService.getCredential("myalias");
-    Assert.assertEquals(password, new String(credential));
-
-    Assert.assertNull(credentialStoreService.getCredential("does_not_exist"));
-  }
-
-  private void getExpiredCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
-                                 MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
-    File directory = tmpFolder.getRoot();
-
-    String masterKey = "ThisIsSomeSecretPassPhrase1234";
-
-    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
-    CredentialStoreService credentialStoreService = credentialStoreServiceFactory.create(directory,
masterKeyService);
-
-    String password = "mypassword";
-    credentialStoreService.addCredential("myalias", password.toCharArray());
-    Assert.assertEquals(password, new String(credentialStoreService.getCredential("myalias")));
-
-    Thread.sleep(250);
-    Assert.assertEquals(password, new String(credentialStoreService.getCredential("myalias")));
-
-    Thread.sleep(550);
-    Assert.assertNull(password, credentialStoreService.getCredential("myalias"));
-
-  }
-
-  private void removeCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
-                                    MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
-    File directory = tmpFolder.getRoot();
-
-    String masterKey = "ThisIsSomeSecretPassPhrase1234";
-
-    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
-    CredentialStoreService credentialStoreService = credentialStoreServiceFactory.create(directory,
masterKeyService);
-
-    String password = "mypassword";
-    credentialStoreService.addCredential("myalias", password.toCharArray());
-
-    char[] credential = credentialStoreService.getCredential("myalias");
-    Assert.assertEquals(password, new String(credential));
-
-    credentialStoreService = credentialStoreServiceFactory.create(directory, masterKeyService);
-    credentialStoreService.setMasterKeyService(masterKeyService);
-
-    credentialStoreService.removeCredential("myalias");
-    Assert.assertNull(credentialStoreService.getCredential("myalias"));
-
-    credentialStoreService.removeCredential("does_not_exist");
-  }
-
-  private interface CredentialStoreServiceFactory {
-    CredentialStoreService create(File directory, MasterKeyService masterKeyService);
-  }
-
-  private class FileBasedCredentialStoreServiceFactory implements CredentialStoreServiceFactory
{
-    @Override
-    public CredentialStoreService create(File directory, MasterKeyService masterKeyService)
{
-      CredentialStoreService credentialStoreService = new FileBasedCredentialStoreService(directory.getAbsolutePath());
-      credentialStoreService.setMasterKeyService(masterKeyService);
-      return credentialStoreService;
-    }
-  }
-
-  private class InMemoryCredentialStoreServiceFactory implements CredentialStoreServiceFactory
{
-    @Override
-    public CredentialStoreService create(File directory, MasterKeyService masterKeyService)
{
-      CredentialStoreService credentialStoreService = new InMemoryCredentialStoreService(500,
TimeUnit.MILLISECONDS, true);
-      credentialStoreService.setMasterKeyService(masterKeyService);
-      return credentialStoreService;
-    }
-  }
-
-  private interface MasterKeyServiceFactory {
-    MasterKeyService create(String masterKey);
-
-    MasterKeyService createPersisted(File masterKeyFile, String masterKey);
-  }
-
-  private class DefaultMasterKeyServiceFactory implements MasterKeyServiceFactory {
-
-    @Override
-    public MasterKeyService create(String masterKey) {
-      return new MasterKeyServiceImpl(masterKey);
-    }
-
-    @Override
-    public MasterKeyService createPersisted(File masterKeyFile, String masterKey) {
-      MasterKeyServiceImpl.initializeMasterKeyFile(masterKeyFile, masterKey);
-      return new MasterKeyServiceImpl(masterKeyFile);
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3b411744/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
new file mode 100644
index 0000000..af0eac6
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
@@ -0,0 +1,248 @@
+/**
+ * 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.ambari.server.security.encryption;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.security.credential.Credential;
+import org.apache.ambari.server.security.credential.GenericKeyCredential;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.util.concurrent.TimeUnit;
+
+public class CredentialStoreTest {
+
+  @Rule
+  public TemporaryFolder tmpFolder = new TemporaryFolder();
+
+  @Before
+  public void setUp() throws Exception {
+    tmpFolder.create();
+  }
+
+  @After
+  public void cleanUp() throws Exception {
+    tmpFolder.delete();
+  }
+
+  @Test
+  public void testFileBasedCredentialStoreService_AddCredentialToStoreWithPersistMaster()
throws Exception {
+    addCredentialToStoreWithPersistMasterTest(new FileBasedCredentialStoreServiceFactory(),
new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testFileBasedCredentialStoreService_AddCredentialToStore() throws Exception
{
+    addCredentialToStoreTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testFileBasedCredentialStoreService_GetCredential() throws Exception {
+    getCredentialTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testFileBasedCredentialStoreService_RemoveCredential() throws Exception {
+    removeCredentialTest(new FileBasedCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testInMemoryCredentialStoreService_AddCredentialToStoreWithPersistMaster()
throws Exception {
+    addCredentialToStoreWithPersistMasterTest(new InMemoryCredentialStoreServiceFactory(),
new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testInMemoryCredentialStoreService_AddCredentialToStore() throws Exception
{
+    addCredentialToStoreTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testInMemoryCredentialStoreService_GetCredential() throws Exception {
+    getCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testInMemoryCredentialStoreService_RemoveCredential() throws Exception {
+    removeCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  @Test
+  public void testInMemoryCredentialStoreService_CredentialExpired() throws Exception {
+    getExpiredCredentialTest(new InMemoryCredentialStoreServiceFactory(), new DefaultMasterKeyServiceFactory());
+  }
+
+  private void addCredentialToStoreWithPersistMasterTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
+                                                         MasterKeyServiceFactory masterKeyServiceFactory)
throws Exception {
+    File directory = tmpFolder.getRoot();
+
+    String masterKey = "ThisIsSomeSecretPassPhrase1234";
+    File masterKeyFile = new File(directory, "master");
+
+    MasterKeyService masterKeyService = masterKeyServiceFactory.createPersisted(masterKeyFile,
masterKey);
+    CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+
+    String password = "mypassword";
+    credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
+    Credential credential = credentialStore.getCredential("myalias");
+    Assert.assertEquals(password, new String(credential.toValue()));
+
+    Assert.assertTrue(masterKeyFile.exists());
+  }
+
+  private void addCredentialToStoreTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
+                                        MasterKeyServiceFactory masterKeyServiceFactory)
throws Exception {
+    File directory = tmpFolder.getRoot();
+
+    String masterKey = "ThisIsSomeSecretPassPhrase1234";
+    File masterKeyFile = new File(directory, "master");
+
+    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
+    CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+
+    String password = "mypassword";
+    credentialStore.addCredential("password", new GenericKeyCredential(password.toCharArray()));
+    Credential credential = credentialStore.getCredential("password");
+    Assert.assertEquals(password, new String(credential.toValue()));
+
+    credentialStore.addCredential("null_password", null);
+    Assert.assertNull(credentialStore.getCredential("null_password"));
+
+    credentialStore.addCredential("empty_password", new GenericKeyCredential(new char[0]));
+    Assert.assertNull(credentialStore.getCredential("empty_password"));
+
+    Assert.assertFalse(masterKeyFile.exists());
+  }
+
+  private void getCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
+                                 MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
+    File directory = tmpFolder.getRoot();
+
+    String masterKey = "ThisIsSomeSecretPassPhrase1234";
+
+    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
+    CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+
+    Assert.assertNull(credentialStore.getCredential(""));
+    Assert.assertNull(credentialStore.getCredential(null));
+
+    String password = "mypassword";
+    credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
+    Credential credential = credentialStore.getCredential("myalias");
+    Assert.assertEquals(password, new String(credential.toValue()));
+
+    Assert.assertNull(credentialStore.getCredential("does_not_exist"));
+  }
+
+  private void getExpiredCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
+                                 MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
+    File directory = tmpFolder.getRoot();
+
+    String masterKey = "ThisIsSomeSecretPassPhrase1234";
+
+    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
+    CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+
+    String password = "mypassword";
+    credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
+    Assert.assertEquals(password, new String(credentialStore.getCredential("myalias").toValue()));
+
+    Thread.sleep(250);
+    Assert.assertEquals(password, new String(credentialStore.getCredential("myalias").toValue()));
+
+    Thread.sleep(550);
+    Assert.assertNull(password, credentialStore.getCredential("myalias"));
+
+  }
+
+  private void removeCredentialTest(CredentialStoreServiceFactory credentialStoreServiceFactory,
+                                    MasterKeyServiceFactory masterKeyServiceFactory) throws
Exception {
+    File directory = tmpFolder.getRoot();
+
+    String masterKey = "ThisIsSomeSecretPassPhrase1234";
+
+    MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
+    CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+
+    String password = "mypassword";
+    credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
+
+    Credential credential = credentialStore.getCredential("myalias");
+    Assert.assertEquals(password, new String(credential.toValue()));
+
+    credentialStore.removeCredential("myalias");
+    Assert.assertNull(credentialStore.getCredential("myalias"));
+
+    credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
+    credential = credentialStore.getCredential("myalias");
+    Assert.assertEquals(password, new String(credential.toValue()));
+
+    credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
+    credentialStore.setMasterKeyService(masterKeyService);
+
+    credentialStore.removeCredential("myalias");
+    Assert.assertNull(credentialStore.getCredential("myalias"));
+
+    credentialStore.removeCredential("does_not_exist");
+  }
+
+  private interface CredentialStoreServiceFactory {
+    CredentialStore create(File path, MasterKeyService masterKeyService);
+  }
+
+  private class FileBasedCredentialStoreServiceFactory implements CredentialStoreServiceFactory
{
+    @Override
+    public CredentialStore create(File path, MasterKeyService masterKeyService) {
+      CredentialStore credentialStore = new FileBasedCredentialStore(path);
+      credentialStore.setMasterKeyService(masterKeyService);
+      return credentialStore;
+    }
+  }
+
+  private class InMemoryCredentialStoreServiceFactory implements CredentialStoreServiceFactory
{
+    @Override
+    public CredentialStore create(File path, MasterKeyService masterKeyService) {
+      CredentialStore credentialStore = new InMemoryCredentialStore(500, TimeUnit.MILLISECONDS,
true);
+      credentialStore.setMasterKeyService(masterKeyService);
+      return credentialStore;
+    }
+  }
+
+  private interface MasterKeyServiceFactory {
+    MasterKeyService create(String masterKey);
+
+    MasterKeyService createPersisted(File masterKeyFile, String masterKey);
+  }
+
+  private class DefaultMasterKeyServiceFactory implements MasterKeyServiceFactory {
+
+    @Override
+    public MasterKeyService create(String masterKey) {
+      return new MasterKeyServiceImpl(masterKey);
+    }
+
+    @Override
+    public MasterKeyService createPersisted(File masterKeyFile, String masterKey) {
+      MasterKeyServiceImpl.initializeMasterKeyFile(masterKeyFile, masterKey);
+      return new MasterKeyServiceImpl(masterKeyFile);
+    }
+  }
+
+}


Mime
View raw message