Skip to content

Commit 524c34b

Browse files
committed
Create a default key
1 parent 54d4573 commit 524c34b

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

streambed-confidant-cli/README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cargo install streambed-confidant-cli
1818
...or build it from this repo:
1919

2020
```
21-
cargo build --bin loggconfidanted --release
21+
cargo build --bin confidant --release
2222
```
2323

2424
...and make a build available on the PATH (only for `cargo build` and just once per shell session):
@@ -27,4 +27,49 @@ cargo build --bin loggconfidanted --release
2727
export PATH="$PWD/target/release":$PATH
2828
```
2929

30+
Before you can use `confidant`, you must provide it with a root secret and a "secret id" (password)
31+
to authenticate the session. Here's an example with some dummy data:
32+
33+
```
34+
echo "1800af9b273e4b9ea71ec723426933a4" > /tmp/root-secret
35+
echo "unused-id" > /tmp/ss-secret-id
36+
```
37+
38+
We also need to create a directory for `confidant` to read and write its secrets. A security feature
39+
of the `confidant` library is that the directory must have a permission of `600` for the owner user.
40+
ACLs should then be used to control access for individual processes. Here's how the directory can be
41+
created:
42+
43+
```
44+
mkdir /tmp/confidant
45+
chmod 700 /tmp/confidant
46+
```
47+
48+
You would normally source the above secrets from your production system, preferably without
49+
them leaving your production system.
50+
51+
Given the root secret, encrypt some data :
52+
53+
```
54+
echo '{"value":"SGkgdGhlcmU="}' | \
55+
confidant --root-path=/tmp/confidant encrypt --file - --path="default/my-secret-path"
56+
```
57+
58+
...which would output:
59+
60+
```
61+
{"value":"EZy4HLnFC4c/W63Qtp288WWFj8U="}
62+
```
63+
64+
That value is now encrypted with a salt.
65+
66+
We can also decrypt in a similar fashion:
67+
68+
```
69+
echo '{"value":"EZy4HLnFC4c/W63Qtp288WWFj8U="}' | \
70+
confidant --root-path=/tmp/confidant decrypt --file - --path="default/my-secret-path"
71+
```
72+
73+
...which will yield the original BASE64 value that we encrypted.
74+
3075
Use `--help` to discover all of the options.

streambed-confidant-cli/src/cryptor.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::{
2+
collections::HashMap,
23
io::{self, Write},
34
time::Duration,
45
};
56

7+
use rand::RngCore;
68
use serde_json::Value;
79
use streambed::{
810
crypto::{self, SALT_SIZE},
911
get_secret_value,
10-
secret_store::SecretStore,
12+
secret_store::{SecretData, SecretStore},
1113
};
1214
use tokio::{sync::mpsc::channel, time};
1315

@@ -101,6 +103,17 @@ pub async fn encrypt(
101103
path: &str,
102104
select: &str,
103105
) -> Result<(), Errors> {
106+
// As a convenience, we create the secret when encrypting if there
107+
// isn't one.
108+
if get_secret_value(&ss, path).await.is_none() {
109+
let mut key = vec![0; 16];
110+
rand::thread_rng().fill_bytes(&mut key);
111+
let data = HashMap::from([("value".to_string(), hex::encode(key))]);
112+
ss.create_secret(path, SecretData { data })
113+
.await
114+
.map_err(Errors::SecretStore)?;
115+
}
116+
104117
fn process(key: Vec<u8>, mut data_bytes: Vec<u8>) -> Option<Vec<u8>> {
105118
let salt = crypto::salt(&mut rand::thread_rng());
106119
crypto::encrypt(&mut data_bytes, &key.try_into().ok()?, &salt);

streambed-confidant-cli/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ struct EncryptCommand {
119119
#[clap(long, default_value = "value")]
120120
pub select: String,
121121

122-
/// The path to the secret e.g. "default/secrets.configurator-events.key"
122+
/// The path to the secret e.g. "default/secrets.configurator-events.key".
123+
/// NOTE: as a convenience, in the case where the there is no secret at
124+
/// this path, then one will be attempted to be created.
123125
#[clap(long)]
124126
pub path: String,
125127
}

0 commit comments

Comments
 (0)