qonfig 0.22.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.rubocop.yml +19 -1
  4. data/.travis.yml +28 -20
  5. data/CHANGELOG.md +57 -1
  6. data/Gemfile.lock +102 -0
  7. data/LICENSE.txt +1 -1
  8. data/README.md +125 -9
  9. data/Rakefile +0 -1
  10. data/bin/rspec +1 -0
  11. data/gemfiles/with_external_deps.gemfile +2 -0
  12. data/gemfiles/with_external_deps.gemfile.lock +112 -0
  13. data/gemfiles/without_external_deps.gemfile.lock +102 -0
  14. data/lib/qonfig.rb +4 -1
  15. data/lib/qonfig/commands/definition/load_from_env.rb +2 -0
  16. data/lib/qonfig/commands/definition/load_from_env/value_converter.rb +2 -0
  17. data/lib/qonfig/data_set.rb +5 -2
  18. data/lib/qonfig/dsl.rb +2 -2
  19. data/lib/qonfig/imports/direct_key.rb +8 -2
  20. data/lib/qonfig/imports/mappings.rb +8 -2
  21. data/lib/qonfig/loaders/basic.rb +2 -0
  22. data/lib/qonfig/loaders/json.rb +2 -1
  23. data/lib/qonfig/plugins.rb +1 -0
  24. data/lib/qonfig/plugins/pretty_print.rb +8 -1
  25. data/lib/qonfig/plugins/pretty_print/requirements.rb +3 -0
  26. data/lib/qonfig/plugins/pretty_print/ruby_2_7_basic_object_pp_patch.rb +44 -0
  27. data/lib/qonfig/plugins/toml/commands/definition/expose_toml.rb +4 -4
  28. data/lib/qonfig/plugins/toml/commands/definition/load_from_toml.rb +1 -1
  29. data/lib/qonfig/plugins/toml/data_set.rb +2 -2
  30. data/lib/qonfig/plugins/toml/dsl.rb +2 -2
  31. data/lib/qonfig/plugins/vault.rb +24 -0
  32. data/lib/qonfig/plugins/vault/commands/definition/expose_vault.rb +142 -0
  33. data/lib/qonfig/plugins/vault/commands/definition/load_from_vault.rb +53 -0
  34. data/lib/qonfig/plugins/vault/dsl.rb +35 -0
  35. data/lib/qonfig/plugins/vault/errors.rb +9 -0
  36. data/lib/qonfig/plugins/vault/loaders/vault.rb +73 -0
  37. data/lib/qonfig/settings.rb +83 -22
  38. data/lib/qonfig/settings/key_matcher.rb +3 -1
  39. data/lib/qonfig/uploaders/base.rb +2 -0
  40. data/lib/qonfig/uploaders/file.rb +2 -2
  41. data/lib/qonfig/uploaders/yaml.rb +3 -1
  42. data/lib/qonfig/version.rb +1 -1
  43. data/qonfig.gemspec +6 -9
  44. data/sig/.keep +0 -0
  45. data/spec/features/clear_options_spec.rb +92 -0
  46. data/spec/features/compacted_config_spec.rb +308 -0
  47. data/spec/features/composition_spec.rb +207 -0
  48. data/spec/features/config_definition_and_representation_spec.rb +535 -0
  49. data/spec/features/definition_order_spec.rb +69 -0
  50. data/spec/features/dig_functionality_spec.rb +47 -0
  51. data/spec/features/dot_notation_spec.rb +159 -0
  52. data/spec/features/export_settings_spec.rb +138 -0
  53. data/spec/features/expose_json_spec.rb +281 -0
  54. data/spec/features/expose_self/format_option_dynamic_spec.rb +69 -0
  55. data/spec/features/expose_self/format_option_json_spec.rb +74 -0
  56. data/spec/features/expose_self/format_option_unsupported_spec.rb +27 -0
  57. data/spec/features/expose_self/format_option_yaml_spec.rb +77 -0
  58. data/spec/features/expose_self_spec.rb +97 -0
  59. data/spec/features/expose_yaml_spec.rb +263 -0
  60. data/spec/features/freeze_state_spec.rb +122 -0
  61. data/spec/features/get_config_keys_spec.rb +62 -0
  62. data/spec/features/get_config_values_spec.rb +41 -0
  63. data/spec/features/has_a_key_spec.rb +48 -0
  64. data/spec/features/import_settings_spec.rb +323 -0
  65. data/spec/features/indifferent_access_spec.rb +57 -0
  66. data/spec/features/inheritance_spec.rb +110 -0
  67. data/spec/features/instantiation_without_class_definition_spec.rb +59 -0
  68. data/spec/features/iteration_over_setting_keys_spec.rb +48 -0
  69. data/spec/features/load_from_env_spec.rb +240 -0
  70. data/spec/features/load_from_json_spec.rb +97 -0
  71. data/spec/features/load_from_self/format_option_json_spec.rb +31 -0
  72. data/spec/features/load_from_self/format_option_unsupported_spec.rb +27 -0
  73. data/spec/features/load_from_self/format_option_yaml_spec.rb +49 -0
  74. data/spec/features/load_from_self/with_erb_instructions_spec.rb +33 -0
  75. data/spec/features/load_from_self/with_hash_like_data_representation_spec.rb +66 -0
  76. data/spec/features/load_from_self/with_non_hash_like_data_representation_spec.rb +19 -0
  77. data/spec/features/load_from_self/without_end_data_spec.rb +11 -0
  78. data/spec/features/load_from_yaml_spec.rb +110 -0
  79. data/spec/features/load_setting_values_from_file/by_instance_method_examples.rb +171 -0
  80. data/spec/features/load_setting_values_from_file/by_macros_examples.rb +165 -0
  81. data/spec/features/load_setting_values_from_file/load_from_json_spec.rb +21 -0
  82. data/spec/features/load_setting_values_from_file/load_from_self/json_format/end_data_with_env_spec.rb +100 -0
  83. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_end_data_spec.rb +129 -0
  84. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_incorrect_end_data_spec.rb +34 -0
  85. data/spec/features/load_setting_values_from_file/load_from_self/json_format/without_end_data_spec.rb +65 -0
  86. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/end_data_with_env_spec.rb +94 -0
  87. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_end_data_spec.rb +126 -0
  88. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_incorrect_end_data_spec.rb +32 -0
  89. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/without_end_data_spec.rb +65 -0
  90. data/spec/features/load_setting_values_from_file/load_from_yaml_spec.rb +21 -0
  91. data/spec/features/load_setting_values_from_file/shared_behavior_spec.rb +33 -0
  92. data/spec/features/mixin_spec.rb +387 -0
  93. data/spec/features/non_redefineable_core_methods_spec.rb +29 -0
  94. data/spec/features/plugins/pretty_print_spec.rb +86 -0
  95. data/spec/features/plugins/toml/expose_self/format_option_toml_spec.rb +71 -0
  96. data/spec/features/plugins/toml/expose_toml_spec.rb +221 -0
  97. data/spec/features/plugins/toml/load_from_self/format_option_toml_spec.rb +27 -0
  98. data/spec/features/plugins/toml/load_from_toml_spec.rb +109 -0
  99. data/spec/features/plugins/toml/load_setting_values_from_file/load_from_toml_spec.rb +27 -0
  100. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/end_data_with_env_spec.rb +95 -0
  101. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_end_data_spec.rb +125 -0
  102. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_incorrect_end_data_spec.rb +34 -0
  103. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/without_end_data_spec.rb +65 -0
  104. data/spec/features/plugins/toml/load_setting_values_from_file/shared_behavior_spec.rb +34 -0
  105. data/spec/features/plugins/toml/save_to_toml_spec.rb +149 -0
  106. data/spec/features/plugins/vault/expose_vault_spec.rb +117 -0
  107. data/spec/features/plugins/vault/load_from_vault_spec.rb +80 -0
  108. data/spec/features/plugins_spec.rb +89 -0
  109. data/spec/features/reload_spec.rb +75 -0
  110. data/spec/features/run_code_with_temporary_settings_spec.rb +104 -0
  111. data/spec/features/save_to_file/save_to_json_spec.rb +157 -0
  112. data/spec/features/save_to_file/save_to_yaml_spec.rb +189 -0
  113. data/spec/features/settings_as_predicates_spec.rb +47 -0
  114. data/spec/features/settings_redefinition_spec.rb +30 -0
  115. data/spec/features/slice_functionality_spec.rb +69 -0
  116. data/spec/features/subset_functionality_spec.rb +49 -0
  117. data/spec/features/validation_spec.rb +916 -0
  118. data/spec/fixtures/array_settings.yml +3 -0
  119. data/spec/fixtures/expose_json/incompatible_root_structure.json +6 -0
  120. data/spec/fixtures/expose_json/incompatible_structure.json +4 -0
  121. data/spec/fixtures/expose_json/project.development.json +7 -0
  122. data/spec/fixtures/expose_json/project.json +30 -0
  123. data/spec/fixtures/expose_json/project.production.json +7 -0
  124. data/spec/fixtures/expose_json/project.staging.json +7 -0
  125. data/spec/fixtures/expose_json/project.test.json +7 -0
  126. data/spec/fixtures/expose_yaml/incompatible_structure.yml +2 -0
  127. data/spec/fixtures/expose_yaml/project.development.yml +4 -0
  128. data/spec/fixtures/expose_yaml/project.production.yml +4 -0
  129. data/spec/fixtures/expose_yaml/project.staging.yml +4 -0
  130. data/spec/fixtures/expose_yaml/project.test.yml +4 -0
  131. data/spec/fixtures/expose_yaml/project.yml +25 -0
  132. data/spec/fixtures/json_array_sample.json +14 -0
  133. data/spec/fixtures/json_object_sample.json +9 -0
  134. data/spec/fixtures/json_with_empty_object.json +6 -0
  135. data/spec/fixtures/json_with_erb.json +6 -0
  136. data/spec/fixtures/plugins/toml/expose_toml/project.development.toml +4 -0
  137. data/spec/fixtures/plugins/toml/expose_toml/project.production.toml +4 -0
  138. data/spec/fixtures/plugins/toml/expose_toml/project.staging.toml +4 -0
  139. data/spec/fixtures/plugins/toml/expose_toml/project.test.toml +4 -0
  140. data/spec/fixtures/plugins/toml/expose_toml/project.toml +27 -0
  141. data/spec/fixtures/plugins/toml/mini_file.toml +6 -0
  142. data/spec/fixtures/plugins/toml/toml_sample_with_all_types.toml +72 -0
  143. data/spec/fixtures/plugins/toml/values_file/with_env.toml +13 -0
  144. data/spec/fixtures/plugins/toml/values_file/without_env.toml +6 -0
  145. data/spec/fixtures/rubocop_settings.yml +12 -0
  146. data/spec/fixtures/shared_settings_with_aliases.yml +10 -0
  147. data/spec/fixtures/travis_settings.yml +6 -0
  148. data/spec/fixtures/values_file/with_env.json +19 -0
  149. data/spec/fixtures/values_file/with_env.yml +17 -0
  150. data/spec/fixtures/values_file/without_env.json +8 -0
  151. data/spec/fixtures/values_file/without_env.yml +5 -0
  152. data/spec/fixtures/with_empty_hash.yml +4 -0
  153. data/spec/fixtures/with_erb_instructions.yml +3 -0
  154. data/spec/spec_helper.rb +27 -0
  155. data/spec/support/meta_scopes.rb +20 -0
  156. data/spec/support/spec_support.rb +42 -0
  157. metadata +140 -30
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pp'
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NOTE: why not `.prepend` ?
4
+ # `prepend` in this case works incorrectly and sometimes this patch can not be correctly
5
+ # injected to the original module and it's children ancestors (Ruby-specific behaviour of
6
+ # module including and prepending).
7
+
8
+ # @api public
9
+ # @since 0.24.0
10
+ module PP::PPMethods
11
+ # :nocov:
12
+ def pp(obj)
13
+ # If obj is a Delegator then use the object being delegated to for cycle
14
+ # detection
15
+
16
+ # NOTE: --- PATCH ---
17
+ if defined?(::Delegator) and (
18
+ begin
19
+ (class << obj; self; end) <= ::Delegator # patch
20
+ rescue ::TypeError
21
+ obj.is_a?(::Delegator)
22
+ end
23
+ )
24
+ obj = obj.__getobj__
25
+ end # instead of: obj = obj.__getobj__ if defined?(::Delegator) and obj.is_a?(::Delegator)
26
+ # NOTE:
27
+ # Old implementation can not be used with BasicObject instances
28
+ # (with Qonfig::Compacted in our case)
29
+ # NOTE: --- PATCH ---
30
+
31
+ if check_inspect_key(obj)
32
+ group { obj.pretty_print_cycle self }
33
+ return
34
+ end
35
+
36
+ begin
37
+ push_inspect_key(obj)
38
+ group { obj.pretty_print self }
39
+ ensure
40
+ pop_inspect_key(obj) unless PP.sharing_detection
41
+ end
42
+ end
43
+ # :nocov:
44
+ end
@@ -19,7 +19,7 @@ class Qonfig::Commands::Definition::ExposeTOML < Qonfig::Commands::Base
19
19
  # @since 0.12.0
20
20
  EMPTY_TOML_DATA = {}.freeze
21
21
 
22
- # @return [String]
22
+ # @return [String, Pathname]
23
23
  #
24
24
  # @api private
25
25
  # @since 0.12.0
@@ -101,7 +101,7 @@ class Qonfig::Commands::Definition::ExposeTOML < Qonfig::Commands::Base
101
101
  realfile = dirname.join(envfile).to_s
102
102
 
103
103
  toml_data = load_toml_data(realfile)
104
- toml_based_settings = builde_data_set_klass(toml_data).new.settings
104
+ toml_based_settings = build_data_set_klass(toml_data).new.settings
105
105
 
106
106
  settings.__append_settings__(toml_based_settings)
107
107
  end
@@ -125,7 +125,7 @@ class Qonfig::Commands::Definition::ExposeTOML < Qonfig::Commands::Base
125
125
  "#{file_path} file does not contain settings with <#{env}> environment key!"
126
126
  ) unless toml_data_slice
127
127
 
128
- toml_based_settings = builde_data_set_klass(toml_data_slice).new.settings
128
+ toml_based_settings = build_data_set_klass(toml_data_slice).new.settings
129
129
 
130
130
  settings.__append_settings__(toml_based_settings)
131
131
  end
@@ -145,7 +145,7 @@ class Qonfig::Commands::Definition::ExposeTOML < Qonfig::Commands::Base
145
145
  #
146
146
  # @api private
147
147
  # @since 0.12.0
148
- def builde_data_set_klass(toml_data)
148
+ def build_data_set_klass(toml_data)
149
149
  Qonfig::DataSet::ClassBuilder.build_from_hash(toml_data)
150
150
  end
151
151
  end
@@ -7,7 +7,7 @@ class Qonfig::Commands::Definition::LoadFromTOML < Qonfig::Commands::Base
7
7
  # @since 0.20.0
8
8
  self.inheritable = true
9
9
 
10
- # @return [String]
10
+ # @return [String, Pathname]
11
11
  #
12
12
  # @api private
13
13
  # @since 0.12.0
@@ -3,7 +3,7 @@
3
3
  # @api public
4
4
  # @since 0.12.0
5
5
  class Qonfig::DataSet
6
- # @option path [String]
6
+ # @option path [String, Pathname]
7
7
  # @option options [Hash<Symbol,Any>] Nothing, just for compatability and consistency
8
8
  # @param value_processor [Block]
9
9
  # @return [void]
@@ -17,7 +17,7 @@ class Qonfig::DataSet
17
17
  end
18
18
  alias_method :dump_to_toml, :save_to_toml
19
19
 
20
- # @param file_path [String]
20
+ # @param file_path [String, Pathmame]
21
21
  # @option strict [Boolean]
22
22
  # @option expose [NilClass, String, Symbol] Environment key
23
23
  # @param configuration [Block]
@@ -3,7 +3,7 @@
3
3
  # @api private
4
4
  # @since 0.12.0
5
5
  module Qonfig::DSL
6
- # @param file_path [String]
6
+ # @param file_path [String, Pathname]
7
7
  # @option strict [Boolean]
8
8
  # @return [void]
9
9
  #
@@ -18,7 +18,7 @@ module Qonfig::DSL
18
18
  )
19
19
  end
20
20
 
21
- # @param file_path [String]
21
+ # @param file_path [String, Pathname]
22
22
  # @option strict [Boolean]
23
23
  # @option via [Symbol]
24
24
  # @option env [Symbol, String]
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.25.0
5
+ class Qonfig::Plugins::Vault < Qonfig::Plugins::Abstract
6
+ class << self
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.25.0
11
+ def install!
12
+ raise(
13
+ Qonfig::UnresolvedPluginDependencyError,
14
+ '::Vault does not exist or "vault" gem is not loaded'
15
+ ) unless const_defined?('::Vault')
16
+
17
+ require_relative 'vault/errors'
18
+ require_relative 'vault/loaders/vault'
19
+ require_relative 'vault/commands/definition/load_from_vault'
20
+ require_relative 'vault/commands/definition/expose_vault'
21
+ require_relative 'vault/dsl'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.25.0
5
+ class Qonfig::Commands::Definition::ExposeVault < Qonfig::Commands::Base
6
+ # @since 0.25.0
7
+ self.inheritable = true
8
+
9
+ # @return [Hash<Symbol,Symbol>]
10
+ #
11
+ # @api private
12
+ # @since 0.25.0
13
+ EXPOSERS = { path: :path, env_key: :env_key }.freeze
14
+
15
+ # @return [Hash]
16
+ #
17
+ # @api private
18
+ # @since 0.25.0
19
+ EMPTY_VAULT_DATA = {}.freeze
20
+
21
+ # @return [String, Pathname]
22
+ #
23
+ # @api private
24
+ # @since 0.25.0
25
+ attr_reader :path
26
+
27
+ # @return [Boolean]
28
+ #
29
+ # @api private
30
+ # @since 0.25.0
31
+ attr_reader :strict
32
+
33
+ # @return [Symbol]
34
+ #
35
+ # @api private
36
+ # @since 0.25.0
37
+ attr_reader :via
38
+
39
+ # @return [Symbol, String]
40
+ #
41
+ # @api private
42
+ # @since 0.25.0
43
+ attr_reader :env
44
+
45
+ # @param path [String Pathname]
46
+ # @option strict [Boolean]
47
+ # @option via [Symbol]
48
+ # @option env [String, Symbol]
49
+ # @return [void]
50
+ #
51
+ # @api private
52
+ # @since 0.25.0
53
+ def initialize(path, strict: true, via:, env:)
54
+ unless env.is_a?(Symbol) || env.is_a?(String) || env.is_a?(Numeric)
55
+ raise Qonfig::ArgumentError, ':env should be a string or a symbol'
56
+ end
57
+
58
+ raise Qonfig::ArgumentError, ':env should be provided' if env.to_s.empty?
59
+ raise Qonfig::ArgumentError, 'used :via is unsupported' unless EXPOSERS.key?(via)
60
+
61
+ @path = path
62
+ @strict = strict
63
+ @via = via
64
+ @env = env
65
+ end
66
+
67
+ # @param data_set [Qonfig::DataSet]
68
+ # @param settings [Qonfig::Settings]
69
+ # @return [void]
70
+ #
71
+ # @api private
72
+ # @since 0.25.0
73
+ def call(_data_set, settings)
74
+ case via
75
+ when EXPOSERS[:path]
76
+ expose_path!(settings)
77
+ when EXPOSERS[:env_key]
78
+ expose_env_key!(settings)
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ # @param settings [Qonfig::Settings]
85
+ # @return [void]
86
+ #
87
+ # @api private
88
+ # @since 0.25.0
89
+ def expose_path!(settings)
90
+ # NOTE: transform path (insert environment name into a secret name)
91
+ # from: kv/data/secret_name
92
+ # to: kv/data/env_name/secret_name
93
+
94
+ splitted_path = path.split('/')
95
+ real_path = splitted_path.insert(-2, env.to_s).join('/')
96
+
97
+ vault_data = load_vault_data(real_path)
98
+ vault_based_settings = build_data_set_class(vault_data).new.settings
99
+
100
+ settings.__append_settings__(vault_based_settings)
101
+ end
102
+
103
+ # @param settings [Qonfig::Settings]
104
+ # @return [void]
105
+ #
106
+ # @raise [Qonfig::ExposeError]
107
+ #
108
+ # @api private
109
+ # @since 0.25.0
110
+ def expose_env_key!(settings)
111
+ vault_data = load_vault_data(path)
112
+ vault_data_slice = vault_data[env.to_sym]
113
+ vault_data_slice = EMPTY_VAULT_DATA.dup if vault_data_slice.nil? && !strict
114
+
115
+ raise(
116
+ Qonfig::ExposeError,
117
+ "#{path} does not contain settings with <#{env}> environment key!"
118
+ ) unless vault_data_slice
119
+
120
+ vault_based_settings = build_data_set_class(vault_data_slice).new.settings
121
+
122
+ settings.__append_settings__(vault_based_settings)
123
+ end
124
+
125
+ # @param path [String]
126
+ # @return [Hash]
127
+ #
128
+ # @api private
129
+ # @since 0.25.0
130
+ def load_vault_data(path)
131
+ Qonfig::Loaders::Vault.load_file(path, fail_on_unexist: strict)
132
+ end
133
+
134
+ # @param vault_data [Hash]
135
+ # @return [Class<Qonfig::DataSet>]
136
+ #
137
+ # @api private
138
+ # @since 0.25.0
139
+ def build_data_set_class(vault_data)
140
+ Qonfig::DataSet::ClassBuilder.build_from_hash(vault_data)
141
+ end
142
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.25.0
5
+ class Qonfig::Commands::Definition::LoadFromVault < Qonfig::Commands::Base
6
+ # @since 0.25.0
7
+ self.inheritable = true
8
+
9
+ # @return [String, Pathname]
10
+ #
11
+ # @api private
12
+ # @since 0.25.0
13
+ attr_reader :path
14
+
15
+ # @return [Boolean]
16
+ #
17
+ # @api private
18
+ # @since 0.25.0
19
+ attr_reader :strict
20
+
21
+ # @param path [String]
22
+ # @option strict [Boolean]
23
+ #
24
+ # @api private
25
+ # @since 0.25.0
26
+ def initialize(path, strict: true)
27
+ @path = path
28
+ @strict = strict
29
+ end
30
+
31
+ # @param data_set [Qonfig::DataSet]
32
+ # @param settings [Qonfig::Settings]
33
+ # @return [void]
34
+ #
35
+ # @api private
36
+ # @since 0.25.0
37
+ def call(_data_set, settings)
38
+ vault_data = Qonfig::Loaders::Vault.load_file(path, fail_on_unexist: strict)
39
+ vault_based_settings = build_data_set_klass(vault_data).new.settings
40
+ settings.__append_settings__(vault_based_settings)
41
+ end
42
+
43
+ private
44
+
45
+ # @param toml_data [Hash]
46
+ # @return [Class<Qonfig::DataSet>]
47
+ #
48
+ # @api private
49
+ # @since 0.25.0
50
+ def build_data_set_klass(toml_data)
51
+ Qonfig::DataSet::ClassBuilder.build_from_hash(toml_data)
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.25.0
5
+ module Qonfig::DSL
6
+ # @param path [String, Pathname]
7
+ # @option strict [Boolean]
8
+ # @return [void]
9
+ #
10
+ # @see Qonfig::Commands::Definition::LoadFromVault
11
+ #
12
+ # @api public
13
+ # @since 0.25.0
14
+ def load_from_vault(path, strict: true)
15
+ definition_commands << Qonfig::Commands::Definition::LoadFromVault.new(
16
+ path, strict: strict
17
+ )
18
+ end
19
+
20
+ # @param path [String, Pathname]
21
+ # @option strict [Boolean]
22
+ # @option via [Symbol]
23
+ # @option env [Symbol, String]
24
+ # @return [void]
25
+ #
26
+ # @see Qonfig::Commands::Definition::ExposeVault
27
+ #
28
+ # @api public
29
+ # @since 0.25.0
30
+ def expose_vault(path, strict: true, via:, env:)
31
+ definition_commands << Qonfig::Commands::Definition::ExposeVault.new(
32
+ path, strict: strict, via: via, env: env
33
+ )
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.25.0
5
+ module Qonfig
6
+ # @api public
7
+ # @since 0.25.0
8
+ VaultLoaderError = Class.new(Vault::VaultError)
9
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.25.0
5
+ class Qonfig::Loaders::Vault < Qonfig::Loaders::Basic
6
+ # @return [Binding]
7
+ #
8
+ # @api private
9
+ # @since 0.25.0
10
+ VAULT_EXPR_EVAL_SCOPE = BasicObject.new.__binding__.tap do |binding|
11
+ Object.new.method(:freeze).unbind.bind(binding.receiver).call
12
+ end
13
+
14
+ class << self
15
+ # @param path [String, Pathname]
16
+ # @option fail_on_unexist [Boolean]
17
+ # @return [Object]
18
+ #
19
+ # @raise [Qonfig::FileNotFoundError]
20
+ #
21
+ # @api private
22
+ # @since 0.25.0
23
+ def load_file(path, fail_on_unexist: true)
24
+ data = ::Vault.with_retries(Vault::HTTPError) do
25
+ ::Vault.logical.read(path.to_s)&.data&.dig(:data)
26
+ end
27
+ raise Qonfig::FileNotFoundError, "Path #{path} not exist" if data.nil? && fail_on_unexist
28
+ result = data || empty_data
29
+ deep_transform_values(result)
30
+ rescue Vault::VaultError => error
31
+ raise(Qonfig::VaultLoaderError.new(error.message).tap do |exception|
32
+ exception.set_backtrace(error.backtrace)
33
+ end)
34
+ end
35
+
36
+ # @return [Hash]
37
+ #
38
+ # @api private
39
+ # @since 0.25.0
40
+ def empty_data
41
+ {}
42
+ end
43
+
44
+ private
45
+
46
+ # @param vault_data [Hash<Object,Object>]
47
+ # @return [Object]
48
+ #
49
+ # @api private
50
+ # @since 0.25.0
51
+ def deep_transform_values(vault_data)
52
+ return vault_data unless vault_data.is_a?(Hash)
53
+
54
+ vault_data.transform_values do |value|
55
+ next safely_evaluate(value) if value.is_a?(String)
56
+
57
+ deep_transform_values(value)
58
+ end
59
+ end
60
+
61
+ # @param vault_expr [String]
62
+ # @return [Object]
63
+ #
64
+ # @api private
65
+ # @since 0.25.0
66
+ def safely_evaluate(vault_expr)
67
+ parsed_expr = ::ERB.new(vault_expr).result
68
+ VAULT_EXPR_EVAL_SCOPE.eval(parsed_expr)
69
+ rescue StandardError, ScriptError
70
+ parsed_expr
71
+ end
72
+ end
73
+ end