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.loaders; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import javax.annotation.Resource; |
24 | import javax.inject.Inject; |
25 | import javax.inject.Named; |
26 | import javax.inject.Singleton; |
27 | |
28 | import org.jclouds.compute.reference.ComputeServiceConstants; |
29 | import org.jclouds.ec2.EC2Client; |
30 | import org.jclouds.ec2.compute.domain.RegionAndName; |
31 | import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; |
32 | import org.jclouds.ec2.domain.IpProtocol; |
33 | import org.jclouds.ec2.domain.UserIdGroupPair; |
34 | import org.jclouds.ec2.services.SecurityGroupClient; |
35 | import org.jclouds.logging.Logger; |
36 | |
37 | import com.google.common.base.Predicate; |
38 | import com.google.common.cache.CacheLoader; |
39 | import com.google.common.collect.Iterables; |
40 | |
41 | /** |
42 | * |
43 | * @author Adrian Cole |
44 | */ |
45 | @Singleton |
46 | public class CreateSecurityGroupIfNeeded extends CacheLoader<RegionAndName, String> { |
47 | @Resource |
48 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
49 | protected Logger logger = Logger.NULL; |
50 | protected final SecurityGroupClient securityClient; |
51 | protected final Predicate<RegionAndName> securityGroupEventualConsistencyDelay; |
52 | |
53 | @Inject |
54 | public CreateSecurityGroupIfNeeded(EC2Client ec2Client, |
55 | @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay) { |
56 | this(checkNotNull(ec2Client, "ec2Client").getSecurityGroupServices(), securityGroupEventualConsistencyDelay); |
57 | } |
58 | |
59 | public CreateSecurityGroupIfNeeded(SecurityGroupClient securityClient, |
60 | @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay) { |
61 | this.securityClient = checkNotNull(securityClient, "securityClient"); |
62 | this.securityGroupEventualConsistencyDelay = checkNotNull(securityGroupEventualConsistencyDelay, |
63 | "securityGroupEventualConsistencyDelay"); |
64 | } |
65 | |
66 | @Override |
67 | public String load(RegionAndName from) { |
68 | RegionNameAndIngressRules realFrom = RegionNameAndIngressRules.class.cast(from); |
69 | createSecurityGroupInRegion(from.getRegion(), from.getName(), realFrom.getPorts()); |
70 | return from.getName(); |
71 | } |
72 | |
73 | private void createSecurityGroupInRegion(String region, String name, int... ports) { |
74 | checkNotNull(region, "region"); |
75 | checkNotNull(name, "name"); |
76 | logger.debug(">> creating securityGroup region(%s) name(%s)", region, name); |
77 | try { |
78 | securityClient.createSecurityGroupInRegion(region, name, name); |
79 | boolean created = securityGroupEventualConsistencyDelay.apply(new RegionAndName(region, name)); |
80 | if (!created) |
81 | throw new RuntimeException(String.format("security group %s/%s is not available after creating", region, |
82 | name)); |
83 | logger.debug("<< created securityGroup(%s)", name); |
84 | for (int port : ports) { |
85 | createIngressRuleForTCPPort(region, name, port); |
86 | } |
87 | if (ports.length > 0) { |
88 | authorizeGroupToItself(region, name); |
89 | } |
90 | } catch (IllegalStateException e) { |
91 | logger.debug("<< reused securityGroup(%s)", name); |
92 | } |
93 | } |
94 | |
95 | protected void createIngressRuleForTCPPort(String region, String name, int port) { |
96 | logger.debug(">> authorizing securityGroup region(%s) name(%s) port(%s)", region, name, port); |
97 | securityClient.authorizeSecurityGroupIngressInRegion(region, name, IpProtocol.TCP, port, port, "0.0.0.0/0"); |
98 | logger.debug("<< authorized securityGroup(%s)", name); |
99 | } |
100 | |
101 | protected void authorizeGroupToItself(String region, String name) { |
102 | logger.debug(">> authorizing securityGroup region(%s) name(%s) permission to itself", region, name); |
103 | String myOwnerId = Iterables.get(securityClient.describeSecurityGroupsInRegion(region, name), 0).getOwnerId(); |
104 | securityClient.authorizeSecurityGroupIngressInRegion(region, name, new UserIdGroupPair(myOwnerId, name)); |
105 | logger.debug("<< authorized securityGroup(%s)", name); |
106 | } |
107 | |
108 | } |