graphql-armory 0.1.0.pre11 → 0.1.0.pre13

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -1
  3. data/README.md +4 -4
  4. data/docs/authorization.md +7 -7
  5. data/docs/rate-limiting.md +1 -1
  6. data/docs/versioning.md +540 -0
  7. data/lib/graphql/armory/authorization/adapters/pundit.rb +26 -1
  8. data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
  9. data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
  10. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
  11. data/lib/graphql/armory/authorization/definitions.rb +8 -92
  12. data/lib/graphql/armory/authorization/integration/object.rb +2 -2
  13. data/lib/graphql/armory/authorization/integration.rb +29 -2
  14. data/lib/graphql/armory/authorization/rule.rb +3 -3
  15. data/lib/graphql/armory/authorization.rb +3 -1
  16. data/lib/graphql/armory/definitions.rb +111 -0
  17. data/lib/graphql/armory/location.rb +101 -0
  18. data/lib/graphql/armory/rate_limits/analysis_guard.rb +9 -2
  19. data/lib/graphql/armory/version.rb +1 -1
  20. data/lib/graphql/armory/versioning/audit/command.rb +43 -0
  21. data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
  22. data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
  23. data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
  24. data/lib/graphql/armory/versioning/audit.rb +559 -0
  25. data/lib/graphql/armory/versioning/controls.rb +403 -0
  26. data/lib/graphql/armory/versioning/decision.rb +89 -0
  27. data/lib/graphql/armory/versioning/deprecation.rb +95 -0
  28. data/lib/graphql/armory/versioning/diff/command.rb +49 -0
  29. data/lib/graphql/armory/versioning/diff.rb +349 -0
  30. data/lib/graphql/armory/versioning/dump/command.rb +46 -0
  31. data/lib/graphql/armory/versioning/dump.rb +107 -0
  32. data/lib/graphql/armory/versioning/gate.rb +140 -0
  33. data/lib/graphql/armory/versioning/integration.rb +903 -0
  34. data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
  35. data/lib/graphql/armory/versioning/profiles.rb +122 -0
  36. data/lib/graphql/armory/versioning/rspec.rb +38 -0
  37. data/lib/graphql/armory/versioning/versions.rb +111 -0
  38. data/lib/graphql/armory/versioning/view.rb +61 -0
  39. data/lib/graphql/armory/versioning.rb +132 -0
  40. data/lib/graphql/armory.rb +1 -0
  41. data/lib/tasks/graphql_armory.rake +20 -0
  42. metadata +24 -2
  43. data/lib/graphql/armory/authorization/location.rb +0 -79
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ class Audit
7
+ # An app's `visible?` that answers before the plugin's decides the version for it: if it does not call `super`, it
8
+ # serves a member outside its lifecycle. The audit cannot read whether it does, so each one is reported once, where it
9
+ # is defined, until `own_checks:` declares it with a reason. Methods of graphql-ruby and of this gem are not the app's,
10
+ # and a method behind the plugin's runs only after the version allowed the member.
11
+ #
12
+ # Read along the whole chain, as Ruby calls it (`method(:visible?)` and its `super_method`): on type classes,
13
+ # resolvers and mutations with `Base`, on field, argument, enum value and membership classes with `Base`, on one
14
+ # member (a `def self.visible?` in a field's block, which no class holds, so it cannot be declared), and on the
15
+ # schema, whose `self.visible?` graphql-ruby asks for every member.
16
+ class OwnChecks
17
+ INTEGRATIONS = [Integration::Type, Integration::Field, Integration::Argument, Integration::EnumValue, Integration::Membership].freeze
18
+ LIBRARIES = [Armory::Location::GRAPHQL_RUBY_PATH, Armory::Location::ARMORY_PATH].freeze
19
+
20
+ def self.call(schema, definitions, members, findings)
21
+ new(definitions, members, findings).(schema, schema.armory_versioning.own_checks)
22
+ end
23
+
24
+ # The class or module `own_checks:` names for a `visible?` method: a class method's holder is the class, and an
25
+ # interface's `definition_methods` belong to the interface.
26
+ def self.holder_of(owner, method)
27
+ held = method.owner
28
+ owner.ancestors.find do |ancestor|
29
+ ancestor.singleton_class.equal?(held) ||
30
+ (!ancestor.is_a?(Class) && ancestor.const_defined?(:DefinitionMethods, false) && ancestor.const_get(:DefinitionMethods, false).equal?(held))
31
+ end || held
32
+ end
33
+
34
+ def initialize(definitions = [], members = [], findings = Findings.new)
35
+ @definitions = definitions
36
+ @members = members
37
+ @findings = findings
38
+ @found = {}.compare_by_identity
39
+ @singletons = []
40
+ end
41
+
42
+ def call(schema, declared)
43
+ types = @definitions + @members.grep(GraphQL::Schema::Field).filter_map(&:resolver)
44
+ types.uniq.each { |type| note_type(type) }
45
+ @members.each { |member| note_member(member) unless member.is_a?(Module) }
46
+ note_schema(schema)
47
+ report(declared)
48
+ @findings
49
+ end
50
+
51
+ private
52
+
53
+ def note_type(type)
54
+ return unless type.is_a?(Module) && type.singleton_class.include?(Integration::Type)
55
+
56
+ ahead(type.method(:visible?)) { |method| note(holder_of(type, method), method, ".") }
57
+ end
58
+
59
+ def note_member(member)
60
+ return unless INTEGRATIONS.any? { |integration| member.is_a?(integration) }
61
+
62
+ ahead(member.method(:visible?)) do |method|
63
+ if member.class.ancestors.include?(method.owner)
64
+ note(method.owner, method, "#")
65
+ else
66
+ @singletons << [member, method]
67
+ end
68
+ end
69
+ end
70
+
71
+ # Every method of the chain: the schema has no plugin method to stop at.
72
+ def note_schema(schema)
73
+ method = schema.method(:visible?)
74
+ while method
75
+ note(holder_of(schema, method), method, ".") unless library?(method)
76
+ method = method.super_method
77
+ end
78
+ end
79
+
80
+ # The app's methods Ruby calls before the plugin's.
81
+ def ahead(method)
82
+ while method && !INTEGRATIONS.include?(method.owner)
83
+ yield method unless library?(method)
84
+ method = method.super_method
85
+ end
86
+ end
87
+
88
+ def library?(method)
89
+ file = method.source_location&.first
90
+ !file.nil? && file.start_with?(*LIBRARIES)
91
+ end
92
+
93
+ def note(holder, method, separator)
94
+ @found[holder] ||= ["#{name_of(holder)}#{separator}visible?", method]
95
+ end
96
+
97
+ def holder_of(owner, method) = self.class.holder_of(owner, method)
98
+
99
+ def name_of(holder) = self.class.name_of(holder)
100
+
101
+ # How a report names a holder: a type by its GraphQL name, a schema or another class by its constant.
102
+ def self.name_of(holder)
103
+ return holder.name || "Schema" if holder.is_a?(Class) && holder <= GraphQL::Schema
104
+ return Integration.display_name(holder) if holder.respond_to?(:graphql_name)
105
+
106
+ holder.name || holder.inspect
107
+ end
108
+
109
+ def report(declared)
110
+ @found.each do |holder, (path, method)|
111
+ reason = declared.fetch(holder, {})[:visible?]
112
+ next @findings.information(:own_check, path, "declared in own_checks: #{reason}", at: method) if reason
113
+
114
+ @findings.problem(:own_check, path, "#{risk(holder)}. Remove it, or declare it in own_checks: with a reason", at: method)
115
+ end
116
+ @singletons.uniq { |member, _| member.object_id }.each do |member, method|
117
+ @findings.problem(:own_check, member.path, "has a visible? of its own (a def self.visible? on this one member): it answers " \
118
+ "before the plugin's, and own_checks: cannot declare it. Move it to a class, or remove it", at: method)
119
+ end
120
+ declared.each_key do |holder|
121
+ next if @found.key?(holder)
122
+
123
+ @findings.problem(:own_check, "#{name_of(holder)} visible?", "named in own_checks: but defines no visible? ahead of the plugin's: " \
124
+ "remove it from the option", at: holder)
125
+ end
126
+ end
127
+
128
+ def risk(holder)
129
+ if holder.is_a?(Class) && holder <= GraphQL::Schema
130
+ "defined by the app on the schema: graphql-ruby asks it about every member, so if it does not call super nothing is versioned"
131
+ else
132
+ "defined by the app, it answers before the plugin's: if it does not call super, it serves members outside their lifecycle"
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ class Audit
7
+ # `:profiles`: under `use ..., profiles: true`, graphql-ruby asks `visible?` once per version with the profile's
8
+ # frozen Hash, never with a query's context. A check that needs the query's user cannot run there: Authorization's
9
+ # `visible_when:` raises on every query that reaches it, and the app's own `visible?` would decide once for every
10
+ # user. Each is reported where it is declared. The audit cannot read whether an app's `visible?` reads the user, so a
11
+ # `visible?` declared in `own_checks:` is not reported here: the declaration is the app's word, listed as `:own_check`.
12
+ module Profiles
13
+ def self.call(schema, findings)
14
+ return findings unless schema.respond_to?(:armory_versioning_profiles) && schema.armory_versioning_profiles.enabled?
15
+
16
+ found = {}
17
+ declared = schema.armory_versioning.own_checks
18
+ Members.of(schema).each do |member|
19
+ visible_when(member, findings)
20
+ klass = member.is_a?(Module) ? member : member.class
21
+ separator = member.is_a?(Module) ? "." : "#"
22
+ method = member.is_a?(Module) ? member.method(:visible?) : klass.instance_method(:visible?)
23
+ own_methods(klass, method, separator, found)
24
+ end
25
+ own_methods(schema, schema.method(:visible?), ".", found)
26
+ found.each do |(holder, _), (path, method)|
27
+ next if declared.fetch(holder, {})[:visible?]
28
+
29
+ findings.problem(:profiles, path, "defined by the app: under profiles: true graphql-ruby asks it once per version, with the " \
30
+ "profile's Hash instead of a query's context, so it decides for every user at once. Drop " \
31
+ "profiles: true, or make sure it reads nothing but the version", at: method)
32
+ end
33
+ findings
34
+ end
35
+
36
+ def self.visible_when(member, findings)
37
+ declared = if member.is_a?(Module)
38
+ member.respond_to?(:armory_rule) && member.armory_rule&.visible_when
39
+ else
40
+ member.respond_to?(:armory_visible_when) && member.armory_visible_when
41
+ end
42
+ return unless declared
43
+
44
+ findings.problem(:profiles, Members.path_of(member), "uses visible_when: #{declared.inspect}, which needs the query's user, but " \
45
+ "under profiles: true visible? is asked with the version's profile Hash, so " \
46
+ "every query that reaches it raises. Drop visible_when: here, or profiles: true", at: member)
47
+ end
48
+ private_class_method :visible_when
49
+
50
+ # Follows the whole chain: a method that only calls super must not hide one below it.
51
+ def self.own_methods(klass, method, separator, found)
52
+ while method
53
+ file = method.source_location&.first
54
+ if file && !file.start_with?(Location::GRAPHQL_RUBY_PATH, Location::ARMORY_PATH)
55
+ holder = OwnChecks.holder_of(klass, method)
56
+ found[[holder, separator]] ||= ["#{OwnChecks.name_of(holder)}#{separator}visible?", method]
57
+ end
58
+ method = method.super_method
59
+ end
60
+ end
61
+ private_class_method :own_methods
62
+ end
63
+
64
+ # `:retired_only`: a member, or one of two twins, whose lifecycle covers only retired versions. No client can reach
65
+ # it any more, so it is dead code: information, which `ok?` ignores, unless `strict: true` makes it a problem.
66
+ module RetiredOnly
67
+ def self.call(schema, findings, strict: false)
68
+ return findings unless schema.respond_to?(:armory_versioning)
69
+
70
+ versions = schema.armory_versioning.versions.to_a
71
+ return findings if versions.none?(&:retired?)
72
+
73
+ Members.of(schema).each do |member|
74
+ lifecycle = Integration.lifecycle_of(member)
75
+ next if Lifecycle::EVERYWHERE.equal?(lifecycle)
76
+
77
+ covered = covered(lifecycle, versions)
78
+ next if covered.empty? || !covered.all?(&:retired?)
79
+
80
+ message = "lives only in retired versions (#{covered.map(&:label).join(', ')}), by its lifecycle #{lifecycle.to_h.inspect}: " \
81
+ "no client can reach it. Delete it"
82
+ if strict
83
+ findings.problem(:retired_only, Members.path_of(member), message, at: member)
84
+ else
85
+ findings.information(:retired_only, Members.path_of(member), message, at: member)
86
+ end
87
+ end
88
+ findings
89
+ end
90
+
91
+ # A label versions: does not hold is reported as :undeclared_version.
92
+ def self.covered(lifecycle, versions)
93
+ versions.select { |version| lifecycle.covers?(version) }
94
+ rescue ConfigurationError
95
+ []
96
+ end
97
+ private_class_method :covered
98
+ end
99
+
100
+ # Every type and member a schema holds, twins included, as the audit walks them.
101
+ module Members
102
+ def self.of(schema)
103
+ members = Integration.definitions(schema).flat_map do |type|
104
+ found = [type, *Armory::Definitions.fields_at_every_level(type), *Integration.arguments_in(type)]
105
+ found.concat(type.all_enum_value_definitions) if type.kind.enum?
106
+ found.concat(type.type_memberships) if type.kind.union?
107
+ found.concat(type.interface_type_memberships.select { |membership| membership.respond_to?(:visible?) }) if type.respond_to?(:interface_type_memberships)
108
+ found
109
+ end
110
+ members.concat(schema.directives.values.flat_map(&:all_argument_definitions))
111
+ members.uniq
112
+ end
113
+
114
+ def self.path_of(member)
115
+ return member.path if member.respond_to?(:path) && !member.is_a?(Module)
116
+
117
+ Integration.display_name(member)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ class Audit
7
+ # graphql-ruby's `Subscriptions#trigger` finds the field with the trigger's context, but reads its arguments with no
8
+ # query, where every definition counts: it fills in the default of every argument and input field whatever its
9
+ # version, then builds the topic with the trigger's context. So an argument (or a nested input field) that a served
10
+ # version has and the latest does not, or the reverse, breaks triggers: with a default, a trigger naming the version
11
+ # without it raises and a trigger naming another version never reaches that version's subscribers; without one, a
12
+ # trigger that passes it raises unless its context names a version that has it.
13
+ #
14
+ # Listed as information: a subscription field the default version does not have, and `default: :refuse`, since a
15
+ # trigger that names no version then raises.
16
+ class SubscriptionTriggers
17
+ def self.call(schema, findings)
18
+ new(findings).(schema)
19
+ end
20
+
21
+ def initialize(findings = Findings.new)
22
+ @findings = findings
23
+ end
24
+
25
+ def call(schema)
26
+ root = schema.subscription
27
+ return @findings unless root
28
+
29
+ gate = schema.armory_versioning
30
+ default = gate.resolve(nil)
31
+ fields = Armory::Definitions.fields_at_every_level(root)
32
+ refuse(root) if default.refused? && fields.any? { |field| field.is_a?(Integration::Field) }
33
+ fields.each { |field| check(field, gate.versions, default) }
34
+ @findings
35
+ end
36
+
37
+ private
38
+
39
+ def refuse(root)
40
+ @findings.information(:subscription_trigger, root.graphql_name, "default: :refuse, so a trigger that names no version raises " \
41
+ "MissingVersionError. Pass context: { armory_version: ... } to every trigger", at: root)
42
+ end
43
+
44
+ def check(field, versions, default)
45
+ served = versions.each_served.select { |version| live?(field, version) }
46
+ return if served.empty?
47
+
48
+ unless default.refused? || served.include?(default.version)
49
+ @findings.information(:subscription_trigger, field.path, "the default version (#{label(default.version)}) does not have it, so a " \
50
+ "trigger that names no version raises InvalidTriggerError. Pass " \
51
+ "context: { armory_version: ... } to its triggers", at: field)
52
+ end
53
+ latest = covered(field.all_argument_definitions, Version::LATEST)
54
+ served.each do |version|
55
+ listed = covered(field.all_argument_definitions, version)
56
+ (listed.keys - latest.keys).each { |argument| differs(argument, Version::LATEST) }
57
+ (latest.keys - listed.keys).each { |argument| differs(argument, version) }
58
+ end
59
+ end
60
+
61
+ # The arguments and nested input fields a version has, by identity.
62
+ def covered(arguments, version, found = {}.compare_by_identity, seen = {}.compare_by_identity)
63
+ arguments.each do |argument|
64
+ next unless Integration.covers?(argument, version)
65
+
66
+ found[argument] = true
67
+ input = argument.type.unwrap
68
+ next unless input.kind.input_object? && !seen.key?(input)
69
+
70
+ seen[input] = true
71
+ covered(input.all_argument_definitions, version, found, seen)
72
+ end
73
+ found
74
+ end
75
+
76
+ def differs(argument, without)
77
+ where = label(without)
78
+ message = if argument.default_value?
79
+ "has a default and is not in #{where}: trigger fills in the default of every argument whatever the version, " \
80
+ "so a trigger that names #{where} raises, and a trigger that names another version never reaches the " \
81
+ "subscribers of #{where}. Remove the default, or keep the argument in every version"
82
+ else
83
+ "is not in #{where}: trigger reads arguments with no version, so a trigger that passes it raises unless its " \
84
+ "context names a version that has it (armory_version:)"
85
+ end
86
+ @findings.problem(:subscription_trigger, argument.path, message, at: argument)
87
+ end
88
+
89
+ def live?(field, version)
90
+ [field, field.owner, field.resolver].compact.all? { |member| Integration.lifecycle_of(member).covers?(version) }
91
+ end
92
+
93
+ def label(version) = Integration.label(version)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end