mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-14 08:52:56 +00:00
Initial remote_keys crate
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -5495,6 +5495,17 @@ version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c707298afce11da2efef2f600116fa93ffa7a032b5d7b628aa17711ec81383ca"
|
||||
|
||||
[[package]]
|
||||
name = "remote_keys"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"aws-config",
|
||||
"aws-sdk-kms",
|
||||
"aws-smithy-types",
|
||||
"utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "remote_storage"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -30,6 +30,7 @@ members = [
|
||||
"libs/tenant_size_model",
|
||||
"libs/metrics",
|
||||
"libs/postgres_connection",
|
||||
"libs/remote_keys",
|
||||
"libs/remote_storage",
|
||||
"libs/tracing-utils",
|
||||
"libs/postgres_ffi/wal_craft",
|
||||
|
||||
12
libs/remote_keys/Cargo.toml
Normal file
12
libs/remote_keys/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "remote_keys"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
aws-smithy-types.workspace = true
|
||||
aws-sdk-kms.workspace = true
|
||||
aws-config.workspace = true
|
||||
utils.workspace = true
|
||||
47
libs/remote_keys/src/aws_keys.rs
Normal file
47
libs/remote_keys/src/aws_keys.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use aws_config::BehaviorVersion;
|
||||
|
||||
use crate::KeyId;
|
||||
|
||||
pub struct AwsRemoteKeyClient {
|
||||
client: aws_sdk_kms::Client,
|
||||
}
|
||||
|
||||
impl AwsRemoteKeyClient {
|
||||
pub async fn new() -> Self {
|
||||
let sdk_config = aws_config::defaults(BehaviorVersion::v2024_03_28())
|
||||
.retry_config(
|
||||
aws_config::retry::RetryConfig::standard()
|
||||
.with_max_attempts(5) // Retry up to 5 times
|
||||
.with_initial_backoff(std::time::Duration::from_millis(200)) // Start with 200ms delay
|
||||
.with_max_backoff(std::time::Duration::from_secs(5)), // Cap at 5 seconds
|
||||
)
|
||||
.load()
|
||||
.await;
|
||||
let client = aws_sdk_kms::Client::new(&sdk_config);
|
||||
Self { client }
|
||||
}
|
||||
|
||||
pub async fn decrypt(&self, key_id: &KeyId, ciphertext: impl Into<Vec<u8>>) -> Vec<u8> {
|
||||
let output = self
|
||||
.client
|
||||
.decrypt()
|
||||
.key_id(&key_id.0)
|
||||
.ciphertext_blob(aws_smithy_types::Blob::new(ciphertext.into()))
|
||||
.send()
|
||||
.await
|
||||
.expect("decrypt");
|
||||
output.plaintext.expect("plaintext").into_inner()
|
||||
}
|
||||
|
||||
pub async fn encrypt(&self, key_id: &KeyId, ciphertext: impl Into<Vec<u8>>) -> Vec<u8> {
|
||||
let output = self
|
||||
.client
|
||||
.encrypt()
|
||||
.key_id(&key_id.0)
|
||||
.plaintext(aws_smithy_types::Blob::new(ciphertext.into()))
|
||||
.send()
|
||||
.await
|
||||
.expect("decrypt");
|
||||
output.ciphertext_blob.expect("ciphertext").into_inner()
|
||||
}
|
||||
}
|
||||
6
libs/remote_keys/src/lib.rs
Normal file
6
libs/remote_keys/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
mod aws_keys;
|
||||
pub use aws_keys::AwsRemoteKeyClient;
|
||||
|
||||
/// A string uniquely identifying a key
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct KeyId(pub String);
|
||||
Reference in New Issue
Block a user