Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to generate secret values #7

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.1.1 (UNRELEASED)

* Add masking support [#1](https://github.com/fpco/amber/issues/1)
* Add subcommand `generate` [#7](https://github.com/fpco/amber/pull/7)

## 0.1.0 (2021-08)

Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub enum SubCommand {
/// Value
value: String,
},
/// Generate a new strong secret value, and add it to the repository
Generate {
/// Key, must be all capital ASCII characters, digits, and underscores
key: String,
},
/// Remove a secret
Remove {
/// Key, must be all capital ASCII characters, digits, and underscores
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() -> Result<()> {
match cmd.sub {
cli::SubCommand::Init => init(cmd.opt),
cli::SubCommand::Encrypt { key, value } => encrypt(cmd.opt, key, value),
cli::SubCommand::Generate { key } => generate(cmd.opt, key),
cli::SubCommand::Remove { key } => remove(cmd.opt, key),
cli::SubCommand::Print => print(cmd.opt),
cli::SubCommand::Exec { cmd: cmd_, args } => exec(cmd.opt, cmd_, args),
Expand Down Expand Up @@ -56,6 +57,15 @@ fn encrypt(opt: cli::Opt, key: String, value: String) -> Result<()> {
config.save(&opt.amber_yaml)
}

fn generate(opt: cli::Opt, key: String) -> Result<()> {
let value = sodiumoxide::randombytes::randombytes(16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should include a command line flag to control how many bytes of entropy are included here. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was leaning towards the hardcoded value for simplicity and footgun-avoidance, but I guess there is a potential use-case when secrets of a speicific size are required? (Though the cases I can think of at the moment would also have their own preferred tools for generating & formatting the required values.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, we can leave as-is until there's a specific requirement.

let value = sodiumoxide::base64::encode(value, sodiumoxide::base64::Variant::UrlSafe);
let msg = format!("Your new secret value is {}: {}", key, value);
let res = encrypt(opt, key, value)?;
println!("{}", &msg);
Ok(res)
}

fn remove(opt: cli::Opt, key: String) -> Result<()> {
validate_key(&key)?;
let mut config = config::Config::load(&opt.amber_yaml)?;
Expand Down