Skip to content

Commit

Permalink
Merge pull request #47 from basecamp/handle-empty-usernames
Browse files Browse the repository at this point in the history
Add option to ask for username when the username is not set.
  • Loading branch information
jorgemanrubia authored Apr 1, 2022
2 parents 2bc00af + 56745be commit eeac862
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 28 deletions.
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT
PATH
remote: .
specs:
console1984 (0.1.22)
console1984 (0.1.23)
colorize
parser

Expand Down Expand Up @@ -109,8 +109,8 @@ GEM
racc (~> 1.4)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
parallel (1.21.0)
parser (3.0.3.2)
parallel (1.22.1)
parser (3.1.1.0)
ast (~> 2.4.1)
pg (1.2.3)
racc (1.6.0)
Expand Down Expand Up @@ -143,21 +143,21 @@ GEM
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.0.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.2.0)
regexp_parser (2.2.1)
rexml (3.2.5)
rubocop (1.23.0)
rubocop (1.26.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.12.0, < 2.0)
rubocop-ast (>= 1.16.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.0)
parser (>= 3.0.1.1)
rubocop-ast (1.16.0)
parser (>= 3.1.1.0)
rubocop-minitest (0.17.0)
rubocop (>= 0.90, < 2.0)
rubocop-packaging (0.5.1)
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,19 @@ When starting a console session, `console1984` will eager load all the applicati

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

| Name | Description |
| ------------------------------------------- | ------------------------------------------------------------ |
| `protected_environments` | The list of environments where `console1984` will act on. Defaults to `%i[ production ]`. |
| `protected_urls` | The list of URLs corresponding with external systems to protect. |
| `session_logger` | The system used to record session data. The default logger is `Console1984::SessionsLogger::Database`. |
| Name | Description |
|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `protected_environments` | The list of environments where `console1984` will act on. Defaults to `%i[ production ]`. |
| `protected_urls` | The list of URLs corresponding with external systems to protect. |
| `session_logger` | The system used to record session data. The default logger is `Console1984::SessionsLogger::Database`. |
| `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`. |
| `production_data_warning` | The text to show when a console session starts. |
| `enter_unprotected_encryption_mode_warning` | The text to show when user enters into unprotected mode. |
| `enter_protected_mode_warning` | The text to show when user go backs to protected mode. |
| `incinerate` | Whether incinerate sessions automatically after a period of time or not. Default to `true`. |
| `incinerate_after` | The period to keep sessions around before incinerate them. Default `30.days`. |
| `incineration_queue` | The name of the queue for session incineration jobs. Default `console1984_incineration`. |
| `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`. |
| `production_data_warning` | The text to show when a console session starts. |
| `enter_unprotected_encryption_mode_warning` | The text to show when user enters into unprotected mode. |
| `enter_protected_mode_warning` | The text to show when user go backs to protected mode. |
| `incinerate` | Whether incinerate sessions automatically after a period of time or not. Default to `true`. |
| `incinerate_after` | The period to keep sessions around before incinerate them. Default `30.days`. |
| `incineration_queue` | The name of the queue for session incineration jobs. Default `console1984_incineration`. |

### SSH Config

Expand Down
3 changes: 2 additions & 1 deletion lib/console1984/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Console1984::Config
PROTECTIONS_CONFIG_FILE_PATH = Console1984::Engine.root.join("config/protections.yml")

PROPERTIES = %i[
session_logger username_resolver shield command_executor
session_logger username_resolver ask_for_username_if_empty shield command_executor
protected_environments protected_urls
production_data_warning enter_unprotected_encryption_mode_warning enter_protected_mode_warning
incinerate incinerate_after incineration_queue
Expand Down Expand Up @@ -54,6 +54,7 @@ def set_defaults
self.incinerate = true
self.incinerate_after = 30.days
self.incineration_queue = "console1984_incineration"
self.ask_for_username_if_empty = false

self.debug = false
self.test_mode = false
Expand Down
3 changes: 3 additions & 0 deletions lib/console1984/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ class ForbiddenCommandExecuted < StandardError; end
# Attempt to incinerate a session ahead of time as determined by
# +config.console1984.incinerate_after+.
class ForbiddenIncineration < StandardError; end

# The console username is not set. Only raised when `config.ask_for_username_if_empty = false`.
class MissingUsername < StandardError; end
end
end
2 changes: 1 addition & 1 deletion lib/console1984/shield/modes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ def protected_mode?

private
def current_username
username_resolver.current
Console1984.supervisor.current_username
end
end
12 changes: 10 additions & 2 deletions lib/console1984/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def exit_irb
IRB.CurrentContext.exit
end

def current_username
@current_username ||= username_resolver.current.presence || handle_empty_username
end

private
def require_dependencies
Kernel.silence_warnings do
Expand All @@ -61,7 +65,11 @@ def stop_session
session_logger.finish_session
end

def current_username
username_resolver.current
def handle_empty_username
if Console1984.config.ask_for_username_if_empty
ask_for_value "Please, enter your name:"
else
raise Console1984::Errors::MissingUsername
end
end
end
2 changes: 1 addition & 1 deletion lib/console1984/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Console1984
VERSION = '0.1.22'
VERSION = '0.1.23'
end
14 changes: 14 additions & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
require "console1984"

module Dummy
class MutableUsernameEnvResolver
attr_accessor :username

def initialize(username)
@username = username
end

def current
"#{username}"
end
end

class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
Expand All @@ -16,6 +28,8 @@ class Application < Rails::Application
# the framework and any gems in your application.
config.console1984.protected_environments = %i[ production test development ]
config.console1984.protected_urls = [ "localhost:#{6379}", "http://elastic:changeme@localhost:39201" ]
config.console1984.ask_for_username_if_empty = true
config.console1984.username_resolver = MutableUsernameEnvResolver.new("jorge")

config.active_record.encryption.encrypt_fixtures = true
end
Expand Down
29 changes: 29 additions & 0 deletions test/supervisor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "test_helper"

class IncinerationTest < ActiveSupport::TestCase
setup do
@supervisor = Console1984::Supervisor.new
end

test "raises error when allow_empty_username is false and no username is provided" do
original, Console1984.config.ask_for_username_if_empty = Console1984.config.ask_for_username_if_empty, false
Console1984.username_resolver.username = ""

assert_raises Console1984::Errors::MissingUsername do
@supervisor.current_username
end
ensure
Console1984.config.ask_for_username_if_empty = original
end

test "asks for username allow_empty_username is true and no username is provided" do
original, Console1984.config.ask_for_username_if_empty = Console1984.config.ask_for_username_if_empty, true
Console1984.username_resolver.username = ""

type_when_prompted "Jorge M." do
assert_equal "Jorge M.", @supervisor.current_username
end
ensure
Console1984.config.ask_for_username_if_empty = original
end
end
4 changes: 2 additions & 2 deletions test/support/supervised_test_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SupervisedTestConsole

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

@context = Context.new
IRB.stubs(CurrentContext: @context)
Expand Down Expand Up @@ -37,7 +37,7 @@ def output
private
def simulate_evaluation(statement)
simulated_console.instance_eval statement
rescue NoMethodError
rescue NoMethodError => e
eval(statement)
end

Expand Down

0 comments on commit eeac862

Please sign in to comment.