Secret

com.geirolz.app.toolkit.config.Secret
See theSecret companion object
sealed trait Secret[T] extends AutoCloseable

Memory-safe and type-safe secret value of type T.

Secret does the best to avoid leaking information in memory and in the code BUT an attack is possible and I don't give any certainties or guarantees about security using this class, you use it at your own risk. Code is open source, you can check the implementation and take your decision consciously. I'll do my best to improve the security and documentation of this class.

Obfuscation

The value is obfuscated when creating the Secret instance using an implicit Obfuserwhich, by default, transform the value into a xor-ed ByteBuffer witch store bytes outside the JVM using direct memory access.

The obfuscated value is de-obfuscated using an implicit DeObfuser instance every time the method use is invoked which returns the original value converting bytes back to T re-apply the xor.

API and Type safety

While obfuscating the value prevents or at least makes it harder to read the value from memory, Secret class API is designed to avoid leaking information in other ways. Preventing developers to improperly use the secret value ( logging, etc...).

Example

 val secretString: Secret[String]  = Secret("my_password")
 val database: F[Database]         = secretString.use(password => initDb(password))

** Credits **

Attributes

Companion
object
Graph
Supertypes
trait AutoCloseable
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def destroy(): Unit

Destroy the secret value by filling the obfuscated value with '\0'.

Destroy the secret value by filling the obfuscated value with '\0'.

This method is idempotent.

Once the secret is destroyed it can't be used anymore. If you try to use it using use, useAndDestroy, evalUse, evalUseAndDestroy and other methods, it will raise a NoLongerValidSecret exception.

Attributes

def evalUse[F[_] : ([F] =>> MonadError[F, _ >: SecretNoLongerValid]), U](f: T => F[U])(implicit evidence$4: MonadSecretError[F], deObfuser: DeObfuser[T]): F[U]

Apply f with the de-obfuscated value WITHOUT destroying it.

Apply f with the de-obfuscated value WITHOUT destroying it.

If the secret is destroyed it will raise a NoLongerValidSecret exception.

Once the secret is destroyed it can't be used anymore. If you try to use it using use, useAndDestroy, evalUse, evalUseAndDestroy and other methods, it will raise a NoLongerValidSecret exception.

Attributes

def hashCode(): Int

Calculate the non-deterministic hash code for this Secret.

Calculate the non-deterministic hash code for this Secret.

This hash code is NOT the hash code of the original value. It is the hash code of the obfuscated value.

Since the obfuscated value based on a random key, the hash code will be different every time. This function is not deterministic.

Attributes

Returns

the hash code of this secret. If the secret is destroyed it will return -1.

def isDestroyed: Boolean

Check if the secret is destroyed

Check if the secret is destroyed

Attributes

Returns

true if the secret is destroyed, false otherwise

Concrete methods

final override def close(): Unit

Alias for destroy

Alias for destroy

Attributes

Definition Classes
AutoCloseable
final override def equals(obj: Any): Boolean

Always returns false, use isEqual instead

Always returns false, use isEqual instead

Attributes

Definition Classes
Any
final def evalUseAndDestroy[F[_] : ([F] =>> MonadError[F, _ >: SecretNoLongerValid]), U](f: T => F[U])(implicit evidence$7: MonadSecretError[F], deObfuser: DeObfuser[T]): F[U]

Apply f with the de-obfuscated value and then destroy the secret value by invoking destroy method.

Apply f with the de-obfuscated value and then destroy the secret value by invoking destroy method.

Once the secret is destroyed it can't be used anymore. If you try to use it using use, useAndDestroy, evalUse, evalUseAndDestroy and other methods, it will raise a NoLongerValidSecret exception.

Attributes

final def isEquals(that: Secret[T])(implicit deObfuser: DeObfuser[T]): Boolean

Safely compare this secret with the provided Secret.

Safely compare this secret with the provided Secret.

Attributes

Returns

true if the secrets are equal, false if they are not equal or if one of the secret is destroyed

final override def toString: String

Attributes

Returns

always returns a static place holder string "** SECRET **" to avoid leaking information

Definition Classes
Any
final def unsafeUse[U](f: T => U)(implicit deObfuser: DeObfuser[T]): U

Avoid this method if possible. Unsafely apply f with the de-obfuscated value WITHOUT destroying it.

Avoid this method if possible. Unsafely apply f with the de-obfuscated value WITHOUT destroying it.

If the secret is destroyed it will raise a NoLongerValidSecret exception.

Throws SecretNoLongerValid if the secret is destroyed

Attributes

final def use[F[_] : ([F] =>> MonadError[F, _ >: SecretNoLongerValid]), U](f: T => U)(implicit evidence$5: MonadSecretError[F], deObfuser: DeObfuser[T]): F[U]

Apply f with the de-obfuscated value WITHOUT destroying it.

Apply f with the de-obfuscated value WITHOUT destroying it.

If the secret is destroyed it will raise a NoLongerValidSecret exception.

Once the secret is destroyed it can't be used anymore. If you try to use it using use, useAndDestroy, evalUse, evalUseAndDestroy and other methods, it will raise a NoLongerValidSecret exception.

Attributes

final def useAndDestroy[F[_] : ([F] =>> MonadError[F, _ >: SecretNoLongerValid]), U](f: T => U)(implicit evidence$6: MonadSecretError[F], deObfuser: DeObfuser[T]): F[U]

Apply f with the de-obfuscated value and then destroy the secret value by invoking destroy method.

Apply f with the de-obfuscated value and then destroy the secret value by invoking destroy method.

Once the secret is destroyed it can't be used anymore. If you try to use it using use, useAndDestroy, evalUse, evalUseAndDestroy and other methods, it will raise a NoLongerValidSecret exception.

Attributes

final def useAndDestroyE[U](f: T => U)(implicit deObfuser: DeObfuser[T]): Either[SecretNoLongerValid, U]

Alias for useAndDestroy with Either[Throwable, *]

Alias for useAndDestroy with Either[Throwable, *]

Attributes

final def useE[U](f: T => U)(implicit deObfuser: DeObfuser[T]): Either[SecretNoLongerValid, U]

Alias for use with Either[Throwable, *]

Alias for use with Either[Throwable, *]

Attributes