Search in sources :

Example 21 with URIParcel

use of com.disney.uriparcel.URIParcel in project groovity by disney.

the class TestParcels method testKeystore.

@Test
public void testKeystore() throws Exception {
    File myMapFile = File.createTempFile("testMapParcel", ".json");
    try {
        URI myUri = myMapFile.toURI();
        String password = "myKeystorePassword";
        Map<String, Object> config = new HashMap<>();
        config.put(KeyStoreValueHandler.KEYSTORE_TYPE, "JCEKS");
        config.put(KeyStoreValueHandler.KEYSTORE_PASSWORD, password);
        SecretKeySpec secretKey = new SecretKeySpec("nobodyKnows".getBytes(), "DES");
        KeyStore orig = KeyStore.getInstance("JCEKS");
        orig.load(null);
        orig.setKeyEntry("foobar", secretKey, password.toCharArray(), null);
        URIParcel<KeyStore> putstore = new URIParcel<KeyStore>(KeyStore.class, myUri, config);
        putstore.put(orig);
        RuntimeException e = null;
        try {
            URIParcel<KeyStore> myParcel = new URIParcel<KeyStore>(KeyStore.class, myUri);
            myParcel.call();
        } catch (RuntimeException r) {
            e = r;
        }
        Assert.assertNotNull("Expected error opening keystore with no config", e);
        URIParcel<KeyStore> myParcel = new URIParcel<KeyStore>(KeyStore.class, myUri, config);
        KeyStore result = myParcel.call();
        Key rkey = result.getKey("foobar", password.toCharArray());
        Assert.assertEquals(secretKey, rkey);
    } finally {
        myMapFile.delete();
    }
}
Also used : HashMap(java.util.HashMap) URIParcel(com.disney.uriparcel.URIParcel) SecretKeySpec(javax.crypto.spec.SecretKeySpec) File(java.io.File) URI(java.net.URI) KeyStore(java.security.KeyStore) Key(java.security.Key) Test(org.junit.Test)

Example 22 with URIParcel

use of com.disney.uriparcel.URIParcel in project groovity by disney.

the class TestJsonParcels method testJsonFile.

@Test
public void testJsonFile() throws Exception {
    File myMapFile = File.createTempFile("testMapParcel", ".json");
    FileWriter fw = new FileWriter(myMapFile);
    try {
        fw.write("{\"a\":1,\"b\":\"foo\"}");
    } finally {
        fw.close();
    }
    try {
        URI myUri = myMapFile.toURI();
        @SuppressWarnings("rawtypes") URIParcel<Map> myParcel = new URIParcel<>(Map.class, myUri);
        Map<?, ?> result = myParcel.call();
        Assert.assertEquals(1.0, result.get("a"));
    } finally {
        myMapFile.delete();
    }
}
Also used : URIParcel(com.disney.uriparcel.URIParcel) FileWriter(java.io.FileWriter) File(java.io.File) URI(java.net.URI) Map(java.util.Map) Test(org.junit.Test)

Example 23 with URIParcel

use of com.disney.uriparcel.URIParcel in project groovity by disney.

the class TestKeys method testKeyPair.

@Test
public void testKeyPair() throws Exception {
    URI keyLoc = new URI("mem:myPrivateKey");
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(2048);
    KeyPair keyPair = generator.generateKeyPair();
    URIParcel<KeyPair> myParcel = new URIParcel<KeyPair>(KeyPair.class, keyLoc);
    myParcel.put(keyPair);
    URIParcel<byte[]> rawParcel = new URIParcel<byte[]>(byte[].class, keyLoc);
    Assert.assertTrue("Expected PKCS8 private key file", new String(rawParcel.call()).startsWith("-----BEGIN RSA PRIVATE KEY-----"));
    myParcel = new URIParcel<KeyPair>(KeyPair.class, keyLoc);
    KeyPair copy = myParcel.call();
    Assert.assertEquals(keyPair.getPrivate(), copy.getPrivate());
}
Also used : KeyPair(java.security.KeyPair) URIParcel(com.disney.uriparcel.URIParcel) KeyPairGenerator(java.security.KeyPairGenerator) URI(java.net.URI) Test(org.junit.Test)

Example 24 with URIParcel

use of com.disney.uriparcel.URIParcel in project groovity by disney.

the class TestKeys method testEncryptedKeyPair.

@Test
public void testEncryptedKeyPair() throws Exception {
    URI keyLoc = new URI("mem:myEncryptedPrivateKey");
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(2048);
    KeyPair keyPair = generator.generateKeyPair();
    Map<String, Object> config = new HashMap<>();
    config.put("password", "hooloo");
    URIParcel<KeyPair> myParcel = new URIParcel<KeyPair>(KeyPair.class, keyLoc, config);
    myParcel.put(keyPair);
    URIParcel<byte[]> rawParcel = new URIParcel<byte[]>(byte[].class, keyLoc);
    Matcher encryptedPrivateMatcher = Pattern.compile("^-----BEGIN RSA PRIVATE KEY-----\\s+Proc-Type: 4,ENCRYPTED").matcher(new String(rawParcel.call()));
    Assert.assertTrue("Expected PKCS8 encrypted private key file", encryptedPrivateMatcher.find());
    Exception e = null;
    try {
        myParcel = new URIParcel<KeyPair>(KeyPair.class, keyLoc);
        myParcel.call();
    } catch (IllegalArgumentException e1) {
        e = e1;
    }
    Assert.assertNotNull("Expected error", e);
    myParcel = new URIParcel<KeyPair>(KeyPair.class, keyLoc, config);
    KeyPair copy = myParcel.call();
    Assert.assertEquals(keyPair.getPrivate(), copy.getPrivate());
}
Also used : KeyPair(java.security.KeyPair) HashMap(java.util.HashMap) URIParcel(com.disney.uriparcel.URIParcel) Matcher(java.util.regex.Matcher) KeyPairGenerator(java.security.KeyPairGenerator) URI(java.net.URI) Test(org.junit.Test)

Aggregations

URIParcel (com.disney.uriparcel.URIParcel)24 URI (java.net.URI)18 Test (org.junit.Test)17 HashMap (java.util.HashMap)11 File (java.io.File)8 KeyStore (java.security.KeyStore)8 KeyStoreKeyChainImpl (com.disney.http.auth.keychain.KeyStoreKeyChainImpl)7 KeyChainKeyLoader (com.disney.http.auth.client.keyloader.KeyChainKeyLoader)5 KeyPair (java.security.KeyPair)5 Date (java.util.Date)5 KeyChain (com.disney.http.auth.keychain.KeyChain)4 Key (java.security.Key)4 KeyPairGenerator (java.security.KeyPairGenerator)4 KeyObjectKeyLoader (com.disney.http.auth.client.keyloader.KeyObjectKeyLoader)3 HttpSignatureSigner (com.disney.http.auth.client.signer.HttpSignatureSigner)3 URL (java.net.URL)3 PublicKey (java.security.PublicKey)3 Map (java.util.Map)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 MemoryPayload (com.disney.uriparcel.MemoryPayload)2