Skip to content

Commit

Permalink
rvps: rework rvps configuration
Browse files Browse the repository at this point in the history
Update the configuration for RVPS store (and rename it to storage).
Previously we were using a type field and an opaque json config field.
Combine them into one enum that contains a config struct following the
format that we have started to use elsewhere in Trustee.

Also, change the configuration of the RVPS server binary.
Previously the RVPS server had a separate config file that duplicated
the options in the main config but added one additional parameter to set
the address of the server.

Instead, take the address of the server as a CLI argument and use the
same config struct as the rest of the crate.

Signed-off-by: Tobin Feldman-Fitzthum <[email protected]>
  • Loading branch information
fitzthum committed Dec 16, 2024
1 parent 0722e4c commit 72e23a4
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 139 deletions.
17 changes: 13 additions & 4 deletions rvps/src/bin/rvps.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use anyhow::{Context, Result};
use clap::Parser;
use log::{info, warn};
use server::config::Config;
use shadow_rs::shadow;

pub mod rvps_api {
tonic::include_proto!("reference");
}

use reference_value_provider_service::config::Config;

shadow!(build);

mod server;

const DEFAULT_CONFIG_PATH: &str = "/etc/rvps.json";
const DEFAULT_ADDRESS: &str = "127.0.0.1:50003";

/// RVPS command-line arguments.
#[derive(Debug, Parser)]
Expand All @@ -23,6 +25,13 @@ pub struct Cli {
/// `--config /etc/rvps.toml`
#[arg(short = 'c', long, default_value = DEFAULT_CONFIG_PATH)]
pub config: String,

/// The address that the RVPS server will listen on.
/// The default is 127.0.0.1:50003
///
/// `--address 127.0.0.1:55554`
#[arg(short = 'a', long, default_value = DEFAULT_ADDRESS)]
pub address: String,
}

#[tokio::main]
Expand All @@ -47,9 +56,9 @@ async fn main() -> Result<()> {
Config::default()
});

info!("Listen socket: {}", config.address);
info!("Listen socket: {}", &cli.address);

let socket = config.address.parse().context("parse socket addr failed")?;
let socket = &cli.address.parse().context("parse socket addr failed")?;

server::start(socket, config.into()).await
server::start(*socket, config).await
}
43 changes: 0 additions & 43 deletions rvps/src/bin/server/config.rs

This file was deleted.

2 changes: 0 additions & 2 deletions rvps/src/bin/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use crate::rvps_api::{
ReferenceValueRegisterResponse,
};

pub mod config;

pub struct RVPSServer {
rvps: Arc<Mutex<Core>>,
}
Expand Down
34 changes: 12 additions & 22 deletions rvps/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@
//
// SPDX-License-Identifier: Apache-2.0
//

use anyhow::{Context, Result};
use serde::Deserialize;
use serde_json::{json, Value};

pub const DEFAULT_STORAGE_TYPE: &str = "LocalFs";
use crate::storage::ReferenceValueStorageConfig;

#[derive(Deserialize, Clone, Debug, PartialEq)]
#[derive(Deserialize, Clone, Debug, PartialEq, Default)]
pub struct Config {
#[serde(default = "default_store_type")]
pub store_type: String,

#[serde(default = "default_store_config")]
pub store_config: Value,
#[serde(default)]
pub storage: ReferenceValueStorageConfig,
}

fn default_store_type() -> String {
DEFAULT_STORAGE_TYPE.to_string()
}

fn default_store_config() -> Value {
json!({})
}
impl Config {
pub fn from_file(config_path: &str) -> Result<Self> {
let c = config::Config::builder()
.add_source(config::File::with_name(config_path))
.build()?;

impl Default for Config {
fn default() -> Self {
Self {
store_type: default_store_type(),
store_config: json!({}),
}
let res = c.try_deserialize().context("invalid config")?;
Ok(res)
}
}
4 changes: 2 additions & 2 deletions rvps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod config;
pub mod extractors;
pub mod pre_processor;
pub mod reference_value;
pub mod store;
pub mod storage;

pub use config::Config;

Expand All @@ -17,7 +17,7 @@ pub use native::Core;
use serde::{Deserialize, Serialize};

pub use reference_value::{ReferenceValue, TrustedDigest};
pub use store::Store;
pub use storage::ReferenceValueStorage;

/// Default version of Message
static MESSAGE_VERSION: &str = "0.1.0";
Expand Down
18 changes: 7 additions & 11 deletions rvps/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,31 @@ use anyhow::{bail, Context, Result};
use log::{info, warn};
use std::collections::HashMap;

use crate::{store::StoreType, Config};

use super::{
config::Config,
extractors::{Extractors, ExtractorsImpl},
pre_processor::{PreProcessor, PreProcessorAPI},
Message, Store, MESSAGE_VERSION,
Message, ReferenceValueStorage, MESSAGE_VERSION,
};

/// The core of the RVPS, s.t. componants except communication componants.
pub struct Core {
pre_processor: PreProcessor,
extractors: ExtractorsImpl,
store: Box<dyn Store + Send + Sync>,
storage: Box<dyn ReferenceValueStorage + Send + Sync>,
}

impl Core {
/// Instantiate a new RVPS Core
pub fn new(config: Config) -> Result<Self> {
let pre_processor = PreProcessor::default();

let extractors = ExtractorsImpl::default();

let store_type = StoreType::try_from(&config.store_type[..])?;
let store = store_type.to_store(config.store_config)?;
let storage = config.storage.to_storage()?;

Ok(Core {
pre_processor,
extractors,
store,
storage,
})
}

Expand All @@ -61,7 +57,7 @@ impl Core {

let rv = self.extractors.process(message)?;
for v in rv.iter() {
let old = self.store.set(v.name().to_string(), v.clone()).await?;
let old = self.storage.set(v.name().to_string(), v.clone()).await?;
if let Some(old) = old {
info!("Old Reference value of {} is replaced.", old.name());
}
Expand All @@ -72,7 +68,7 @@ impl Core {

pub async fn get_digests(&self) -> Result<HashMap<String, Vec<String>>> {
let mut rv_map = HashMap::new();
let reference_values = self.store.get_values().await?;
let reference_values = self.storage.get_values().await?;

for rv in reference_values {
if rv.expired() {
Expand Down
File renamed without changes.
55 changes: 23 additions & 32 deletions rvps/src/store/local_fs/mod.rs → rvps/src/storage/local_fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
use anyhow::*;
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::Value;

use crate::ReferenceValue;

use super::Store;
use super::ReferenceValueStorage;

/// Local directory path to store the reference values,
/// which is created by sled engine.
const FILE_PATH: &str = "/opt/confidential-containers/attestation-service/reference_values";

/// `LocalFs` implements [`Store`] trait. And
/// `LocalFs` implements [`ReferenceValueStorage`] trait. And
/// it uses rocksdb inside.
pub struct LocalFs {
engine: sled::Db,
Expand All @@ -28,23 +27,22 @@ fn default_file_path() -> String {
FILE_PATH.to_string()
}

#[derive(Deserialize, Default)]
struct Config {
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct Config {
#[serde(default = "default_file_path")]
file_path: String,
}

impl LocalFs {
/// Create a new [`LocalFs`] with given config
pub fn new(config: Value) -> Result<Self> {
let config: Config = serde_json::from_value(config)?;
pub fn new(config: Config) -> Result<Self> {
let engine = sled::open(config.file_path)?;
Ok(Self { engine })
}
}

#[async_trait]
impl Store for LocalFs {
impl ReferenceValueStorage for LocalFs {
async fn set(&self, name: String, rv: ReferenceValue) -> Result<Option<ReferenceValue>> {
let rv_serde = serde_json::to_vec(&rv)?;
let res = match self
Expand Down Expand Up @@ -86,12 +84,11 @@ impl Store for LocalFs {

#[cfg(test)]
mod tests {
use serde_json::json;
use serial_test::serial;

use crate::{ReferenceValue, Store};
use crate::{ReferenceValue, ReferenceValueStorage};

use super::LocalFs;
use super::{Config, LocalFs};

const KEY: &str = "test1";

Expand All @@ -103,21 +100,19 @@ mod tests {
let temp_dir = tempfile::tempdir().expect("create tempdir failed");
let dir_str = temp_dir.path().to_string_lossy().to_string();
{
let store = LocalFs::new(json!({
"file_path": dir_str
}))
.expect("create local fs store failed.");
let storage =
LocalFs::new(Config { file_path: dir_str }).expect("create local fs store failed.");
let rv = ReferenceValue::new().expect("create ReferenceValue failed.");
assert!(
store
storage
.set(KEY.to_owned(), rv.clone())
.await
.expect("set rv failed.")
.is_none(),
"the storage has previous key of {}",
KEY
);
let got = store
let got = storage
.get(KEY)
.await
.expect("get rv failed.")
Expand All @@ -134,10 +129,8 @@ mod tests {
let temp_dir = tempfile::tempdir().expect("create tempdir failed");
let dir_str = temp_dir.path().to_string_lossy().to_string();
{
let store = LocalFs::new(json!({
"file_path": dir_str
}))
.expect("create local fs store failed.");
let storage =
LocalFs::new(Config { file_path: dir_str }).expect("create local fs store failed.");
let rv_old = ReferenceValue::new()
.expect("create ReferenceValue failed.")
.set_name("old");
Expand All @@ -147,7 +140,7 @@ mod tests {
.set_name("new");

assert!(
store
storage
.set(KEY.to_owned(), rv_old.clone())
.await
.expect("set rv failed.")
Expand All @@ -156,7 +149,7 @@ mod tests {
KEY
);

let got = store
let got = storage
.set(KEY.to_owned(), rv_new)
.await
.expect("get rv failed.")
Expand All @@ -175,21 +168,19 @@ mod tests {
let temp_dir = tempfile::tempdir().expect("create tempdir failed");
let dir_str = temp_dir.path().to_string_lossy().to_string();
{
let store = LocalFs::new(json!({
"file_path": dir_str
}))
let storage = LocalFs::new(Config {
file_path: dir_str.clone(),
})
.expect("create local fs store failed.");
store
storage
.set(KEY.to_owned(), rv.clone())
.await
.expect("set rv failed.");
}
{
let store = LocalFs::new(json!({
"file_path": dir_str
}))
.expect("create local fs store failed.");
let got = store
let storage =
LocalFs::new(Config { file_path: dir_str }).expect("create local fs store failed.");
let got = storage
.get(KEY)
.await
.expect("get rv failed.")
Expand Down
Loading

0 comments on commit 72e23a4

Please sign in to comment.