| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 29 | import org.jclouds.ec2.domain.KeyPair; |
| 30 | import org.jclouds.ec2.domain.RunningInstance; |
| 31 | import org.jclouds.compute.domain.Image; |
| 32 | import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; |
| 33 | import org.jclouds.domain.Credentials; |
| 34 | |
| 35 | import com.google.common.annotations.VisibleForTesting; |
| 36 | import com.google.common.base.Function; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class CredentialsForInstance implements Function<RunningInstance, Credentials> { |
| 44 | private final Map<RegionAndName, KeyPair> credentialsMap; |
| 45 | private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider; |
| 46 | private final Map<RegionAndName, Image> imageForInstance; |
| 47 | |
| 48 | @Inject |
| 49 | CredentialsForInstance(Map<RegionAndName, KeyPair> credentialsMap, |
| 50 | PopulateDefaultLoginCredentialsForImageStrategy credentialProvider, Map<RegionAndName, Image> imageForInstance) { |
| 51 | this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); |
| 52 | this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider"); |
| 53 | this.imageForInstance = imageForInstance; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Credentials apply(RunningInstance instance) { |
| 58 | Credentials credentials = null;// default if no keypair exists |
| 59 | |
| 60 | if (instance.getKeyName() != null) { |
| 61 | credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance)); |
| 62 | } |
| 63 | return credentials; |
| 64 | } |
| 65 | |
| 66 | @VisibleForTesting |
| 67 | String getPrivateKeyOrNull(RunningInstance instance) { |
| 68 | KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName())); |
| 69 | return keyPair != null ? keyPair.getKeyMaterial() : null; |
| 70 | } |
| 71 | |
| 72 | @VisibleForTesting |
| 73 | String getLoginAccountFor(RunningInstance from) { |
| 74 | return checkNotNull( |
| 75 | credentialProvider.execute(imageForInstance.get(new RegionAndName(from.getRegion(), from.getImageId()))), |
| 76 | "login from image: " + from.getImageId()).identity; |
| 77 | } |
| 78 | } |