Skip to content

Commit

Permalink
Import ssh key (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFriesen authored Oct 28, 2022
1 parent 8819541 commit ad3c87b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ curl -fsSL https://raw.githubusercontent.com/capeprivacy/nitrogen/main/install.s

## Commands

- `nitrogen setup <STACK_NAME> <KEY_NAME> --instance-type <EC2_INSTANCE_TYPE> -p <PORT> -s <SSH_LOCATION>`
- `nitrogen setup <STACK_NAME> <public_key_file> --instance-type <EC2_INSTANCE_TYPE> -p <PORT> -s <SSH_LOCATION>`
- `nitrogen build <DOCKER_CONTEXT> <DOCKERFILE> --eif <EIF_LOCATION>`
- `nitrogen deploy <EC2_HOSTNAME> <EIF> <SSH_KEY> <CPU_COUNT> <MEMORY>`
- `nitrogen delete <EC2_HOSTNAME>`
Expand Down
27 changes: 13 additions & 14 deletions src/bin/nitrogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -134,7 +133,7 @@ async fn main() -> Result<(), Error> {
&name,
&instance_type,
&port,
&key_name,
&public_key_file,
&ssh_location,
)
.await?;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -225,7 +224,7 @@ async fn main() -> Result<(), Error> {
&id,
&instance_type,
&port,
&key_name,
&public_key_file,
&ssh_location,
)
.await?;
Expand All @@ -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);

Expand Down
11 changes: 7 additions & 4 deletions src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, value: impl Into<String>) -> Parameter {
Parameter::builder()
Expand All @@ -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<CreateStackOutput, Error> {
let stack = client
Expand All @@ -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)
Expand Down Expand Up @@ -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<Vec<(String, String)>, 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?;
Expand Down
17 changes: 12 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/templates/setupTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -44,6 +43,14 @@
},

"Resources" : {
"ImportedKeyPair": {
"Type": "AWS::EC2::KeyPair",
"Properties": {
"KeyName": { "Ref": "InstanceName" },
"PublicKeyMaterial": { "Ref": "PublicKey"}
}
},

"EC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Metadata": {
Expand Down Expand Up @@ -82,7 +89,7 @@
"Properties" : {
"InstanceType" : { "Ref" : "InstanceType" },
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"KeyName" : { "Ref" : "KeyName" },
"KeyName" : { "Ref" : "ImportedKeyPair" },
"ImageId" : { "Ref" : "LatestAmiId" },
"EnclaveOptions": {
"Enabled": true
Expand Down

0 comments on commit ad3c87b

Please sign in to comment.