1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use log::{debug, info};
use rusoto_core::{HttpClient, Region};
use rusoto_credential::ChainProvider;
use rusoto_ec2::{DescribeInstancesRequest, DescribeInstancesResult, Ec2, Ec2Client, Filter};
use std::{convert::TryFrom, str::FromStr};
use crate::args::ParsedArgs;
use super::{DiscoverError, Provider};
#[derive(Debug, Clone, PartialEq)]
pub enum AddrType {
PrivateV4,
PublicV4,
PublicV6,
}
impl TryFrom<String> for AddrType {
type Error = DiscoverError;
fn try_from(value: String) -> Result<Self, Self::Error> {
match &value[..] {
"public_v4" => Ok(AddrType::PublicV4),
"public_v6" => Ok(AddrType::PublicV6),
"private_v4" => Ok(AddrType::PrivateV4),
_ => Err(DiscoverError::MalformedArgument(
format!("addr_type={}", value),
"Invalid addr_type".to_string(),
)),
}
}
}
#[derive(Debug, Clone)]
pub struct AWSProvider {
tag_key: String,
tag_value: String,
region: Region,
addr_type: AddrType,
}
impl TryFrom<ParsedArgs> for AWSProvider {
type Error = DiscoverError;
fn try_from(args: ParsedArgs) -> Result<Self, Self::Error> {
let mut tag_key = None;
let mut tag_value = None;
let mut region = None;
let mut addr_type = AddrType::PrivateV4;
for (key, value) in args {
match &key[..] {
"tag_key" => tag_key = Some(value),
"tag_value" => tag_value = Some(value),
"region" => {
region = Some(Region::from_str(&value).map_err(|_| {
DiscoverError::MalformedArgument(
format!("region={}", value),
"The value is not a valid AWS Region".to_string(),
)
})?)
}
"addr_type" => addr_type = AddrType::try_from(value)?,
"provider" => (),
_ => return Err(DiscoverError::UnexpectedArgument(key)),
}
}
let tag_key = tag_key.ok_or_else(|| DiscoverError::MissingArgument("tag_key".into()))?;
let tag_value =
tag_value.ok_or_else(|| DiscoverError::MissingArgument("tag_value".into()))?;
let region = region.unwrap_or_default();
Ok(AWSProvider {
tag_key,
tag_value,
region,
addr_type,
})
}
}
impl TryFrom<Vec<String>> for AWSProvider {
type Error = DiscoverError;
fn try_from(value: Vec<String>) -> Result<Self, Self::Error> {
let args = ParsedArgs::try_from(value)?;
match args.get("provider") {
None => Err(DiscoverError::MissingArgument("provider".into())),
Some(provider) => match &provider[..] {
"aws" => AWSProvider::try_from(args),
_ => Err(DiscoverError::MalformedArgument(
format!("provider={}", provider),
"you should not see this ...".to_string(),
)),
},
}
}
}
impl AWSProvider {
pub fn tag_key(&self) -> &str {
&self.tag_key
}
pub fn tag_value(&self) -> &str {
&self.tag_value
}
pub fn region(&self) -> &Region {
&self.region
}
pub fn addr_type(&self) -> &AddrType {
&self.addr_type
}
async fn get_instances(&self) -> Result<DescribeInstancesResult, DiscoverError> {
let provider = ChainProvider::new();
let client = Ec2Client::new_with(HttpClient::new().unwrap(), provider, self.region.clone());
let mut input = DescribeInstancesRequest::default();
let mut filters: Vec<Filter> = Vec::new();
filters.push(Filter {
name: Some(format!("tag:{}", self.tag_key.clone())),
values: Some(vec![self.tag_value.clone()]),
});
filters.push(Filter {
name: Some("instance-state-name".into()),
values: Some(vec!["running".into()]),
});
input.filters = Some(filters);
debug!(
"Using region={:?} tag_key={:?} tag_value={:?} addr_type={:?}",
self.region, self.tag_key, self.tag_value, self.addr_type
);
client.describe_instances(input).await.map_err(|e| {
println!("Provider req failed: {:?}", e);
DiscoverError::ProviderRequestFailed(format!("{:?}", e))
})
}
}
#[async_trait::async_trait]
impl Provider for AWSProvider {
async fn addrs(&self) -> Result<Vec<String>, DiscoverError> {
let res = self.get_instances().await?;
let reservations = res.reservations.unwrap_or_default();
debug!("Found {} reservations", reservations.len());
let addrs = reservations
.into_iter()
.map(|reservation| {
let reservation_id = reservation.reservation_id.clone()
.expect("Reservation to have a reservation id");
let instances = reservation.instances.unwrap_or_default();
debug!(
"Reservation {:?} has {} instances",
reservation_id,
instances.len()
);
instances
.into_iter()
.filter_map(|instance| {
let instance_id = instance.instance_id.clone()
.expect("Instance to have an instance id");
debug!("Found instance {:?}", instance_id);
match self.addr_type {
AddrType::PrivateV4 => match instance.private_ip_address {
Some(addr) => {
info!("Instance {:?} has private ip {:?}", instance_id, addr);
Some(vec![addr])
}
None => {
debug!("Instance {:?} has no private ip", instance_id);
None
}
},
AddrType::PublicV4 => match instance.public_ip_address {
Some(addr) => {
info!("Instance {:?} has public ip {:?}", instance_id, addr);
Some(vec![addr])
}
None => {
debug!("Instance {:?} has no public ip", instance_id);
None
}
},
AddrType::PublicV6 => {
let network_interfaces =
instance.network_interfaces.unwrap_or_default();
debug!(
"Instance {:?} has {} network interfaces",
instance_id,
network_interfaces.len()
);
Some(
network_interfaces
.into_iter()
.filter_map(|i| {
let network_interface_id = i.network_interface_id.clone();
debug!(
"Checking NetworInterfaceId {:?} on Instance {:?}",
network_interface_id, instance_id
);
if i.ipv_6_addresses.is_none() {
debug!(
"Instance {:?} has no IPv6 on NetworkInterfaceId {:?}",
instance_id, network_interface_id
);
}
i.ipv_6_addresses.map(|ipv6| {
ipv6.into_iter()
.filter_map(|ipv6| {
info!("Instance {:?} has IPv6 {:?} on NetworkInterfaceId {:?}", instance_id, ipv6.ipv_6_address, network_interface_id);
ipv6.ipv_6_address
})
.collect::<Vec<_>>()
})
})
.flatten()
.collect::<Vec<_>>(),
)
}
}
})
.flatten()
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();
Ok(addrs)
}
fn help() -> String {
"Amazon AWS:
provider: \"aws\"
region: The AWS region. Default to region of instance.
tag_key: The tag key to filter on
tag_value: The tag value to filter on
addr_type: \"private_v4\", \"public_v4\" or \"public_v6\". Defaults to \"private_v4\".
access_key_id: The AWS access key to use
secret_access_key: The AWS secret access key to use
The only required IAM permission is 'ec2:DescribeInstances'. If the Consul agent is
running on AWS instance it is recommended you use an IAM role, otherwise it is
recommended you make a dedicated IAM user and access key used only for auto-joining.
"
.to_string()
}
}