Class: EasyCrypt::SecretsProvider::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_crypt/secrets_provider/base.rb

Overview

This class is abstract.

Subclass and override #secret, #salt, and #cipher to implement a custom secrets provider.

Base class for all secret providers. Implements the interface that concrete providers must follow.

This abstract base class defines the structure for secrets providers, including required methods for retrieving encryption configuration values such as ‘salt`, `secret`, and `cipher`. Subclasses are expected to implement these methods based on their respective sources (e.g., environment variables, Rails credentials).

Direct Known Subclasses

EnvProvider, RailsCredentialsProvider

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(purpose) ⇒ Base

Initialize a new secrets provider

Parameters:

  • purpose (Symbol)

    The purpose of the credential (e.g., :authentication)



22
23
24
# File 'lib/easy_crypt/secrets_provider/base.rb', line 22

def initialize(purpose)
  @purpose = purpose
end

Instance Attribute Details

#purposeSymbol (readonly)

Returns The purpose of the credential being accessed.

Returns:

  • (Symbol)

    The purpose of the credential being accessed



17
18
19
# File 'lib/easy_crypt/secrets_provider/base.rb', line 17

def purpose
  @purpose
end

Instance Method Details

#cipherString?

This method is abstract.

Returns the encryption cipher.

Returns:

  • (String, nil)

    The encryption cipher.

Raises:

  • (NotImplementedError)

    If not implemented in the subclass.



42
43
44
# File 'lib/easy_crypt/secrets_provider/base.rb', line 42

def cipher
  raise NotImplementedError, 'Subclasses must implement #cipher'
end

#saltString

This method is abstract.

Returns the salt value for key generation

Returns:

  • (String)

    The salt value

Raises:

  • (NotImplementedError)

    If the subclass doesn’t implement this method



51
52
53
# File 'lib/easy_crypt/secrets_provider/base.rb', line 51

def salt
  raise NotImplementedError, 'Subclasses must implement #salt'
end

#secretString

This method is abstract.

Returns the secret key for encryption

Returns:

  • (String)

    The secret key

Raises:

  • (NotImplementedError)

    If the subclass doesn’t implement this method



60
61
62
# File 'lib/easy_crypt/secrets_provider/base.rb', line 60

def secret
  raise NotImplementedError, 'Subclasses must implement #secret'
end

#validate_attributes!Object

Validates the presence of required attributes for the secrets provider.

Checks if the attributes ‘salt`, and `secret` are defined. Raises a `MissingSecretsConfigurationError` error if any of these attributes are missing.

Raises:



32
33
34
35
# File 'lib/easy_crypt/secrets_provider/base.rb', line 32

def validate_attributes!
  missing_attributes = %w[salt secret].reject { |attr| send(attr) }
  raise MissingSecretsConfigurationError.new(purpose, missing_attributes) if missing_attributes.any?
end