Class: Vkit::Policy::PolicyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/vkit/policy/policy_validator.rb

Constant Summary collapse

ACTION_KEYS =
%w[deny require_approval mask allow].freeze

Class Method Summary collapse

Class Method Details

.require_reason!(action, prefix) ⇒ Object

Raises:



96
97
98
99
100
101
102
103
104
# File 'lib/vkit/policy/policy_validator.rb', line 96

def self.require_reason!(action, prefix)
  reason = action["reason"]
  return if reason.is_a?(String) && !reason.strip.empty?

  raise ValidationError, <<~MSG
    #{prefix}action.reason is required and must be a non-empty string
    when using deny or require_approval.
  MSG
end

.require_string!(hash, key, prefix) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
# File 'lib/vkit/policy/policy_validator.rb', line 37

def self.require_string!(hash, key, prefix)
  value = hash[key]
  return if value.is_a?(String) && !value.strip.empty?

  raise ValidationError, <<~MSG
    #{prefix}#{key} is required and must be a non-empty string.
  MSG
end

.validate!(policy, file: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vkit/policy/policy_validator.rb', line 8

def self.validate!(policy, file: nil)
  prefix = file ? "In #{file}:\n\n" : ""

  require_string!(policy, "id", prefix)
  require_string!(policy, "description", prefix)

  action = policy["action"]
  unless action.is_a?(Hash)
    raise ValidationError, <<~MSG
      #{prefix}Invalid action format.

      Expected:

        action:
          deny: true

      Got:

        action: #{action.inspect}

      VaultKit requires `action` to be a mapping so it can attach
      metadata like reason, ttl, approvals, and masking rules.
    MSG
  end

  validate_action!(action, policy, prefix)
  true
end

.validate_action!(action, policy, prefix) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vkit/policy/policy_validator.rb', line 46

def self.validate_action!(action, policy, prefix)
  intents = ACTION_KEYS.select { |k| action[k] == true }

  if intents.empty?
    raise ValidationError, <<~MSG
      #{prefix}Action must specify exactly one intent.

      Choose one of:

        - deny
        - require_approval
        - mask
        - allow

      Example:

        action:
          deny: true
    MSG
  end

  if intents.size > 1
    raise ValidationError, <<~MSG
      #{prefix}Action specifies multiple intents: #{intents.join(', ')}.

      Only one intent may be set to true.
    MSG
  end

  intent = intents.first

  case intent
  when "deny"
    require_reason!(action, prefix)

  when "require_approval"
    require_reason!(action, prefix)
    require_string!(action, "approver_role", prefix)

  when "mask"
    # masking is optional, but if present must be valid
    validate_masking!(policy, prefix) if policy.key?("masking")

  when "allow"
    # nothing required
  end

  validate_ttl!(action["ttl"], prefix) if action.key?("ttl")
end

.validate_mask_method!(method, prefix, path) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/vkit/policy/policy_validator.rb', line 177

def self.validate_mask_method!(method, prefix, path)
  allowed = %w[redact hash truncate nullify full partial]

  unless allowed.include?(method.to_s)
    raise ValidationError, <<~MSG
      #{prefix}Invalid masking method at #{path}.

      Allowed values:
        - redact
        - hash
        - truncate
        - nullify
        - full
        - partial

      Got:
        #{method.inspect}
    MSG
  end
end

.validate_masking!(policy, prefix) ⇒ Object

Masking validation (aligned with compiler + runtime)



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vkit/policy/policy_validator.rb', line 132

def self.validate_masking!(policy, prefix)
  masking = policy["masking"]
  return unless masking

  unless masking.is_a?(Hash)
    raise ValidationError, "#{prefix}masking must be a mapping."
  end

  allowed_keys = %w[default_method rules]
  unknown_keys = masking.keys - allowed_keys

  if unknown_keys.any?
    raise ValidationError, <<~MSG
      #{prefix}Unknown keys in masking: #{unknown_keys.join(', ')}

      Allowed keys:
        - default_method
        - rules
    MSG
  end

  if masking["default_method"]
    validate_mask_method!(
      masking["default_method"],
      prefix,
      "masking.default_method"
    )
  end

  if masking["rules"]
    unless masking["rules"].is_a?(Hash)
      raise ValidationError,
        "#{prefix}masking.rules must be a mapping of field → method."
    end

    masking["rules"].each do |field, method|
      validate_mask_method!(
        method,
        prefix,
        "masking.rules.#{field}"
      )
    end
  end
end

.validate_ttl!(ttl, prefix) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vkit/policy/policy_validator.rb', line 106

def self.validate_ttl!(ttl, prefix)
  case ttl
  when Integer
    return
  when String
    return if ttl.match?(/\A\d+[smhd]\z/)

    raise ValidationError, <<~MSG
      #{prefix}Invalid ttl format.

      Expected examples:
        - "30m"
        - "1h"
        - "2d"

      Got:
        ttl: #{ttl.inspect}
    MSG
  else
    raise ValidationError, <<~MSG
      #{prefix}ttl must be a string duration (e.g. "1h") or an integer (seconds).
    MSG
  end
end