config_server_agent 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc879021defba75dd0f80bdf720a6aed93bfc7087c1dde865b298acbe5ffc0c0
4
- data.tar.gz: 21e44879624e3bb30cc30d1c8f7fac444893c50d353f3a51c1ad32e93d4190d0
3
+ metadata.gz: 2761ff07482cc6eef122709934af77aad62c83e2a4dba6ffc555b0f5ad0f030e
4
+ data.tar.gz: 65fb0a5494fbb54936043bc81195cde6c349632b20f0984ed43a89442279a8c6
5
5
  SHA512:
6
- metadata.gz: a7582e8caa5d37f8f437bd931eb39748bc5f0841f2132e69f1840b1fc4d541ac27e467e5be19e6ba4db71c44ae7fb0118e415420004ce022e80a944dae6cc751
7
- data.tar.gz: 6f2e0ea7c91554480778d2cf59c6c776b17e53530f89f82d333353452b739786e841adf54aa5763f098e03f86cf652028e149e710e4411c1eaf1b8f03965562d
6
+ metadata.gz: 78c89dda50b1a1f3f9ce452097357625cf3a5f8ee2c309195789b5c27efffb30fdf8d500bdc245d2532d4bd41f9dfae3c768b1ceb156fdcad79fc2e9e8cd201c
7
+ data.tar.gz: a01f1794f088ad44359d042139ea2476ab56415c50b78c679ba6f120c58f5f4bbef2977e9ce6ce49857b3ad6d34cb3e9ecdfb3d53ad111a096995c19ce20971e
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 0.2.0 - 2019-02-11
8
+ ### Changed
9
+ - Changed the the get_config method from POST to GET
10
+ - Implemented the notify_missing method
11
+ - Added auth token caching with TTL
12
+
7
13
  ## 0.1.1 - 2019-02-06
8
14
  ### Added
9
15
  - Changed the default User-Agent string from 'Ruby' to 'ConfigServerAgent/0.1.x'
@@ -1,3 +1,3 @@
1
1
  class ConfigServerAgent
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -15,7 +15,8 @@ class ConfigServerAgent
15
15
  config_server_api_key: ENV['CONFIG_SERVER_API_KEY'],
16
16
  config_server_host: ENV['CONFIG_SERVER_HOST'],
17
17
  user_agent: "ConfigServerAgent/#{ConfigServerAgent::VERSION}",
18
- user_agent_comment: nil
18
+ user_agent_comment: nil,
19
+ token_ttl: 60 * 60 * 24
19
20
  )
20
21
  @auth0_client_id = auth0_client_id or raise ArgumentError, "Missing auth0_client_id parameter"
21
22
  @auth0_client_secret = auth0_client_secret or raise ArgumentError, "Missing auth0_client_secret parameter"
@@ -23,16 +24,21 @@ class ConfigServerAgent
23
24
  @config_server_audience = config_server_audience or raise ArgumentError, "Missing config_server_audience parameter"
24
25
  @config_server_api_key = config_server_api_key or raise ArgumentError, "Missing config_server_api_key parameter"
25
26
  @config_server_host = config_server_host or raise ArgumentError, "Missing config_server_host parameter"
26
- @config = nil
27
- @mutex = Mutex.new
27
+
28
+ @config = nil
29
+ @mutex = Mutex.new
28
30
  @user_agent = user_agent
29
31
  @user_agent += " (#{user_agent_comment})" if user_agent_comment
32
+
33
+ @token_birthday = nil
34
+ @token = nil
35
+ @token_ttl = token_ttl
30
36
  end
31
37
 
32
38
  def get_config
33
39
  return @config if @config
34
40
  @mutex.synchronize do
35
- @config ||= _get_config
41
+ @config ||= get_request 'config_pack'
36
42
  end
37
43
  end
38
44
 
@@ -40,44 +46,66 @@ class ConfigServerAgent
40
46
  @config = nil
41
47
  end
42
48
 
49
+ def notify_missing(area, missing)
50
+ post_request 'notify_missing', area: area, name: missing
51
+ end
52
+
43
53
  private
44
54
 
45
- def _get_config
55
+ def post_request(endpoint, data={})
56
+ url = URI "#{@config_server_host}/api/#{endpoint}"
57
+ http = Net::HTTP.new url.host, url.port
58
+ http.use_ssl = url.scheme == 'https'
59
+
60
+ request = Net::HTTP::Post.new url
61
+ configure_request request
62
+ request.body = data.to_json
46
63
 
47
- token = get_token
64
+ JSON.parse http.request(request).read_body
65
+ end
48
66
 
49
- url = URI "#{@config_server_host}/api/config_pack"
50
- http = Net::HTTP.new url.host, url.port
67
+ def get_request(endpoint, data={})
68
+ url = URI "#{@config_server_host}/api/#{endpoint}"
69
+ url.query = URI.encode_www_form data
70
+ http = Net::HTTP.new url.host, url.port
51
71
  http.use_ssl = url.scheme == 'https'
52
72
 
53
- request = Net::HTTP::Post.new url
73
+ request = Net::HTTP::Get.new url
74
+ configure_request request
75
+
76
+ JSON.parse http.request(request).read_body
77
+ end
78
+
79
+ def configure_request(request)
54
80
  request['user-agent'] = @user_agent
81
+ request['authorization'] = "Bearer #{get_token}"
55
82
  request['content-type'] = 'application/json'
56
- request['authorization'] = "Bearer #{token}"
57
83
  request['accept'] = 'application/json'
58
- request.body = { api_key: @config_server_api_key }.to_json
59
-
60
- response = http.request request
61
- JSON.parse response.read_body
84
+ request['api-key'] = @config_server_api_key
62
85
  end
63
86
 
64
87
  def get_token
65
- url = URI "https://#{@auth0_host}/oauth/token"
66
- http = Net::HTTP.new url.host, url.port
67
- http.use_ssl = url.scheme == 'https'
88
+ if @token.nil? or @token_timestamp.nil? or @token_timestamp <= Time.now.to_i
89
+ url = URI "https://#{@auth0_host}/oauth/token"
90
+ http = Net::HTTP.new url.host, url.port
91
+ http.use_ssl = url.scheme == 'https'
92
+
93
+ request = Net::HTTP::Post.new url
94
+ request['user-agent'] = @user_agent
95
+ request['content-type'] = 'application/json'
96
+ request.body = {
97
+ client_id: @auth0_client_id,
98
+ client_secret: @auth0_client_secret,
99
+ audience: @config_server_audience,
100
+ grant_type: 'client_credentials',
101
+ }.to_json
102
+
103
+ response = http.request request
104
+ @token = JSON.parse(response.read_body)["access_token"] or raise Error, "No token from #{@auth0_host}"
105
+ @token_timestamp = Time.now.to_i + @token_ttl
106
+ end
68
107
 
69
- request = Net::HTTP::Post.new url
70
- request['user-agent'] = @user_agent
71
- request['content-type'] = 'application/json'
72
- request.body = {
73
- client_id: @auth0_client_id,
74
- client_secret: @auth0_client_secret,
75
- audience: @config_server_audience,
76
- grant_type: 'client_credentials',
77
- }.to_json
78
-
79
- response = http.request request
80
- JSON.parse(response.read_body)["access_token"] or raise Error, "No token from #{@auth0_host}"
108
+ @token
81
109
 
82
110
  rescue JSON::ParserError
83
111
  raise Error, "Invalid response from #{@auth0_host}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_server_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aidan Samuel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-06 00:00:00.000000000 Z
11
+ date: 2019-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler