Struct cryptr::keys::EncKeys

source ·
pub struct EncKeys {
    pub enc_key_active: String,
    pub enc_keys: Vec<(String, Vec<u8>)>,
}
Expand description

Encryption keys used for all operations

These can be either used statically initialized for ease of use, or given dynamically each time. You just need to use the appropriate functions for the EncValue.

Fields§

§enc_key_active: String§enc_keys: Vec<(String, Vec<u8>)>

Implementations§

source§

impl EncKeys

source

pub fn append_new_random(&mut self) -> Result<(), CryptrError>

Generates and appends a new random encryption key

source

pub fn append_new_random_with_id( &mut self, id: String ) -> Result<(), CryptrError>

Generates and appends a new random encryption key with a specific ID

source

pub fn config_path() -> Result<String, CryptrError>

Returns the default config path.

Available with feature cli only

source

pub fn delete(&mut self, enc_key_id: &str) -> Result<(), CryptrError>

Mutate the keys and deletes the key with the given ID, if it exists

source

pub fn fmt_enc_keys_str_for_config(enc_keys: &str) -> (String, String)

Formats a converted ENC_KEYS string in the correct format for config / K8s secret

This is useful for generating keys somewhere else to paste them into K8s / Docker definitions later on.

Returns 2 values:
  1. ENC_KEYS= value for a config or environment variable
  2. ENC_KEYS: with an additional base64 encoding which can be used inside a K8s secret directly
source

pub fn read_from_config() -> Result<Self, CryptrError>

Reads the keys from the default config

Available with feature cli only

source

pub fn read_from_file(path: &str) -> Result<Self, CryptrError>

Reads the keys from a given file location on disk

source

pub fn from_env() -> Result<Self, CryptrError>

Builds the keys from environment variables

Expects 2 values:

  1. ENC_KEY_ACTIVE which indicates the active, default key
  2. ENC_KEYS with the available keys in the korrect format, for instance:
ENC_KEYS="
z8ycdOXnOv7E/nxOhIuLo1oiQBpcg6lYz2Jkc3TgAYoD7h4+orRdlYAk=
test1337/HQyncjvJUNLTv2YvoTWeVmMKQLBe7+xVSHMXUVES8qE=
"
source

pub fn keys_as_b64(&self) -> Result<String, CryptrError>

source

pub async fn save_to_file(&self, file: &str) -> Result<(), CryptrError>

source

pub async fn save_to_file_with_path( &self, path: &str, file_name: &str ) -> Result<(), CryptrError>

source

pub fn get_key(&self, enc_key_id: &str) -> Result<&[u8], CryptrError>

Returns a reference to specified EncKey

source

pub fn get_static<'a>() -> &'a Self

Returns a reference to the initialized EncKeys.

init() must have been called at application startup for this to succeed.

Panics

If the EncKeys have not been set up at startup with init()

source

pub fn get_static_key<'a>(enc_key_id: &str) -> Result<&'a [u8], CryptrError>

Returns a reference to specified EncKey

init() must have been called at application startup for this to succeed.

source

pub fn get_key_active<'a>() -> Result<&'a [u8], CryptrError>

Returns a reference to currently active EncKey

init() must have been called at application startup for this to succeed.

source

pub fn init(self) -> Result<(), CryptrError>

Initialize the encryption keys statically for ease of use.

This function must be called before accessing EncKeys::get(), or basically with any function that uses the static keys.

Throws an error if called more than once.

source

pub fn generate() -> Result<Self, CryptrError>

Generates a new random encryption key

source

pub fn generate_multiple(number_of_keys: u16) -> Result<Self, CryptrError>

Generates a new random set of encryption keys

source

pub fn generate_with_id(id: String) -> Result<Self, CryptrError>

Generates a new random encryption key with a specific ID

source

pub fn try_convert_legacy_keys(keys: &str) -> Result<String, CryptrError>

Used for compatibility with the older system

This will convert the old encryption key format into the new one

Trait Implementations§

source§

impl Debug for EncKeys

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for EncKeys

source§

fn default() -> EncKeys

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for EncKeys

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl PartialEq for EncKeys

source§

fn eq(&self, other: &EncKeys) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for EncKeys

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&[u8]> for EncKeys

§

type Error = CryptrError

The type returned in the event of a conversion error.
source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for EncKeys

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,