| 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.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.compute.domain.OsFamily; |
| 30 | import org.jclouds.compute.strategy.impl.ReturnCredentialsBoundToImage; |
| 31 | import org.jclouds.domain.Credentials; |
| 32 | import org.jclouds.domain.LoginCredentials; |
| 33 | import org.jclouds.domain.LoginCredentials.Builder; |
| 34 | import org.jclouds.ec2.domain.Image; |
| 35 | import org.jclouds.javax.annotation.Nullable; |
| 36 | |
| 37 | import com.google.common.collect.ImmutableMap; |
| 38 | |
| 39 | /** |
| 40 | * @author Oleksiy Yarmula |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class EC2PopulateDefaultLoginCredentialsForImageStrategy extends ReturnCredentialsBoundToImage { |
| 44 | public EC2PopulateDefaultLoginCredentialsForImageStrategy() { |
| 45 | this(null, ImmutableMap.<String, Credentials> of(), ImmutableMap.<OsFamily, LoginCredentials>of()); |
| 46 | } |
| 47 | |
| 48 | @Inject |
| 49 | public EC2PopulateDefaultLoginCredentialsForImageStrategy(@Nullable @Named("image") LoginCredentials creds, |
| 50 | Map<String, Credentials> credentialStore, Map<OsFamily, LoginCredentials> osFamilyToCredentials) { |
| 51 | super(creds, credentialStore, osFamilyToCredentials); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public LoginCredentials apply(Object resourceToAuthenticate) { |
| 56 | if (creds != null) |
| 57 | return creds; |
| 58 | Builder credentials = LoginCredentials.builder().user("root"); |
| 59 | if (resourceToAuthenticate != null) { |
| 60 | String owner = null; |
| 61 | if (resourceToAuthenticate instanceof Image) { |
| 62 | owner = Image.class.cast(resourceToAuthenticate).getImageOwnerId(); |
| 63 | } else if (resourceToAuthenticate instanceof org.jclouds.compute.domain.Image) { |
| 64 | owner = org.jclouds.compute.domain.Image.class.cast(resourceToAuthenticate).getUserMetadata().get("owner"); |
| 65 | } |
| 66 | checkArgument(owner != null, "Resource must be an image (for EC2)"); |
| 67 | // canonical/alestic images use the ubuntu user to login |
| 68 | if (owner.matches("063491364108|099720109477")) { |
| 69 | credentials.user("ubuntu"); |
| 70 | // http://typepad.com/2010/09/introducing-amazon-linux-ami.html |
| 71 | } else if (owner.equals("137112412989")) { |
| 72 | credentials.user("ec2-user"); |
| 73 | } |
| 74 | } |
| 75 | return credentials.build(); |
| 76 | } |
| 77 | } |