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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
pub mod ec2_instance;
mod iam;
mod ssh;
pub use aws_sdk_ec2::types::InstanceType;

use aws_config::meta::region::RegionProviderChain;
use aws_config::SdkConfig;
use aws_sdk_ec2::types::{
    BlockDeviceMapping, EbsBlockDevice, KeyType, ResourceType, Tag, TagSpecification, VolumeType,
};
use aws_sdk_ec2::{config::Region, types::Filter};
use base64::Engine;
use ec2_instance::Ec2Instance;
use ssh_key::rand_core::OsRng;
use ssh_key::PrivateKey;
use uuid::Uuid;

pub async fn config() -> SdkConfig {
    let region_provider = RegionProviderChain::first_try(Region::new("us-east-1"));
    aws_config::from_env().region(region_provider).load().await
}

pub struct Aws {
    client: aws_sdk_ec2::Client,
    user_name: String,
    keyname: String,
    client_private_key: String,
    host_public_key: String,
    host_public_key_bytes: Vec<u8>,
    host_private_key: String,
    security_group: String,
}

// include a magic number in the keyname to avoid collisions
// This can never change or we may fail to cleanup resources.
const USER_TAG_NAME: &str = "aws-throwaway-23c2d22c-d929-43fc-b2a4-c1c72f0b733f:user";

impl Aws {
    pub async fn new() -> Self {
        let config = config().await;
        let user_name = iam::user_name(&config).await;
        let keyname = format!("aws-throwaway-{user_name}-{}", Uuid::new_v4());
        let client = aws_sdk_ec2::Client::new(&config);

        // Cleanup any resources that were previously failed to cleanup
        Self::cleanup_resources_inner(&client, &user_name).await;

        let keypair = client
            .create_key_pair()
            .key_name(&keyname)
            .key_type(KeyType::Ed25519)
            .tag_specifications(
                TagSpecification::builder()
                    .resource_type(ResourceType::KeyPair)
                    .tags(Tag::builder().key(USER_TAG_NAME).value(&user_name).build())
                    .build(),
            )
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap();
        let client_private_key = keypair.key_material().unwrap().to_string();
        tracing::info!("client_private_key:\n{}", client_private_key);

        let security_group = format!("aws-throwaway-{user_name}-{}", Uuid::new_v4());
        client
            .create_security_group()
            .group_name(&security_group)
            .description("aws-throwaway security group")
            .tag_specifications(
                TagSpecification::builder()
                    .resource_type(ResourceType::SecurityGroup)
                    .tags(Tag::builder().key("Name").value("aws-throwaway").build())
                    .tags(Tag::builder().key(USER_TAG_NAME).value(&user_name).build())
                    .build(),
            )
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap();
        tracing::info!("created security group");
        assert!(client
            .authorize_security_group_ingress()
            .group_name(&security_group)
            .source_security_group_name(&security_group)
            .tag_specifications(
                TagSpecification::builder()
                    .resource_type(ResourceType::SecurityGroupRule)
                    .tags(
                        Tag::builder()
                            .key("Name")
                            .value("within aws-throwaway SG")
                            .build()
                    )
                    .tags(Tag::builder().key(USER_TAG_NAME).value(&user_name).build())
                    .build(),
            )
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap()
            .r#return()
            .unwrap());
        tracing::info!("created security group rule");
        assert!(client
            .authorize_security_group_ingress()
            .group_name(&security_group)
            .ip_protocol("tcp")
            .from_port(22)
            .to_port(22)
            .cidr_ip("0.0.0.0/0")
            .tag_specifications(
                TagSpecification::builder()
                    .resource_type(ResourceType::SecurityGroupRule)
                    .tags(Tag::builder().key("Name").value("ssh").build())
                    .tags(Tag::builder().key(USER_TAG_NAME).value(&user_name).build())
                    .build(),
            )
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap()
            .r#return()
            .unwrap());
        tracing::info!("created security group rule");

        let key = PrivateKey::random(OsRng {}, ssh_key::Algorithm::Ed25519).unwrap();
        let host_public_key_bytes = key.public_key().to_bytes().unwrap();
        let host_public_key = key.public_key().to_openssh().unwrap();
        let host_private_key = key.to_openssh(ssh_key::LineEnding::LF).unwrap().to_string();

        Aws {
            client,
            user_name,
            keyname,
            client_private_key,
            host_public_key_bytes,
            host_public_key,
            host_private_key,
            security_group,
        }
    }

    /// Call before dropping [`Aws`]
    pub async fn cleanup_resources(&self) {
        Self::cleanup_resources_inner(&self.client, &self.user_name).await
    }

    /// Call to cleanup without constructing an [`Aws`]
    pub async fn cleanup_resources_static() {
        let config = config().await;
        let user_name = iam::user_name(&config).await;
        let client = aws_sdk_ec2::Client::new(&config);
        Aws::cleanup_resources_inner(&client, &user_name).await;
    }

    async fn get_all_throwaway_tags(
        client: &aws_sdk_ec2::Client,
        user_name: &str,
        resource_type: &str,
    ) -> Vec<String> {
        let user_filter_name = format!("tag:{}", USER_TAG_NAME);

        let mut ids = vec![];
        for tag in client
            .describe_tags()
            .set_filters(Some(vec![
                Filter::builder()
                    .name(&user_filter_name)
                    .values(user_name)
                    .build(),
                Filter::builder()
                    .name("resource-type")
                    .values(resource_type)
                    .build(),
            ]))
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap()
            .tags()
            .unwrap()
        {
            if let Some(id) = tag.resource_id() {
                ids.push(id.to_owned());
            }
        }
        ids
    }

    pub async fn cleanup_resources_inner(client: &aws_sdk_ec2::Client, user_name: &str) {
        // delete instances
        tracing::info!("Terminating instances");
        let instance_ids = Self::get_all_throwaway_tags(client, user_name, "instance").await;
        if !instance_ids.is_empty() {
            for result in client
                .terminate_instances()
                .set_instance_ids(Some(instance_ids))
                .send()
                .await
                .map_err(|e| e.into_service_error())
                .unwrap()
                .terminating_instances()
                .unwrap()
            {
                tracing::info!(
                    "Instance {:?} {:?} -> {:?}",
                    result.instance_id.as_ref().unwrap(),
                    result.previous_state().unwrap().name().unwrap(),
                    result.current_state().unwrap().name().unwrap()
                );
            }
        }

        // delete security groups
        for id in Self::get_all_throwaway_tags(client, user_name, "security-group").await {
            if let Err(err) = client.delete_security_group().group_id(&id).send().await {
                tracing::info!(
                    "security group {id:?} could not be deleted, this will get cleaned up eventually on a future aws-throwaway cleanup: {:?}",
                    err.into_service_error().meta().message()
                )
            } else {
                tracing::info!("security group {id:?} was succesfully deleted",)
            }
        }

        // delete keypairs
        for id in Self::get_all_throwaway_tags(client, user_name, "key-pair").await {
            client
                .delete_key_pair()
                .key_pair_id(&id)
                .send()
                .await
                .map_err(|e| {
                    anyhow::anyhow!(e.into_service_error())
                        .context(format!("Failed to delete keypair {id:?}"))
                })
                .unwrap();
            tracing::info!("keypair {id:?} was succesfully deleted");
        }
    }

    pub async fn create_ec2_instance(
        &self,
        instance_type: InstanceType,
        storage_gb: u32,
    ) -> Ec2Instance {
        let result = self
            .client
            .run_instances()
            .instance_type(instance_type.clone())
            .min_count(1)
            .max_count(1)
            .block_device_mappings(
                BlockDeviceMapping::builder().device_name("/dev/sda1").ebs(
                    EbsBlockDevice::builder()
                        .delete_on_termination(true)
                        .volume_size(storage_gb as i32)
                        .volume_type(VolumeType::Gp2)
                        .build()
                ).build()
            )
            .security_groups(&self.security_group)
            .key_name(&self.keyname)
            .user_data(base64::engine::general_purpose::STANDARD.encode(format!(
                r#"#!/bin/bash
sudo systemctl stop ssh
echo "{}" > /etc/ssh/ssh_host_ed25519_key.pub
echo "{}" > /etc/ssh/ssh_host_ed25519_key

echo "ClientAliveInterval 30" >> /etc/ssh/sshd_config
sudo systemctl start ssh
            "#,
                self.host_public_key, self.host_private_key
            )))
            .tag_specifications(
                TagSpecification::builder()
                    .resource_type(ResourceType::Instance)
                    .set_tags(Some(vec![
                        Tag::builder().key("Name").value("aws-throwaway").build(),
                        Tag::builder()
                            .key(USER_TAG_NAME)
                            .value(&self.user_name)
                            .build(),
                    ]))
                    .build(),
            )
            .image_id(format!(
                "resolve:ssm:/aws/service/canonical/ubuntu/server/22.04/stable/current/{}/hvm/ebs-gp2/ami-id",
                get_arch_of_instance_type(instance_type).get_ubuntu_arch_identifier()
            ))
            .send()
            .await
            .map_err(|e| e.into_service_error())
            .unwrap();
        let instance_id = result
            .instances()
            .unwrap()
            .iter()
            .next()
            .unwrap()
            .instance_id()
            .unwrap()
            .to_owned();

        let mut public_ip = None;
        let mut private_ip = None;

        while public_ip.is_none() || private_ip.is_none() {
            tokio::time::sleep(std::time::Duration::from_secs(1)).await;
            for reservation in self
                .client
                .describe_instances()
                .instance_ids(&instance_id)
                .send()
                .await
                .map_err(|e| e.into_service_error())
                .unwrap()
                .reservations()
                .unwrap()
            {
                for instance in reservation.instances().unwrap() {
                    public_ip = instance.public_ip_address().map(|x| x.parse().unwrap());
                    private_ip = instance.private_ip_address().map(|x| x.parse().unwrap());
                }
            }
        }
        let public_ip = public_ip.unwrap();
        let private_ip = private_ip.unwrap();
        tracing::info!("created EC2 instance at: {public_ip}");

        Ec2Instance::new(
            public_ip,
            private_ip,
            self.host_public_key_bytes.clone(),
            &self.client_private_key,
        )
        .await
    }
}

enum CpuArch {
    X86_64,
    Aarch64,
}

impl CpuArch {
    fn get_ubuntu_arch_identifier(&self) -> &'static str {
        match self {
            CpuArch::X86_64 => "amd64",
            CpuArch::Aarch64 => "arm64",
        }
    }
}

fn get_arch_of_instance_type(instance_type: InstanceType) -> CpuArch {
    // Instance names looke like something like:
    // type + revision_number + subtypes + '.' + size
    // So say for example `Im4gn.large` would be split into:
    // type = "Im"
    // revision_number = 4
    // subtypes = "gn"
    // size = "large"
    //
    // The 'g' character existing in subtypes indicates that the instance type is a gravitron aka arm instance.
    // We can check for the existence of 'g' to determine if we are aarch64 or x86_64
    // This is a bit hacky because this format is not explicitly documented anywhere but the instance type naming does consistently follow this pattern.
    let mut reached_revision_number = false;
    for c in instance_type.as_str().chars() {
        if !reached_revision_number {
            if c.is_ascii_digit() {
                reached_revision_number = true;
            }
        } else if c == '.' {
            return CpuArch::X86_64;
        } else if c == 'g' {
            return CpuArch::Aarch64;
        }
    }
    unreachable!("Cannot parse instance type: {instance_type:?}")
}