Class: Vangrail::Admission

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/origin.rb

Overview

Whether a named capability may be exercised, given the cell that asked and the cell that supplied the arguments.

Two questions, because they are not the same. Who requested the tool is the planner's question: only privileged origin may ask. What the arguments were derived from is the policy's question: a deployment that wants retrieved data to feed cite says so. A deployment that does not say so cannot be surprised by a wiki page that asked for a shell.

gate = Admission.new(allow: { cite: %i[data], search: [] })
gate.permit?(:cite, request: Cell.user(q), arguments: Cell.data(page))
# => true
gate.permit?(:search, request: Cell.user(q))
# => true
gate.permit?(:shell, request: Cell.user(q))
# => false
gate.permit?(:shell, request: Cell.data(page))
# => false

An empty gate grants nothing. A key in allow is the grant; the value is which untrusted origins may supply arguments. A request cell that carries its own capability set must include the name.

Instance Method Summary collapse

Constructor Details

#initialize(allow: {}) ⇒ Admission

Returns a new instance of Admission.



221
222
223
224
225
# File 'lib/vangrail/origin.rb', line 221

def initialize(allow: {})
  @allow = allow.transform_keys(&:to_sym)
                .transform_values { |kinds| Array(kinds).map(&:to_sym) }
                .freeze
end

Instance Method Details

#permit?(capability, request:, arguments: nil) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/vangrail/origin.rb', line 227

def permit?(capability, request:, arguments: nil)
  raise ArgumentError, 'request must be a Cell' unless request.is_a?(Cell)
  raise ArgumentError, 'arguments must be a Cell' if !arguments.nil? && !arguments.is_a?(Cell)

  cap = capability.to_sym
  return false unless request.privileged?
  return false unless granted?(cap, request)
  return true if arguments.nil? || arguments.privileged?

  allowed = @allow[cap] || []
  arguments.origins.all? { |origin| origin.privileged? || allowed.include?(origin.kind) }
end