From ad3c87bace04e05f81fe99f1899796bd67dac7af Mon Sep 17 00:00:00 2001 From: ChrisFriesen <26197502+ChrisFriesen@users.noreply.github.com> Date: Fri, 28 Oct 2022 12:24:25 -0400 Subject: [PATCH] Import ssh key (#34) --- README.md | 2 +- src/bin/nitrogen.rs | 27 +++++++++++++-------------- src/commands/setup.rs | 11 +++++++---- src/template.rs | 17 ++++++++++++----- src/templates/setupTemplate.json | 17 ++++++++++++----- 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 42efa17..62c4947 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ curl -fsSL https://raw.githubusercontent.com/capeprivacy/nitrogen/main/install.s ## Commands -- `nitrogen setup --instance-type -p -s ` +- `nitrogen setup --instance-type -p -s ` - `nitrogen build --eif ` - `nitrogen deploy ` - `nitrogen delete ` diff --git a/src/bin/nitrogen.rs b/src/bin/nitrogen.rs index 3d7c810..bce7b9d 100644 --- a/src/bin/nitrogen.rs +++ b/src/bin/nitrogen.rs @@ -35,8 +35,8 @@ enum Commands { Setup { /// Name of the CloudFormation stack/provisioned EC2 instance name: String, - /// EC2 key-pair to use for the provisioned instance - key_name: String, + /// File of public key to be used for ssh with the provisioned instance + public_key_file: String, /// EC2-instance type. Must be Nitro compatible #[arg(long, default_value_t = String::from("m5a.xlarge"))] instance_type: String, @@ -81,13 +81,12 @@ enum Commands { }, Start { + /// Name of the CloudFormation stack/provisioned EC2 instance name: String, - - /// EC2 key-pair to use for the provisioned instance - key_name: String, - - /// EC2 key-pair to use for the provisioned instance - public_key: String, + /// File of public key to be used for ssh with the provisioned instance + public_key_file: String, + /// File of private key to be used for ssh + private_key: String, /// EC2-instance type. Must be Nitro compatible #[arg(long, default_value_t = String::from("m5a.xlarge"))] instance_type: String, @@ -119,7 +118,7 @@ async fn main() -> Result<(), Error> { name, instance_type, port, - key_name, + public_key_file, ssh_location, } => { let ssh_location = ssh_location.to_string(); @@ -134,7 +133,7 @@ async fn main() -> Result<(), Error> { &name, &instance_type, &port, - &key_name, + &public_key_file, &ssh_location, ) .await?; @@ -183,11 +182,11 @@ async fn main() -> Result<(), Error> { } Commands::Start { name, - key_name, + public_key_file, port, instance_type, ssh_location, - public_key, + private_key, } => { let dockerfile = Asset::get(&format!("{}/Dockerfile", name)).expect("unable to get dockerfile"); @@ -225,7 +224,7 @@ async fn main() -> Result<(), Error> { &id, &instance_type, &port, - &key_name, + &public_key_file, &ssh_location, ) .await?; @@ -242,7 +241,7 @@ async fn main() -> Result<(), Error> { println!("Sleeping for 20s to give ec2 instance a chance to boot..."); tokio::time::sleep(Duration::from_secs(20)).await; - let out = deploy(&client, &id, eif_path, &public_key, 2, None).await?; + let out = deploy(&client, &id, eif_path, &private_key, 2, None).await?; println!("{:?}", out); diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 4660436..4e74cc3 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -5,6 +5,7 @@ use aws_sdk_cloudformation::{ }; use failure::Error; use tracing::{info, instrument}; +use std::fs; fn lift_to_param(key: impl Into, value: impl Into) -> Parameter { Parameter::builder() @@ -19,7 +20,7 @@ async fn setup_stack( name: &String, instance_type: &String, port: &usize, - key_name: &String, + public_key: &String, ssh_location: &String, ) -> Result { let stack = client @@ -29,7 +30,7 @@ async fn setup_stack( .parameters(lift_to_param("InstanceName", name)) .parameters(lift_to_param("InstanceType", instance_type)) .parameters(lift_to_param("Port", port.to_string())) - .parameters(lift_to_param("KeyName", key_name)) + .parameters(lift_to_param("PublicKey", public_key)) .parameters(lift_to_param("SSHLocation", ssh_location)); let stack_output = stack.send().await?; Ok(stack_output) @@ -58,16 +59,18 @@ pub async fn setup( name: &String, instance_type: &String, port: &usize, - key_name: &String, + public_key_file: &String, ssh_location: &String, ) -> Result, Error> { + let public_key = fs::read_to_string(public_key_file)?; + let stack_output = setup_stack( client, setup_template, name, instance_type, port, - key_name, + &public_key, ssh_location, ) .await?; diff --git a/src/template.rs b/src/template.rs index 5bcdf50..1bde41b 100644 --- a/src/template.rs +++ b/src/template.rs @@ -5,10 +5,9 @@ pub const SETUP_TEMPLATE: &str = r##"{ "Description" : "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.", "Parameters" : { - "KeyName": { - "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance", - "Type": "AWS::EC2::KeyPair::KeyName", - "ConstraintDescription" : "must be the name of an existing EC2 KeyPair." + "PublicKey": { + "Description" : "Public key material of pair for SSH access to the instance", + "Type": "String" }, "InstanceName": { @@ -45,6 +44,14 @@ pub const SETUP_TEMPLATE: &str = r##"{ }, "Resources" : { + "ImportedKeyPair": { + "Type": "AWS::EC2::KeyPair", + "Properties": { + "KeyName": { "Ref": "InstanceName" }, + "PublicKeyMaterial": { "Ref": "PublicKey"} + } + }, + "EC2Instance" : { "Type" : "AWS::EC2::Instance", "Metadata": { @@ -83,7 +90,7 @@ pub const SETUP_TEMPLATE: &str = r##"{ "Properties" : { "InstanceType" : { "Ref" : "InstanceType" }, "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ], - "KeyName" : { "Ref" : "KeyName" }, + "KeyName" : { "Ref" : "ImportedKeyPair" }, "ImageId" : { "Ref" : "LatestAmiId" }, "EnclaveOptions": { "Enabled": true diff --git a/src/templates/setupTemplate.json b/src/templates/setupTemplate.json index 2c73727..0630c5d 100644 --- a/src/templates/setupTemplate.json +++ b/src/templates/setupTemplate.json @@ -4,10 +4,9 @@ "Description" : "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.", "Parameters" : { - "KeyName": { - "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance", - "Type": "AWS::EC2::KeyPair::KeyName", - "ConstraintDescription" : "must be the name of an existing EC2 KeyPair." + "PublicKey": { + "Description" : "Public key material of pair for SSH access to the instance", + "Type": "String" }, "InstanceName": { @@ -44,6 +43,14 @@ }, "Resources" : { + "ImportedKeyPair": { + "Type": "AWS::EC2::KeyPair", + "Properties": { + "KeyName": { "Ref": "InstanceName" }, + "PublicKeyMaterial": { "Ref": "PublicKey"} + } + }, + "EC2Instance" : { "Type" : "AWS::EC2::Instance", "Metadata": { @@ -82,7 +89,7 @@ "Properties" : { "InstanceType" : { "Ref" : "InstanceType" }, "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ], - "KeyName" : { "Ref" : "KeyName" }, + "KeyName" : { "Ref" : "ImportedKeyPair" }, "ImageId" : { "Ref" : "LatestAmiId" }, "EnclaveOptions": { "Enabled": true