graphql-armory 0.1.0.pre12 → 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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -1
  3. data/README.md +3 -3
  4. data/docs/versioning.md +540 -0
  5. data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
  6. data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
  7. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
  8. data/lib/graphql/armory/authorization/definitions.rb +8 -92
  9. data/lib/graphql/armory/authorization.rb +3 -1
  10. data/lib/graphql/armory/definitions.rb +111 -0
  11. data/lib/graphql/armory/location.rb +101 -0
  12. data/lib/graphql/armory/version.rb +1 -1
  13. data/lib/graphql/armory/versioning/audit/command.rb +43 -0
  14. data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
  15. data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
  16. data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
  17. data/lib/graphql/armory/versioning/audit.rb +559 -0
  18. data/lib/graphql/armory/versioning/controls.rb +403 -0
  19. data/lib/graphql/armory/versioning/decision.rb +89 -0
  20. data/lib/graphql/armory/versioning/deprecation.rb +95 -0
  21. data/lib/graphql/armory/versioning/diff/command.rb +49 -0
  22. data/lib/graphql/armory/versioning/diff.rb +349 -0
  23. data/lib/graphql/armory/versioning/dump/command.rb +46 -0
  24. data/lib/graphql/armory/versioning/dump.rb +107 -0
  25. data/lib/graphql/armory/versioning/gate.rb +140 -0
  26. data/lib/graphql/armory/versioning/integration.rb +903 -0
  27. data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
  28. data/lib/graphql/armory/versioning/profiles.rb +122 -0
  29. data/lib/graphql/armory/versioning/rspec.rb +38 -0
  30. data/lib/graphql/armory/versioning/versions.rb +111 -0
  31. data/lib/graphql/armory/versioning/view.rb +61 -0
  32. data/lib/graphql/armory/versioning.rb +132 -0
  33. data/lib/graphql/armory.rb +1 -0
  34. data/lib/tasks/graphql_armory.rake +20 -0
  35. metadata +24 -2
  36. data/lib/graphql/armory/authorization/location.rb +0 -103
@@ -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