config_server_agent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cba5de75917c2c406b472c604ce084f0c051625c5e24b547b57fe594ca03c2f1
4
+ data.tar.gz: 72c253ecbf9ef983bcfec697c57847eb7945d997a3a9ca151fd50672278d51ba
5
+ SHA512:
6
+ metadata.gz: 5b928f1c843ea560eea1568c672008e5efe9e2fa6b0d0168603664b4d884acc8dd36b1fab9ed5e5ab57627618ba95da64a5076b4f03789782a4efa7359ef4e2b
7
+ data.tar.gz: 0defd64e147843af7ad7d79ff38326c06d5d83d3f4d9e81f0cf439e88dc152379ea0f34d31865aa87f8edb435f509a5e3a920316236b0eb02cd9b389f02bbe60
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 0.1.0 - 2018-12-06
8
+ ### Added
9
+ - Initial commit
@@ -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 TODO: Write your email address. 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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in config_server_agent.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Aidan Samuel
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,83 @@
1
+ # ConfigServerAgent
2
+
3
+ This is a client for the Config Server project. It allows you to retrieve configuration for your Ruby project.
4
+
5
+ The first time `get_config` is called, `ConfigServerAgent` will request your config pack from the configured Config Server. This will be cached and returned on subsequent calls to `get_config`, without further requests being made to the Config Server.
6
+
7
+ Call `clear` to clear the cache. The next call to `get_config` will cause the Config Server to be contacted again.
8
+
9
+ It is thread safe.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'config_server_agent'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install config_server_agent
26
+
27
+ ## Usage
28
+
29
+ `ConfigServerAgent` requires six parameters to operate correctly. These parameters can either be explicitly provided to theq constructor, or they will be pulled from environment variables. An `ArgumentError` will be raised if the parameters are not available for either source. Parameters supplied to the constructor will take precedence over environment variables.
30
+
31
+ ```ruby
32
+ # Providing explicitly:
33
+ client = ConfigServerAgent.new(
34
+ auth0_client_id: '4F4l7leKKigMN8CziKpgjvXGKrihih1F',
35
+ auth0_client_secret: '3SrKmNAdKNzFBA3Lb6fgnTDyyFOQLt7hREWSlcU9QGc1SMKNKRL9DqmxyXQerS0E',
36
+ auth0_host: 'yourdomain.au.auth0.com',
37
+ config_server_audience: 'https://yourdomain.com',
38
+ config_server_api_key: 'Rd5N9hQlbkK7pYxMZGGsQAv...snip...C2OpRq9hEIGtp4Aw6OpEOsec5==',
39
+ config_server_host: 'https://your.config.server.com'
40
+ )
41
+
42
+ # Using environment variables:
43
+ client = ConfigServerAgent.new
44
+ # ConfigServerAgent will raise an exception if any of the following environment variables are not set:
45
+ # AUTH0_CLIENT_ID
46
+ # AUTH0_CLIENT_SECRET
47
+ # AUTH0_HOST
48
+ # CONFIG_SERVER_AUDIENCE
49
+ # CONFIG_SERVER_API_KEY
50
+ # CONFIG_SERVER_HOST
51
+
52
+ client.get_config
53
+ # => [{"id"=>"2481ea78-7699-4f59-8639-7992a6440c0f",
54
+ # "value"=>"Braintree",
55
+ # "name"=>"default_gateway",
56
+ # "area"=>"Payments",
57
+ # "created_at"=>"2018-12-06T00:39:12.236Z",
58
+ # "updated_at"=>"2018-12-06T00:39:12.236Z"},
59
+ # {"id"=>"02c74435-5309-4b7f-9a83-353cbf43e8cf",
60
+ # "value"=>false,
61
+ # "name"=>"payments_enabled",
62
+ # "area"=>"Payments",
63
+ # "created_at"=>"2018-12-06T00:39:12.240Z",
64
+ # "updated_at"=>"2018-12-06T00:39:12.240Z"},
65
+ # {"id"=>"17a0abc2-a0ac-4a2a-bfad-45a4e2afc00f",
66
+ # "value"=>"L68Wh+[foZCVW/y3h",
67
+ # "name"=>"account_password",
68
+ # "area"=>"Payments",
69
+ # "created_at"=>"2018-11-27T06:49:37.841Z",
70
+ # "updated_at"=>"2018-11-27T06:49:37.841Z"}]
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests. You can also run `rake console` for an interactive prompt that will allow you to experiment.
76
+
77
+ ## Contributing
78
+
79
+ To experiment with that code, run `rake console` for an interactive prompt.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ desc "Run an interactive Pry console"
13
+ task :console do
14
+ exec 'pry -I ./lib -r config_server_agent'
15
+ end
@@ -0,0 +1,53 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "config_server_agent/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "config_server_agent"
8
+ spec.version = ConfigServerAgent::VERSION
9
+ spec.authors = ["Aidan Samuel"]
10
+ spec.email = ["aidan.samuel@supporterhub.com"]
11
+
12
+ spec.summary = "A thread-safe caching client for Config Server"
13
+ spec.description = <<-EOM.gsub(/[\n\s]+/, ' ')
14
+ Use this client to retrieve your configuration from a Config Server.
15
+ The required parameters can either be supplied to the constructor, or
16
+ they will be pulled from environment variables. An ArgumentError will
17
+ be raised if the parameters are not available for either source.
18
+ Parameters supplied to the constructor will take precedence over environment
19
+ variables.
20
+ EOM
21
+ spec.homepage = "https://bitbucket.org/supporterhub/"
22
+ spec.license = "MIT"
23
+
24
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
25
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
26
+ if spec.respond_to?(:metadata)
27
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
28
+
29
+ spec.metadata["homepage_uri"] = spec.homepage
30
+ spec.metadata["source_code_uri"] = "https://bitbucket.org/supporterhub/config_server_agent"
31
+ spec.metadata["changelog_uri"] = "https://bitbucket.org/supporterhub/config_server_agent/srv/master/CHANGELOG.md"
32
+ else
33
+ raise "RubyGems 2.0 or newer is required to protect against " \
34
+ "public gem pushes."
35
+ end
36
+
37
+ # Specify which files should be added to the gem when it is released.
38
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
39
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
40
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
41
+ end
42
+ # spec.bindir = "exe"
43
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
44
+ spec.require_paths = ["lib"]
45
+
46
+ spec.required_ruby_version = '>= 2.3.0'
47
+
48
+ spec.add_development_dependency "bundler", "~> 1.17"
49
+ spec.add_development_dependency "rake", "~> 10.0"
50
+ spec.add_development_dependency "minitest", "~> 5.0"
51
+ spec.add_development_dependency "pry", "~> 0.10"
52
+ spec.add_development_dependency "webmock", "~> 2.0"
53
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "config_server_agent/version"
4
+ require "net/http"
5
+ require "json"
6
+
7
+ class ConfigServerAgent
8
+ class Error < StandardError; end
9
+
10
+ def initialize(
11
+ auth0_client_id: ENV['AUTH0_CLIENT_ID'],
12
+ auth0_client_secret: ENV['AUTH0_CLIENT_SECRET'],
13
+ auth0_host: ENV['AUTH0_HOST'],
14
+ config_server_audience: ENV['CONFIG_SERVER_AUDIENCE'],
15
+ config_server_api_key: ENV['CONFIG_SERVER_API_KEY'],
16
+ config_server_host: ENV['CONFIG_SERVER_HOST']
17
+ )
18
+ @auth0_client_id = auth0_client_id or raise ArgumentError, "Missing auth0_client_id parameter"
19
+ @auth0_client_secret = auth0_client_secret or raise ArgumentError, "Missing auth0_client_secret parameter"
20
+ @auth0_host = auth0_host or raise ArgumentError, "Missing auth0_host parameter"
21
+ @config_server_audience = config_server_audience or raise ArgumentError, "Missing config_server_audience parameter"
22
+ @config_server_api_key = config_server_api_key or raise ArgumentError, "Missing config_server_api_key parameter"
23
+ @config_server_host = config_server_host or raise ArgumentError, "Missing config_server_host parameter"
24
+ @config = nil
25
+ @mutex = Mutex.new
26
+ end
27
+
28
+ def get_config
29
+ return @config if @config
30
+ @mutex.synchronize do
31
+ @config ||= _get_config
32
+ end
33
+ end
34
+
35
+ def clear
36
+ @config = nil
37
+ end
38
+
39
+ private
40
+
41
+ def _get_config
42
+
43
+ token = get_token
44
+
45
+ url = URI "#{@config_server_host}/api/config_pack"
46
+ http = Net::HTTP.new url.host, url.port
47
+ http.use_ssl = url.scheme == 'https'
48
+
49
+ request = Net::HTTP::Post.new url
50
+ request['content-type'] = 'application/json'
51
+ request['authorization'] = "Bearer #{token}"
52
+ request['accept'] = 'application/json'
53
+ request.body = { api_key: @config_server_api_key }.to_json
54
+
55
+ response = http.request request
56
+ JSON.parse response.read_body
57
+ end
58
+
59
+ def get_token
60
+ url = URI "https://#{@auth0_host}/oauth/token"
61
+ http = Net::HTTP.new url.host, url.port
62
+ http.use_ssl = url.scheme == 'https'
63
+
64
+ request = Net::HTTP::Post.new url
65
+ request['content-type'] = 'application/json'
66
+ request.body = {
67
+ client_id: @auth0_client_id,
68
+ client_secret: @auth0_client_secret,
69
+ audience: @config_server_audience,
70
+ grant_type: 'client_credentials',
71
+ }.to_json
72
+
73
+ response = http.request request
74
+ JSON.parse(response.read_body)["access_token"] or raise Error, "No token from #{@auth0_host}"
75
+
76
+ rescue JSON::ParserError
77
+ raise Error, "Invalid response from #{@auth0_host}"
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,3 @@
1
+ class ConfigServerAgent
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_server_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Samuel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: " Use this client to retrieve your configuration from a Config Server.
84
+ The required parameters can either be supplied to the constructor, or they will
85
+ be pulled from environment variables. An ArgumentError will be raised if the parameters
86
+ are not available for either source. Parameters supplied to the constructor will
87
+ take precedence over environment variables. "
88
+ email:
89
+ - aidan.samuel@supporterhub.com
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - ".gitignore"
95
+ - ".travis.yml"
96
+ - CHANGELOG.md
97
+ - CODE_OF_CONDUCT.md
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - config_server_agent.gemspec
103
+ - lib/config_server_agent.rb
104
+ - lib/config_server_agent/version.rb
105
+ homepage: https://bitbucket.org/supporterhub/
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ homepage_uri: https://bitbucket.org/supporterhub/
111
+ source_code_uri: https://bitbucket.org/supporterhub/config_server_agent
112
+ changelog_uri: https://bitbucket.org/supporterhub/config_server_agent/srv/master/CHANGELOG.md
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 2.3.0
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.6
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: A thread-safe caching client for Config Server
133
+ test_files: []