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, "    \#{prefix}action.reason is required and must be a non-empty string\n    when using deny or require_approval.\n  MSG\nend\n"

.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, "    \#{prefix}\#{key} is required and must be a non-empty string.\n  MSG\nend\n"

.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, "      \#{prefix}Invalid action format.\n\n      Expected:\n\n        action:\n          deny: true\n\n      Got:\n\n        action: \#{action.inspect}\n\n      VaultKit requires `action` to be a mapping so it can attach\n      metadata like reason, ttl, approvals, and masking rules.\n    MSG\n  end\n\n  validate_action!(action, policy, prefix)\n  true\nend\n"

.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, "      \#{prefix}Action must specify exactly one intent.\n\n      Choose one of:\n\n        - deny\n        - require_approval\n        - mask\n        - allow\n\n      Example:\n\n        action:\n          deny: true\n    MSG\n  end\n\n  if intents.size > 1\n    raise ValidationError, <<~MSG\n      \#{prefix}Action specifies multiple intents: \#{intents.join(', ')}.\n\n      Only one intent may be set to true.\n    MSG\n  end\n\n  intent = intents.first\n\n  case intent\n  when \"deny\"\n    require_reason!(action, prefix)\n\n  when \"require_approval\"\n    require_reason!(action, prefix)\n    require_string!(action, \"approver_role\", prefix)\n\n  when \"mask\"\n    # masking is optional, but if present must be valid\n    validate_masking!(policy, prefix) if policy.key?(\"masking\")\n\n  when \"allow\"\n    # nothing required\n  end\n\n  validate_ttl!(action[\"ttl\"], prefix) if action.key?(\"ttl\")\nend\n"

.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, "      \#{prefix}Invalid masking method at \#{path}.\n\n      Allowed values:\n        - redact\n        - hash\n        - truncate\n        - nullify\n        - full\n        - partial\n\n      Got:\n        \#{method.inspect}\n    MSG\n  end\nend\n"

.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, "      \#{prefix}Unknown keys in masking: \#{unknown_keys.join(', ')}\n\n      Allowed keys:\n        - default_method\n        - rules\n    MSG\n  end\n\n  if masking[\"default_method\"]\n    validate_mask_method!(\n      masking[\"default_method\"],\n      prefix,\n      \"masking.default_method\"\n    )\n  end\n\n  if masking[\"rules\"]\n    unless masking[\"rules\"].is_a?(Hash)\n      raise ValidationError,\n        \"\#{prefix}masking.rules must be a mapping of field \u2192 method.\"\n    end\n\n    masking[\"rules\"].each do |field, method|\n      validate_mask_method!(\n        method,\n        prefix,\n        \"masking.rules.\#{field}\"\n      )\n    end\n  end\nend\n"

.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, "      \#{prefix}Invalid ttl format.\n\n      Expected examples:\n        - \"30m\"\n        - \"1h\"\n        - \"2d\"\n\n      Got:\n        ttl: \#{ttl.inspect}\n    MSG\n  else\n    raise ValidationError, <<~MSG\n      \#{prefix}ttl must be a string duration (e.g. \"1h\") or an integer (seconds).\n    MSG\n  end\nend\n"