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.util; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Map; |
24 | import java.util.Map.Entry; |
25 | |
26 | import org.jclouds.ec2.domain.IpPermission; |
27 | import org.jclouds.ec2.domain.IpPermissionImpl; |
28 | import org.jclouds.ec2.domain.IpProtocol; |
29 | import org.jclouds.util.Maps2; |
30 | |
31 | import com.google.common.annotations.Beta; |
32 | import com.google.common.base.Function; |
33 | import com.google.common.collect.ImmutableMultimap; |
34 | import com.google.common.collect.ImmutableSet; |
35 | import com.google.common.collect.Maps; |
36 | import com.google.common.collect.Multimap; |
37 | import com.google.common.collect.Multimaps; |
38 | |
39 | /** |
40 | * |
41 | * Shortcut to create ingress rules |
42 | * |
43 | * @author Adrian Cole |
44 | */ |
45 | public class IpPermissions extends IpPermissionImpl { |
46 | |
47 | protected IpPermissions(IpProtocol ipProtocol, int fromPort, int toPort, |
48 | Multimap<String, String> userIdGroupPairs, Iterable<String> groupIds, Iterable<String> ipRanges) { |
49 | super(ipProtocol, fromPort, toPort, userIdGroupPairs, groupIds, userIdGroupPairs.size() == 0 ? ipRanges |
50 | : ImmutableSet.<String> of()); |
51 | } |
52 | |
53 | /** |
54 | * don't rely on this being here.. it will move |
55 | */ |
56 | @Beta |
57 | public static Multimap<String, String> buildFormParametersForIndex(final int index, IpPermission permission) { |
58 | Map<String, String> headers = Maps.newLinkedHashMap(); |
59 | headers.put("IpPermissions.%d.IpProtocol", permission.getIpProtocol().toString()); |
60 | headers.put("IpPermissions.%d.FromPort", permission.getFromPort() + ""); |
61 | headers.put("IpPermissions.%d.ToPort", permission.getToPort() + ""); |
62 | String prefix = "IpPermissions.%d.IpRanges."; |
63 | int i = 0; |
64 | for (String cidrIp : checkNotNull(permission.getIpRanges(), "cidrIps")) { |
65 | headers.put(prefix + i++ + ".CidrIp", cidrIp); |
66 | } |
67 | prefix = "IpPermissions.%d.Groups."; |
68 | i = 0; |
69 | for (String groupId : checkNotNull(permission.getGroupIds(), "groupIds")) { |
70 | headers.put(prefix + i++ + ".GroupId", groupId); |
71 | } |
72 | prefix = "IpPermissions.%d.Groups."; |
73 | i = 0; |
74 | for (Entry<String, String> userIdGroupNamePair : checkNotNull(permission.getUserIdGroupPairs(), |
75 | "userIdGroupNamePairs").entries()) { |
76 | headers.put(prefix + i++ + ".UserId", userIdGroupNamePair.getKey()); |
77 | headers.put(prefix + i + ".GroupName", userIdGroupNamePair.getValue()); |
78 | } |
79 | prefix = "IpPermissions.%d.IpRanges."; |
80 | i = 0; |
81 | for (String cidrIp : checkNotNull(permission.getIpRanges(), "cidrIps")) { |
82 | headers.put(prefix + i++ + ".CidrIp", cidrIp); |
83 | } |
84 | return Multimaps.forMap(Maps2.transformKeys(headers, new Function<String, String>() { |
85 | |
86 | @Override |
87 | public String apply(String arg0) { |
88 | return String.format(arg0, index); |
89 | } |
90 | |
91 | })); |
92 | } |
93 | |
94 | public static ICMPTypeSelection permitICMP() { |
95 | return new ICMPTypeSelection(); |
96 | } |
97 | |
98 | public static ToSourceSelection permitAnyProtocol() { |
99 | return new ToSourceSelection(IpProtocol.ALL, 1, 65535); |
100 | } |
101 | |
102 | public static PortSelection permit(IpProtocol protocol) { |
103 | return new PortSelection(checkNotNull(protocol, "protocol")); |
104 | } |
105 | |
106 | public static class ICMPTypeSelection extends ToSourceSelection { |
107 | |
108 | ICMPTypeSelection() { |
109 | super(IpProtocol.ICMP, -1, -1); |
110 | } |
111 | |
112 | /** |
113 | * @param type ex. 8 for ECHO (i.e. Ping) |
114 | * @see <a href="http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml"> ICMP Types</a> |
115 | */ |
116 | public AndCodeSelection type(int type) { |
117 | return new AndCodeSelection(type); |
118 | } |
119 | } |
120 | |
121 | public static class AndCodeSelection extends ToSourceSelection { |
122 | AndCodeSelection(int type) { |
123 | super(IpProtocol.ICMP, type, -1); |
124 | } |
125 | |
126 | public ToSourceSelection andCode(int code) { |
127 | return new ToSourceSelection(getIpProtocol(), getFromPort(), code); |
128 | } |
129 | |
130 | } |
131 | |
132 | public static class PortSelection extends ToSourceSelection { |
133 | |
134 | PortSelection(IpProtocol ipProtocol) { |
135 | super(ipProtocol, ipProtocol == IpProtocol.ICMP ? -1 : 1, ipProtocol == IpProtocol.ICMP ? -1 : 65535); |
136 | } |
137 | |
138 | public ToPortSelection fromPort(int port) { |
139 | return new ToPortSelection(getIpProtocol(), port); |
140 | } |
141 | |
142 | public ToSourceSelection port(int port) { |
143 | return new ToSourceSelection(getIpProtocol(), port, port); |
144 | } |
145 | } |
146 | |
147 | public static class ToPortSelection extends ToSourceSelection { |
148 | |
149 | ToPortSelection(IpProtocol ipProtocol, int fromPort) { |
150 | super(ipProtocol, fromPort, ipProtocol == IpProtocol.ICMP ? -1 : 65535); |
151 | } |
152 | |
153 | public ToSourceSelection to(int port) { |
154 | return new ToSourceSelection(getIpProtocol(), getFromPort(), port); |
155 | } |
156 | } |
157 | |
158 | public static class ToVPCSourceSelection extends IpPermissions { |
159 | |
160 | protected ToVPCSourceSelection(IpProtocol ipProtocol, int fromPort, int toPort) { |
161 | super(ipProtocol, fromPort, toPort, ImmutableMultimap.<String, String> of(), ImmutableSet.<String> of(), |
162 | ImmutableSet.of("0.0.0.0/0")); |
163 | } |
164 | |
165 | public IpPermissions originatingFromSecurityGroupId(String groupId) { |
166 | return toVPCSecurityGroups(ImmutableSet.of(checkNotNull(groupId, "groupId"))); |
167 | } |
168 | |
169 | public IpPermissions toVPCSecurityGroups(Iterable<String> groupIds) { |
170 | return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), getUserIdGroupPairs(), groupIds, |
171 | ImmutableSet.<String> of()); |
172 | } |
173 | } |
174 | |
175 | public static class ToSourceSelection extends ToVPCSourceSelection { |
176 | ToSourceSelection(IpProtocol ipProtocol, int fromPort, int toPort) { |
177 | super(ipProtocol, fromPort, toPort); |
178 | } |
179 | |
180 | public IpPermissions originatingFromCidrBlock(String cidrIp) { |
181 | return originatingFromCidrBlocks(ImmutableSet.of(checkNotNull(cidrIp, "cidrIp"))); |
182 | } |
183 | |
184 | public IpPermissions originatingFromCidrBlocks(Iterable<String> cidrIps) { |
185 | return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), |
186 | ImmutableMultimap.<String, String> of(), ImmutableSet.<String> of(), cidrIps); |
187 | } |
188 | |
189 | public IpPermissions originatingFromUserAndSecurityGroup(String userId, String groupName) { |
190 | return toEC2SecurityGroups(ImmutableMultimap.of(checkNotNull(userId, "userId"), |
191 | checkNotNull(groupName, "groupName"))); |
192 | } |
193 | |
194 | public IpPermissions toEC2SecurityGroups(Multimap<String, String> userIdGroupNamePairs) { |
195 | return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), userIdGroupNamePairs, getGroupIds(), |
196 | ImmutableSet.<String> of()); |
197 | } |
198 | } |
199 | } |