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.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import org.jclouds.crypto.SshKeys; |
24 | import org.jclouds.javax.annotation.Nullable; |
25 | |
26 | /** |
27 | * |
28 | * @see <a href= |
29 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateKeyPair.html" |
30 | * /> |
31 | * @author Adrian Cole |
32 | */ |
33 | public class KeyPair implements Comparable<KeyPair> { |
34 | @Override |
35 | public String toString() { |
36 | return "[region=" + region + ", keyName=" + keyName + ", fingerprint=" + fingerprint + ", sha1OfPrivateKey=" |
37 | + sha1OfPrivateKey + ", keyMaterial?=" + (keyMaterial != null) + "]"; |
38 | } |
39 | |
40 | public static Builder builder() { |
41 | return new Builder(); |
42 | } |
43 | |
44 | public static class Builder { |
45 | private String region; |
46 | private String keyName; |
47 | private String fingerprint; |
48 | private String sha1OfPrivateKey; |
49 | private String keyMaterial; |
50 | |
51 | public Builder region(String region) { |
52 | this.region = region; |
53 | return this; |
54 | } |
55 | |
56 | public Builder keyName(String keyName) { |
57 | this.keyName = keyName; |
58 | return this; |
59 | } |
60 | |
61 | public Builder sha1OfPrivateKey(String sha1OfPrivateKey) { |
62 | this.sha1OfPrivateKey = sha1OfPrivateKey; |
63 | return this; |
64 | } |
65 | |
66 | public Builder keyMaterial(String keyMaterial) { |
67 | this.keyMaterial = keyMaterial; |
68 | return this; |
69 | } |
70 | |
71 | public Builder fingerprint(String fingerprint) { |
72 | this.fingerprint = fingerprint; |
73 | return this; |
74 | } |
75 | |
76 | public KeyPair build() { |
77 | if (fingerprint == null && keyMaterial != null) |
78 | fingerprint(SshKeys.fingerprintPrivateKey(keyMaterial)); |
79 | return new KeyPair(region, keyName, sha1OfPrivateKey, keyMaterial, fingerprint); |
80 | } |
81 | |
82 | public static Builder fromKeyPair(KeyPair in) { |
83 | return new Builder().region(in.getRegion()).keyName(in.getKeyName()).sha1OfPrivateKey(in.getSha1OfPrivateKey()) |
84 | .keyMaterial(in.getKeyMaterial()); |
85 | } |
86 | } |
87 | |
88 | private final String region; |
89 | private final String keyName; |
90 | private final String sha1OfPrivateKey; |
91 | @Nullable |
92 | private final String keyMaterial; |
93 | @Nullable |
94 | private final String fingerprint; |
95 | |
96 | public KeyPair(String region, String keyName, String sha1OfPrivateKey, @Nullable String keyMaterial, |
97 | @Nullable String fingerprint) { |
98 | this.region = checkNotNull(region, "region"); |
99 | this.keyName = checkNotNull(keyName, "keyName"); |
100 | this.sha1OfPrivateKey = checkNotNull(sha1OfPrivateKey, "sha1OfPrivateKey"); |
101 | this.keyMaterial = keyMaterial;// nullable on list |
102 | this.fingerprint = fingerprint;// nullable on list |
103 | } |
104 | |
105 | /** |
106 | * Key pairs (to connect to instances) are Region-specific. |
107 | */ |
108 | public String getRegion() { |
109 | return region; |
110 | } |
111 | |
112 | /** |
113 | * {@inheritDoc} |
114 | */ |
115 | public int compareTo(KeyPair o) { |
116 | return (this == o) ? 0 : getKeyName().compareTo(o.getKeyName()); |
117 | } |
118 | |
119 | /** |
120 | * A SHA-1 digest of the DER encoded private key. |
121 | * |
122 | * @see SshKeys#sha1 |
123 | */ |
124 | public String getSha1OfPrivateKey() { |
125 | return sha1OfPrivateKey; |
126 | } |
127 | |
128 | /** |
129 | * fingerprint per the following <a |
130 | * href="http://tools.ietf.org/html/draft-friedl-secsh-fingerprint-00" >spec</a> |
131 | * |
132 | * @see SshKeys#fingerprint |
133 | */ |
134 | public String getFingerprint() { |
135 | return fingerprint; |
136 | } |
137 | |
138 | /** |
139 | * An unencrypted PEM encoded RSA private key. |
140 | */ |
141 | public String getKeyMaterial() { |
142 | return keyMaterial; |
143 | } |
144 | |
145 | /** |
146 | * The key pair name provided in the original request. |
147 | */ |
148 | public String getKeyName() { |
149 | return keyName; |
150 | } |
151 | |
152 | @Override |
153 | public int hashCode() { |
154 | final int prime = 31; |
155 | int result = 1; |
156 | result = prime * result + ((fingerprint == null) ? 0 : fingerprint.hashCode()); |
157 | result = prime * result + ((keyMaterial == null) ? 0 : keyMaterial.hashCode()); |
158 | result = prime * result + ((keyName == null) ? 0 : keyName.hashCode()); |
159 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
160 | result = prime * result + ((sha1OfPrivateKey == null) ? 0 : sha1OfPrivateKey.hashCode()); |
161 | return result; |
162 | } |
163 | |
164 | @Override |
165 | public boolean equals(Object obj) { |
166 | if (this == obj) |
167 | return true; |
168 | if (obj == null) |
169 | return false; |
170 | if (getClass() != obj.getClass()) |
171 | return false; |
172 | KeyPair other = (KeyPair) obj; |
173 | if (fingerprint == null) { |
174 | if (other.fingerprint != null) |
175 | return false; |
176 | } else if (!fingerprint.equals(other.fingerprint)) |
177 | return false; |
178 | if (keyMaterial == null) { |
179 | if (other.keyMaterial != null) |
180 | return false; |
181 | } else if (!keyMaterial.equals(other.keyMaterial)) |
182 | return false; |
183 | if (keyName == null) { |
184 | if (other.keyName != null) |
185 | return false; |
186 | } else if (!keyName.equals(other.keyName)) |
187 | return false; |
188 | if (region == null) { |
189 | if (other.region != null) |
190 | return false; |
191 | } else if (!region.equals(other.region)) |
192 | return false; |
193 | if (sha1OfPrivateKey == null) { |
194 | if (other.sha1OfPrivateKey != null) |
195 | return false; |
196 | } else if (!sha1OfPrivateKey.equals(other.sha1OfPrivateKey)) |
197 | return false; |
198 | return true; |
199 | } |
200 | |
201 | public Builder toBuilder() { |
202 | return Builder.fromKeyPair(this); |
203 | } |
204 | } |