feat: add UTF-8 chars to output when supported and allowed

To enhance the readability and visibility of important messages
(such as critical ones). This can be disabled with the `allowUTF8`
global option set to `false`. It's never enabled if the user locale
or their terminal don't seem to support it.
This commit is contained in:
Stéphane Lesimple 2021-04-14 08:35:50 +00:00 committed by Stéphane Lesimple
parent 60ad30ce5b
commit adb9d8c374
6 changed files with 61 additions and 19 deletions

View file

@ -22,7 +22,7 @@ my $remainingOptions = OVH::Bastion::Plugin::begin(
);
sub help {
print <<"EOF";
my $text = <<"EOF";
Create a new public + private key pair on your bastion account
Usage: --osh $scriptName --algo ALGO --size SIZE [--encrypted]
@ -37,6 +37,7 @@ Usage: --osh $scriptName --algo ALGO --size SIZE [--encrypted]
--encrypted if specified, a passphrase will be prompted for the new key
EOF
osh_info($text);
OVH::Bastion::Plugin::generateEgressKey::help_algos();
return 1;
}

View file

@ -89,11 +89,11 @@ if (!-r $ttyrecfile) {
osh_exit R('ERR_NOT_FOUND', msg => "Recorded session for ID $id couldn't be found, it might have been archived");
}
osh_warn "Press '+' to play faster";
osh_warn "Press '-' to play slower";
osh_warn "Press '1' to restore normal playing speed";
osh_warn "\nWhen you're ready to replay session $id, press ENTER.";
osh_warn "Starting from the next line, the Total Recall begins. Press CTRL+C to jolt awake.";
osh_info "Press '+' to play faster";
osh_info "Press '-' to play slower";
osh_info "Press '1' to restore normal playing speed";
osh_info "\nWhen you're ready to replay session $id, press ENTER.";
osh_info "Starting from the next line, the Total Recall begins. Press CTRL+C to jolt awake.";
<STDIN>;
my $sysret = system('ttyplay', $ttyrecfile);
osh_ok {};

View file

@ -14,7 +14,6 @@ use POSIX qw(strftime);
use Term::ANSIColor;
use JSON;
$ENV{'LANG'} = 'C';
$| = 1;
my $fnret;

View file

@ -262,6 +262,11 @@
# DEFAULT: true
"displayLastLogin": true,
#
# allowUTF8 (boolean)
# DESC: When ``true``, The Bastion will use some UTF-8 characters on the output, for a more pleasant experience (for warnings, critical messages, etc.). Note that if the terminal doesn't advertise UTF-8 support, UTF-8 will not be used, even when enabled here.
# DEFAULT: true
"allowUTF8": true,
#
# interactiveModeAllowed (boolean)
# DESC: If set to ``true``, ``--interactive`` mode is allowed. Otherwise, this feature is disabled.
# DEFAULT: true

View file

@ -367,9 +367,18 @@ sub osh_header {
my $hostname = Sys::Hostname::hostname();
my $versionline = 'the-bastion-' . $VERSION;
my $output = '';
$output .= colored('---' . $hostname . '-' x (80 - length($hostname) - length($versionline) - 6) . "$versionline---" . "\n", 'bold blue');
$output .= colored("=> $text\n", "blue");
$output .= colored('-' x 80 . "\n", 'blue');
if (OVH::Bastion::can_use_utf8()) {
my $line = "\N{U+256D}\N{U+2500}\N{U+2500}" . $hostname . "\N{U+2500}" x (80 - length($hostname) - length($versionline) - 6) . $versionline . "\N{U+2500}" x 3 . "\n";
$output .= colored($line, 'bold blue');
$output .= colored("\N{U+2502} \N{U+25B6} $text\n", 'blue');
$output .= colored("\N{U+251C}" . "\N{U+2500}" x 79 . "\n", 'blue');
}
else {
my $line = '-' x 3 . $hostname . '-' x (80 - length($hostname) - length($versionline) - 6) . $versionline . '-' x 3 . "\n";
$output .= colored($line, 'bold blue');
$output .= colored("=> $text\n", 'blue');
$output .= colored('-' x 80 . "\n", 'blue');
}
print $output unless ($ENV{'PLUGIN_QUIET'});
return;
@ -381,8 +390,13 @@ sub osh_footer {
$text = $ENV{'PLUGIN_NAME'};
}
my $output = '';
$output .= colored('-' x (80 - length($text) - 6) . "</$text>---" . "\n", 'bold blue');
my $output;
if (OVH::Bastion::can_use_utf8()) {
$output = colored("\N{U+2570}" . "\N{U+2500}" x (79 - length($text) - 6) . "</$text>" . "\N{U+2500}" x 3 . "\n", 'bold blue');
}
else {
$output = colored('-' x (80 - length($text) - 6) . "</$text>---" . "\n", 'bold blue');
}
print $output unless ($ENV{'PLUGIN_QUIET'});
return;
@ -472,15 +486,16 @@ sub osh_debug {
}
sub osh_info {
return _osh_log(text => shift, color => 'blue', onlyPrefix => 1);
return _osh_log(text => shift, type => 'info');
}
sub osh_warn {
return _osh_log(text => shift, color => 'magenta');
return _osh_log(text => shift, type => 'warn');
}
sub osh_crit {
return _osh_log(text => shift, color => 'red bold');
my $text = shift;
return _osh_log(text => "\n$text", type => 'crit');
}
sub _osh_log {
@ -491,14 +506,30 @@ sub _osh_log {
print $output $params{'text'} . "\n";
}
else {
my $prefix = OVH::Bastion::can_use_utf8() ? "\N{U+2502}" : '~';
my $prefixIfNotEmpty = '';
my $color;
if ($params{'type'} eq 'crit') {
$prefixIfNotEmpty = (OVH::Bastion::can_use_utf8() ? "\N{U+26D4}" : "[!]");
$color = 'red bold';
}
elsif ($params{'type'} eq 'warn') {
$prefixIfNotEmpty = (OVH::Bastion::can_use_utf8() ? "\N{U+2757}" : "[#]");
$color = 'yellow';
}
else {
$color = 'blue';
}
foreach my $line (split /^/, $params{'text'}) {
chomp $line;
my $realPrefix = $prefix;
$realPrefix .= ' ' . $prefixIfNotEmpty if (length($line) && $prefixIfNotEmpty);
if ($params{'onlyPrefix'}) {
print $output colored('~ ', $params{'color'}) . "$line\n";
if ($params{'type'} eq 'info') {
print $output colored("$realPrefix ", $color) . "$line\n";
}
else {
print $output colored("~ $line", $params{'color'}) . "\n";
print $output colored("$realPrefix $line", $color) . "\n";
}
}
}
@ -1019,4 +1050,10 @@ sub do_pamtester {
return R('OK_MFA_SUCCESS');
}
sub can_use_utf8 {
# only use UTF-8 if allowed in the config, if user LANG seems to support it, and if TERM is defined and not dumb
return (OVH::Bastion::config('allowUTF8')->value && $ENV{'LANG'} && ($ENV{'LANG'} =~ /utf-?8/i) && $ENV{'TERM'} && $ENV{'TERM'} !~ /dumb|unknown/i);
}
1;

View file

@ -226,7 +226,7 @@ sub load_configuration {
options => [
qw{
enableSyslog enableGlobalAccessLog enableAccountAccessLog enableGlobalSqlLog enableAccountSqlLog displayLastLogin
interactiveModeByDefault
interactiveModeByDefault allowUTF8
}
],
},