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.ec2.compute.functions; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.concurrent.ConcurrentMap; |
24 | import java.util.concurrent.ExecutionException; |
25 | |
26 | import javax.inject.Inject; |
27 | import javax.inject.Singleton; |
28 | |
29 | import org.jclouds.compute.domain.Image; |
30 | import org.jclouds.domain.Credentials; |
31 | import org.jclouds.ec2.compute.domain.RegionAndName; |
32 | import org.jclouds.ec2.domain.KeyPair; |
33 | import org.jclouds.ec2.domain.RunningInstance; |
34 | |
35 | import com.google.common.annotations.VisibleForTesting; |
36 | import com.google.common.base.Supplier; |
37 | import com.google.common.cache.CacheLoader; |
38 | import com.google.common.cache.LoadingCache; |
39 | |
40 | /** |
41 | * |
42 | * @author Adrian Cole |
43 | */ |
44 | @Singleton |
45 | public class CredentialsForInstance extends CacheLoader<RunningInstance, Credentials> { |
46 | private final ConcurrentMap<RegionAndName, KeyPair> credentialsMap; |
47 | private final Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap; |
48 | |
49 | @Inject |
50 | CredentialsForInstance(ConcurrentMap<RegionAndName, KeyPair> credentialsMap, |
51 | Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) { |
52 | this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); |
53 | this.imageMap = imageMap; |
54 | } |
55 | |
56 | @Override |
57 | public Credentials load(RunningInstance instance) throws ExecutionException { |
58 | Credentials credentials = null;// default if no keypair exists |
59 | if (instance.getKeyName() != null) { |
60 | credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance)); |
61 | } |
62 | return credentials; |
63 | } |
64 | |
65 | @VisibleForTesting |
66 | String getPrivateKeyOrNull(RunningInstance instance) throws ExecutionException { |
67 | KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName())); |
68 | return keyPair != null ? keyPair.getKeyMaterial() : null; |
69 | } |
70 | |
71 | @VisibleForTesting |
72 | String getLoginAccountFor(RunningInstance from) throws ExecutionException { |
73 | return imageMap.get().get(new RegionAndName(from.getRegion(), from.getImageId())).getDefaultCredentials().identity; |
74 | } |
75 | } |