Skip to content

Commit

Permalink
impl aes-siv
Browse files Browse the repository at this point in the history
Signed-off-by: tabVersion <[email protected]>
  • Loading branch information
tabVersion committed May 28, 2024
1 parent f475dbc commit af892fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
35 changes: 32 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ ignored = ["workspace-hack"]
normal = ["workspace-hack"]

[dependencies]
aes-siv = "0.7"
anyhow = "1"
arc-swap = "1"
assert_matches = "1"
async-trait = "0.1"
aws-config = { workspace = true }
aws-sdk-ec2 = { workspace = true }
base64-url = { version = "3.0.0" }
bincode = "1.3"
bytes = { version = "1", features = ["serde"] }
chrono = "0.4"
clap = { workspace = true }
Expand Down
50 changes: 39 additions & 11 deletions src/meta/src/rpc/ddl_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;

use aes_siv::aead::generic_array::GenericArray;
use aes_siv::aead::Aead;
use aes_siv::{Aes128SivAead, KeyInit};
use anyhow::Context;
use itertools::Itertools;
use rand::Rng;
use rand::{Rng, RngCore};
use risingwave_common::config::DefaultParallelism;
use risingwave_common::hash::{ParallelUnitMapping, VirtualNode};
use risingwave_common::system_param::reader::SystemParamsRead;
Expand Down Expand Up @@ -58,6 +61,7 @@ use risingwave_pb::stream_plan::{
Dispatcher, DispatcherType, FragmentTypeFlag, MergeNode, PbStreamFragmentGraph,
StreamFragmentGraph as StreamFragmentGraphProto,
};
use serde::{Deserialize, Serialize};
use thiserror_ext::AsReport;
use tokio::sync::Semaphore;
use tokio::time::sleep;
Expand Down Expand Up @@ -156,6 +160,12 @@ pub enum DdlCommand {
DropSubscription(SubscriptionId, DropMode),
}

#[derive(Deserialize, Serialize)]
struct SecretEncryption {
nonce: [u8; 16],
ciphertext: Vec<u8>,
}

impl DdlCommand {
fn allow_in_recovery(&self) -> bool {
match self {
Expand Down Expand Up @@ -620,16 +630,34 @@ impl DdlController {
// The 'secret' part of the request we receive from the frontend is in plaintext;
// here, we need to encrypt it before storing it in the catalog.

let encrypted_payload = simplestcrypt::encrypt_and_serialize(
self.env.opts.secret_store_private_key.as_slice(),
secret.get_value().as_slice(),
)
.map_err(|e| {
MetaError::from(MetaErrorInner::InvalidParameter(format!(
"failed to encrypt secret {}: {:?}",
secret.name, e
)))
})?;
let encrypted_payload = {
let data = secret.get_value().as_slice();
let key = self.env.opts.secret_store_private_key.as_slice();
let encrypt_key = {
let mut k = key[..(std::cmp::min(key.len(), 32))].to_vec();
k.resize_with(32, || 0);
k
};

let mut rng = rand::thread_rng();
let mut nonce: [u8; 16] = [0; 16];
rng.fill_bytes(&mut nonce);
let nonce_array = GenericArray::from_slice(&nonce);
let cipher = Aes128SivAead::new(encrypt_key.as_slice().into());

let ciphertext = cipher.encrypt(nonce_array, data).map_err(|e| {
MetaError::from(MetaErrorInner::InvalidParameter(format!(
"failed to encrypt secret {}: {:?}",
secret.name, e
)))
})?;
bincode::serialize(&SecretEncryption { nonce, ciphertext }).map_err(|e| {
MetaError::from(MetaErrorInner::InvalidParameter(format!(
"failed to serialize secret {}: {:?}",
secret.name, e
)))
})?
};
secret.value = encrypted_payload;

match &self.metadata_manager {
Expand Down

0 comments on commit af892fa

Please sign in to comment.