Skip to content

Commit eeac862

Browse files
authoredApr 1, 2022
Merge pull request #47 from basecamp/handle-empty-usernames
Add option to ask for username when the username is not set.
2 parents 2bc00af + 56745be commit eeac862

File tree

10 files changed

+84
-28
lines changed

10 files changed

+84
-28
lines changed
 

‎Gemfile.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ GIT
1010
PATH
1111
remote: .
1212
specs:
13-
console1984 (0.1.22)
13+
console1984 (0.1.23)
1414
colorize
1515
parser
1616

@@ -109,8 +109,8 @@ GEM
109109
racc (~> 1.4)
110110
nokogiri (1.12.5-x86_64-linux)
111111
racc (~> 1.4)
112-
parallel (1.21.0)
113-
parser (3.0.3.2)
112+
parallel (1.22.1)
113+
parser (3.1.1.0)
114114
ast (~> 2.4.1)
115115
pg (1.2.3)
116116
racc (1.6.0)
@@ -143,21 +143,21 @@ GEM
143143
rake (>= 12.2)
144144
thor (~> 1.0)
145145
zeitwerk (~> 2.5)
146-
rainbow (3.0.0)
146+
rainbow (3.1.1)
147147
rake (13.0.6)
148-
regexp_parser (2.2.0)
148+
regexp_parser (2.2.1)
149149
rexml (3.2.5)
150-
rubocop (1.23.0)
150+
rubocop (1.26.1)
151151
parallel (~> 1.10)
152-
parser (>= 3.0.0.0)
152+
parser (>= 3.1.0.0)
153153
rainbow (>= 2.2.2, < 4.0)
154154
regexp_parser (>= 1.8, < 3.0)
155155
rexml
156-
rubocop-ast (>= 1.12.0, < 2.0)
156+
rubocop-ast (>= 1.16.0, < 2.0)
157157
ruby-progressbar (~> 1.7)
158158
unicode-display_width (>= 1.4.0, < 3.0)
159-
rubocop-ast (1.15.0)
160-
parser (>= 3.0.1.1)
159+
rubocop-ast (1.16.0)
160+
parser (>= 3.1.1.0)
161161
rubocop-minitest (0.17.0)
162162
rubocop (>= 0.90, < 2.0)
163163
rubocop-packaging (0.5.1)

‎README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,19 @@ When starting a console session, `console1984` will eager load all the applicati
143143

144144
These config options are namespaced in `config.console1984`:
145145

146-
| Name | Description |
147-
| ------------------------------------------- | ------------------------------------------------------------ |
148-
| `protected_environments` | The list of environments where `console1984` will act on. Defaults to `%i[ production ]`. |
149-
| `protected_urls` | The list of URLs corresponding with external systems to protect. |
150-
| `session_logger` | The system used to record session data. The default logger is `Console1984::SessionsLogger::Database`. |
146+
| Name | Description |
147+
|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
148+
| `protected_environments` | The list of environments where `console1984` will act on. Defaults to `%i[ production ]`. |
149+
| `protected_urls` | The list of URLs corresponding with external systems to protect. |
150+
| `session_logger` | The system used to record session data. The default logger is `Console1984::SessionsLogger::Database`. |
151151
| `username_resolver` | Configure how the current user is determined for a given console session. The default is `Console1984::Username::EnvResolver.new("CONSOLE_USER")`, which returns the value of the environment variable `CONSOLE_USER`. |
152-
| `production_data_warning` | The text to show when a console session starts. |
153-
| `enter_unprotected_encryption_mode_warning` | The text to show when user enters into unprotected mode. |
154-
| `enter_protected_mode_warning` | The text to show when user go backs to protected mode. |
155-
| `incinerate` | Whether incinerate sessions automatically after a period of time or not. Default to `true`. |
156-
| `incinerate_after` | The period to keep sessions around before incinerate them. Default `30.days`. |
157-
| `incineration_queue` | The name of the queue for session incineration jobs. Default `console1984_incineration`. |
152+
| `ask_for_username_if_empty` | If `true`, the console will ask for a username if it is empty. If `false`, it will raise an error if no username is set. Defaults to `false`. |
153+
| `production_data_warning` | The text to show when a console session starts. |
154+
| `enter_unprotected_encryption_mode_warning` | The text to show when user enters into unprotected mode. |
155+
| `enter_protected_mode_warning` | The text to show when user go backs to protected mode. |
156+
| `incinerate` | Whether incinerate sessions automatically after a period of time or not. Default to `true`. |
157+
| `incinerate_after` | The period to keep sessions around before incinerate them. Default `30.days`. |
158+
| `incineration_queue` | The name of the queue for session incineration jobs. Default `console1984_incineration`. |
158159

159160
### SSH Config
160161

‎lib/console1984/config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Console1984::Config
77
PROTECTIONS_CONFIG_FILE_PATH = Console1984::Engine.root.join("config/protections.yml")
88

99
PROPERTIES = %i[
10-
session_logger username_resolver shield command_executor
10+
session_logger username_resolver ask_for_username_if_empty shield command_executor
1111
protected_environments protected_urls
1212
production_data_warning enter_unprotected_encryption_mode_warning enter_protected_mode_warning
1313
incinerate incinerate_after incineration_queue
@@ -54,6 +54,7 @@ def set_defaults
5454
self.incinerate = true
5555
self.incinerate_after = 30.days
5656
self.incineration_queue = "console1984_incineration"
57+
self.ask_for_username_if_empty = false
5758

5859
self.debug = false
5960
self.test_mode = false

‎lib/console1984/errors.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ class ForbiddenCommandExecuted < StandardError; end
2323
# Attempt to incinerate a session ahead of time as determined by
2424
# +config.console1984.incinerate_after+.
2525
class ForbiddenIncineration < StandardError; end
26+
27+
# The console username is not set. Only raised when `config.ask_for_username_if_empty = false`.
28+
class MissingUsername < StandardError; end
2629
end
2730
end

‎lib/console1984/shield/modes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ def protected_mode?
5555

5656
private
5757
def current_username
58-
username_resolver.current
58+
Console1984.supervisor.current_username
5959
end
6060
end

‎lib/console1984/supervisor.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def exit_irb
3535
IRB.CurrentContext.exit
3636
end
3737

38+
def current_username
39+
@current_username ||= username_resolver.current.presence || handle_empty_username
40+
end
41+
3842
private
3943
def require_dependencies
4044
Kernel.silence_warnings do
@@ -61,7 +65,11 @@ def stop_session
6165
session_logger.finish_session
6266
end
6367

64-
def current_username
65-
username_resolver.current
68+
def handle_empty_username
69+
if Console1984.config.ask_for_username_if_empty
70+
ask_for_value "Please, enter your name:"
71+
else
72+
raise Console1984::Errors::MissingUsername
73+
end
6674
end
6775
end

‎lib/console1984/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Console1984
2-
VERSION = '0.1.22'
2+
VERSION = '0.1.23'
33
end

‎test/dummy/config/application.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
require "console1984"
77

88
module Dummy
9+
class MutableUsernameEnvResolver
10+
attr_accessor :username
11+
12+
def initialize(username)
13+
@username = username
14+
end
15+
16+
def current
17+
"#{username}"
18+
end
19+
end
20+
921
class Application < Rails::Application
1022
# Initialize configuration defaults for originally generated Rails version.
1123
config.load_defaults 6.0
@@ -16,6 +28,8 @@ class Application < Rails::Application
1628
# the framework and any gems in your application.
1729
config.console1984.protected_environments = %i[ production test development ]
1830
config.console1984.protected_urls = [ "localhost:#{6379}", "http://elastic:changeme@localhost:39201" ]
31+
config.console1984.ask_for_username_if_empty = true
32+
config.console1984.username_resolver = MutableUsernameEnvResolver.new("jorge")
1933

2034
config.active_record.encryption.encrypt_fixtures = true
2135
end

‎test/supervisor_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "test_helper"
2+
3+
class IncinerationTest < ActiveSupport::TestCase
4+
setup do
5+
@supervisor = Console1984::Supervisor.new
6+
end
7+
8+
test "raises error when allow_empty_username is false and no username is provided" do
9+
original, Console1984.config.ask_for_username_if_empty = Console1984.config.ask_for_username_if_empty, false
10+
Console1984.username_resolver.username = ""
11+
12+
assert_raises Console1984::Errors::MissingUsername do
13+
@supervisor.current_username
14+
end
15+
ensure
16+
Console1984.config.ask_for_username_if_empty = original
17+
end
18+
19+
test "asks for username allow_empty_username is true and no username is provided" do
20+
original, Console1984.config.ask_for_username_if_empty = Console1984.config.ask_for_username_if_empty, true
21+
Console1984.username_resolver.username = ""
22+
23+
type_when_prompted "Jorge M." do
24+
assert_equal "Jorge M.", @supervisor.current_username
25+
end
26+
ensure
27+
Console1984.config.ask_for_username_if_empty = original
28+
end
29+
end

‎test/support/supervised_test_console.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SupervisedTestConsole
55

66
def initialize(reason: "No reason", user: "Not set")
77
@string_io = StringIO.new
8-
ENV["CONSOLE_USER"] = user
8+
Console1984.username_resolver.username = user
99

1010
@context = Context.new
1111
IRB.stubs(CurrentContext: @context)
@@ -37,7 +37,7 @@ def output
3737
private
3838
def simulate_evaluation(statement)
3939
simulated_console.instance_eval statement
40-
rescue NoMethodError
40+
rescue NoMethodError => e
4141
eval(statement)
4242
end
4343

0 commit comments

Comments
 (0)
Please sign in to comment.