| 1 |
package Lemonldap::NG::Manager::Cli; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Crypt::URandom; |
| 5 |
use Mouse; |
| 6 |
use Data::Dumper; |
| 7 |
use JSON; |
| 8 |
use Hash::Merge::Simple; |
| 9 |
use Getopt::Long qw(GetOptionsFromArray); |
| 10 |
use Lemonldap::NG::Common::Conf::ReConstants; |
| 11 |
|
| 12 |
use constant booleanOptions => qr/^(?:json)$/; |
| 13 |
|
| 14 |
our $VERSION = '2.23.0'; |
| 15 |
$Data::Dumper::Useperl = 1; |
| 16 |
|
| 17 |
extends('Lemonldap::NG::Manager::Cli::Lib'); |
| 18 |
|
| 19 |
has cfgNum => ( |
| 20 |
is => 'rw', |
| 21 |
isa => 'Int', |
| 22 |
trigger => sub { |
| 23 |
$_[0]->{req} = |
| 24 |
Lemonldap::NG::Manager::Cli::Request->new( |
| 25 |
cfgNum => $_[0]->{cfgNum} ); |
| 26 |
} |
| 27 |
); |
| 28 |
|
| 29 |
has log => ( is => 'rw' ); |
| 30 |
has req => ( is => 'ro' ); |
| 31 |
has sep => ( is => 'rw', isa => 'Str', default => '/' ); |
| 32 |
has format => ( is => 'rw', isa => 'Str', default => "%-25s | %-25s | %-25s" ); |
| 33 |
has yes => ( is => 'rw', isa => 'Bool', default => 0 ); |
| 34 |
has safe => ( is => 'rw', isa => 'Bool' ); |
| 35 |
has unsafe => ( is => 'rw', isa => 'Bool', default => 0 ); |
| 36 |
has force => ( is => 'rw', isa => 'Bool', default => 0 ); |
| 37 |
has logger => ( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr->logger } ); |
| 38 |
has userLogger => |
| 39 |
( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr->userLogger } ); |
| 40 |
has localConf => ( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr } ); |
| 41 |
has json => ( is => 'rw' ); |
| 42 |
has jsonConverter => ( |
| 43 |
is => 'rw', |
| 44 |
default => sub { JSON->new->allow_nonref->canonical->pretty }, |
| 45 |
); |
| 46 |
|
| 47 |
sub get { |
| 48 |
my ( $self, @keys ) = @_; |
| 49 |
my $res = {}; |
| 50 |
die 'get requires at least one key' unless (@keys); |
| 51 |
L: foreach my $key (@keys) { |
| 52 |
my $value = $self->_getKey($key); |
| 53 |
if ( $self->json ) { |
| 54 |
$res->{$key} = $value; |
| 55 |
next; |
| 56 |
} |
| 57 |
if ( ref $value eq 'HASH' ) { |
| 58 |
print "$key has the following keys:\n"; |
| 59 |
print " $_\n" foreach ( sort keys %$value ); |
| 60 |
} |
| 61 |
elsif ( ref $value eq 'ARRAY' ) { |
| 62 |
print "$key is an array with values:\n"; |
| 63 |
foreach my $avalue (@$value) { |
| 64 |
if ( ref $avalue eq 'HASH' ) { |
| 65 |
print "\tHash with following keys:\n"; |
| 66 |
print "\t\t$_\n" foreach ( sort keys %$avalue ); |
| 67 |
} |
| 68 |
elsif ( ref $value eq 'ARRAY' ) { |
| 69 |
print "\tArray with following keys:\n"; |
| 70 |
print "\t\t$_\n" foreach (@$avalue); |
| 71 |
} |
| 72 |
else { |
| 73 |
$avalue //= ''; |
| 74 |
print "\tValue = $avalue\n"; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
$value //= ''; |
| 80 |
print "$key = $value\n"; |
| 81 |
} |
| 82 |
} |
| 83 |
if ( @keys == 1 ) { |
| 84 |
$res = $res->{ $keys[0] }; |
| 85 |
} |
| 86 |
print $self->jsonConverter->encode($res) if $self->json; |
| 87 |
} |
| 88 |
|
| 89 |
sub set { |
| 90 |
my ( $self, %pairs ) = @_; |
| 91 |
my $format = $self->format . "\n"; |
| 92 |
die 'set requires at least one key and one value' unless (%pairs); |
| 93 |
my @list; |
| 94 |
foreach my $key ( keys %pairs ) { |
| 95 |
my $oldValue = $self->_getKey($key); |
| 96 |
my $value = $pairs{$key}; |
| 97 |
if ( ref $oldValue ) { |
| 98 |
if ( $self->json ) { |
| 99 |
$value = eval { $self->jsonConverter->decode( $pairs{$key} ) }; |
| 100 |
die "Bad value $pairs{$key}: $@" if $@; |
| 101 |
$oldValue = $self->jsonConverter->encode($oldValue); |
| 102 |
} |
| 103 |
else { |
| 104 |
die "$key seems to be a hash, modification refused"; |
| 105 |
} |
| 106 |
} |
| 107 |
elsif ( $pairs{$key} =~ /^\s*[\{\[]/ ) { |
| 108 |
$value = eval { $self->jsonConverter->decode( $pairs{$key} ) }; |
| 109 |
if ($@) { |
| 110 |
warn "Unable to decode: $pairs{$key}"; |
| 111 |
$value = $pairs{$key}; |
| 112 |
} |
| 113 |
} |
| 114 |
$oldValue //= ''; |
| 115 |
$self->logger->info("CLI: Set key $key with $pairs{$key}"); |
| 116 |
push @list, [ $key, $oldValue, $pairs{$key}, $value ]; |
| 117 |
} |
| 118 |
unless ( $self->yes ) { |
| 119 |
print "Proposed changes:\n"; |
| 120 |
printf $format, 'Key', 'Old value', 'New value'; |
| 121 |
foreach (@list) { |
| 122 |
printf $format, @$_; |
| 123 |
} |
| 124 |
print "Confirm (N/y)? "; |
| 125 |
my $c = <STDIN>; |
| 126 |
unless ( $c =~ /^y(?:es)?$/ ) { |
| 127 |
die "Aborting"; |
| 128 |
} |
| 129 |
} |
| 130 |
require Clone; |
| 131 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 132 |
foreach (@list) { |
| 133 |
$self->_setKey( $new, $_->[0], $_->[3] ); |
| 134 |
} |
| 135 |
return $self->_save($new); |
| 136 |
} |
| 137 |
|
| 138 |
sub del { |
| 139 |
my ( $self, @keys ) = @_; |
| 140 |
die 'del requires at least one key' unless (@keys); |
| 141 |
my $oldValues = {}; |
| 142 |
foreach my $key (@keys) { |
| 143 |
my $value = $self->_getKey($key); |
| 144 |
if ( ref $value eq 'HASH' ) { |
| 145 |
print STDERR "$key seems to be a hash, delete refused\n"; |
| 146 |
next; |
| 147 |
} |
| 148 |
unless ( defined $value ) { |
| 149 |
print STDERR "$key does not exists, skip it\n"; |
| 150 |
next; |
| 151 |
} |
| 152 |
$oldValues->{$key} = $value; |
| 153 |
} |
| 154 |
return unless keys %$oldValues; |
| 155 |
unless ( $self->yes ) { |
| 156 |
print "Proposed changes:\n"; |
| 157 |
printf "%-25s | %-25s\n", 'Key', 'Old value'; |
| 158 |
foreach ( keys %$oldValues ) { |
| 159 |
printf "%-25s | %-25s\n", $_, $oldValues->{$_}; |
| 160 |
} |
| 161 |
print "Confirm (N/y)? "; |
| 162 |
my $c = <STDIN>; |
| 163 |
unless ( $c =~ /^y(?:es)?$/ ) { |
| 164 |
die "Aborting"; |
| 165 |
} |
| 166 |
} |
| 167 |
require Clone; |
| 168 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 169 |
foreach ( keys %$oldValues ) { |
| 170 |
delete $new->{$_}; |
| 171 |
} |
| 172 |
return $self->_save($new); |
| 173 |
} |
| 174 |
|
| 175 |
sub merge { |
| 176 |
my ( $self, @files ) = @_; |
| 177 |
die 'merge requires at least one file' unless (@files); |
| 178 |
require Clone; |
| 179 |
my $old = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 180 |
my $new; |
| 181 |
my @valid_files; |
| 182 |
|
| 183 |
foreach my $file (@files) { |
| 184 |
|
| 185 |
my $merging; |
| 186 |
if ( $file =~ /\.ya?ml/ ) { |
| 187 |
eval { |
| 188 |
require YAML; |
| 189 |
$merging = YAML::LoadFile($file); |
| 190 |
}; |
| 191 |
if ($@) { |
| 192 |
print STDERR "Skipping invalid YAML file $file: " . $@; |
| 193 |
} |
| 194 |
} |
| 195 |
else { |
| 196 |
eval { |
| 197 |
local $/ = undef; |
| 198 |
open my $fh, '<', $file or die $!; |
| 199 |
$merging = from_json(<$fh>); |
| 200 |
}; |
| 201 |
if ($@) { |
| 202 |
print STDERR "Skipping invalid JSON file $file: " . $@; |
| 203 |
} |
| 204 |
} |
| 205 |
if ( ref($merging) eq "HASH" ) { |
| 206 |
$new = Hash::Merge::Simple::merge( $new, $merging ); |
| 207 |
push @valid_files, $file; |
| 208 |
} |
| 209 |
} |
| 210 |
die "Nothing to do" unless ref($new) eq "HASH"; |
| 211 |
|
| 212 |
unless ( $self->yes ) { |
| 213 |
print "Merge configuration changes from " |
| 214 |
. join( " ", @valid_files ) . " \n"; |
| 215 |
print Dumper($new); |
| 216 |
print "Confirm (N/y)? "; |
| 217 |
my $c = <STDIN>; |
| 218 |
unless ( $c =~ /^y(?:es)?$/ ) { |
| 219 |
die "Aborting"; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$new = Hash::Merge::Simple::merge( $old, $new ); |
| 224 |
_clean_hash_undef($new); |
| 225 |
return $self->_save($new); |
| 226 |
} |
| 227 |
|
| 228 |
# Remove key => undef from hashes |
| 229 |
# This allows you to remove keys from the config |
| 230 |
sub _clean_hash_undef { |
| 231 |
my ($hash) = @_; |
| 232 |
for my $key ( keys %$hash ) { |
| 233 |
if ( !defined( $hash->{$key} ) ) { |
| 234 |
delete $hash->{$key}; |
| 235 |
} |
| 236 |
if ( ref( $hash->{$key} ) eq "HASH" ) { |
| 237 |
_clean_hash_undef( $hash->{$key} ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
sub addKey { |
| 243 |
my $self = shift; |
| 244 |
unless ( @_ % 3 == 0 ) { |
| 245 |
die 'usage: "addKey (?:rootKey newKey newValue)+'; |
| 246 |
} |
| 247 |
my $sep = $self->sep; |
| 248 |
my @list; |
| 249 |
while (@_) { |
| 250 |
my $root = shift; |
| 251 |
my $newKey = shift; |
| 252 |
my $value = shift; |
| 253 |
unless ( $root =~ /$simpleHashKeys$/ or $root =~ /$sep/ ) { |
| 254 |
die "$root is not a simple hash. Aborting"; |
| 255 |
} |
| 256 |
$self->logger->info("CLI: Append key $root/$newKey $value"); |
| 257 |
push @list, [ $root, $newKey, $value ]; |
| 258 |
} |
| 259 |
require Clone; |
| 260 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 261 |
foreach my $el (@list) { |
| 262 |
my @path = split $sep, $el->[0]; |
| 263 |
if ( $#path == 0 ) { |
| 264 |
$new->{ $path[0] }->{ $el->[1] } = $el->[2]; |
| 265 |
} |
| 266 |
elsif ( $#path == 1 ) { |
| 267 |
$new->{ $path[0] }->{ $path[1] }->{ $el->[1] } = $el->[2]; |
| 268 |
} |
| 269 |
elsif ( $#path == 2 ) { |
| 270 |
$new->{ $path[0] }->{ $path[1] }->{ $path[2] }->{ $el->[1] } = |
| 271 |
$el->[2]; |
| 272 |
} |
| 273 |
elsif ( $#path == 3 ) { |
| 274 |
$new->{ $path[0] }->{ $path[1] }->{ $path[2] }->{ $path[3] } |
| 275 |
->{ $el->[1] } = $el->[2]; |
| 276 |
} |
| 277 |
else { |
| 278 |
die $el->[0] . " has too many levels. Aborting"; |
| 279 |
} |
| 280 |
} |
| 281 |
return $self->_save($new); |
| 282 |
} |
| 283 |
|
| 284 |
sub delKey { |
| 285 |
my $self = shift; |
| 286 |
unless ( @_ % 2 == 0 ) { |
| 287 |
die 'usage: "delKey (?:rootKey key)+'; |
| 288 |
} |
| 289 |
my $sep = $self->sep; |
| 290 |
my @list; |
| 291 |
while (@_) { |
| 292 |
my $root = shift; |
| 293 |
my $key = shift; |
| 294 |
unless ( $root =~ /$simpleHashKeys$/ or $root =~ /$sep/ ) { |
| 295 |
die "$root is not a simple hash. Aborting"; |
| 296 |
} |
| 297 |
$self->logger->info("CLI: Remove key $root/$key"); |
| 298 |
push @list, [ $root, $key ]; |
| 299 |
} |
| 300 |
require Clone; |
| 301 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 302 |
foreach my $el (@list) { |
| 303 |
my @path = split $sep, $el->[0]; |
| 304 |
if ( $#path == 0 ) { |
| 305 |
if ( exists $new->{ $path[0] } |
| 306 |
&& exists $new->{ $path[0] }->{ $el->[1] } ) |
| 307 |
{ |
| 308 |
delete $new->{ $path[0] }->{ $el->[1] } |
| 309 |
if exists $new->{ $path[0] }->{ $el->[1] }; |
| 310 |
} |
| 311 |
} |
| 312 |
elsif ( $#path == 1 ) { |
| 313 |
if ( exists $new->{ $path[0] } |
| 314 |
&& exists $new->{ $path[0] }->{ $path[1] } |
| 315 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $el->[1] } ) |
| 316 |
{ |
| 317 |
delete $new->{ $path[0] }->{ $path[1] }->{ $el->[1] }; |
| 318 |
} |
| 319 |
} |
| 320 |
elsif ( $#path == 2 ) { |
| 321 |
if ( exists $new->{ $path[0] } |
| 322 |
&& exists $new->{ $path[0] }->{ $path[1] } |
| 323 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 324 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 325 |
->{ $el->[1] } ) |
| 326 |
{ |
| 327 |
delete $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 328 |
->{ $el->[1] }; |
| 329 |
} |
| 330 |
} |
| 331 |
elsif ( $#path == 3 ) { |
| 332 |
if ( exists $new->{ $path[0] } |
| 333 |
&& exists $new->{ $path[0] }->{ $path[1] } |
| 334 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 335 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 336 |
->{ $path[3] } |
| 337 |
&& exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 338 |
->{ $path[3] }->{ $el->[1] } ) |
| 339 |
{ |
| 340 |
delete $new->{ $path[0] }->{ $path[1] }->{ $path[2] } |
| 341 |
->{ $path[3] }->{ $el->[1] }; |
| 342 |
} |
| 343 |
} |
| 344 |
else { |
| 345 |
die $el->[0] . " has too many levels. Aborting"; |
| 346 |
} |
| 347 |
} |
| 348 |
return $self->_save($new); |
| 349 |
} |
| 350 |
|
| 351 |
sub addPostVars { |
| 352 |
my $self = shift; |
| 353 |
unless ( @_ % 4 == 0 ) { |
| 354 |
die 'usage: "addPostVars (?:vhost uri key value)+'; |
| 355 |
} |
| 356 |
my @list; |
| 357 |
while (@_) { |
| 358 |
my $vhost = shift; |
| 359 |
my $uri = shift; |
| 360 |
my $key = shift; |
| 361 |
my $value = shift; |
| 362 |
$self->logger->info( |
| 363 |
"CLI: Append post vars $key $value to URI $uri for vhost $vhost"); |
| 364 |
push @list, [ $vhost, $uri, $key, $value ]; |
| 365 |
} |
| 366 |
require Clone; |
| 367 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 368 |
foreach my $el (@list) { |
| 369 |
$new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} = [] |
| 370 |
unless ( defined $new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} ); |
| 371 |
push( |
| 372 |
@{ $new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} }, |
| 373 |
[ $el->[2], $el->[3] ] |
| 374 |
); |
| 375 |
} |
| 376 |
return $self->_save($new); |
| 377 |
} |
| 378 |
|
| 379 |
sub delPostVars { |
| 380 |
my $self = shift; |
| 381 |
unless ( @_ % 3 == 0 ) { |
| 382 |
die 'usage: "delPostVars (?:vhost uri key)+'; |
| 383 |
} |
| 384 |
my @list; |
| 385 |
while (@_) { |
| 386 |
my $vhost = shift; |
| 387 |
my $uri = shift; |
| 388 |
my $key = shift; |
| 389 |
$self->logger->info( |
| 390 |
"CLI: Delete post vars $key from URI $uri for vhost $vhost"); |
| 391 |
push @list, [ $vhost, $uri, $key ]; |
| 392 |
} |
| 393 |
require Clone; |
| 394 |
my $new = Clone::clone( $self->mgr->hLoadedPlugins->{conf}->currentConf ); |
| 395 |
foreach my $el (@list) { |
| 396 |
$new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} = [] |
| 397 |
unless ( defined $new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} ); |
| 398 |
for ( |
| 399 |
my $i = 0 ; |
| 400 |
$i <= $#{ $new->{post}->{ $el->[0] }->{ $el->[1] }->{vars} } ; |
| 401 |
$i++ |
| 402 |
) |
| 403 |
{ |
| 404 |
delete( $new->{post}->{ $el->[0] }->{ $el->[1] }->{vars}->[$i] ) |
| 405 |
if ( |
| 406 |
$new->{post}->{ $el->[0] }->{ $el->[1] }->{vars}->[$i]->[0] eq |
| 407 |
$el->[2] ); |
| 408 |
} |
| 409 |
} |
| 410 |
return $self->_save($new); |
| 411 |
} |
| 412 |
|
| 413 |
sub lastCfg { |
| 414 |
my ($self) = @_; |
| 415 |
$self->logger->info("CLI: Retrieve last conf."); |
| 416 |
return $self->jsonResponse('/confs/latest')->{cfgNum}; |
| 417 |
} |
| 418 |
|
| 419 |
sub save { |
| 420 |
my ($self) = @_; |
| 421 |
my $conf = $self->jsonResponse( '/confs/' . $self->cfgNum, 'full=1' ); |
| 422 |
print $self->jsonConverter->encode($conf); |
| 423 |
} |
| 424 |
|
| 425 |
sub restore { |
| 426 |
my ( $self, $file ) = @_; |
| 427 |
unless ($file) { |
| 428 |
die "No file provided. Aborting"; |
| 429 |
} |
| 430 |
require IO::String; |
| 431 |
my $conf; |
| 432 |
if ( $file eq '-' ) { |
| 433 |
$conf = join '', <STDIN>; |
| 434 |
} |
| 435 |
else { |
| 436 |
open( my $f, '<', $file ) or die "Unable to read $file: $!"; |
| 437 |
$conf = join '', <$f>; |
| 438 |
close $f; |
| 439 |
die "Empty or malformed file $file" unless ( $conf =~ /\w/s ); |
| 440 |
} |
| 441 |
$self->logger->info("CLI: Restore conf."); |
| 442 |
my $res = $self->_post( '/confs/raw', '', IO::String->new($conf), |
| 443 |
'application/json', length($conf) ); |
| 444 |
|
| 445 |
my $response = eval { from_json( $res->[2]->[0], { allow_nonref => 1 } ) }; |
| 446 |
if ( $res->[0] == 200 and $response->{result} == 1 ) { |
| 447 |
print STDERR |
| 448 |
"Configuration has been restored as version $response->{cfgNum}\n"; |
| 449 |
|
| 450 |
} |
| 451 |
else { |
| 452 |
if ( my $message = $response->{error} ) { |
| 453 |
print STDERR "Failed to restore configuration: $message\n"; |
| 454 |
} |
| 455 |
else { |
| 456 |
print STDERR "The supplied configuration could not be restored" |
| 457 |
. " because it contains errors:\n"; |
| 458 |
|
| 459 |
my @errors = eval { |
| 460 |
map { $_->{message} } @{ $response->{details}->{__errors__} }; |
| 461 |
}; |
| 462 |
if (@errors) { |
| 463 |
print STDERR " - $_\n" for @errors; |
| 464 |
} |
| 465 |
else { |
| 466 |
use Data::Dumper; |
| 467 |
print Dumper($res); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
sub rollback { |
| 474 |
my ($self) = @_; |
| 475 |
my $lastCfg = $self->mgr->confAcc->lastCfg; |
| 476 |
my $previousCfg = $lastCfg - 1; |
| 477 |
my $conf = |
| 478 |
$self->mgr->confAcc->getConf( { cfgNum => $previousCfg, raw => 1 } ) |
| 479 |
or die $Lemonldap::NG::Common::Conf::msg; |
| 480 |
|
| 481 |
$conf->{cfgNum} = $lastCfg; |
| 482 |
$conf->{cfgAuthor} = &_user . ' (command-line-interface)'; |
| 483 |
$conf->{cfgAuthorIP} = '127.0.0.1'; |
| 484 |
$conf->{cfgDate} = time; |
| 485 |
$conf->{cfgVersion} = $Lemonldap::NG::Manager::VERSION; |
| 486 |
$conf->{cfgLog} = $self->log // "Rolled back configuration $lastCfg"; |
| 487 |
|
| 488 |
my $s = $self->mgr->confAcc->saveConf($conf); |
| 489 |
if ( $s > 0 ) { |
| 490 |
$self->logger->info("CLI: Configuration $lastCfg has been rolled back"); |
| 491 |
print STDERR "Configuration $lastCfg has been rolled back\n"; |
| 492 |
} |
| 493 |
else { |
| 494 |
$self->logger->error("CLI: Failed to rollback configuration $lastCfg"); |
| 495 |
print STDERR "Failed to rollback configuration $lastCfg\n"; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
sub purge { |
| 500 |
my ( $self, @args ) = @_; |
| 501 |
|
| 502 |
my %opts; |
| 503 |
GetOptionsFromArray( \@args, \%opts, "keep-last=i", "keep-recent=i" ); |
| 504 |
|
| 505 |
unless ( $opts{'keep-last'} xor $opts{'keep-recent'} ) { |
| 506 |
die |
| 507 |
'usage: purge --keep-last <value> | --keep-recent <number of seconds to keep>'; |
| 508 |
} |
| 509 |
my @configs = $self->mgr->confAcc->available(); |
| 510 |
my $n_config = scalar @configs; |
| 511 |
|
| 512 |
if ( $opts{'keep-last'} ) { |
| 513 |
if ( $n_config < $opts{'keep-last'} ) { |
| 514 |
$self->logger->warn( |
| 515 |
"The number of configs to keep ($opts{'keep-last'}) is more than the number of available configs ($n_config)" |
| 516 |
); |
| 517 |
} |
| 518 |
|
| 519 |
my @to_delete = @configs[ 0 .. $#configs - $opts{'keep-last'} ]; |
| 520 |
if ( scalar @to_delete == $n_config ) { |
| 521 |
die 'This would delete all configs'; |
| 522 |
} |
| 523 |
_config_delete( $self, @to_delete ); |
| 524 |
} |
| 525 |
if ( $opts{'keep-recent'} ) { |
| 526 |
|
| 527 |
# The keep-recent argument is a relative number of seconds to keep. Compute an absolute offset to keep |
| 528 |
my $absoluteOffset = time() - $opts{'keep-recent'}; |
| 529 |
my @to_delete = grep { |
| 530 |
my $r = $self->mgr->confAcc->getConf( { cfgNum => $_ } ); |
| 531 |
$r && ref $r && $r->{cfgDate} < $absoluteOffset |
| 532 |
} @configs; |
| 533 |
if ( scalar @to_delete == $n_config ) { |
| 534 |
die 'This would delete all configs'; |
| 535 |
} |
| 536 |
_config_delete( $self, @to_delete ); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
sub _config_delete { |
| 541 |
my ( $self, @to_delete ) = @_; |
| 542 |
unless ( $self->yes ) { |
| 543 |
print "Configs that will be deleted: " |
| 544 |
. _format_to_delete(@to_delete) . "\n"; |
| 545 |
print "Confirm (N/y)? "; |
| 546 |
my $c = <STDIN>; |
| 547 |
unless ( $c =~ /^y(?:es)?$/ ) { |
| 548 |
die "Aborting"; |
| 549 |
} |
| 550 |
} |
| 551 |
for my $config_id (@to_delete) { |
| 552 |
$self->mgr->confAcc->delete($config_id); |
| 553 |
} |
| 554 |
$self->logger->info( "CLI: Deleted " . scalar @to_delete . " configs" ); |
| 555 |
print STDERR "Deleted " . scalar(@to_delete) . " configs\n"; |
| 556 |
} |
| 557 |
|
| 558 |
sub _format_to_delete { |
| 559 |
my (@to_delete) = (@_); |
| 560 |
if ( @to_delete > 100 ) { |
| 561 |
my @sorted = sort { $a <=> $b } @to_delete; |
| 562 |
my $min = join( ", ", @sorted[ 0 .. 4 ] ); |
| 563 |
my $max = join( ", ", @sorted[ -5 .. -1 ] ); |
| 564 |
my $inter = @to_delete - 10; |
| 565 |
return "$min .. [ $inter more ] .. $max"; |
| 566 |
} |
| 567 |
else { |
| 568 |
return join( ", ", @to_delete ); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
sub _getKey { |
| 573 |
my ( $self, $key ) = @_; |
| 574 |
my $sep = $self->sep; |
| 575 |
my ( $base, @path ) = split $sep, $key; |
| 576 |
unless ( $base =~ /^\w+$/ ) { |
| 577 |
warn "Malformed key $base"; |
| 578 |
return (); |
| 579 |
} |
| 580 |
my $value = $self->mgr->hLoadedPlugins->{conf} |
| 581 |
->getConfKey( $self->req, $base, noCache => 1 ); |
| 582 |
if ( $self->req->error ) { |
| 583 |
die $self->req->error; |
| 584 |
} |
| 585 |
if ( ref $value eq 'HASH' ) { |
| 586 |
while ( my $next = shift @path ) { |
| 587 |
unless ( exists $value->{$next} ) { |
| 588 |
warn "Unknown subkey $next for $key"; |
| 589 |
next L; |
| 590 |
} |
| 591 |
$value = $value->{$next}; |
| 592 |
} |
| 593 |
} |
| 594 |
elsif (@path) { |
| 595 |
warn "No subkeys for $base"; |
| 596 |
return (); |
| 597 |
} |
| 598 |
return $value; |
| 599 |
} |
| 600 |
|
| 601 |
sub _setKey { |
| 602 |
my ( $self, $conf, $key, $value ) = @_; |
| 603 |
my $sep = $self->sep; |
| 604 |
my (@path) = split $sep, $key; |
| 605 |
my $last = pop @path; |
| 606 |
while ( my $next = shift @path ) { |
| 607 |
$conf = $conf->{$next}; |
| 608 |
} |
| 609 |
$conf->{$last} = $value; |
| 610 |
} |
| 611 |
|
| 612 |
sub _save { |
| 613 |
my ( $self, $new ) = @_; |
| 614 |
require Lemonldap::NG::Manager::Conf::Parser; |
| 615 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( { |
| 616 |
newConf => $new, |
| 617 |
refConf => $self->mgr->hLoadedPlugins->{conf}->currentConf, |
| 618 |
req => $self->req |
| 619 |
} |
| 620 |
); |
| 621 |
unless ( $parser->testNewConf( $self->localConf ) ) { |
| 622 |
my $msg = "Configuration rejected with message: " . $parser->message; |
| 623 |
$self->logger->error("CLI: $msg"); |
| 624 |
unless ( $self->unsafe or ( defined $self->safe and !$self->safe ) ) { |
| 625 |
die "$msg"; |
| 626 |
} |
| 627 |
else { |
| 628 |
print STDERR "$msg\n"; |
| 629 |
} |
| 630 |
} |
| 631 |
my $saveParams = { force => $self->force }; |
| 632 |
if ( $self->force and $self->cfgNum ) { |
| 633 |
$self->logger->debug( "CLI: cfgNum forced with " . $self->cfgNum ); |
| 634 |
print STDERR "cfgNum forced with ", $self->cfgNum; |
| 635 |
$saveParams->{cfgNum} = $self->cfgNum; |
| 636 |
$saveParams->{cfgNumFixed} = 1; |
| 637 |
} |
| 638 |
$new->{cfgAuthor} = &_user . ' (command-line-interface)'; |
| 639 |
$new->{cfgAuthorIP} = '127.0.0.1'; |
| 640 |
$new->{cfgDate} = time; |
| 641 |
$new->{cfgVersion} = $Lemonldap::NG::Manager::VERSION; |
| 642 |
$new->{cfgLog} = $self->log // 'Modified with LL::NG CLI'; |
| 643 |
$new->{key} ||= join( '', |
| 644 |
map { chr( int( ord( Crypt::URandom::urandom(1) ) * 94 / 256 ) + 33 ) } |
| 645 |
( 1 .. 16 ) ); |
| 646 |
|
| 647 |
my $s = $self->mgr->confAcc->saveConf( $new, %$saveParams ); |
| 648 |
if ( $s > 0 ) { |
| 649 |
$self->logger->debug( |
| 650 |
"CLI: Configuration $s has been saved by $new->{cfgAuthor}"); |
| 651 |
$self->logger->info("CLI: Configuration $s saved"); |
| 652 |
print STDERR "Saved under number $s\n"; |
| 653 |
$parser->{status} = |
| 654 |
[ $self->mgr->hLoadedPlugins->{conf}->applyConf($new) ]; |
| 655 |
} |
| 656 |
else { |
| 657 |
$self->logger->error("CLI: Configuration not saved!"); |
| 658 |
printf STDERR "Could not save configuration:"; |
| 659 |
printf STDERR $Lemonldap::NG::Common::Conf::msg; |
| 660 |
printf STDERR "Modifications rejected: %s:\n", $parser->{message} |
| 661 |
if $parser->{message}; |
| 662 |
} |
| 663 |
|
| 664 |
# Open "en" lang file to get default messages |
| 665 |
my $langFile = $self->mgr->templateDir . "/languages/en.json"; |
| 666 |
$langFile =~ s/templates/static/; |
| 667 |
my $langMessages; |
| 668 |
if ( open my $_json, "<", $langFile ) { |
| 669 |
local $/ = undef; |
| 670 |
$langMessages = $self->jsonConverter->decode(<$_json>); |
| 671 |
} |
| 672 |
|
| 673 |
# Display result |
| 674 |
foreach (qw(errors warnings status)) { |
| 675 |
if ( $parser->{$_} and @{ $parser->{$_} } ) { |
| 676 |
my $s = Dumper( $parser->{$_} ); |
| 677 |
$s =~ s/\$VAR1\s*=\s*//; |
| 678 |
$s =~ s/__(\w+)__/$langMessages->{$1}/ if ( defined $langMessages ); |
| 679 |
printf STDERR "%-8s: %s", ucfirst($_), $s; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
sub run { |
| 685 |
my $self = shift; |
| 686 |
|
| 687 |
# Options simply call corresponding accessor |
| 688 |
my $args = {}; |
| 689 |
while ( $_[0] =~ s/^--?// ) { |
| 690 |
my $k = shift; |
| 691 |
my $v = $k =~ $self->booleanOptions ? 1 : shift; |
| 692 |
if ( ref $self ) { |
| 693 |
eval { $self->$k($v) }; |
| 694 |
if ($@) { |
| 695 |
die "Unknown option -$k or bad value ($@)"; |
| 696 |
} |
| 697 |
} |
| 698 |
else { |
| 699 |
$args->{$k} = $v; |
| 700 |
} |
| 701 |
} |
| 702 |
unless ( ref $self ) { |
| 703 |
$self = $self->new($args); |
| 704 |
} |
| 705 |
unless (@_) { |
| 706 |
die 'nothing to do, aborting'; |
| 707 |
} |
| 708 |
my $action = shift; |
| 709 |
unless ( $action =~ |
| 710 |
/^(?:get|set|del|addKey|delKey|addPostVars|delPostVars|merge|save|restore|rollback|purge)$/ |
| 711 |
) |
| 712 |
{ |
| 713 |
die |
| 714 |
"Unknown action $action. Only get, set, del, addKey, delKey, addPostVars, delPostVars, merge, save, restore, rollback, purge allowed"; |
| 715 |
} |
| 716 |
|
| 717 |
unless ( $action eq "restore" ) { |
| 718 |
|
| 719 |
# This step prevents restoring when config DB is empty (#2340) |
| 720 |
$self->cfgNum( $self->lastCfg ) unless ( $self->cfgNum ); |
| 721 |
} |
| 722 |
|
| 723 |
$self->$action(@_); |
| 724 |
} |
| 725 |
|
| 726 |
sub _user { |
| 727 |
my $sysUser = scalar( getpwuid $< ); |
| 728 |
chomp $sysUser; |
| 729 |
my $envUser; |
| 730 |
foreach (qw(REMOTE_USER SUDO_USER USER)) { |
| 731 |
( $envUser = $ENV{$_} ) and last; |
| 732 |
} |
| 733 |
$envUser = undef unless $envUser =~ /\w/; |
| 734 |
if ($sysUser) { |
| 735 |
$sysUser .= " [$envUser] " if $envUser; |
| 736 |
} |
| 737 |
else { |
| 738 |
$sysUser = $envUser || 'Unknown'; |
| 739 |
} |
| 740 |
return $sysUser; |
| 741 |
} |
| 742 |
|
| 743 |
package Lemonldap::NG::Manager::Cli::Request; |
| 744 |
|
| 745 |
use Mouse; |
| 746 |
|
| 747 |
has cfgNum => ( is => 'rw' ); |
| 748 |
has error => ( is => 'rw' ); |
| 749 |
|
| 750 |
sub params { |
| 751 |
my ( $self, $key ) = @_; |
| 752 |
return $self->{$key}; |
| 753 |
} |
| 754 |
|
| 755 |
1; |
| 756 |
__END__ |
| 757 |
|
| 758 |
=head1 NAME |
| 759 |
|
| 760 |
=encoding utf8 |
| 761 |
|
| 762 |
Lemonldap::NG::Manager::Cli - Command line manager for Lemonldap::NG web SSO |
| 763 |
system. |
| 764 |
|
| 765 |
=head1 SYNOPSIS |
| 766 |
|
| 767 |
#!/usr/bin/env perl |
| 768 |
|
| 769 |
use warnings; |
| 770 |
use strict; |
| 771 |
use Lemonldap::NG::Manager::Cli; |
| 772 |
|
| 773 |
# Optional: you can specify here some parameters |
| 774 |
my $cli = Lemonldap::NG::Manager::Cli->new(iniFile=>'t/lemonldap-ng.ini'); |
| 775 |
|
| 776 |
$cli->run(@ARGV); |
| 777 |
|
| 778 |
or use llng-manager-cli provides with this package. |
| 779 |
|
| 780 |
llng-manager-cli <options> <command> <keys> |
| 781 |
|
| 782 |
=head1 DESCRIPTION |
| 783 |
|
| 784 |
Lemonldap::NG::Manager provides a web interface to manage Lemonldap::NG Web-SSO |
| 785 |
system. |
| 786 |
|
| 787 |
Lemonldap::NG Manager::Cli provides a command line client to read or modify |
| 788 |
configuration. |
| 789 |
|
| 790 |
=head1 METHODS |
| 791 |
|
| 792 |
=head2 ACCESSORS |
| 793 |
|
| 794 |
All accessors can be set using the command line: just set a '-' before their |
| 795 |
names. Example |
| 796 |
|
| 797 |
llng-manager-cli -sep ',' get macros,_whatToTrace |
| 798 |
|
| 799 |
=head3 iniFile() |
| 800 |
|
| 801 |
The lemonldap-ng.ini file to use is not default value. |
| 802 |
|
| 803 |
=head3 sep() |
| 804 |
|
| 805 |
The key separator, default to '/'. For example to read the value of macro |
| 806 |
_whatToTrace using ',', use: |
| 807 |
|
| 808 |
llng-manager-cli -sep ',' get macros,_whatToTrace |
| 809 |
|
| 810 |
=head3 cfgNum() |
| 811 |
|
| 812 |
The configuration number. If not set, it will use the latest configuration. |
| 813 |
|
| 814 |
=head3 yes() |
| 815 |
|
| 816 |
If set to 1, no confirmation is asked to save new values: |
| 817 |
|
| 818 |
llng-manager -yes 1 set portal http://somewhere/ |
| 819 |
|
| 820 |
=head3 force() |
| 821 |
|
| 822 |
Set it to 1 to save a configuration earlier than latest |
| 823 |
|
| 824 |
=head3 format() |
| 825 |
|
| 826 |
Confirmation array line format. Default to "%-25s | %-25s | %-25s" |
| 827 |
|
| 828 |
=head3 log() |
| 829 |
|
| 830 |
String to insert in configuration log field (cfgLog) |
| 831 |
|
| 832 |
=head2 run() |
| 833 |
|
| 834 |
The main method: it reads option, command and launch the corresponding |
| 835 |
subroutine. |
| 836 |
|
| 837 |
=head3 Commands |
| 838 |
|
| 839 |
=head4 get |
| 840 |
|
| 841 |
Using get, you can read several keys. Example: |
| 842 |
|
| 843 |
llng-manager-cli get portal cookieName domain |
| 844 |
|
| 845 |
=head4 purge |
| 846 |
|
| 847 |
Use purge to delete configurations |
| 848 |
|
| 849 |
=over |
| 850 |
|
| 851 |
=item * The "B<C<purge --keep-last E<lt>NE<gt>>>" command keeps only the Nth latest configurations. |
| 852 |
|
| 853 |
=item * The "B<C<purge --keep-recent E<lt>NE<gt>>>" command keeps only the configurations that have been created less than N seconds ago. |
| 854 |
|
| 855 |
=back |
| 856 |
|
| 857 |
=head1 SEE ALSO |
| 858 |
|
| 859 |
For other features of llng-cli, see L<Lemonldap::NG::Common::Cli> |
| 860 |
|
| 861 |
Other links: L<Lemonldap::NG::Manager>, L<http://lemonldap-ng.org/> |
| 862 |
|
| 863 |
=head1 AUTHORS |
| 864 |
|
| 865 |
=over |
| 866 |
|
| 867 |
=item Original idea from David Delassus in 2012 |
| 868 |
|
| 869 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 870 |
|
| 871 |
=back |
| 872 |
|
| 873 |
=head1 BUG REPORT |
| 874 |
|
| 875 |
Use OW2 system to report bug or ask for features: |
| 876 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 877 |
|
| 878 |
=head1 DOWNLOAD |
| 879 |
|
| 880 |
Lemonldap::NG is available at |
| 881 |
L<https://lemonldap-ng.org/download> |
| 882 |
|
| 883 |
=head1 COPYRIGHT AND LICENSE |
| 884 |
|
| 885 |
See COPYING file for details. |
| 886 |
|
| 887 |
This library is free software; you can redistribute it and/or modify |
| 888 |
it under the terms of the GNU General Public License as published by |
| 889 |
the Free Software Foundation; either version 2, or (at your option) |
| 890 |
any later version. |
| 891 |
|
| 892 |
This program is distributed in the hope that it will be useful, |
| 893 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 894 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 895 |
GNU General Public License for more details. |
| 896 |
|
| 897 |
You should have received a copy of the GNU General Public License |
| 898 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 899 |
|
| 900 |
=cut |