|
7 | 7 | */ |
8 | 8 | package com.joyent.manta.config; |
9 | 9 |
|
| 10 | +import com.joyent.http.signature.KeyFingerprinter; |
| 11 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 12 | +import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator; |
| 13 | +import org.bouncycastle.openssl.jcajce.JcaPEMWriter; |
| 14 | +import org.bouncycastle.openssl.jcajce.JcePEMEncryptorBuilder; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.StringWriter; |
10 | 18 | import java.net.URL; |
| 19 | +import java.security.KeyPair; |
| 20 | +import java.security.KeyPairGenerator; |
| 21 | +import java.security.NoSuchAlgorithmException; |
11 | 22 | import java.util.Properties; |
12 | 23 |
|
13 | 24 | /** |
@@ -97,4 +108,60 @@ static ConfigContext buildTestContext(String mantaUrl, |
97 | 108 |
|
98 | 109 | return testConfig; |
99 | 110 | } |
| 111 | + |
| 112 | + public static ImmutablePair<KeyPair, BaseChainedConfigContext> generateKeyPairBackedConfig() { |
| 113 | + return generateKeyPairBackedConfig(null); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Some test cases need a direct reference to a KeyPair along with it's associated config. Manually calling |
| 118 | + * KeyPairFactory with a half-baked config can get cumbersome, so let's build a ConfigContext which has |
| 119 | + * everything ready and supplies the relevant KeyPair. |
| 120 | + * |
| 121 | + * @return the generated keypair and a config which uses a serialized version of that keypair |
| 122 | + */ |
| 123 | + public static ImmutablePair<KeyPair, BaseChainedConfigContext> generateKeyPairBackedConfig(final String passphrase) { |
| 124 | + final KeyPair keyPair; |
| 125 | + try { |
| 126 | + keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
| 127 | + } catch (final NoSuchAlgorithmException impossible) { |
| 128 | + throw new Error(impossible); // "RSA" is always provided |
| 129 | + } |
| 130 | + |
| 131 | + final Object keySerializer; |
| 132 | + if (passphrase != null) { |
| 133 | + try { |
| 134 | + keySerializer = new JcaMiscPEMGenerator( |
| 135 | + keyPair.getPrivate(), |
| 136 | + new JcePEMEncryptorBuilder("AES-128-CBC").build(passphrase.toCharArray())); |
| 137 | + } catch (IOException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } else { |
| 141 | + keySerializer = keyPair.getPrivate(); |
| 142 | + } |
| 143 | + |
| 144 | + final String keyContent; |
| 145 | + try (final StringWriter content = new StringWriter(); |
| 146 | + final JcaPEMWriter writer = new JcaPEMWriter(content)) { |
| 147 | + writer.writeObject(keySerializer); |
| 148 | + writer.flush(); |
| 149 | + keyContent = content.toString(); |
| 150 | + } catch (IOException e) { |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + |
| 154 | + final BaseChainedConfigContext config = new ChainedConfigContext(DEFAULT_CONFIG) |
| 155 | + // we need to unset the key path in case one exists at ~/.ssh/id_rsa |
| 156 | + // see the static initializer in DefaultsConfigContext |
| 157 | + .setMantaKeyPath(null) |
| 158 | + .setPrivateKeyContent(keyContent) |
| 159 | + .setMantaKeyId(KeyFingerprinter.md5Fingerprint(keyPair)); |
| 160 | + |
| 161 | + if (passphrase != null) { |
| 162 | + config.setPassword(passphrase); |
| 163 | + } |
| 164 | + |
| 165 | + return new ImmutablePair<>(keyPair, config); |
| 166 | + } |
100 | 167 | } |
0 commit comments