Skip to content

Commit

Permalink
Rust: EC2 Getting Started fix #7031 invalid OpenOptions creating keyf…
Browse files Browse the repository at this point in the history
…ile (#7054)

* Rust: EC2 Getting Started fix #7031 invalid OpenOptions creating keyfile
  • Loading branch information
DavidSouther authored Nov 13, 2024
1 parent 78d9c6e commit d2f88c1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion rustv1/examples/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Code excerpts that show you how to call individual service functions.
- [CreateKeyPair](src/ec2.rs#L41)
- [CreateSecurityGroup](src/ec2.rs#L77)
- [CreateTags](src/ec2.rs#L233)
- [DeleteKeyPair](src/getting_started/key_pair.rs#L66)
- [DeleteKeyPair](src/getting_started/key_pair.rs#L67)
- [DeleteSecurityGroup](src/ec2.rs#L167)
- [DeleteSnapshot](../ebs/src/bin/delete-snapshot.rs#L26)
- [DescribeImages](src/ec2.rs#L179)
Expand Down
8 changes: 4 additions & 4 deletions rustv1/examples/ec2/src/ec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ impl EC2Impl {
// snippet-end:[ec2.rust.list_keys.impl]

// snippet-start:[ec2.rust.delete_key.impl]
pub async fn delete_key_pair(&self, key_pair_id: &str) -> Result<(), EC2Error> {
let key_pair_id: String = key_pair_id.into();
tracing::info!("Deleting key pair {key_pair_id}");
pub async fn delete_key_pair(&self, key_name: &str) -> Result<(), EC2Error> {
let key_name: String = key_name.into();
tracing::info!("Deleting key pair {key_name}");
self.client
.delete_key_pair()
.key_pair_id(key_pair_id)
.key_name(key_name)
.send()
.await?;
Ok(())
Expand Down
19 changes: 10 additions & 9 deletions rustv1/examples/ec2/src/getting_started/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,27 @@ impl KeyPairManager {
util: &Util,
key_name: String,
) -> Result<KeyPairInfo, EC2Error> {
let (key_pair, material) = ec2
.create_key_pair(key_name.clone())
.await
.map_err(|e| e.add_message(format!("Couldn't create key {key_name}")))?;
let (key_pair, material) = ec2.create_key_pair(key_name.clone()).await.map_err(|e| {
self.key_pair = KeyPairInfo::builder().key_name(key_name.clone()).build();
e.add_message(format!("Couldn't create key {key_name}"))
})?;

let path = self.key_file_dir.join(format!("{key_name}.pem"));

util.write_secure(&key_name, &path, material)?;

self.key_file_path = Some(path);
// Save the key_pair information immediately, so it can get cleaned up if write_secure fails.
self.key_file_path = Some(path.clone());
self.key_pair = key_pair.clone();

util.write_secure(&key_name, &path, material)?;

Ok(key_pair)
}
// snippet-end:[ec2.rust.create_key.wrapper]

// snippet-start:[ec2.rust.delete_key.wrapper]
pub async fn delete(self, ec2: &EC2, util: &Util) -> Result<(), EC2Error> {
if let Some(key_pair_id) = self.key_pair.key_pair_id() {
ec2.delete_key_pair(key_pair_id).await?;
if let Some(key_name) = self.key_pair.key_name() {
ec2.delete_key_pair(key_name).await?;
if let Some(key_path) = self.key_file_path() {
if let Err(err) = util.remove(key_path) {
eprintln!("Failed to remove {key_path:?} ({err:?})");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ async fn test_happy_path() {
{
mock_ec2
.expect_delete_key_pair()
.with(eq("kp-12345"))
.with(eq("test_key"))
.returning(|_| Ok(()));

mock_util
Expand Down Expand Up @@ -591,7 +591,7 @@ async fn test_unhappy_path_instance_takes_too_long() {
{
mock_ec2
.expect_delete_key_pair()
.with(eq("kp-12345"))
.with(eq("test_key"))
.returning(|_| Ok(()));

mock_util
Expand Down
3 changes: 3 additions & 0 deletions rustv1/examples/ec2/src/getting_started/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ impl UtilImpl {
fn open_file_0600(path: &PathBuf) -> Result<std::fs::File, EC2Error> {
use std::os::unix::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(path.clone())
.map_err(|e| EC2Error::new(format!("Failed to create {path:?} ({e:?})")))
Expand Down

0 comments on commit d2f88c1

Please sign in to comment.