Module: LocalVault::InputValidation

Defined in:
lib/localvault/input_validation.rb

Defined Under Namespace

Classes: InvalidInput

Constant Summary collapse

SEGMENT_PATTERN =
/\A[A-Za-z_][A-Za-z0-9_]*\z/
VAULT_PATTERN =
/\A[A-Za-z0-9][A-Za-z0-9_-]{0,63}\z/
PROFILES =
%w[aws].freeze

Class Method Summary collapse

Class Method Details

.argv!(value) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/localvault/input_validation.rb', line 47

def argv!(value)
  unless value.is_a?(Array) && !value.empty? &&
      value.all? { |token| token.is_a?(String) && !token.empty? && !token.include?("\0") }
    raise InvalidInput, "command must be a non-empty array of non-empty NUL-free strings"
  end

  value
end

.mapping!(source, target) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/localvault/input_validation.rb', line 22

def mapping!(source, target)
  selector!(source)
  raise InvalidInput, "mapping source must be an exact key" if source.end_with?(".*")
  string!(target, "mapping target")
  reject_delimiters!(target, "mapping target")
  raise InvalidInput, "mapping target must be an environment variable name" unless segment?(target)

  [source, target]
end

.profile!(value) ⇒ Object

Raises:



56
57
58
59
60
61
# File 'lib/localvault/input_validation.rb', line 56

def profile!(value)
  string!(value, "profile")
  raise InvalidInput, "profile must be one of: #{PROFILES.join(", ")}" unless PROFILES.include?(value)

  value
end

.project!(value) ⇒ Object

Raises:



32
33
34
35
36
37
38
# File 'lib/localvault/input_validation.rb', line 32

def project!(value)
  string!(value, "project")
  reject_delimiters!(value, "project")
  raise InvalidInput, "project must be one key segment" unless segment?(value)

  value
end

.segment?(value) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/localvault/input_validation.rb', line 63

def segment?(value)
  value.is_a?(String) && value.match?(SEGMENT_PATTERN)
end

.selector!(value) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
# File 'lib/localvault/input_validation.rb', line 11

def selector!(value)
  string!(value, "selector")
  reject_delimiters!(value, "selector")
  parts = value.split(".", -1)
  valid = parts.length == 1 ? segment?(parts.first) :
    parts.length == 2 && segment?(parts.first) && (segment?(parts.last) || parts.last == "*")
  raise InvalidInput, "selector must be KEY, GROUP.KEY, or GROUP.*" unless valid

  value
end

.vault_name!(value) ⇒ Object

Raises:



40
41
42
43
44
45
# File 'lib/localvault/input_validation.rb', line 40

def vault_name!(value)
  string!(value, "vault")
  raise InvalidInput, "vault must be a valid vault name" unless value.match?(VAULT_PATTERN)

  value
end