forked from awslabs/tough
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the KeySource trait to tough. It is meant to replace the KeySource enum in tuftool and allows for users to implement their own sources of Keys. Initially we implement this trait for local files.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use crate::error; | ||
use crate::sign::{parse_keypair, Sign}; | ||
use snafu::ResultExt; | ||
use std::fmt::Debug; | ||
use std::path::PathBuf; | ||
use std::result::Result; | ||
|
||
/// This trait should be implemented for each source of signing keys. Examples | ||
/// of sources include: files, AWS SSM, etc. | ||
pub trait KeySource: Debug + Send + Sync { | ||
/// Returns an object that implements the `Sign` trait | ||
fn as_sign(&self) -> Result<Box<dyn Sign>, Box<dyn std::error::Error + Send + Sync + 'static>>; | ||
|
||
/// Writes a key back to the `KeySource` | ||
fn write( | ||
&self, | ||
value: &str, | ||
key_id_hex: &str, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct LocalKeySource { | ||
pub path: PathBuf, | ||
} | ||
|
||
/// Implements the `KeySource` trait for a `LocalKeySource` (file) | ||
impl KeySource for LocalKeySource { | ||
fn as_sign(&self) -> Result<Box<dyn Sign>, Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
let data = std::fs::read(&self.path).context(error::FileRead { path: &self.path })?; | ||
Ok(Box::new(parse_keypair(&data)?)) | ||
} | ||
|
||
fn write( | ||
&self, | ||
value: &str, | ||
_key_id_hex: &str, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
Ok(std::fs::write(&self.path, value.as_bytes()) | ||
.context(error::FileWrite { path: &self.path })?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters