liveness 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8c4374a066d22f71ed43d6cbef9d3e8fa84d91ee74f4dcd99420dc1f0f05232
4
- data.tar.gz: '0482fa040d357f8b4486576ababe08591dbc589a3e353bee9f0b4c9e332baf70'
3
+ metadata.gz: bc3f2c9583634053d7b1b7f26da909e37aeafe6048882bd06ff1aaee2c681683
4
+ data.tar.gz: 15e0923ce5223ef75c789e5891319bdc8671475e5f1c332aee022ef446b9dfdc
5
5
  SHA512:
6
- metadata.gz: f2f68bd1ee8ea102b3c8517f839e0d750ee6d9dc32487d8c9578ff31635eb5f1ae1b7eea39e1f0825438e9ff572824ee41d7dd49e8956ad938e7aa37d16dbdc9
7
- data.tar.gz: f0083b3f15da4d97f12561f29af8628bf0b02ea53cd083d98783e5d5b29fb4bffdd4de90672ef733bf5c92a464914cae543a86021d0b6bbbef6dffbc439f6ee7
6
+ metadata.gz: 15b7ebf2bb197dc4ba0a8ac5e35c666a7370a85b808e2891424ac94dc87d703cad76fe3c171e9f13e51f40cadc7970bca940bd29fce558d294a5581fb1f28034
7
+ data.tar.gz: ebd29d69b05be32070e172f532a1e301151c55b7eb9b391c66700680f0b350ceccf9a58bfe2f0f47c89df07b6a068a99259b36070bb334e4046cd4a80bee3ec6
data/Gemfile.lock CHANGED
@@ -1,59 +1,59 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- liveness (0.2.2)
4
+ liveness (0.3.0)
5
5
  rack (>= 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
- bundler-audit (0.9.0.1)
11
+ bundler-audit (0.9.1)
12
12
  bundler (>= 1.2.0, < 3)
13
13
  thor (~> 1.0)
14
14
  childprocess (4.1.0)
15
15
  diff-lcs (1.5.0)
16
16
  docile (1.4.0)
17
17
  iniparse (1.5.0)
18
- overcommit (0.58.0)
18
+ overcommit (0.59.1)
19
19
  childprocess (>= 0.6.3, < 5)
20
20
  iniparse (~> 1.4)
21
21
  rexml (~> 3.2)
22
- parallel (1.21.0)
23
- parser (3.1.0.0)
22
+ parallel (1.22.1)
23
+ parser (3.1.2.0)
24
24
  ast (~> 2.4.1)
25
- rack (2.2.3)
25
+ rack (2.2.3.1)
26
26
  rainbow (3.1.1)
27
27
  rake (13.0.6)
28
- regexp_parser (2.2.0)
28
+ regexp_parser (2.4.0)
29
29
  rexml (3.2.5)
30
- rspec (3.10.0)
31
- rspec-core (~> 3.10.0)
32
- rspec-expectations (~> 3.10.0)
33
- rspec-mocks (~> 3.10.0)
34
- rspec-core (3.10.1)
35
- rspec-support (~> 3.10.0)
36
- rspec-expectations (3.10.1)
30
+ rspec (3.11.0)
31
+ rspec-core (~> 3.11.0)
32
+ rspec-expectations (~> 3.11.0)
33
+ rspec-mocks (~> 3.11.0)
34
+ rspec-core (3.11.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-expectations (3.11.0)
37
37
  diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.10.0)
39
- rspec-mocks (3.10.2)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-mocks (3.11.1)
40
40
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.10.0)
42
- rspec-support (3.10.3)
43
- rubocop (1.24.1)
41
+ rspec-support (~> 3.11.0)
42
+ rspec-support (3.11.0)
43
+ rubocop (1.30.0)
44
44
  parallel (~> 1.10)
45
- parser (>= 3.0.0.0)
45
+ parser (>= 3.1.0.0)
46
46
  rainbow (>= 2.2.2, < 4.0)
47
47
  regexp_parser (>= 1.8, < 3.0)
48
- rexml
49
- rubocop-ast (>= 1.15.1, < 2.0)
48
+ rexml (>= 3.2.5, < 4.0)
49
+ rubocop-ast (>= 1.18.0, < 2.0)
50
50
  ruby-progressbar (~> 1.7)
51
51
  unicode-display_width (>= 1.4.0, < 3.0)
52
- rubocop-ast (1.15.1)
53
- parser (>= 3.0.1.1)
52
+ rubocop-ast (1.18.0)
53
+ parser (>= 3.1.1.0)
54
54
  rubocop-rake (0.6.0)
55
55
  rubocop (~> 1.0)
56
- rubocop-rspec (2.7.0)
56
+ rubocop-rspec (2.11.1)
57
57
  rubocop (~> 1.19)
58
58
  ruby-progressbar (1.11.0)
59
59
  simplecov (0.21.2)
@@ -61,12 +61,13 @@ GEM
61
61
  simplecov-html (~> 0.11)
62
62
  simplecov_json_formatter (~> 0.1)
63
63
  simplecov-html (0.12.3)
64
- simplecov_json_formatter (0.1.3)
64
+ simplecov_json_formatter (0.1.4)
65
65
  thor (1.2.1)
66
66
  unicode-display_width (2.1.0)
67
67
 
68
68
  PLATFORMS
69
69
  x86_64-darwin-20
70
+ x86_64-darwin-21
70
71
  x86_64-linux
71
72
 
72
73
  DEPENDENCIES
@@ -81,4 +82,4 @@ DEPENDENCIES
81
82
  simplecov
82
83
 
83
84
  BUNDLED WITH
84
- 2.2.32
85
+ 2.3.14
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # Liveness
1
+ Liveness [![Ruby](https://github.com/elct9620/liveness/actions/workflows/main.yml/badge.svg)](https://github.com/elct9620/liveness/actions/workflows/main.yml)
2
+ ===
2
3
 
3
4
  The Rack middleware to provide health check endpoints.
4
5
 
@@ -28,6 +29,23 @@ Liveness.config do |c|
28
29
  c.add :postgres, name: :read_replica, timeout: 10
29
30
  end
30
31
  ```
32
+ ### Access Protected
33
+
34
+ ```ruby
35
+ Liveness.config do |c|
36
+ c.token = 'unm@tnh8ugq-WAR-myr'
37
+ end
38
+ ```
39
+
40
+ To access the status endpoint should add `?token=unm@tnh8ugq-WAR-myr` params to access it.
41
+
42
+ ```ruby
43
+ Liveness.config do |c|
44
+ c.ip_whitelist = ['192.168.0.0/24']
45
+ end
46
+ ```
47
+
48
+ To access the status endpoint should called from `127.0.0.1` or `::1` or under `192.168.0.0/24`.
31
49
 
32
50
  ### Customize Connector
33
51
 
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+
5
+ module Liveness
6
+ # Access Control
7
+ #
8
+ # @since 0.3.0
9
+ class Access
10
+ # @param request [Rack::Request]
11
+ # @param config [Liveness::Config]
12
+ #
13
+ # @since 0.3.0
14
+ def initialize(request, config:)
15
+ @request = request
16
+ @config = config
17
+ end
18
+
19
+ # Is allowed to access
20
+ #
21
+ # @return [Boolean]
22
+ #
23
+ # @since 0.3.0
24
+ def allowed?
25
+ whitelist? && valid_token?
26
+ end
27
+
28
+ # Is token valid
29
+ #
30
+ # @return [Boolean]
31
+ #
32
+ # @since 0.3.0
33
+ def valid_token?
34
+ return true if @config.token.nil?
35
+
36
+ @config.token == @request.params['token']
37
+ end
38
+
39
+ # Is ip in whitelist
40
+ #
41
+ # @return [Boolean]
42
+ #
43
+ # @sicne 0.3.0
44
+ def whitelist?
45
+ return true if @config.ip_whitelist.empty?
46
+ return true if IPAddr.new('127.0.0.1').include?(@request.ip)
47
+ return true if IPAddr.new('::1').include?(@request.ip)
48
+
49
+ @config
50
+ .ip_whitelist
51
+ .map { |ip| IPAddr.new(ip) }
52
+ .reduce(true) { |curr, addr| curr & addr.include?(@request.ip) }
53
+ end
54
+ end
55
+ end
@@ -8,12 +8,16 @@ module Liveness
8
8
  # @since 0.1.0
9
9
  attr_reader :dependencies
10
10
 
11
+ # @since 0.3.0
12
+ attr_accessor :token, :ip_whitelist
13
+
11
14
  # @return [Liveness::Config]
12
15
  #
13
16
  # @since 0.1.0
14
17
  def initialize(container: Liveness.container, &block)
15
18
  @container = container
16
19
  @dependencies = []
20
+ @ip_whitelist = []
17
21
 
18
22
  instance_exec(self, &block) if defined?(yield)
19
23
  end
@@ -12,11 +12,18 @@ module Liveness
12
12
  end
13
13
  end
14
14
 
15
+ # @since 0.3.0
16
+ HEADERS = { 'Content-Type' => 'application/json' }.freeze
17
+
18
+ # @since 0.3.0
19
+ FORBIDDEN_MESSAGE = { message: 'access denied' }.freeze
20
+
15
21
  # @return [Liveness::Status]
16
22
  #
17
23
  # @since 0.1.0
18
24
  def initialize(env, config: Liveness.config)
19
25
  @env = env
26
+ @request = Rack::Request.new(env)
20
27
  @config = config
21
28
  end
22
29
 
@@ -47,11 +54,23 @@ module Liveness
47
54
  #
48
55
  # @since 0.1.0
49
56
  def response
57
+ access = Access.new(@request, config: @config)
58
+ return forbidden unless access.allowed?
59
+
50
60
  [
51
61
  live? ? 200 : 503,
52
- { 'Content-Type' => 'application/json' },
62
+ HEADERS,
53
63
  [metrics.to_json]
54
64
  ]
55
65
  end
66
+
67
+ # @return [Rack::Response]
68
+ #
69
+ # @since 0.3.0
70
+ def forbidden
71
+ [
72
+ 403, HEADERS, [FORBIDDEN_MESSAGE.to_json]
73
+ ]
74
+ end
56
75
  end
57
76
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Liveness
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/liveness.rb CHANGED
@@ -6,6 +6,7 @@ require_relative 'liveness/version'
6
6
  require_relative 'liveness/dependency'
7
7
  require_relative 'liveness/container'
8
8
  require_relative 'liveness/config'
9
+ require_relative 'liveness/access'
9
10
  require_relative 'liveness/status'
10
11
 
11
12
  # The Rack middleware to provide health check endpoints.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liveness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 蒼時弦也
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-08 00:00:00.000000000 Z
11
+ date: 2022-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -42,6 +42,7 @@ files:
42
42
  - bin/console
43
43
  - bin/setup
44
44
  - lib/liveness.rb
45
+ - lib/liveness/access.rb
45
46
  - lib/liveness/config.rb
46
47
  - lib/liveness/container.rb
47
48
  - lib/liveness/dependencies.rb
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.2.26
77
+ rubygems_version: 3.1.6
77
78
  signing_key:
78
79
  specification_version: 4
79
80
  summary: The Rack middleware to provide health check endpoints.