liveness 0.2.2 → 0.3.2

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: 630199531d58103ad49748b74a839676e701ac9e85efb7f2880f9242b66ae30b
4
+ data.tar.gz: 11efa4ea8d069d8fcd1d132eeaad736b2f3755ee390ea147b40843b68795a202
5
5
  SHA512:
6
- metadata.gz: f2f68bd1ee8ea102b3c8517f839e0d750ee6d9dc32487d8c9578ff31635eb5f1ae1b7eea39e1f0825438e9ff572824ee41d7dd49e8956ad938e7aa37d16dbdc9
7
- data.tar.gz: f0083b3f15da4d97f12561f29af8628bf0b02ea53cd083d98783e5d5b29fb4bffdd4de90672ef733bf5c92a464914cae543a86021d0b6bbbef6dffbc439f6ee7
6
+ metadata.gz: 6e22c3008961610bb37e82ece2372c4b66899a0990d93164cdbbc16620e44958fe26571075ac1ade583391a7d13a3f04792efae5a710f3ba14a778e9c520be88
7
+ data.tar.gz: d23c983e988c45bf0a3e4b438c29e72b391c8af559b6f1d448479dbe91a9637c0c0c35de8e7a57eb157720e68f30461ab8f6e6ffd36a76d611d753337c98dba4
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.2)
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,66 @@
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
+ # @since 0.3.2
11
+ LOCAL_IPV4 = IPAddr.new('127.0.0.1')
12
+ LOCAL_IPV6 = IPAddr.new('::1')
13
+
14
+ # @param request [Rack::Request]
15
+ # @param config [Liveness::Config]
16
+ #
17
+ # @since 0.3.0
18
+ def initialize(request, config:)
19
+ @request = request
20
+ @config = config
21
+ end
22
+
23
+ # Is allowed to access
24
+ #
25
+ # @return [Boolean]
26
+ #
27
+ # @since 0.3.0
28
+ def allowed?
29
+ local? || (whitelist? && valid_token?)
30
+ end
31
+
32
+ # Is token valid
33
+ #
34
+ # @return [Boolean]
35
+ #
36
+ # @since 0.3.0
37
+ def valid_token?
38
+ return true if @config.token.nil?
39
+
40
+ @config.token == @request.params['token']
41
+ end
42
+
43
+ # Is from localhost
44
+ #
45
+ # @return [Boolean]
46
+ #
47
+ # @since 0.3.2
48
+ def local?
49
+ LOCAL_IPV4.include?(@request.ip) || LOCAL_IPV6.include?(@request.ip)
50
+ end
51
+
52
+ # Is ip in whitelist
53
+ #
54
+ # @return [Boolean]
55
+ #
56
+ # @sicne 0.3.0
57
+ def whitelist?
58
+ return true if @config.ip_whitelist.empty?
59
+
60
+ @config
61
+ .ip_whitelist
62
+ .map { |ip| IPAddr.new(ip) }
63
+ .reduce(true) { |curr, addr| curr & addr.include?(@request.ip) }
64
+ end
65
+ end
66
+ 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.dup,
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.dup, [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.2'
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.2
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.