class Secrets
- Secrets
- Reference
- Object
Overview
The Secrets class keeps track of a YAML-like structure of secrets for an application, and is responsible for encrypting and decrypting the file where those secrets are stored.
The instantiation of a Secrets object loads data from an encoded YAML file, and presents the data as a hash-like object.
secrets = Secrets.new
secrets["username"] # => "[email protected]"
An alternate way to use the library is with the Secrets#raw
, combined with
the YAML::Serializable
module.
class MySecrets
include YAML::Serializable
property username : String
property password : String
end
secrets = Secrets.new
secrets.raw # => "---\nusername: [email protected]\npassword: WARMACHINEROX\n"
my_secrets = MySecrets.from_yaml(secrets.raw)
my_secrets["username"] # => [email protected]
Note: Changes to the internal data of a Secrets
object doesn't result in
data being saved to the file. If this is intended behaviour, it must be done
manually.
Defined in:
secrets.crsecrets/any.cr
Constant Summary
-
DEFAULT_KEY_PATH =
"secrets.key"
-
DEFAULT_PATH =
"secrets.yml.enc"
-
VERSION =
"0.1.0"
Constructors
-
.new(file_path = DEFAULT_PATH, key_path = DEFAULT_KEY_PATH)
Initializes a new
Secrets
object, and loads it from the encrypted YAML file at the specified location.
Class Method Summary
-
.generate(path = DEFAULT_PATH, key_path = DEFAULT_KEY_PATH)
Generates an encrypted secrets file and key file at the specified locations, overwriting the files if they already exist.
-
.generate!(path = DEFAULT_PATH, key_path = DEFAULT_KEY_PATH)
Generates an encrypted secrets file and key file at the specified locations, raising an error if the files already exist.
Instance Method Summary
-
#[](*args, **options)
returns the element at the given index_or_key, raises if out of bounds or the key is missing.
-
#[](*args, **options, &)
returns the element at the given index_or_key, raises if out of bounds or the key is missing.
-
#[]=(*args, **options)
Sets the value of key to the given value.
-
#[]?(*args, **options)
returns the element at the given index_or_key, or
nil
if out of bounds or the key is missing. -
#[]?(*args, **options, &)
returns the element at the given index_or_key, or
nil
if out of bounds or the key is missing. -
#decrypt(data : String) : String
Decrypts data using the key and returns the decrypted data as a
String
. -
#encrypt(data : String) : String
Encrypts data using the object's key and returns the encrypted data as a
String
. - #file_path : String
- #key_file_path : String
-
#load_data : Any
Loads the YAML data from the encrypted secrets file.
-
#raw : String
Returns the raw YAML of the Secrets file
-
#reset
Generates a new encryption key, saves it to the key file, and encrypts the data file using the new key.
-
#save
Saves data to the secrets file.
Constructor Detail
Initializes a new Secrets
object, and loads it from the encrypted YAML
file at the specified location.
Raises a File::NotFoundError if the specified secrets file doesn't exist.
Class Method Detail
Generates an encrypted secrets file and key file at the specified locations, overwriting the files if they already exist.
If this command is run from the same directory as the gitignore
file, it
will also read the file, and add the key file to it if necessary.
Note: This command won't create any folders required, and will throw a NotFoundError if the folder doesn't exist.
Generates an encrypted secrets file and key file at the specified locations, raising an error if the files already exist.
If this command is run from the same directory as the gitignore
file, it
will also read the file, and add the key file to it if necessary.
Note: As with the standard .generate
method, this command won't create
any folders required, and will throw a NotFoundError if the folder doesn't
exist.
Instance Method Detail
returns the element at the given index_or_key, raises if out of bounds or the key is missing.
returns the element at the given index_or_key, raises if out of bounds or the key is missing.
returns the element at the given index_or_key, or nil
if out of bounds
or the key is missing.
returns the element at the given index_or_key, or nil
if out of bounds
or the key is missing.
Decrypts data using the key and returns the decrypted data as a String
.
Encrypts data using the object's key and returns the encrypted data as
a String
.
Returns the raw YAML of the Secrets file
This allows the use of Secrets
with the YAML::Serializable
module.
class MySecrets
include YAML::Serializable
property username : String
property password : String
end
my_secrets = MySecrets.from_yaml(Secrets.new.raw)
my_secrets["username"] # => [email protected]
Generates a new encryption key, saves it to the key file, and encrypts the data file using the new key.