mirror of https://github.com/apache/jclouds.git
added SshKeyPairGenerator
This commit is contained in:
parent
cab271d30f
commit
821b422b94
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.jclouds.crypto;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@Singleton
|
||||
public class RsaSshKeyPairGenerator implements SshKeyPairGenerator {
|
||||
protected final Crypto crypto;
|
||||
protected final SecureRandom secureRandom;
|
||||
|
||||
@Inject
|
||||
public RsaSshKeyPairGenerator(Crypto crypto, SecureRandom secureRandom) {
|
||||
this.crypto = checkNotNull(crypto, "crypto");
|
||||
this.secureRandom = checkNotNull(secureRandom, "secureRandom");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> get() {
|
||||
return SshKeys.generate(crypto.rsaKeyPairGenerator(), secureRandom);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.jclouds.crypto;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.inject.ImplementedBy;
|
||||
|
||||
/**
|
||||
* Creates a unique keypair without a passphrase. The resulting map has the following keys
|
||||
* <ul>
|
||||
* <li>public</li>
|
||||
* <li>private</li>
|
||||
* </ul>
|
||||
*/
|
||||
@ImplementedBy(RsaSshKeyPairGenerator.class)
|
||||
public interface SshKeyPairGenerator extends Supplier<Map<String, String>> {
|
||||
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.jclouds.crypto;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.jclouds.crypto.PemsTest.PRIVATE_KEY;
|
||||
import static org.jclouds.crypto.PemsTest.PUBLIC_KEY;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit", testName = "RsaSshKeyPairGeneratorTest")
|
||||
public class RsaSshKeyPairGeneratorTest {
|
||||
private static final String lineSeparator = System.getProperty("line.separator");
|
||||
|
||||
private KeyPair keyPair;
|
||||
private String openSshKey;
|
||||
|
||||
@BeforeClass
|
||||
public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {
|
||||
KeyFactory keyfactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = keyfactory.generatePrivate(Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY)));
|
||||
|
||||
PublicKey publicKey = keyfactory.generatePublic(Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY)));
|
||||
|
||||
keyPair = new KeyPair(publicKey, privateKey);
|
||||
openSshKey = SshKeys.encodeAsOpenSSH(RSAPublicKey.class.cast(publicKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApply() {
|
||||
Crypto crypto = createMock(Crypto.class);
|
||||
KeyPairGenerator rsaKeyPairGenerator = createMock(KeyPairGenerator.class);
|
||||
SecureRandom secureRandom = createMock(SecureRandom.class);
|
||||
|
||||
expect(crypto.rsaKeyPairGenerator()).andReturn(rsaKeyPairGenerator);
|
||||
rsaKeyPairGenerator.initialize(2048, secureRandom);
|
||||
expect(rsaKeyPairGenerator.genKeyPair()).andReturn(keyPair);
|
||||
|
||||
replay(crypto, rsaKeyPairGenerator, secureRandom);
|
||||
|
||||
RsaSshKeyPairGenerator supplier = new RsaSshKeyPairGenerator(crypto, secureRandom);
|
||||
|
||||
assertEquals(supplier.get(),
|
||||
ImmutableMap.of("public", openSshKey, "private", PRIVATE_KEY.replaceAll("\n", lineSeparator)));
|
||||
|
||||
verify(crypto, rsaKeyPairGenerator, secureRandom);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue