rx-healthcheck 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -2
- data/lib/rx/middleware.rb +20 -7
- data/lib/rx/util/health_check_authorization.rb +25 -0
- data/lib/rx/version.rb +1 -1
- data/lib/rx.rb +1 -0
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83bc5957f3ce3f426b416b53e7d2e24782da340da998a030cd5f15c04d6b9dde
|
4
|
+
data.tar.gz: 5d6d75b3be95189399d743c234d0303d17183f43482a51ef20a1bed15d59e482
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 182ab4e67e807f373b4148f492e4cfc62d1f7af7176d187edc86a2282fd65813436b1fc65d71313c2bcebc6a559c2d28fe9140ffe4fb277922e2a4a610805dd1
|
7
|
+
data.tar.gz: e1e7bbeb1240b5994433b8e27775cb5ca5a9dab74fd3159b69ef1c5d93a76468a8c74068f65776b052b0ca1b219d1b4caf1bd50b1982f844f7b726101b0ed9e0
|
data/README.md
CHANGED
@@ -59,14 +59,30 @@ deep_secondary: []
|
|
59
59
|
|
60
60
|
Each collection must contain 0 or more `Rx::Check` objects. Those checks will be performed when the health check is queried. Deep checks will always also run the readiness checks.
|
61
61
|
|
62
|
+
### Deep end-point authorization
|
63
|
+
|
64
|
+
It is considered as a good practice to protect the deep checks with a GUID to mitigate DDOS attacks. Hence you have 2 options to enable this. One is to use the `default_authorization` by passing the token to the authorization inside options hash, which you can use in a request with the authorization_token in the header of it. The other option would be to pass a lambda with an env argument (this gives you access to hash of request data) and have your own `custom_authorization`:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
options: {
|
68
|
+
#default
|
69
|
+
authorization: <token>
|
70
|
+
|
71
|
+
#custom
|
72
|
+
authorization: -> (env) {
|
73
|
+
#your code goes here
|
74
|
+
}
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
62
78
|
## Contributing
|
63
79
|
|
64
80
|
Bug reports and pull requests are welcome on GitHub at https://github.com/zachpendleton/rx.
|
65
81
|
|
66
82
|
Some tips for developing the gem locally:
|
67
83
|
|
68
|
-
|
69
|
-
|
84
|
+
- Tests can be run by calling `rake`
|
85
|
+
- You can point your Rails app to a local gem by adding a `path` option to your Gemfile, a la `gem "rx", path: "path/to/rx" (though you _will_ need to restart Rails whenever you change the gem).
|
70
86
|
|
71
87
|
## License
|
72
88
|
|
data/lib/rx/middleware.rb
CHANGED
@@ -3,7 +3,8 @@ require "json"
|
|
3
3
|
module Rx
|
4
4
|
class Middleware
|
5
5
|
DEFAULT_OPTIONS = {
|
6
|
-
cache: true
|
6
|
+
cache: true,
|
7
|
+
authorization: nil
|
7
8
|
}.freeze
|
8
9
|
|
9
10
|
def initialize(app,
|
@@ -34,12 +35,16 @@ module Rx
|
|
34
35
|
when "/readiness"
|
35
36
|
readiness_response(check_to_component(readiness_checks))
|
36
37
|
when "/deep"
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
if !Rx::Util::HealthCheckAuthorization.new(env, @options[:authorization]).ok?
|
39
|
+
deep_response_authorization_failed
|
40
|
+
else
|
41
|
+
@cache.cache("deep") do
|
42
|
+
readiness = check_to_component(readiness_checks)
|
43
|
+
critical = check_to_component(deep_critical_checks)
|
44
|
+
secondary = check_to_component(deep_secondary_checks)
|
45
|
+
|
46
|
+
deep_response(readiness, critical, secondary)
|
47
|
+
end
|
43
48
|
end
|
44
49
|
end
|
45
50
|
end
|
@@ -79,6 +84,14 @@ module Rx
|
|
79
84
|
]
|
80
85
|
end
|
81
86
|
|
87
|
+
def deep_response_authorization_failed
|
88
|
+
[
|
89
|
+
403,
|
90
|
+
{"content-type" => "application/json"},
|
91
|
+
[JSON.dump({ message: "authorization failed" })]
|
92
|
+
]
|
93
|
+
end
|
94
|
+
|
82
95
|
def deep_response(readiness, critical, secondary)
|
83
96
|
status = (readiness.map { |x| x[:status] == 200 } + critical.map { |x| x[:status] == 200 }).all? ? 200 : 503
|
84
97
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rx
|
2
|
+
module Util
|
3
|
+
class HealthCheckAuthorization
|
4
|
+
HTTP_HEADER = "HTTP_AUTHORIZATION"
|
5
|
+
|
6
|
+
def initialize(env, authorization)
|
7
|
+
@authorization = authorization
|
8
|
+
@env = env
|
9
|
+
end
|
10
|
+
|
11
|
+
def ok?
|
12
|
+
case @authorization
|
13
|
+
when NilClass
|
14
|
+
true
|
15
|
+
when Proc
|
16
|
+
@authorization.call(@env)
|
17
|
+
when String
|
18
|
+
@authorization == @env[HTTP_HEADER]
|
19
|
+
else
|
20
|
+
raise StandardError.new("Authorization is not configured properly")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/rx/version.rb
CHANGED
data/lib/rx.rb
CHANGED
@@ -12,6 +12,7 @@ require_relative "rx/check/result"
|
|
12
12
|
require_relative "rx/concurrent/future"
|
13
13
|
require_relative "rx/concurrent/thread_pool"
|
14
14
|
require_relative "rx/util/heap"
|
15
|
+
require_relative "rx/util/health_check_authorization"
|
15
16
|
|
16
17
|
module Rx
|
17
18
|
class Error < StandardError; end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rx-healthcheck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Pendleton
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email:
|
15
15
|
- zachpendleton@gmail.com
|
16
16
|
executables: []
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/rx/concurrent/future.rb
|
37
37
|
- lib/rx/concurrent/thread_pool.rb
|
38
38
|
- lib/rx/middleware.rb
|
39
|
+
- lib/rx/util/health_check_authorization.rb
|
39
40
|
- lib/rx/util/heap.rb
|
40
41
|
- lib/rx/version.rb
|
41
42
|
- rx.gemspec
|
@@ -45,7 +46,7 @@ licenses:
|
|
45
46
|
metadata:
|
46
47
|
homepage_uri: https://github.com/zachpendleton/rx
|
47
48
|
source_code_uri: https://github.com/zachpendleton/rx
|
48
|
-
post_install_message:
|
49
|
+
post_install_message:
|
49
50
|
rdoc_options: []
|
50
51
|
require_paths:
|
51
52
|
- lib
|
@@ -60,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
61
|
- !ruby/object:Gem::Version
|
61
62
|
version: '0'
|
62
63
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
64
|
-
signing_key:
|
64
|
+
rubygems_version: 3.2.22
|
65
|
+
signing_key:
|
65
66
|
specification_version: 4
|
66
67
|
summary: Standard health checks for Rails and Rack applications
|
67
68
|
test_files: []
|