console1984 0.1.1 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +130 -70
- data/lib/console1984/commands.rb +2 -0
- data/lib/console1984/config.rb +49 -0
- data/lib/console1984/engine.rb +5 -8
- data/lib/console1984/errors.rb +1 -0
- data/lib/console1984/freezeable.rb +54 -0
- data/lib/console1984/messages.rb +1 -2
- data/lib/console1984/protected_auditable_tables.rb +24 -20
- data/lib/console1984/protected_context.rb +7 -2
- data/lib/console1984/protected_object.rb +15 -0
- data/lib/console1984/protected_tcp_socket.rb +5 -0
- data/lib/console1984/sessions_logger/database.rb +5 -2
- data/lib/console1984/supervisor/accesses/protected.rb +2 -0
- data/lib/console1984/supervisor/accesses/unprotected.rb +2 -0
- data/lib/console1984/supervisor/accesses.rb +1 -1
- data/lib/console1984/supervisor/executor.rb +28 -4
- data/lib/console1984/supervisor/input_output.rb +29 -9
- data/lib/console1984/supervisor/protector.rb +55 -0
- data/lib/console1984/supervisor.rb +35 -22
- data/lib/console1984/username/env_resolver.rb +4 -0
- data/lib/console1984/version.rb +1 -1
- data/lib/console1984.rb +10 -54
- metadata +20 -3
- data/lib/console1984/env_variable_username.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44d77add0193cc1bb27955cb70d0d3e58153a17524e6d3b93a15179700322c58
|
4
|
+
data.tar.gz: b36e25eb75d6b1d113ae3653118115da747122e10af23d38ea00ebd72f3427cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07d029ed6dcd845cbc30c035ccdb2a9879983f1660c028530dfc8873673d212f9230ffc9ad8870b1806a981317dad06d48c95b53cdebe728a37bc585bd182e29
|
7
|
+
data.tar.gz: c9dfd6cd41b27c000b0f37a22ee9b1d4fbbdd32081b22735636d90c62175c2cb8f55b3ef94f90c9ac706a72838b67e8a8130e813943fe908f3860bf631365c8f
|
data/README.md
CHANGED
@@ -1,95 +1,155 @@
|
|
1
|
+
![example workflow](https://github.com/basecamp/console1984/actions/workflows/build.yml/badge.svg)
|
2
|
+
|
1
3
|
# Console1984
|
2
4
|
|
3
|
-
A Rails
|
5
|
+
A Rails console extension that protects sensitive accesses and makes them auditable.
|
4
6
|
|
5
7
|
> “If you want to keep a secret, you must also hide it from yourself.”
|
6
|
-
>
|
8
|
+
>
|
7
9
|
> ― George Orwell, 1984
|
8
10
|
|
9
|
-
|
11
|
+
If you are looking for the auditing tool, check [`audits1984`](https://github.com/basecamp/audits1984).
|
12
|
+
|
13
|
+
![Terminal screenshot showing console1984 asking for a reason for the session](docs/images/console-session-reason.png)
|
10
14
|
|
11
|
-
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add it to your `Gemfile`:
|
12
18
|
|
13
19
|
```ruby
|
14
20
|
gem 'console1984'
|
15
21
|
```
|
16
22
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
The console will ask for a reason for the console session, identifying the user via the environment
|
24
|
-
variable `CONSOLE_USER`.
|
25
|
-
|
26
|
-
After that, every command the user types will be captured and logged. `console1984` uses
|
27
|
-
[`rails-structured-logggin`](https://github.com/basecamp/rails-structured-logging) to form
|
28
|
-
a JSON entry that looks like this:
|
29
|
-
|
30
|
-
```json
|
31
|
-
{
|
32
|
-
"@timestamp": "2020-05-15T15:05:45.845642+02:00",
|
33
|
-
"ecs": {
|
34
|
-
"version": "1.2.0"
|
35
|
-
},
|
36
|
-
"event": {
|
37
|
-
"action": "console.audit_trail",
|
38
|
-
"duration": {
|
39
|
-
"ms": 0.01
|
40
|
-
}
|
41
|
-
},
|
42
|
-
"console": {
|
43
|
-
"user": "Jorge",
|
44
|
-
"reason": "fix something",
|
45
|
-
"commands": "Account.first\n"
|
46
|
-
},
|
47
|
-
"rails": {
|
48
|
-
"application": "haystack",
|
49
|
-
"env": "beta"
|
50
|
-
},
|
51
|
-
"ruby": {
|
52
|
-
"allocations": {
|
53
|
-
"count": 0
|
54
|
-
}
|
55
|
-
},
|
56
|
-
"process": {
|
57
|
-
"pid": 8539,
|
58
|
-
"name": "rails_console",
|
59
|
-
"working_directory": "/Users/jorge/Work/basecamp/haystack"
|
60
|
-
},
|
61
|
-
"performance": {
|
62
|
-
"time": {
|
63
|
-
"cpu": {
|
64
|
-
"ms": 0.01
|
65
|
-
},
|
66
|
-
"idle": {
|
67
|
-
"ms": 0.0
|
68
|
-
}
|
69
|
-
}
|
70
|
-
},
|
71
|
-
"original": " Account Load (1.0ms) SELECT `accounts`.* FROM `accounts` ORDER BY `accounts`.`id` ASC LIMIT 1\n"
|
72
|
-
}
|
23
|
+
Create tables to store console activity in the database:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
rails console1984:install:migrations
|
27
|
+
rails db:migrate
|
73
28
|
```
|
74
29
|
|
75
|
-
|
30
|
+
By default, console1984 is only enabled in `production`. You can configure the target environments in your `application.rb`:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
config.console1984.protected_environments = %i[ production staging ]
|
34
|
+
```
|
35
|
+
|
36
|
+
## How it works
|
37
|
+
|
38
|
+
### Session activity logging
|
39
|
+
|
40
|
+
When starting a console session, it will ask for a reason. Internally, it will use this reason to document the console session and record all the commands executed in it.
|
41
|
+
|
42
|
+
```
|
43
|
+
$ rails c
|
44
|
+
|
45
|
+
You have access to production data here. That's a big deal. As part of our promise to keep customer data safe and private, we audit the commands you type here. Let's get started!
|
46
|
+
|
76
47
|
|
77
|
-
### Protected environments
|
78
48
|
|
79
|
-
|
49
|
+
Commands:
|
80
50
|
|
81
|
-
|
82
|
-
|
51
|
+
* decrypt!: enter unprotected mode with access to encrypted information
|
52
|
+
|
53
|
+
Unnamed, why are you using this console today?
|
54
|
+
|
55
|
+
> ...
|
56
|
+
```
|
57
|
+
|
58
|
+
### Auditing sessions
|
59
|
+
|
60
|
+
Check out [`audits1984`](https://github.com/basecamp/audits1984), a companion auditing tool prepared to work with `console1984` database session trails.
|
61
|
+
|
62
|
+
### Access to encrypted data
|
63
|
+
|
64
|
+
By default, `console1984` won't decrypt data encrypted with [Active Record encryption](https://edgeguides.rubyonrails.org/active_record_encryption.html). Users will just see the ciphertexts.
|
65
|
+
|
66
|
+
To decrypt data, enter the command `decrypt!`. It will ask for a justification, and these accesses will be flagged internally as sensitive.
|
83
67
|
|
84
68
|
```ruby
|
85
|
-
|
69
|
+
irb(main)> Topic.last.name
|
70
|
+
Topic Load (1.4ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
|
71
|
+
=> "{\"p\":\"iu6+LfnNlurC6sL++JyOIDvedjNSz/AvnZQ=\",\"h\":{\"iv\":\"BYa86+JNM/LdkC18\",\"at\":\"r4sQNoSyIlAjJdZEKHVMow==\",\"k\":{\"p\":\"7L1l/5UiYsFQqqo4jfMZtLwp90KqcrIgS7HqgteVjuM=\",\"h\":{\"iv\":\"ItwRYxZAerKIoSZ8\",\"at\":\"ZUSNVfvtm4wAYWLBKRAx/g==\",\"e\":\"QVNDSUktOEJJVA==\"}},\"i\":\"OTdiOQ==\"}}"
|
72
|
+
irb(main)> decrypt!
|
86
73
|
```
|
87
74
|
|
88
|
-
|
75
|
+
```
|
76
|
+
Before you can access personal information, you need to ask for and get explicit consent from the user(s). Unnamed, where can we find this consent (a URL would be great)?
|
77
|
+
|
78
|
+
> ...
|
89
79
|
|
90
|
-
|
91
|
-
|
80
|
+
Ok! You have access to encrypted information now. We pay extra close attention to any commands entered while you have this access. You can go back to protected mode with 'encrypt!'
|
81
|
+
|
82
|
+
WARNING: Make sure you don`t save objects that were loaded while in protected mode, as this can result in saving the encrypted texts.
|
83
|
+
```
|
92
84
|
|
93
85
|
```ruby
|
94
|
-
|
86
|
+
irb(main)> Topic.last.name
|
87
|
+
Topic Load (1.2ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
|
88
|
+
=> "Thanks for the inspiration"
|
95
89
|
```
|
90
|
+
|
91
|
+
You can type `encrypt!` to go back to protected mode again.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
irb(main):004:0> encrypt!
|
95
|
+
```
|
96
|
+
|
97
|
+
```
|
98
|
+
Great! You are back in protected mode. When we audit, we may reach out for a conversation about the commands you entered. What went well? Did you solve the problem without accessing personal data?
|
99
|
+
```
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
irb(main)> Topic.last.name
|
103
|
+
Topic Load (1.4ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
|
104
|
+
=> "{\"p\":\"iu6+LfnNlurC6sL++JyOIDvedjNSz/AvnZQ=\",\"h\":{\"iv\":\"BYa86+JNM/LdkC18\",\"at\":\"r4sQNoSyIlAjJdZEKHVMow==\",\"k\":{\"p\":\"7L1l/5UiYsFQqqo4jfMZtLwp90KqcrIgS7HqgteVjuM=\",\"h\":{\"iv\":\"ItwRYxZAerKIoSZ8\",\"at\":\"ZUSNVfvtm4wAYWLBKRAx/g==\",\"e\":\"QVNDSUktOEJJVA==\"}},\"i\":\"OTdiOQ==\"}}"
|
105
|
+
```
|
106
|
+
|
107
|
+
While in protected mode, you can't modify encrypted data, but can save unencrypted attributes normally. If you try to modify an encrypted column it will raise an error:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
irb(main)> Rails.cache.read("some key") # raises Console1984::Errors::ProtectedConnection
|
111
|
+
```
|
112
|
+
|
113
|
+
### Access to external systems
|
114
|
+
|
115
|
+
While Active Record encryption can protect personal information in the database, are other systems can contain very sensitive information. For example: Elasticsearch indexing user information or Redis caching template fragments.
|
116
|
+
|
117
|
+
To protect the access to such systems, you can add their URLs to `config.console1984.protected_urls` in the corresponding environment config file (e.g: `production.rb`):
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
config.console1984.protected_urls = [ "https://my-app-us-east-1-whatever.us-east-1.es.amazonaws.com", "redis://my-app-cache-1.whatever.cache.amazonaws.com:6379" ]
|
121
|
+
```
|
122
|
+
|
123
|
+
As with encryption data, running `decrypt!` will let you access these systems normally. The system will ask for a justfication and will flag those accesses as sensitive.
|
124
|
+
|
125
|
+
This will work for systems that use Ruby sockets as the underlying communication mechanism.
|
126
|
+
|
127
|
+
### Automatic scheduled incineration for sessions
|
128
|
+
|
129
|
+
By default, sessions will be incinerated with a job 30 days after they are created. You can configure this period by setting `config.console1984.incinerate_after = 1.year` and you can disable incineration completely by setting `config.console1984.incinerate = false`.
|
130
|
+
|
131
|
+
### Eager loading
|
132
|
+
|
133
|
+
When starting a console session, `console1984` will eager load all the application classes if necessary. In practice, production environments already load classes eagerly, so this won't represent any change for those.
|
134
|
+
|
135
|
+
## Configuration
|
136
|
+
|
137
|
+
These config options are namespaced in `config.console1984`:
|
138
|
+
|
139
|
+
| Name | Description |
|
140
|
+
| ------------------------------------------- | ------------------------------------------------------------ |
|
141
|
+
| `protected_environments` | The list of environments where `console1984` will act on. Defaults to `%i[ production ]`. |
|
142
|
+
| `protected_urls` | The list of URLs corresponding with external systems to protect. |
|
143
|
+
| `session_logger` | The system used to record session data. The default logger is `Console1984::SessionsLogger::Database`. |
|
144
|
+
| `username_resolver` | Configure an object responsible of resolving the current database username. The default is `Console1984::Username::EnvResolver.new("CONSOLE_USER")`, which returns the value of the environment variable `CONSOLE_USER`. |
|
145
|
+
| `production_data_warning` | The text to show when a console session starts. |
|
146
|
+
| `enter_unprotected_encryption_mode_warning` | The text to show when user enters into unprotected mode. |
|
147
|
+
| `enter_protected_mode_warning` | The text to show when user go backs to protected mode. |
|
148
|
+
| `incinerate` | Whether incinerate sessions automatically after a period of time or not. Default to `true`. |
|
149
|
+
| `incinerate_after` | The period to keep sessions around before incinerate them. Default `30.days`. |
|
150
|
+
| `incineration_queue` | The name of the queue for session incineration jobs. Default `console1984_incineration`. |
|
151
|
+
|
152
|
+
## About built-in protection mechanisms
|
153
|
+
|
154
|
+
`console1984` uses Ruby to add several protection mechanisms. However, because Ruby is highly dynamic, it's technically possible to circumvent most of these controls if you know what you are doing. We have made an effort to prevent such attempts, but if your organization needs bullet-proof protection against malicious actors using the console, you should consider additional security measures.
|
155
|
+
|
data/lib/console1984/commands.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Container for config options.
|
2
|
+
class Console1984::Config
|
3
|
+
include Console1984::Freezeable, Console1984::Messages
|
4
|
+
|
5
|
+
PROPERTIES = %i[
|
6
|
+
session_logger username_resolver
|
7
|
+
protected_environments protected_urls
|
8
|
+
production_data_warning enter_unprotected_encryption_mode_warning enter_protected_mode_warning
|
9
|
+
incinerate incinerate_after incineration_queue
|
10
|
+
debug test_mode
|
11
|
+
]
|
12
|
+
|
13
|
+
attr_accessor(*PROPERTIES)
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
set_defaults
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_from(properties)
|
20
|
+
properties.each do |key, value|
|
21
|
+
public_send("#{key}=", value) if value.present?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def freeze
|
26
|
+
super
|
27
|
+
protected_urls.freeze
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def set_defaults
|
32
|
+
self.protected_environments = []
|
33
|
+
self.protected_urls = []
|
34
|
+
|
35
|
+
self.session_logger = Console1984::SessionsLogger::Database.new
|
36
|
+
self.username_resolver = Console1984::Username::EnvResolver.new("CONSOLE_USER")
|
37
|
+
|
38
|
+
self.production_data_warning = DEFAULT_PRODUCTION_DATA_WARNING
|
39
|
+
self.enter_unprotected_encryption_mode_warning = DEFAULT_ENTER_UNPROTECTED_ENCRYPTION_MODE_WARNING
|
40
|
+
self.enter_protected_mode_warning = DEFAULT_ENTER_PROTECTED_MODE_WARNING
|
41
|
+
|
42
|
+
self.incinerate = true
|
43
|
+
self.incinerate_after = 30.days
|
44
|
+
self.incineration_queue = "console1984_incineration"
|
45
|
+
|
46
|
+
self.debug = false
|
47
|
+
self.test_mode = false
|
48
|
+
end
|
49
|
+
end
|
data/lib/console1984/engine.rb
CHANGED
@@ -10,19 +10,16 @@ module Console1984
|
|
10
10
|
|
11
11
|
initializer "console1984.config" do
|
12
12
|
config.console1984.each do |key, value|
|
13
|
-
Console1984.send("#{key}=", value) unless %i[ protected_urls protected_environments ].include?(key.to_sym)
|
13
|
+
Console1984.config.send("#{key}=", value) unless %i[ protected_urls protected_environments ].include?(key.to_sym)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
console do
|
18
|
-
Console1984.
|
19
|
-
Console1984.supervisor.start if Console1984.running_protected_environment?
|
18
|
+
Console1984.config.set_from(config.console1984)
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
Addrinfo.getaddrinfo(hostname, 443).first
|
25
|
-
end
|
20
|
+
if Console1984.running_protected_environment?
|
21
|
+
Console1984.supervisor.install
|
22
|
+
Console1984.supervisor.start
|
26
23
|
end
|
27
24
|
end
|
28
25
|
end
|
data/lib/console1984/errors.rb
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Prevents adding new methods to classes.
|
2
|
+
#
|
3
|
+
# This prevents manipulating certain Console1984 classes
|
4
|
+
# during a console session.
|
5
|
+
module Console1984::Freezeable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
mattr_reader :to_freeze, default: Set.new
|
9
|
+
|
10
|
+
included do
|
11
|
+
Console1984::Freezeable.to_freeze << self
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
SENSITIVE_INSTANCE_METHODS = %i[ instance_variable_set ]
|
16
|
+
|
17
|
+
def prevent_sensitive_overrides
|
18
|
+
SENSITIVE_INSTANCE_METHODS.each do |method|
|
19
|
+
prevent_sensitive_method method
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def prevent_sensitive_method(method_name)
|
25
|
+
define_method method_name do |*arguments|
|
26
|
+
raise Console1984::Errors::ForbiddenCodeManipulation, "You can't invoke #{method_name} on #{self}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
def freeze_all
|
33
|
+
class_and_modules_to_freeze.each do |class_or_module|
|
34
|
+
freeze_class_or_module(class_or_module)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def class_and_modules_to_freeze
|
40
|
+
with_descendants(to_freeze)
|
41
|
+
end
|
42
|
+
|
43
|
+
def freeze_class_or_module(class_or_module)
|
44
|
+
class_or_module.prevent_sensitive_overrides
|
45
|
+
class_or_module.freeze
|
46
|
+
end
|
47
|
+
|
48
|
+
def with_descendants(classes_and_modules)
|
49
|
+
classes_and_modules + classes_and_modules.grep(Class).flat_map(&:descendants)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
freeze
|
54
|
+
end
|
data/lib/console1984/messages.rb
CHANGED
@@ -17,8 +17,7 @@ module Console1984::Messages
|
|
17
17
|
TXT
|
18
18
|
|
19
19
|
COMMANDS = {
|
20
|
-
"decrypt!": "enter unprotected mode with access to encrypted information"
|
21
|
-
"log '<reason>'": "provide further information about what you are going to do in the middle of a console session"
|
20
|
+
"decrypt!": "enter unprotected mode with access to encrypted information"
|
22
21
|
}
|
23
22
|
|
24
23
|
COMMANDS_HELP = <<~TXT
|
@@ -1,26 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
# Prevents accessing trail model tables when executing console commands.
|
2
|
+
module Console1984::ProtectedAuditableTables
|
3
|
+
include Console1984::Freezeable
|
4
|
+
|
5
|
+
%i[ execute exec_query exec_insert exec_delete exec_update exec_insert_all ].each do |method|
|
6
|
+
define_method method do |*args|
|
7
|
+
sql = args.first
|
8
|
+
if Console1984.supervisor.executing_user_command? && sql =~ auditable_tables_regexp
|
9
|
+
raise Console1984::Errors::ForbiddenCommand, "#{sql}"
|
10
|
+
else
|
11
|
+
super(*args)
|
11
12
|
end
|
12
13
|
end
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
+
private
|
17
|
+
def auditable_tables_regexp
|
18
|
+
@auditable_tables_regexp ||= Regexp.new("#{auditable_tables.join("|")}")
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
21
|
+
def auditable_tables
|
22
|
+
@auditable_tables ||= auditable_models.collect(&:table_name)
|
23
|
+
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def auditable_models
|
26
|
+
@auditable_models ||= Console1984::Base.descendants
|
27
|
+
end
|
28
|
+
|
29
|
+
include Console1984::Freezeable
|
26
30
|
end
|
@@ -1,15 +1,20 @@
|
|
1
1
|
module Console1984::ProtectedContext
|
2
|
-
|
3
|
-
|
2
|
+
include Console1984::Freezeable
|
3
|
+
|
4
|
+
# This method is invoked for showing returned objects in the console
|
5
|
+
# Overridden to make sure their evaluation is supervised.
|
4
6
|
def inspect_last_value
|
5
7
|
Console1984.supervisor.execute do
|
6
8
|
super
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
12
|
+
#
|
10
13
|
def evaluate(line, line_no, exception: nil)
|
11
14
|
Console1984.supervisor.execute_supervised(Array(line)) do
|
12
15
|
super
|
13
16
|
end
|
14
17
|
end
|
18
|
+
|
19
|
+
include Console1984::Freezeable
|
15
20
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Console1984::ProtectedObject
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
include Console1984::Freezeable
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def const_get(*arguments)
|
8
|
+
if Console1984.supervisor.executing_user_command? && arguments.first.to_s =~ /Console1984|ActiveRecord/
|
9
|
+
raise Console1984::Errors::ForbiddenCommand
|
10
|
+
else
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,4 +1,7 @@
|
|
1
|
+
# Wraps socket methods to execute supervised.
|
1
2
|
module Console1984::ProtectedTcpSocket
|
3
|
+
include Console1984::Freezeable
|
4
|
+
|
2
5
|
def write(*args)
|
3
6
|
protecting do
|
4
7
|
super
|
@@ -53,4 +56,6 @@ module Console1984::ProtectedTcpSocket
|
|
53
56
|
super(addrinfo.ip_address, addrinfo.ip_port)
|
54
57
|
end
|
55
58
|
end
|
59
|
+
|
60
|
+
include Console1984::Freezeable
|
56
61
|
end
|
@@ -1,10 +1,13 @@
|
|
1
|
+
# A session logger that saves audit trails in the database.
|
1
2
|
class Console1984::SessionsLogger::Database
|
3
|
+
include Console1984::Freezeable
|
4
|
+
|
2
5
|
attr_reader :current_session, :current_sensitive_access
|
3
6
|
|
4
7
|
def start_session(username, reason)
|
5
8
|
silence_logging do
|
6
|
-
user = Console1984::User.
|
7
|
-
@current_session = user.sessions.create!
|
9
|
+
user = Console1984::User.find_or_create_by!(username: username)
|
10
|
+
@current_session = user.sessions.create!(reason: reason)
|
8
11
|
end
|
9
12
|
end
|
10
13
|
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module Console1984::Supervisor::Executor
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
|
+
include Console1984::Freezeable
|
5
|
+
|
4
6
|
def execute_supervised(commands, &block)
|
5
7
|
run_system_command { session_logger.before_executing commands }
|
8
|
+
validate_commands(commands)
|
6
9
|
execute(&block)
|
7
|
-
rescue Console1984::Errors::ForbiddenCommand
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
rescue Console1984::Errors::ForbiddenCommand, Console1984::Errors::ForbiddenCodeManipulation, FrozenError
|
11
|
+
flag_forbidden(commands)
|
12
|
+
rescue FrozenError
|
13
|
+
flag_forbidden(commands)
|
11
14
|
ensure
|
12
15
|
run_system_command { session_logger.after_executing commands }
|
13
16
|
end
|
@@ -23,6 +26,12 @@ module Console1984::Supervisor::Executor
|
|
23
26
|
end
|
24
27
|
|
25
28
|
private
|
29
|
+
def flag_forbidden(commands)
|
30
|
+
puts "Forbidden command attempted: #{commands.join("\n")}"
|
31
|
+
run_system_command { session_logger.suspicious_commands_attempted commands }
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
26
35
|
def run_user_command(&block)
|
27
36
|
run_command true, &block
|
28
37
|
end
|
@@ -31,6 +40,21 @@ module Console1984::Supervisor::Executor
|
|
31
40
|
run_command false, &block
|
32
41
|
end
|
33
42
|
|
43
|
+
def validate_commands(commands)
|
44
|
+
if Array(commands).find { |command| forbidden_command?(command) }
|
45
|
+
raise Console1984::Errors::ForbiddenCommand
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def forbidden_command?(command)
|
50
|
+
# This is a first protection layer. Very simple for now. We'll likely make this
|
51
|
+
# more sophisticated and configurable in future versions.
|
52
|
+
#
|
53
|
+
# We can't use our +Freezable+ concern in ActiveRecord since it relies on code
|
54
|
+
# generation on the fly.
|
55
|
+
command =~ /Console1984|console_1984|(class|module)\s+ActiveRecord::/
|
56
|
+
end
|
57
|
+
|
34
58
|
def run_command(run_by_user, &block)
|
35
59
|
original_value = @executing_user_command
|
36
60
|
@executing_user_command = run_by_user
|
@@ -1,11 +1,31 @@
|
|
1
1
|
module Console1984::Supervisor::InputOutput
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
2
|
+
include Console1984::Freezeable, Console1984::Messages
|
3
|
+
|
4
|
+
private
|
5
|
+
def show_welcome_message
|
6
|
+
show_production_data_warning
|
7
|
+
show_commands
|
8
|
+
end
|
9
|
+
|
10
|
+
def show_production_data_warning
|
11
|
+
show_warning Console1984.production_data_warning
|
12
|
+
end
|
13
|
+
|
14
|
+
def ask_for_session_reason
|
15
|
+
ask_for_value("#{current_username}, why are you using this console today?")
|
16
|
+
end
|
17
|
+
|
18
|
+
def show_commands
|
19
|
+
puts COMMANDS_HELP
|
20
|
+
end
|
21
|
+
|
22
|
+
def show_warning(message)
|
23
|
+
puts ColorizedString.new("\n#{message}\n").yellow
|
24
|
+
end
|
25
|
+
|
26
|
+
def ask_for_value(message)
|
27
|
+
puts ColorizedString.new("#{message}").green
|
28
|
+
reason = $stdin.gets.strip until reason.present?
|
29
|
+
reason
|
30
|
+
end
|
11
31
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Console1984::Supervisor::Protector
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
include Console1984::Freezeable
|
5
|
+
|
6
|
+
private
|
7
|
+
def extend_protected_systems
|
8
|
+
extend_object
|
9
|
+
extend_irb
|
10
|
+
extend_active_record
|
11
|
+
extend_socket_classes
|
12
|
+
end
|
13
|
+
|
14
|
+
def extend_object
|
15
|
+
Object.prepend Console1984::ProtectedObject
|
16
|
+
end
|
17
|
+
|
18
|
+
def extend_irb
|
19
|
+
IRB::Context.prepend(Console1984::ProtectedContext)
|
20
|
+
Rails::ConsoleMethods.include(Console1984::Commands)
|
21
|
+
end
|
22
|
+
|
23
|
+
ACTIVE_RECORD_CONNECTION_ADAPTERS = %w[ActiveRecord::ConnectionAdapters::Mysql2Adapter ActiveRecord::ConnectionAdapters::PostgreSQLAdapter ActiveRecord::ConnectionAdapters::SQLite3Adapter]
|
24
|
+
|
25
|
+
def extend_active_record
|
26
|
+
ACTIVE_RECORD_CONNECTION_ADAPTERS.each do |class_string|
|
27
|
+
if Object.const_defined?(class_string)
|
28
|
+
klass = class_string.constantize
|
29
|
+
klass.prepend(Console1984::ProtectedAuditableTables)
|
30
|
+
klass.include(Console1984::Freezeable)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def extend_socket_classes
|
36
|
+
socket_classes = [TCPSocket, OpenSSL::SSL::SSLSocket]
|
37
|
+
OpenSSL::SSL::SSLSocket.include(SSLSocketRemoteAddress)
|
38
|
+
|
39
|
+
if defined?(Redis::Connection)
|
40
|
+
socket_classes.push(*[Redis::Connection::TCPSocket, Redis::Connection::SSLSocket])
|
41
|
+
end
|
42
|
+
|
43
|
+
socket_classes.compact.each do |socket_klass|
|
44
|
+
socket_klass.prepend Console1984::ProtectedTcpSocket
|
45
|
+
socket_klass.freeze
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module SSLSocketRemoteAddress
|
50
|
+
# Make it serve remote address as TCPSocket so that our extension works for it
|
51
|
+
def remote_address
|
52
|
+
Addrinfo.getaddrinfo(hostname, 443).first
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,48 +1,61 @@
|
|
1
1
|
require 'colorized_string'
|
2
2
|
require 'rails/console/app'
|
3
3
|
|
4
|
+
# Protects console sessions and executes code in supervised mode.
|
4
5
|
class Console1984::Supervisor
|
5
|
-
include Accesses, InputOutput,
|
6
|
+
include Accesses, Console1984::Freezeable, Executor, InputOutput, Protector
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
disable_access_to_encrypted_content(silent: true)
|
8
|
+
def install
|
9
|
+
extend_protected_systems
|
10
|
+
freeze_all
|
12
11
|
end
|
13
12
|
|
13
|
+
# Starts a console session extending IRB and several systems to inject
|
14
|
+
# the protection logic, and notifies the session logger to record the
|
15
|
+
# session.
|
14
16
|
def start
|
15
|
-
|
16
|
-
show_commands
|
17
|
+
disable_access_to_encrypted_content(silent: true)
|
17
18
|
|
18
|
-
|
19
|
+
show_welcome_message
|
19
20
|
|
20
|
-
|
21
|
+
start_session
|
21
22
|
end
|
22
23
|
|
23
24
|
def stop
|
24
|
-
|
25
|
+
stop_session
|
25
26
|
end
|
26
27
|
|
27
28
|
private
|
28
|
-
def
|
29
|
-
|
29
|
+
def start_session
|
30
|
+
session_logger.start_session current_username, ask_for_session_reason
|
31
|
+
end
|
32
|
+
|
33
|
+
def stop_session
|
34
|
+
session_logger.finish_session
|
30
35
|
end
|
31
36
|
|
32
|
-
def
|
33
|
-
|
37
|
+
def freeze_all
|
38
|
+
eager_load_all_classes
|
39
|
+
Console1984.config.freeze unless Console1984.config.test_mode
|
40
|
+
Console1984::Freezeable.freeze_all
|
34
41
|
end
|
35
42
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
43
|
+
def eager_load_all_classes
|
44
|
+
Rails.application.eager_load! unless Rails.application.config.eager_load
|
45
|
+
Console1984.class_loader.eager_load
|
39
46
|
end
|
40
47
|
|
41
|
-
def
|
42
|
-
|
48
|
+
def session_logger
|
49
|
+
Console1984.session_logger
|
43
50
|
end
|
44
51
|
|
45
|
-
def
|
46
|
-
|
52
|
+
def current_username
|
53
|
+
Console1984.username_resolver.current
|
47
54
|
end
|
55
|
+
|
56
|
+
def username_resolver
|
57
|
+
Console1984.username_resolver
|
58
|
+
end
|
59
|
+
|
60
|
+
include Console1984::Freezeable
|
48
61
|
end
|
data/lib/console1984/version.rb
CHANGED
data/lib/console1984.rb
CHANGED
@@ -1,42 +1,21 @@
|
|
1
1
|
require 'console1984/engine'
|
2
2
|
|
3
3
|
require "zeitwerk"
|
4
|
-
|
5
|
-
|
4
|
+
class_loader = Zeitwerk::Loader.for_gem
|
5
|
+
class_loader.setup
|
6
6
|
|
7
7
|
module Console1984
|
8
|
-
include Messages
|
8
|
+
include Messages, Freezeable
|
9
9
|
|
10
|
-
mattr_accessor :supervisor
|
11
|
-
|
12
|
-
mattr_accessor :
|
13
|
-
|
14
|
-
mattr_accessor :protected_environments
|
15
|
-
mattr_reader :protected_urls, default: []
|
16
|
-
|
17
|
-
mattr_reader :production_data_warning, default: DEFAULT_PRODUCTION_DATA_WARNING
|
18
|
-
mattr_reader :enter_unprotected_encryption_mode_warning, default: DEFAULT_ENTER_UNPROTECTED_ENCRYPTION_MODE_WARNING
|
19
|
-
mattr_reader :enter_protected_mode_warning, default: DEFAULT_ENTER_PROTECTED_MODE_WARNING
|
20
|
-
|
21
|
-
mattr_accessor :incinerate, default: true
|
22
|
-
mattr_accessor :incinerate_after, default: 30.days
|
23
|
-
mattr_accessor :incineration_queue, default: "console1984_incineration"
|
24
|
-
|
25
|
-
mattr_accessor :debug, default: false
|
10
|
+
mattr_accessor :supervisor, default: Supervisor.new
|
11
|
+
mattr_reader :config, default: Config.new
|
12
|
+
mattr_accessor :class_loader
|
26
13
|
|
27
14
|
thread_mattr_accessor :currently_protected_urls, default: []
|
28
15
|
|
29
16
|
class << self
|
30
|
-
|
31
|
-
|
32
|
-
self.protected_urls.push(*config.protected_urls)
|
33
|
-
self.session_logger = config.session_logger || Console1984::SessionsLogger::Database.new
|
34
|
-
self.username_resolver = config.username_resolver || Console1984::Username::EnvResolver.new("CONSOLE_USER")
|
35
|
-
|
36
|
-
self.supervisor = Supervisor.new
|
37
|
-
self.protected_urls.freeze
|
38
|
-
|
39
|
-
extend_protected_systems
|
17
|
+
Config::PROPERTIES.each do |property|
|
18
|
+
delegate property, to: :config
|
40
19
|
end
|
41
20
|
|
42
21
|
def running_protected_environment?
|
@@ -50,31 +29,6 @@ module Console1984
|
|
50
29
|
end
|
51
30
|
|
52
31
|
private
|
53
|
-
def extend_protected_systems
|
54
|
-
extend_active_record
|
55
|
-
extend_socket_classes
|
56
|
-
end
|
57
|
-
|
58
|
-
def extend_active_record
|
59
|
-
%w[ActiveRecord::ConnectionAdapters::Mysql2Adapter ActiveRecord::ConnectionAdapters::PostgreSQLAdapter ActiveRecord::ConnectionAdapters::SQLite3Adapter].each do |class_string|
|
60
|
-
if Object.const_defined?(class_string)
|
61
|
-
klass = class_string.constantize
|
62
|
-
klass.prepend(Console1984::ProtectedAuditableTables)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def extend_socket_classes
|
68
|
-
socket_classes = [TCPSocket, OpenSSL::SSL::SSLSocket]
|
69
|
-
if defined?(Redis::Connection)
|
70
|
-
socket_classes.push(*[Redis::Connection::TCPSocket, Redis::Connection::SSLSocket])
|
71
|
-
end
|
72
|
-
|
73
|
-
socket_classes.compact.each do |socket_klass|
|
74
|
-
socket_klass.prepend Console1984::ProtectedTcpSocket
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
32
|
def protecting_connections
|
79
33
|
old_currently_protected_urls = self.currently_protected_urls
|
80
34
|
self.currently_protected_urls = protected_urls
|
@@ -84,3 +38,5 @@ module Console1984
|
|
84
38
|
end
|
85
39
|
end
|
86
40
|
end
|
41
|
+
|
42
|
+
Console1984.class_loader = class_loader
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console1984
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jorge Manrubia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-minitest
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rubocop-rails
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,12 +157,14 @@ files:
|
|
143
157
|
- db/migrate/20210517203931_create_console1984_tables.rb
|
144
158
|
- lib/console1984.rb
|
145
159
|
- lib/console1984/commands.rb
|
160
|
+
- lib/console1984/config.rb
|
146
161
|
- lib/console1984/engine.rb
|
147
|
-
- lib/console1984/env_variable_username.rb
|
148
162
|
- lib/console1984/errors.rb
|
163
|
+
- lib/console1984/freezeable.rb
|
149
164
|
- lib/console1984/messages.rb
|
150
165
|
- lib/console1984/protected_auditable_tables.rb
|
151
166
|
- lib/console1984/protected_context.rb
|
167
|
+
- lib/console1984/protected_object.rb
|
152
168
|
- lib/console1984/protected_tcp_socket.rb
|
153
169
|
- lib/console1984/sessions_logger/database.rb
|
154
170
|
- lib/console1984/supervisor.rb
|
@@ -157,6 +173,7 @@ files:
|
|
157
173
|
- lib/console1984/supervisor/accesses/unprotected.rb
|
158
174
|
- lib/console1984/supervisor/executor.rb
|
159
175
|
- lib/console1984/supervisor/input_output.rb
|
176
|
+
- lib/console1984/supervisor/protector.rb
|
160
177
|
- lib/console1984/username/env_resolver.rb
|
161
178
|
- lib/console1984/version.rb
|
162
179
|
- test/fixtures/console1984/commands.yml
|