| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.crypto.SshKeys.fingerprintPublicKey; |
| 23 | |
| 24 | import javax.annotation.Resource; |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 30 | import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial; |
| 31 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 32 | import org.jclouds.ec2.domain.KeyPair; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | |
| 35 | import com.google.common.annotations.VisibleForTesting; |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.collect.Iterables; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class ImportOrReturnExistingKeypair implements Function<RegionNameAndPublicKeyMaterial, KeyPair> { |
| 45 | @Resource |
| 46 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 47 | protected Logger logger = Logger.NULL; |
| 48 | protected final AWSEC2Client ec2Client; |
| 49 | |
| 50 | @Inject |
| 51 | public ImportOrReturnExistingKeypair(AWSEC2Client ec2Client) { |
| 52 | this.ec2Client = ec2Client; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public KeyPair apply(RegionNameAndPublicKeyMaterial from) { |
| 57 | return importOrReturnExistingKeypair(from.getRegion(), from.getName(), from.getPublicKeyMaterial()); |
| 58 | } |
| 59 | |
| 60 | @VisibleForTesting |
| 61 | KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) { |
| 62 | checkNotNull(region, "region"); |
| 63 | checkNotNull(group, "group"); |
| 64 | checkNotNull(publicKeyMaterial, "publicKeyMaterial"); |
| 65 | logger.debug(">> importing keyPair region(%s) group(%s)", region, group); |
| 66 | KeyPair keyPair = null; |
| 67 | // loop for eventual consistency or race condition. |
| 68 | // as this command is idempotent, it should be ok |
| 69 | while (keyPair == null) |
| 70 | try { |
| 71 | keyPair = ec2Client.getKeyPairServices().importKeyPairInRegion(region, "jclouds#" + group, |
| 72 | publicKeyMaterial); |
| 73 | keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); |
| 74 | logger.debug("<< imported keyPair(%s)", keyPair); |
| 75 | } catch (IllegalStateException e) { |
| 76 | keyPair = Iterables.getFirst(ec2Client.getKeyPairServices().describeKeyPairsInRegion(region, |
| 77 | "jclouds#" + group), null); |
| 78 | if (keyPair != null) { |
| 79 | keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); |
| 80 | logger.debug("<< retrieved existing keyPair(%s)", keyPair); |
| 81 | } |
| 82 | } |
| 83 | return keyPair; |
| 84 | } |
| 85 | |
| 86 | public KeyPair addFingerprintToKeyPair(String publicKeyMaterial, KeyPair keyPair) { |
| 87 | // add in the fingerprint as it makes correlating keys in ssh logs possible |
| 88 | keyPair = keyPair.toBuilder().fingerprint(fingerprintPublicKey(publicKeyMaterial)).build(); |
| 89 | return keyPair; |
| 90 | } |
| 91 | |
| 92 | } |