Struct HessraConfig
pub struct HessraConfig {
pub base_url: String,
pub port: Option<u16>,
pub mtls_cert: String,
pub mtls_key: String,
pub server_ca: String,
pub protocol: Protocol,
pub public_key: Option<String>,
pub personal_keypair: Option<String>,
}
Expand description
Configuration for Hessra SDK client
This structure contains all the configuration parameters needed to create a Hessra client. It can be created manually or loaded from various sources.
§Examples
§Creating a configuration manually
use hessra_config::{HessraConfig, Protocol};
let config = HessraConfig::new(
"https://test.hessra.net", // base URL
Some(443), // port (optional)
Protocol::Http1, // protocol
"client-cert-content", // mTLS certificate
"client-key-content", // mTLS key
"ca-cert-content", // Server CA certificate
);
§Loading from a JSON file
use hessra_config::HessraConfig;
use std::path::Path;
let config = HessraConfig::from_file(Path::new("./config.json"))
.expect("Failed to load configuration");
§Loading from environment variables
use hessra_config::HessraConfig;
// Assuming the following environment variables are set:
// HESSRA_BASE_URL=https://test.hessra.net
// HESSRA_PORT=443
// HESSRA_MTLS_CERT=<certificate content>
// HESSRA_MTLS_KEY=<key content>
// HESSRA_SERVER_CA=<CA certificate content>
let config = HessraConfig::from_env("HESSRA")
.expect("Failed to load configuration from environment");
§Using the global configuration
use hessra_config::{HessraConfig, Protocol, set_default_config, get_default_config};
// Set up the global configuration
let config = HessraConfig::new(
"https://test.hessra.net",
Some(443),
Protocol::Http1,
"<certificate content>",
"<key content>",
"<CA certificate content>",
);
// Set as the default configuration
set_default_config(config).expect("Failed to set default configuration");
// Later in your code, get the default configuration
let default_config = get_default_config()
.expect("No default configuration set");
Fields§
§base_url: String
§port: Option<u16>
§mtls_cert: String
§mtls_key: String
§server_ca: String
§protocol: Protocol
§public_key: Option<String>
The server’s public key for token verification as a PEM formatted string
personal_keypair: Option<String>
The personal keypair for the user as a PEM formatted string
This is used for service chain attestations. When acting as a node in a service chain, this keypair is used to sign attestations that this node has processed the request. The private key should be kept secret and only the public key should be shared with the authorization service.
Implementations§
§impl HessraConfig
impl HessraConfig
pub fn new(
base_url: impl Into<String>,
port: Option<u16>,
protocol: Protocol,
mtls_cert: impl Into<String>,
mtls_key: impl Into<String>,
server_ca: impl Into<String>,
) -> HessraConfig
pub fn new( base_url: impl Into<String>, port: Option<u16>, protocol: Protocol, mtls_cert: impl Into<String>, mtls_key: impl Into<String>, server_ca: impl Into<String>, ) -> HessraConfig
Create a new HessraConfig with the given parameters
§Arguments
base_url
- The base URL for the Hessra service, e.g. “test.hessra.net”port
- Optional port to use for the Hessra service, e.g. 443protocol
- The protocol to use (HTTP/1.1 or HTTP/3)mtls_cert
- The client certificate in PEM format, e.g. “—–BEGIN CERTIFICATE—–\nENV CERT\n—–END CERTIFICATE—–”mtls_key
- The client private key in PEM format, e.g. “—–BEGIN PRIVATE KEY—–\nENV KEY\n—–END PRIVATE KEY—–”server_ca
- The server CA certificate in PEM format, e.g. “—–BEGIN CERTIFICATE—–\nENV CA\n—–END CERTIFICATE—–”
pub fn builder() -> HessraConfigBuilder
pub fn builder() -> HessraConfigBuilder
Create a new HessraConfigBuilder
pub fn to_builder(&self) -> HessraConfigBuilder
pub fn to_builder(&self) -> HessraConfigBuilder
Convert this config to a builder for modification
pub fn from_file(path: impl AsRef<Path>) -> Result<HessraConfig, ConfigError>
pub fn from_file(path: impl AsRef<Path>) -> Result<HessraConfig, ConfigError>
pub fn from_toml(path: impl AsRef<Path>) -> Result<HessraConfig, ConfigError>
pub fn from_toml(path: impl AsRef<Path>) -> Result<HessraConfig, ConfigError>
pub fn from_env(prefix: &str) -> Result<HessraConfig, ConfigError>
pub fn from_env(prefix: &str) -> Result<HessraConfig, ConfigError>
Load configuration from environment variables
§Arguments
prefix
- Prefix for environment variables (e.g., “HESSRA”)
§Environment Variables
The following environment variables are used (with the given prefix):
{PREFIX}_BASE_URL
- Base URL for the Hessra service{PREFIX}_PORT
- Port for the Hessra service (optional){PREFIX}_PROTOCOL
- Protocol to use (“http1” or “http3”){PREFIX}_MTLS_CERT
- Base64-encoded client certificate in PEM format{PREFIX}_MTLS_KEY
- Base64-encoded client private key in PEM format{PREFIX}_SERVER_CA
- Base64-encoded server CA certificate in PEM format{PREFIX}_PUBLIC_KEY
- Base64-encoded server’s public key for token verification (optional){PREFIX}_PERSONAL_KEYPAIR
- Base64-encoded personal keypair in PEM format (optional)
The certificate and key values can also be loaded from files by using:
{PREFIX}_MTLS_CERT_FILE
- Path to client certificate file{PREFIX}_MTLS_KEY_FILE
- Path to client private key file{PREFIX}_SERVER_CA_FILE
- Path to server CA certificate file{PREFIX}_PUBLIC_KEY_FILE
- Path to server’s public key file{PREFIX}_PERSONAL_KEYPAIR_FILE
- Path to personal keypair file
Note: When using environment variables for certificates and keys, the PEM content should be base64-encoded to avoid issues with newlines and special characters in environment variables.
pub fn from_env_or_file(prefix: &str) -> Result<HessraConfig, ConfigError>
pub fn from_env_or_file(prefix: &str) -> Result<HessraConfig, ConfigError>
Load configuration from environment variables or fall back to a config file
This method will first attempt to load the configuration from environment variables. If that fails, it will look for a configuration file in the following locations:
- Path specified in the
{PREFIX}_CONFIG_FILE
environment variable ./hessra.json
in the current directory~/.config/hessra/config.json
in the user’s home directory
§Arguments
prefix
- Prefix for environment variables (e.g., “HESSRA”)
pub fn validate(&self) -> Result<(), ConfigError>
pub fn validate(&self) -> Result<(), ConfigError>
Validate the current configuration
Checks that all required fields are present and have valid values.
Trait Implementations§
§impl Clone for HessraConfig
impl Clone for HessraConfig
§fn clone(&self) -> HessraConfig
fn clone(&self) -> HessraConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more