graphql-armory 0.1.0.pre7 → 0.1.0.pre8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -1
  3. data/README.md +5 -4
  4. data/docs/authorization.md +133 -6
  5. data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
  6. data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
  7. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  8. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  9. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  10. data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
  11. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  12. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  13. data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
  14. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  15. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  16. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  17. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  18. data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
  19. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  20. data/lib/graphql/armory/authorization/audit.rb +50 -191
  21. data/lib/graphql/armory/authorization/authorize.rb +9 -3
  22. data/lib/graphql/armory/authorization/configuration.rb +30 -2
  23. data/lib/graphql/armory/authorization/definitions.rb +114 -0
  24. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  25. data/lib/graphql/armory/authorization/integration/field.rb +13 -5
  26. data/lib/graphql/armory/authorization/integration/object.rb +1 -1
  27. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  28. data/lib/graphql/armory/authorization/integration.rb +22 -1
  29. data/lib/graphql/armory/authorization/rule.rb +5 -0
  30. data/lib/graphql/armory/authorization.rb +4 -1
  31. data/lib/graphql/armory/version.rb +1 -1
  32. metadata +14 -1
@@ -7,21 +7,49 @@ module GraphQL
7
7
  class Configuration
8
8
  MODES = %i[null error raise].freeze
9
9
 
10
- attr_reader :adapter, :on_unauthorized, :sinks
10
+ DENIAL_HOOKS = %i[unauthorized_field unauthorized_object].freeze
11
11
 
12
- def initialize(adapter:, on_unauthorized: :null, sinks: [])
12
+ OWN_CHECKS = %i[authorized? authorizes? authorized_new wrap unauthorized_object visible? scope_items reauthorize_scoped_objects].freeze
13
+
14
+ attr_reader :adapter, :on_unauthorized, :sinks, :own_denial_hooks, :own_checks
15
+
16
+ def initialize(adapter:, on_unauthorized: :null, sinks: [], own_denial_hooks: {}, own_checks: {})
13
17
  unless MODES.include?(on_unauthorized)
14
18
  raise ArgumentError, "on_unauthorized must be one of #{MODES.inspect}, got #{on_unauthorized.inspect}"
15
19
  end
20
+ check_own_denial_hooks(own_denial_hooks)
21
+ check_own_checks(own_checks)
16
22
 
23
+ @own_checks = own_checks.to_h { |holder, methods| [holder, methods.to_h { |name, reason| [name, reason.dup.freeze] }.freeze] }.freeze
17
24
  @adapter = resolve(adapter)
18
25
  @on_unauthorized = on_unauthorized
26
+ @own_denial_hooks = own_denial_hooks.to_h { |hook, reason| [hook, reason.dup.freeze] }.freeze
19
27
  Telemetry.build(sinks: sinks) # raises at boot when the sinks are not callables
20
28
  @sinks = sinks
21
29
  end
22
30
 
23
31
  private
24
32
 
33
+ def check_own_denial_hooks(hooks)
34
+ valid = hooks.is_a?(Hash) && hooks.all? do |hook, reason|
35
+ DENIAL_HOOKS.include?(hook) && reason.is_a?(String) && !reason.strip.empty?
36
+ end
37
+ return if valid
38
+
39
+ raise ArgumentError, "own_denial_hooks must map #{DENIAL_HOOKS.inspect} to the reason you keep your own, got #{hooks.inspect}"
40
+ end
41
+
42
+ def check_own_checks(checks)
43
+ valid = checks.is_a?(Hash) && checks.all? do |holder, methods|
44
+ holder.is_a?(Module) && methods.is_a?(Hash) && methods.all? do |name, reason|
45
+ OWN_CHECKS.include?(name) && reason.is_a?(String) && !reason.strip.empty?
46
+ end
47
+ end
48
+ return if valid
49
+
50
+ raise ArgumentError, "own_checks must map a class to #{OWN_CHECKS.inspect} and the reason you keep each, got #{checks.inspect}"
51
+ end
52
+
25
53
  def resolve(adapter)
26
54
  case adapter
27
55
  when :pundit then Adapters::Pundit.new
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Authorization
8
+ # Every definition a schema holds, hidden or not. `schema.types` is read with no request, so it leaves out what is
9
+ # hidden there and a request can still reach: a type or field hidden by `visible?`, or one of several definitions of
10
+ # a name. This follows the definitions themselves from the roots and `orphan_types`.
11
+ module Definitions
12
+ def self.call(schema, &on_listing_error)
13
+ found = Set.new.compare_by_identity
14
+ pending = [schema.query, schema.mutation, schema.subscription, *schema.orphan_types, *directive_types(schema), *introspection_field_types(schema), *listed_types(schema, &on_listing_error)]
15
+ until pending.empty?
16
+ type = pending.pop
17
+ next if !type.respond_to?(:kind) || !found.add?(type)
18
+
19
+ pending.concat(referenced_types(type))
20
+ end
21
+ found.to_a
22
+ end
23
+
24
+ # With only static profiles, graphql-ruby refuses to list types without naming one: the roots still lead everywhere.
25
+ # A `visible_when:` that cannot name its model raises here, on the first listing only. The roots still lead to the
26
+ # rest, and the audit passes a block because it must not say ok after that.
27
+ def self.listed_types(schema)
28
+ schema.types.values
29
+ rescue ConfigurationError => error
30
+ yield error if block_given?
31
+ []
32
+ rescue ArgumentError
33
+ raise unless schema.visibility
34
+
35
+ []
36
+ end
37
+ private_class_method :listed_types
38
+
39
+ # A directive argument is client input like any other, and only `schema.types` used to reach its input objects.
40
+ def self.directive_types(schema)
41
+ schema.directives.values.flat_map { |directive| arguments_of(directive) }.compact
42
+ end
43
+ private_class_method :directive_types
44
+
45
+ # `introspection(true)` is a flag anyone can set, and graphql-ruby serves such a type like any other. The `__` prefix
46
+ # is reserved for introspection by the GraphQL spec: built-in types, what `Schema.introspection` replaces them with,
47
+ # and their copies all carry it.
48
+ def self.introspection?(type)
49
+ type.introspection? && type.graphql_name.start_with?("__")
50
+ end
51
+
52
+ # `Schema.introspection` can add entry points and dynamic fields, which no type holds and a client can reach.
53
+ def self.introspection_field_types(schema)
54
+ system = schema.introspection_system
55
+ (system.entry_points + system.dynamic_fields).flat_map { |field| references_of(field) }.flatten.compact
56
+ end
57
+ private_class_method :introspection_field_types
58
+
59
+ def self.connection?(type)
60
+ type.is_a?(Class) && type.include?(GraphQL::Types::Relay::ConnectionBehaviors)
61
+ end
62
+
63
+ def self.referenced_types(type)
64
+ references = []
65
+ references << fields_of(type)
66
+ references << arguments_of(type) if type.kind.input_object?
67
+ references.concat(type.interface_type_memberships.map(&:abstract_type)) if type.respond_to?(:interface_type_memberships)
68
+ references.concat(type.orphan_types) if type.kind.interface?
69
+ references.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
70
+ references << type.node_type if connection?(type)
71
+ references.flatten.compact
72
+ end
73
+ private_class_method :referenced_types
74
+
75
+ # Every definition of every field, nearest first: a hidden one falls back to the next of its name, and
76
+ # `all_field_definitions` merges them by name. `field ... do ... end` blocks are loaded here, because only
77
+ # `schema.types` or a request loads them, and the audit reads arguments from them.
78
+ def self.fields_at_every_level(type)
79
+ return [] unless type.respond_to?(:all_field_definitions)
80
+
81
+ fields = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) }.flat_map { |ancestor| ancestor.own_fields.values.flatten }
82
+ fields.each { |field| field.ensure_loaded if field.respond_to?(:ensure_loaded) }
83
+ fields.uniq
84
+ end
85
+
86
+ # A shadowed definition is only served by a fallback, so one whose type cannot load fails nothing that runs today.
87
+ def self.shadowed?(type, field)
88
+ !type.all_field_definitions.include?(field)
89
+ end
90
+
91
+ def self.fields_of(type)
92
+ fields_at_every_level(type).map do |field|
93
+ references_of(field)
94
+ rescue GraphQL::Error
95
+ raise unless shadowed?(type, field)
96
+
97
+ nil
98
+ end
99
+ end
100
+ private_class_method :fields_of
101
+
102
+ def self.references_of(field)
103
+ [field.type.unwrap, arguments_of(field)]
104
+ end
105
+ private_class_method :references_of
106
+
107
+ def self.arguments_of(member)
108
+ member.all_argument_definitions.flat_map { |argument| [argument.type.unwrap, argument.loads] }
109
+ end
110
+ private_class_method :arguments_of
111
+ end
112
+ end
113
+ end
114
+ end
@@ -17,10 +17,11 @@ module GraphQL
17
17
  # The question is asked of the parent object when it is an instance of the model, else of the model's
18
18
  # class (a root field has no record). The model is the one the field's type wraps, or `model:`.
19
19
  module Argument
20
- def initialize(*args, authorize: nil, reason: nil, model: nil, visible_when: nil, **kwargs, &block)
20
+ def initialize(*args, authorize: nil, reason: nil, model: nil, visible_when: nil, override_reason: nil, **kwargs, &block)
21
21
  unless [nil, :none].include?(authorize) || authorize.is_a?(Symbol)
22
22
  raise ArgumentError, "an argument's `authorize:` is an action or :none, not #{authorize.inspect}"
23
23
  end
24
+ raise ConfigurationError, "override_reason: must say why the override is safe" if !override_reason.nil? && override_reason.to_s.strip.empty?
24
25
 
25
26
  if authorize && authorize != :none && kwargs.key?(:default_value)
26
27
  raise ArgumentError,
@@ -32,32 +33,49 @@ module GraphQL
32
33
  @armory_reason = reason
33
34
  @armory_model = model
34
35
  @armory_visible_when = visible_when&.to_sym
36
+ @armory_override_reason = override_reason
35
37
  Rule.none(reason: reason) if authorize == :none
36
38
  super(*args, **kwargs, &block)
37
39
  end
38
40
 
39
- attr_reader :armory_declared, :armory_reason, :armory_visible_when
41
+ attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
40
42
 
41
43
  def armory_model
42
- @armory_model || armory_field_model
44
+ return armory_field_model unless @armory_model
45
+ return @armory_model unless @armory_model.is_a?(String)
46
+
47
+ armory_named_model
43
48
  end
44
49
 
45
50
  def authorized?(object, value, context)
46
51
  return super if @armory_declared.nil? || @armory_declared == :none
47
52
 
48
53
  model = armory_model
49
- subject = object.is_a?(model) ? object : model
50
- Integration.allowed?(subject, Rule.new(@armory_declared, attribute: keyword), context) && super
54
+ subject = Module === model && object.is_a?(model) ? object : model
55
+ Integration.allowed?(subject, Rule.new(@armory_declared, policy: armory_policy, attribute: keyword, model: model), context) && super
51
56
  end
52
57
 
53
58
  def visible?(context)
54
59
  return super unless @armory_visible_when
55
60
 
56
- super && Integration.visible?(armory_model, Rule.new(@armory_visible_when, attribute: keyword), context)
61
+ super && Integration.visible?(armory_model, Rule.new(@armory_visible_when, policy: armory_policy, attribute: keyword, model: armory_model), context)
62
+ end
63
+
64
+ # The `policy:` of the type or resolver the argument belongs to.
65
+ def armory_policy
66
+ type = owner.respond_to?(:owner) ? owner.owner : owner
67
+ type.armory_rule&.policy if type.respond_to?(:armory_rule)
57
68
  end
58
69
 
59
70
  private
60
71
 
72
+ # `is_a?` needs a Module, and Pundit reads a String as a record. A name with no constant stays a model name.
73
+ def armory_named_model
74
+ Object.const_get(@armory_model)
75
+ rescue NameError
76
+ @armory_model.to_sym
77
+ end
78
+
61
79
  def armory_field_model
62
80
  type = owner.respond_to?(:owner) ? owner.owner : owner
63
81
  model = type.armory_model if type.respond_to?(:armory_model)
@@ -19,7 +19,10 @@ module GraphQL
19
19
  module Field
20
20
  # `find_by: :id` looks the object up inside the user's scope instead of calling the resolver.
21
21
  # `loads:` arguments also load through the scope unless `load_through_scope: false`.
22
- def initialize(*args, authorize: nil, reason: nil, find_by: nil, load_through_scope: true, visible_when: nil, **kwargs, &block)
22
+ def initialize(*args, authorize: nil, reason: nil, find_by: nil, load_through_scope: true, visible_when: nil, override_reason: nil, **kwargs, &block)
23
+ raise ConfigurationError, "override_reason: must say why the override is safe" if !override_reason.nil? && override_reason.to_s.strip.empty?
24
+
25
+ @armory_override_reason = override_reason
23
26
  @armory_declared = authorize
24
27
  @armory_reason = reason
25
28
  @armory_visible_when = visible_when&.to_sym
@@ -29,7 +32,7 @@ module GraphQL
29
32
  extension(Integration::FindBy, key: find_by) if find_by
30
33
  end
31
34
 
32
- attr_reader :armory_declared, :armory_reason, :armory_visible_when
35
+ attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
33
36
 
34
37
  def armory_load_through_scope?
35
38
  @armory_load_through_scope
@@ -57,15 +60,20 @@ module GraphQL
57
60
  raise ConfigurationError, "#{owner.graphql_name}.#{original_name} uses visible_when:, so its type must include Base"
58
61
  end
59
62
 
60
- rule = Rule.new(@armory_visible_when, attribute: original_name)
63
+ rule = Rule.new(@armory_visible_when, policy: armory_policy, attribute: original_name)
61
64
  super && Integration.visible?(owner.armory_model, rule, context)
62
65
  end
63
66
 
67
+ # The `policy:` of the type that defines the field: a rule on the field asks it another question.
68
+ def armory_policy
69
+ owner.armory_rule&.policy if owner.respond_to?(:armory_rule)
70
+ end
71
+
64
72
  def authorized?(object, args, context)
65
73
  rule = armory_rule_for(owner, context)
66
74
  return false if rule.nil?
67
75
 
68
- (rule == :covered || Integration.allowed?(object, rule, context)) && super
76
+ (rule == :covered || Integration.allowed?(object, Integration.with_owner_model(rule, owner), context)) && super
69
77
  end
70
78
 
71
79
  private
@@ -92,7 +100,7 @@ module GraphQL
92
100
  when :none
93
101
  Rule.none(reason: @armory_reason)
94
102
  else
95
- Rule.new(@armory_declared, attribute: original_name)
103
+ Rule.new(@armory_declared, policy: armory_policy, attribute: original_name)
96
104
  end
97
105
  end
98
106
  end
@@ -60,7 +60,7 @@ module GraphQL
60
60
  rule = armory_rule
61
61
  return false if rule.nil?
62
62
 
63
- Integration.allowed?(object, rule, context) && super
63
+ Integration.allowed?(object, Integration.with_owner_model(rule, self), context) && super
64
64
  end
65
65
  end
66
66
  end
@@ -16,6 +16,13 @@ module GraphQL
16
16
  (superclass.armory_configuration if superclass.respond_to?(:armory_configuration))
17
17
  end
18
18
 
19
+ # Read when a query first needs it, after the schema is defined; each schema holds its own.
20
+ def armory_implementers(interface)
21
+ @armory_definitions ||= Definitions.(self)
22
+ @armory_implementers ||= {}.compare_by_identity
23
+ @armory_implementers[interface] ||= @armory_definitions.select { |type| type.kind.object? && type <= interface }
24
+ end
25
+
19
26
  # `loads:` asks the schema which type an object is. For an object type the answer is known:
20
27
  # the lookup went through that type's model. Abstract types and foreign objects are the app's call.
21
28
  def resolve_type(type, object, context)
@@ -96,13 +96,34 @@ module GraphQL
96
96
 
97
97
  # An interface field with no rule of its own cannot name the type it is resolved on, so it is only
98
98
  # covered when no implementer is a bypass: a field on a bypassed type is denied, and so is this one.
99
+ # `possible_types` is read with no request, so it drops an implementer hidden there.
99
100
  def self.bypassed_implementer?(interface, schema)
100
- schema.possible_types(interface).any? do |type|
101
+ hidden = schema.respond_to?(:armory_implementers) ? schema.armory_implementers(interface) : []
102
+ any_bypass?(schema.possible_types(interface) + hidden)
103
+ end
104
+
105
+ def self.any_bypass?(types)
106
+ types.any? do |type|
101
107
  rule = type.armory_rule if type.respond_to?(:armory_rule)
102
108
  rule && rule.none? && !rule.covers_fields?
103
109
  end
104
110
  end
105
111
 
112
+ # The rule with the model its owner wraps, the one the audit reads: named by the rule, or named like the type.
113
+ def self.with_owner_model(rule, owner)
114
+ return rule if rule.none? || rule.policy || rule.model || !owner.respond_to?(:armory_model)
115
+
116
+ rule.with_model(declared_model(owner) || owner.armory_model)
117
+ rescue ConfigurationError, GraphQL::RequiredImplementationMissingError
118
+ rule
119
+ end
120
+
121
+ # A Symbol or a String stays as written: only that spelling can name a policy for a Symbol the type wraps.
122
+ def self.declared_model(owner)
123
+ owner.armory_rule&.model if owner.respond_to?(:armory_rule)
124
+ end
125
+ private_class_method :declared_model
126
+
106
127
  # One Authorize per query: building it (and its memo and telemetry) once per object is pure overhead.
107
128
  def self.allowed?(subject, rule, context)
108
129
  store = context.namespace(:armory)
@@ -32,6 +32,11 @@ module GraphQL
32
32
  freeze
33
33
  end
34
34
 
35
+ def with_model(model)
36
+ self.class.new(action, policy: policy, subject: subject, attribute: attribute, model: model, scope: scope,
37
+ visible_when: visible_when, covers_fields: covers_fields, reason: reason)
38
+ end
39
+
35
40
  # A bypassed type whose fields are covered by something else, such as the payload of a mutation.
36
41
  def covers_fields?
37
42
  covers_fields
@@ -8,6 +8,7 @@ require "graphql/armory/authorization/adapters/null"
8
8
  require "graphql/armory/authorization/adapters/substitute"
9
9
  require "graphql/armory/authorization/authorize"
10
10
  require "graphql/armory/authorization/scope"
11
+ require "graphql/armory/authorization/definitions"
11
12
  require "graphql/armory/authorization/audit"
12
13
  require "graphql/armory/authorization/controls/rule"
13
14
  require "graphql/armory/authorization/controls/context"
@@ -35,7 +36,9 @@ module GraphQL
35
36
  module Authorization
36
37
  # The schema plugin: `use GraphQL::Armory::Authorization, adapter: :pundit`.
37
38
  # Options: `adapter:` (:pundit or an adapter object), `on_unauthorized:` (:null, :error or :raise),
38
- # `sinks:` (callables that receive each telemetry signal).
39
+ # `sinks:` (callables that receive each telemetry signal), `own_checks:` (an app's own `authorized?`, `authorizes?`, `authorized_new`, `wrap`,
40
+ # `unauthorized_object`, `visible?`, `scope_items` or `reauthorize_scoped_objects(false)` on a class, with the reason, for the audit), `own_denial_hooks:` (the schema's own
41
+ # `unauthorized_field` or `unauthorized_object`, each with the reason, for the audit).
39
42
  def self.use(schema_defn, **options)
40
43
  schema_defn.extend(Integration::Schema)
41
44
  schema_defn.armory_configuration = Configuration.new(**options)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Armory
5
- VERSION = "0.1.0.pre7"
5
+ VERSION = "0.1.0.pre8"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-armory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre7
4
+ version: 0.1.0.pre8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Israel
@@ -56,13 +56,26 @@ files:
56
56
  - lib/graphql/armory/authorization/adapters/substitute.rb
57
57
  - lib/graphql/armory/authorization/audit.rb
58
58
  - lib/graphql/armory/authorization/audit/command.rb
59
+ - lib/graphql/armory/authorization/audit/denial_hooks.rb
60
+ - lib/graphql/armory/authorization/audit/edge_rules.rb
61
+ - lib/graphql/armory/authorization/audit/findings.rb
62
+ - lib/graphql/armory/authorization/audit/foreign_policies.rb
63
+ - lib/graphql/armory/authorization/audit/input_loads.rb
64
+ - lib/graphql/armory/authorization/audit/interface_rules.rb
65
+ - lib/graphql/armory/authorization/audit/introspection_fields.rb
66
+ - lib/graphql/armory/authorization/audit/library_methods.rb
67
+ - lib/graphql/armory/authorization/audit/overrides.rb
68
+ - lib/graphql/armory/authorization/audit/own_checks.rb
59
69
  - lib/graphql/armory/authorization/audit/report.rb
70
+ - lib/graphql/armory/authorization/audit/type_rules.rb
71
+ - lib/graphql/armory/authorization/audit/visibility_models.rb
60
72
  - lib/graphql/armory/authorization/authorize.rb
61
73
  - lib/graphql/armory/authorization/configuration.rb
62
74
  - lib/graphql/armory/authorization/controls/actor.rb
63
75
  - lib/graphql/armory/authorization/controls/context.rb
64
76
  - lib/graphql/armory/authorization/controls/rule.rb
65
77
  - lib/graphql/armory/authorization/controls/subject.rb
78
+ - lib/graphql/armory/authorization/definitions.rb
66
79
  - lib/graphql/armory/authorization/errors.rb
67
80
  - lib/graphql/armory/authorization/integration.rb
68
81
  - lib/graphql/armory/authorization/integration/argument.rb