settings_reader-vault_resolver 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 684115b87887c0d2774679160f1f0d7d7136035053b3af87a9878fd5ab4b5778
4
+ data.tar.gz: 9d3747fdd02285d0664569ec73566951637aeb1c145223da5be9239078c4a6cf
5
+ SHA512:
6
+ metadata.gz: 3b7b5512574b61c2b7fd96fdf121152938c7c232e12c8da23a864f52db1502501406dec213e28eec118decdfe142f7f90dbcd1748ae551558583691af26de97f
7
+ data.tar.gz: 81f2e979bee9b9cfca5ec316c4218bc2260042875eab9fc580443e0d4edcd159299c64398b17ddc12fe9a25aac545d6f217cabd5f71e146fe1c4eb6c31033e4d
@@ -0,0 +1,100 @@
1
+ name: ci
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ pull_request:
7
+ branches:
8
+ - main
9
+ release:
10
+ types: [published]
11
+
12
+ jobs:
13
+ build:
14
+ env:
15
+ COVERAGE: true
16
+ VAULT_ADDR: 'http://127.0.0.1:8200'
17
+ VAULT_TOKEN: 'vault_root_token'
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby: [ '2.5.x', '2.6.x', '2.7.x', '3.0.x' ]
23
+ services:
24
+ vault:
25
+ image: vault
26
+ ports:
27
+ - "8200:8200"
28
+ env:
29
+ VAULT_DEV_ROOT_TOKEN_ID: vault_root_token
30
+ SKIP_SETCAP: true
31
+ db:
32
+ image: postgres:14.1-alpine
33
+ ports:
34
+ - "5432:5432"
35
+ env:
36
+ POSTGRES_USER: 'vault_root'
37
+ POSTGRES_PASSWORD: 'root_password'
38
+ POSTGRES_DB: 'app_db'
39
+
40
+ steps:
41
+ - name: Checkout
42
+ uses: actions/checkout@v1
43
+ - name: Cache dependencies
44
+ uses: actions/cache@v1
45
+ with:
46
+ path: vendor/bundle
47
+ key: ${{ runner.OS }}-ruby-${{ matrix.ruby }}
48
+ restore-keys: ${{ runner.OS }}-
49
+
50
+ - name: Set up Ruby
51
+ uses: actions/setup-ruby@v1
52
+ with:
53
+ ruby-version: ${{ matrix.ruby }}
54
+ - name: Set up Bundler
55
+ run: gem install bundler:2.1.4
56
+ - name: Set up Dependencies
57
+ run: bundle install --path vendor/bundle
58
+ - name: Set up Vault
59
+ run: sh local/vault/setup.sh
60
+
61
+ - name: Run specs
62
+ env:
63
+ COVERAGE: true
64
+ run: bundle exec rspec
65
+
66
+ - name: Upload coverage
67
+ env:
68
+ CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
69
+ run: bash <(curl -s https://codecov.io/bash)
70
+
71
+ release:
72
+ runs-on: ubuntu-latest
73
+ needs: build
74
+ if: github.event_name == 'release' && github.event.action == 'published'
75
+ steps:
76
+ - name: Checkout
77
+ uses: actions/checkout@v1
78
+
79
+ - name: Set up Ruby
80
+ uses: actions/setup-ruby@v1
81
+ with:
82
+ ruby-version: 2.7.x
83
+ - name: Set up Bundler
84
+ run: gem install bundler:2.1.4
85
+ - name: Set up credentials
86
+ run: |
87
+ mkdir -p $HOME/.gem
88
+ touch $HOME/.gem/credentials
89
+ chmod 0600 $HOME/.gem/credentials
90
+ printf -- "---\n:rubygems_api_key: ${{secrets.RUBYGEMS_AUTH_TOKEN}}\n" > $HOME/.gem/credentials
91
+
92
+ - name: Get version
93
+ run: echo "${GITHUB_REF/refs\/tags\//}" > release.tag
94
+ - name: Set version
95
+ run: sed -i "s/0.0.0/$(<release.tag)/g" $(find . -name "version.rb")
96
+
97
+ - name: Build gem
98
+ run: gem build *.gemspec
99
+ - name: Push gem
100
+ run: gem push *.gem
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.simplecov ADDED
@@ -0,0 +1,9 @@
1
+ if ENV["COVERAGE"]
2
+ require 'simplecov'
3
+ require 'codecov'
4
+ SimpleCov.start do
5
+ enable_coverage :branch
6
+ primary_coverage :branch
7
+ formatter SimpleCov::Formatter::Codecov
8
+ end
9
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0]
4
+ ### Fixes
5
+ - Fix CI release
6
+ - Add changelog
7
+
8
+ ## [0.1.0]
9
+ ### New features
10
+ - Retrieving static and dynamic secrets from vault
11
+ - Secrets caching
12
+ - Automatic secrets lease renewal
13
+
14
+ [Unreleased]: https://github.com/matic-insurance/settings_reader-vault_resolver/compare/0.1.1...HEAD
15
+ [0.1.0]: https://github.com/matic-insurance/settings_reader-vault_resolver/commits/0.1.1
16
+ [0.1.0]: https://github.com/matic-insurance/settings_reader-vault_resolver/commits/0.1.0
17
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 712680+volodymyr-mykhailyk@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in settings_reader-vault_resolver.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ settings_reader-vault_resolver (0.0.0)
5
+ concurrent-ruby (~> 1.1)
6
+ vault (~> 0.16)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ aws-eventstream (1.2.0)
13
+ aws-sigv4 (1.4.0)
14
+ aws-eventstream (~> 1, >= 1.0.2)
15
+ codecov (0.6.0)
16
+ simplecov (>= 0.15, < 0.22)
17
+ concurrent-ruby (1.1.9)
18
+ diff-lcs (1.5.0)
19
+ docile (1.4.0)
20
+ parallel (1.21.0)
21
+ parser (3.1.0.0)
22
+ ast (~> 2.4.1)
23
+ rainbow (3.0.0)
24
+ rake (13.0.6)
25
+ regexp_parser (2.2.0)
26
+ rexml (3.2.5)
27
+ rspec (3.10.0)
28
+ rspec-core (~> 3.10.0)
29
+ rspec-expectations (~> 3.10.0)
30
+ rspec-mocks (~> 3.10.0)
31
+ rspec-core (3.10.1)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-expectations (3.10.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-mocks (3.10.2)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-support (3.10.3)
40
+ rubocop (0.93.1)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.1.5)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8)
45
+ rexml
46
+ rubocop-ast (>= 0.6.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 2.0)
49
+ rubocop-ast (1.15.1)
50
+ parser (>= 3.0.1.1)
51
+ rubocop-rspec (1.32.0)
52
+ rubocop (>= 0.60.0)
53
+ ruby-progressbar (1.11.0)
54
+ simplecov (0.21.2)
55
+ docile (~> 1.1)
56
+ simplecov-html (~> 0.11)
57
+ simplecov_json_formatter (~> 0.1)
58
+ simplecov-html (0.12.3)
59
+ simplecov_json_formatter (0.1.3)
60
+ timecop (0.9.4)
61
+ unicode-display_width (1.8.0)
62
+ vault (0.16.0)
63
+ aws-sigv4
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (~> 2.0)
70
+ codecov (~> 0.4)
71
+ rake (~> 13.0)
72
+ rspec (~> 3.0)
73
+ rubocop (~> 0.66)
74
+ rubocop-rspec (~> 1.32.0)
75
+ settings_reader-vault_resolver!
76
+ simplecov (~> 0.16)
77
+ timecop (~> 0.9)
78
+
79
+ BUNDLED WITH
80
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Volodymyr Mykhailyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # SettingsReader::VaultResolver
2
+
3
+ ![Build Status](https://github.com/matic-insurance/settings_reader-vault_resolver/workflows/ci/badge.svg?branch=main)
4
+ [![Test Coverage](https://codecov.io/gh/matic-insurance/settings_reader-vault_resolver/branch/main/graph/badge.svg?token=dGVDB9judr)](https://codecov.io/gh/matic-insurance/settings_reader-vault_resolver)
5
+
6
+ Settings Reader plugin to resolve values using in Hashicorp Vault
7
+
8
+ This gem works as a plugin for [Settings Reader](https://github.com/matic-insurance/consul_application_settings)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'settings_reader'
16
+ gem 'settings_reader-vault_resolver'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Initialization
22
+
23
+ At the load of application when initializing `settings_reader`:
24
+ ```ruby
25
+ #Init vault
26
+ Vault.address = 'http://127.0.0.1:8200'
27
+ Vault.token = 'MY_SUPER_SECRET_TOKEN'
28
+
29
+ #Init Settings Reader
30
+ SettingsReader.configure do |config|
31
+ config.settings_providers = [
32
+ SettingsReader::Providers::Yaml,
33
+ ]
34
+
35
+ config.value_resolvers = [
36
+ SettingsReader::Resolvers::Vault,
37
+ SettingsReader::Resolvers::Env,
38
+ ]
39
+ end
40
+
41
+ #Load Settings
42
+ APP_SETTINGS = SettingsReader.load
43
+ ```
44
+
45
+ ### Usage
46
+ If one of the values provided will begin with `vault://` scheme -
47
+ `VaultResolver` gem will kick in and will try to resolve path in Vault
48
+
49
+ Assuming your settings has following structure:
50
+ ```yaml
51
+ app:
52
+ name: 'MyCoolApp'
53
+ hostname: 'http://localhost:3001'
54
+ secret: 'vault://secret/apps/my_cool_app#app_secret'
55
+ ```
56
+
57
+ When requesting `app/secret` from `SettingsReader` it will resolve in Vault as:
58
+
59
+ ```ruby
60
+ secret = APP_SETTINGS.get('app/secret')
61
+ # Gem will read `vault://secret/app#secret` from YAML
62
+ # Gem will resolve value in Vault using Vault.kv('secret').read('apps/my_cool_app')
63
+ # Gem will return `app_secret` attribute from the secret resolved above
64
+ ```
65
+
66
+ ## Development
67
+
68
+ 1. Run `bin/setup` to install dependencies
69
+ 2. Run `docker-compose up` to spin up dependencies (Vault)
70
+ 3. Run tests `rspec`
71
+ 4. Add new test
72
+ 5. Add new code
73
+ 6. Go to step 3
74
+ 7. Create PR
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matic-insurance/settings_reader-vault_resolver. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/matic-insurance/settings_reader-vault_resolver/blob/master/CODE_OF_CONDUCT.md).
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
84
+
85
+ ## Code of Conduct
86
+
87
+ Everyone interacting in the SettingsReader::VaultResolver project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/matic-insurance/settings_reader-vault_resolver/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "settings_reader/vault_resolver"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # dependencies needed for development environment
2
+ version: '3'
3
+ services:
4
+ vault:
5
+ image: vault
6
+ ports:
7
+ - "8200:8200"
8
+ environment:
9
+ VAULT_DEV_ROOT_TOKEN_ID: 'vault_root_token'
10
+ SKIP_SETCAP: true
11
+ db:
12
+ image: postgres:14.1-alpine
13
+ ports:
14
+ - "5432:5432"
15
+ environment:
16
+ POSTGRES_USER: 'vault_root'
17
+ POSTGRES_PASSWORD: 'root_password'
18
+ POSTGRES_DB: 'app_db'
19
+ init:
20
+ image: curlimages/curl
21
+ depends_on:
22
+ - vault
23
+ - db
24
+ volumes:
25
+ - './local/vault/setup.sh:/etc/vault/setup.sh'
26
+ environment:
27
+ VAULT_ADDR: 'http://vault:8200'
28
+ VAULT_TOKEN: 'vault_root_token'
29
+ SKIP_SETCAP: true
30
+ command: sh /etc/vault/setup.sh
@@ -0,0 +1,50 @@
1
+ require 'vault'
2
+
3
+ module SettingsReader
4
+ module Resolver
5
+ class Vault
6
+ IDENTIFIER = 'vault://'.freeze
7
+ DATABASE_MOUNT = 'database'.freeze
8
+
9
+ def resolvable?(value, _path)
10
+ return unless value.respond_to?(:start_with?)
11
+
12
+ value.start_with?(IDENTIFIER)
13
+ end
14
+
15
+ # Expect value in format `vault://mount/path/to/secret?attribute_name`
16
+ def resolve(value, _path)
17
+ address = SettingsReader::VaultResolver::Address.new(value)
18
+ entry = fetch_entry(address)
19
+ entry&.value_for(address.attribute)
20
+ end
21
+
22
+ #Resolve KV secret
23
+ def kv_secret(address)
24
+ ::Vault.kv(address.mount).read(address.path)
25
+ end
26
+
27
+ def database_secret(address)
28
+ ::Vault.logical.read(address.full_path)
29
+ rescue ::Vault::HTTPClientError => e
30
+ raise unless e.message.include?('* unknown role')
31
+
32
+ nil
33
+ end
34
+
35
+ private
36
+
37
+ def fetch_entry(address)
38
+ cache.fetch(address) do
39
+ if (secret = address.mount == DATABASE_MOUNT ? database_secret(address) : kv_secret(address))
40
+ SettingsReader::VaultResolver::Entry.new(address, secret)
41
+ end
42
+ end
43
+ end
44
+
45
+ def cache
46
+ SettingsReader::VaultResolver.cache
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,33 @@
1
+ module SettingsReader
2
+ module VaultResolver
3
+ class Address
4
+ def initialize(uri)
5
+ @uri = URI.parse(uri)
6
+ end
7
+
8
+ def mount
9
+ @uri.host
10
+ end
11
+
12
+ def path
13
+ @uri.path
14
+ end
15
+
16
+ def full_path
17
+ "#{mount}#{path}"
18
+ end
19
+
20
+ def attribute
21
+ @uri.fragment
22
+ end
23
+
24
+ def options
25
+ URI::decode_www_form(@uri.query || '').to_h
26
+ end
27
+
28
+ def to_s
29
+ @uri.to_s
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module SettingsReader
2
+ module VaultResolver
3
+ class Cache
4
+ def initialize
5
+ @secrets = {}
6
+ end
7
+
8
+ def retrieve(address)
9
+ return nil unless (entry = @secrets[cache_key(address)])
10
+ return clear(entry) if entry.expired?
11
+
12
+ entry
13
+ end
14
+
15
+ def save(entry)
16
+ @secrets[cache_key(entry.address)] = entry
17
+ end
18
+
19
+ def clear(entry)
20
+ @secrets.delete(cache_key(entry.address))
21
+ nil
22
+ end
23
+
24
+ def fetch(address, &block)
25
+ if (exiting_entry = retrieve(address))
26
+ exiting_entry
27
+ else
28
+ new_entry = block.call(address)
29
+ save(new_entry) if new_entry
30
+ new_entry
31
+ end
32
+ end
33
+
34
+ def entries(&block)
35
+ @secrets.each_value(&block)
36
+ end
37
+
38
+ private
39
+
40
+ def cache_key(address)
41
+ address.full_path
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ module SettingsReader
2
+ module VaultResolver
3
+ class Entry
4
+ attr_reader :address, :secret
5
+ MONTH = 30 * 60 * 60
6
+
7
+ def initialize(address, secret)
8
+ @address = address
9
+ @secret = secret
10
+ @lease_started = Time.now
11
+ end
12
+
13
+ def leased?
14
+ @secret.lease_id && lease_duration.positive?
15
+ end
16
+
17
+ def expired?
18
+ return false unless leased?
19
+
20
+ Time.now > @lease_started + lease_duration
21
+ end
22
+
23
+ def expires_in
24
+ return MONTH unless leased?
25
+
26
+ @lease_started + lease_duration - Time.now
27
+ end
28
+
29
+ def renew
30
+ return unless leased?
31
+
32
+ @secret = Vault.sys.renew(@secret.lease_id)
33
+ @lease_started = Time.now
34
+ true
35
+ end
36
+
37
+ def value_for(attribute)
38
+ secret.data[attribute.to_sym]
39
+ end
40
+
41
+ private
42
+
43
+ def lease_duration
44
+ @secret.lease_duration.to_i
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ module SettingsReader
2
+ module VaultResolver
3
+ class Refresher
4
+ DEFAULT_RENEW_DELAY = 120
5
+ REFRESH_INTERVAL = 60
6
+
7
+ def initialize(cache)
8
+ @cache = cache
9
+ end
10
+
11
+ def refresh
12
+ @cache.entries.each do |entry|
13
+ next unless entry.leased?
14
+ next unless entry.expires_in < DEFAULT_RENEW_DELAY
15
+
16
+ entry.renew
17
+ end
18
+ end
19
+
20
+ def self.refresh_task(cache)
21
+ refresher = self
22
+ Concurrent::TimerTask.new(execution_interval: refresher::REFRESH_INTERVAL) do
23
+ refresher.new(cache).refresh
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module SettingsReader
2
+ module VaultResolver
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'concurrent/timer_task.rb'
2
+ require "settings_reader/vault_resolver/version"
3
+ require "settings_reader/vault_resolver/address"
4
+ require "settings_reader/vault_resolver/entry"
5
+ require "settings_reader/vault_resolver/cache"
6
+ require "settings_reader/vault_resolver/refresher"
7
+ require "settings_reader/resolvers/vault"
8
+
9
+ module SettingsReader
10
+ module VaultResolver
11
+ class Error < StandardError; end
12
+
13
+ class << self
14
+ attr_accessor :cache, :refresher_timer_task
15
+ end
16
+
17
+ def self.setup_cache
18
+ self.cache ||= SettingsReader::VaultResolver::Cache.new
19
+ end
20
+
21
+ def self.setup_lease_refresher
22
+ self.refresher_timer_task ||= SettingsReader::VaultResolver::Refresher.refresh_task(self.cache)
23
+ self.refresher_timer_task.execute
24
+ end
25
+
26
+ def self.resolver
27
+ SettingsReader::VaultResolver::Vault
28
+ end
29
+
30
+ setup_cache
31
+ setup_lease_refresher
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'lib/settings_reader/vault_resolver/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'settings_reader-vault_resolver'
5
+ spec.version = SettingsReader::VaultResolver::VERSION
6
+ spec.authors = ['Volodymyr Mykhailyk']
7
+ spec.email = ['712680+volodymyr-mykhailyk@users.noreply.github.com']
8
+
9
+ spec.summary = 'Settings Reader plugin to resolve values using in Hashicorp Vault'
10
+ spec.description = 'This gem works as a resolver for `settings_reader` gem. \
11
+ Any value with matching format will be resolved using Vault \
12
+ with support of dynamic secrets and lease renewal.'
13
+ spec.homepage = 'https://github.com/matic-insurance/settings_reader-vault_resolver'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = spec.homepage + "/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|local)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'concurrent-ruby', '~> 1.1'
31
+ spec.add_dependency 'vault', '~> 0.16'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 2.0'
34
+ spec.add_development_dependency 'codecov', '~> 0.4'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.66'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.32.0'
39
+ spec.add_development_dependency 'simplecov', '~> 0.16'
40
+ spec.add_development_dependency 'timecop', '~> 0.9'
41
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings_reader-vault_resolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Volodymyr Mykhailyk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vault
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.16'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.66'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.66'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.32.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.32.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.16'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.16'
139
+ - !ruby/object:Gem::Dependency
140
+ name: timecop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.9'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.9'
153
+ description: |-
154
+ This gem works as a resolver for `settings_reader` gem. \
155
+ Any value with matching format will be resolved using Vault \
156
+ with support of dynamic secrets and lease renewal.
157
+ email:
158
+ - 712680+volodymyr-mykhailyk@users.noreply.github.com
159
+ executables: []
160
+ extensions: []
161
+ extra_rdoc_files: []
162
+ files:
163
+ - ".github/workflows/main.yml"
164
+ - ".gitignore"
165
+ - ".rspec"
166
+ - ".simplecov"
167
+ - CHANGELOG.md
168
+ - CODE_OF_CONDUCT.md
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/console
175
+ - bin/setup
176
+ - docker-compose.yml
177
+ - lib/settings_reader/resolvers/vault.rb
178
+ - lib/settings_reader/vault_resolver.rb
179
+ - lib/settings_reader/vault_resolver/address.rb
180
+ - lib/settings_reader/vault_resolver/cache.rb
181
+ - lib/settings_reader/vault_resolver/entry.rb
182
+ - lib/settings_reader/vault_resolver/refresher.rb
183
+ - lib/settings_reader/vault_resolver/version.rb
184
+ - settings_reader-vault_resolver.gemspec
185
+ homepage: https://github.com/matic-insurance/settings_reader-vault_resolver
186
+ licenses:
187
+ - MIT
188
+ metadata:
189
+ homepage_uri: https://github.com/matic-insurance/settings_reader-vault_resolver
190
+ source_code_uri: https://github.com/matic-insurance/settings_reader-vault_resolver
191
+ changelog_uri: https://github.com/matic-insurance/settings_reader-vault_resolver/blob/master/CHANGELOG.md
192
+ post_install_message:
193
+ rdoc_options: []
194
+ require_paths:
195
+ - lib
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: 2.3.0
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubygems_version: 3.1.6
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: Settings Reader plugin to resolve values using in Hashicorp Vault
211
+ test_files: []