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

KV2: add support for custom metadata #47

Merged
merged 1 commit into from
Feb 11, 2024
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
2 changes: 2 additions & 0 deletions src/api/kv2/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::responses::{
};
use rustify_derive::Endpoint;
use serde_json::Value;
use std::collections::HashMap;
use std::fmt::Debug;

/// ## Configure the KV Engine
Expand Down Expand Up @@ -256,6 +257,7 @@ pub struct SetSecretMetadataRequest {
pub max_versions: Option<u64>,
pub cas_required: Option<bool>,
pub delete_version_after: Option<String>,
pub custom_metadata: Option<HashMap<String, String>>,
nicoulaj marked this conversation as resolved.
Show resolved Hide resolved
}

/// ## Delete Metadata and All Versions
Expand Down
2 changes: 2 additions & 0 deletions src/api/kv2/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct ReadSecretResponse {
pub struct SecretVersionMetadata {
pub created_time: String,
pub deletion_time: String,
pub custom_metadata: Option<HashMap<String, String>>,
pub destroyed: bool,
pub version: u64,
}
Expand All @@ -48,6 +49,7 @@ pub struct ReadSecretMetadataResponse {
pub max_versions: u64,
pub oldest_version: u64,
pub updated_time: String,
pub custom_metadata: Option<HashMap<String, String>>,
pub versions: HashMap<String, SecretMetadata>,
}

Expand Down
16 changes: 13 additions & 3 deletions tests/kv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod common;

use common::{VaultServer, VaultServerHelper};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use test_log::test;
use vaultrs::api::kv2::requests::{SetSecretMetadataRequest, SetSecretRequestOptions};
use vaultrs::client::Client;
Expand All @@ -22,11 +23,11 @@ fn test() {
// Test set / read
test_list(&client, &endpoint).await;
test_read(&client, &endpoint).await;
test_read_metadata(&client, &endpoint).await;
test_read_version(&client, &endpoint).await;
test_set(&client, &endpoint).await;
test_set_with_compare_and_swap(&client, &endpoint).await;
test_set_metadata(&client, &endpoint).await;
test_read_metadata(&client, &endpoint).await;

// Test delete
test_delete_latest(&client, &endpoint).await;
Expand Down Expand Up @@ -94,7 +95,9 @@ async fn test_read(client: &impl Client, endpoint: &SecretEndpoint) {
async fn test_read_metadata(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::read_metadata(client, endpoint.path.as_str(), endpoint.name.as_str()).await;
assert!(res.is_ok());
assert!(!res.unwrap().versions.is_empty());
let response = res.unwrap();
assert!(!response.versions.is_empty());
assert!(!response.custom_metadata.unwrap().is_empty());
}

async fn test_read_version(client: &impl Client, endpoint: &SecretEndpoint) {
Expand Down Expand Up @@ -135,7 +138,14 @@ async fn test_set_metadata(client: &impl Client, endpoint: &SecretEndpoint) {
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
Some(SetSecretMetadataRequest::builder().delete_version_after("1h")),
Some(
SetSecretMetadataRequest::builder()
.delete_version_after("1h")
.custom_metadata(HashMap::from([
("key1".to_string(), "foo".to_string()),
("key2".to_string(), "bar".to_string()),
])),
),
)
.await;
assert!(res.is_ok());
Expand Down