Cli.pm 11.9 KB
Newer Older
1 2 3
package Lemonldap::NG::Manager::Cli;

use strict;
4
use Crypt::URandom;
5 6
use Mouse;
use Data::Dumper;
7
use Lemonldap::NG::Common::Conf::ReConstants;
8

Xavier Guimard's avatar
Xavier Guimard committed
9
our $VERSION = '2.0.2';
10
$Data::Dumper::Useperl = 1;
Xavier Guimard's avatar
Xavier Guimard committed
11

12 13 14 15 16 17 18 19 20 21 22 23
extends('Lemonldap::NG::Manager::Cli::Lib');

has cfgNum => (
    is      => 'rw',
    isa     => 'Int',
    trigger => sub {
        $_[0]->{req} =
          Lemonldap::NG::Manager::Cli::Request->new(
            cfgNum => $_[0]->{cfgNum} );
    }
);

Xavier Guimard's avatar
Xavier Guimard committed
24 25
has sep => ( is => 'rw', isa => 'Str', default => '/' );

26 27
has req => ( is => 'ro' );

Xavier Guimard's avatar
Xavier Guimard committed
28 29 30 31 32 33
has format => ( is => 'rw', isa => 'Str', default => "%-25s | %-25s | %-25s" );

has yes => ( is => 'rw', isa => 'Bool', default => 0 );

has force => ( is => 'rw', isa => 'Bool', default => 0 );

34
sub get {
Xavier Guimard's avatar
Xavier Guimard committed
35 36 37 38 39
    my ( $self, @keys ) = @_;
    die 'get requires at least one key' unless (@keys);
  L: foreach my $key (@keys) {
        my $value = $self->_getKey($key);
        if ( ref $value eq 'HASH' ) {
40
            print "$key has the following keys:\n";
Xavier Guimard's avatar
Xavier Guimard committed
41
            print "   $_\n" foreach ( sort keys %$value );
42 43
        }
        else {
Xavier Guimard's avatar
Xavier Guimard committed
44
            $value //= '';
45 46 47 48 49
            print "$key = $value\n";
        }
    }
}

Xavier Guimard's avatar
Xavier Guimard committed
50 51 52 53 54 55 56 57 58 59
sub set {
    my ( $self, %pairs ) = @_;
    my $format = $self->format . "\n";
    die 'set requires at least one key and one value' unless (%pairs);
    my @list;
    foreach my $key ( keys %pairs ) {
        my $oldValue = $self->_getKey($key);
        if ( ref $oldValue ) {
            die "$key seems to be a hash, modification refused";
        }
60
        $oldValue //= '';
Xavier Guimard's avatar
Xavier Guimard committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        push @list, [ $key, $oldValue, $pairs{$key} ];
    }
    unless ( $self->yes ) {
        print "Proposed changes:\n";
        printf $format, 'Key', 'Old value', 'New value';
        foreach (@list) {
            printf $format, @$_;
        }
        print "Confirm (N/y)? ";
        my $c = <STDIN>;
        unless ( $c =~ /^y(?:es)?$/ ) {
            die "Aborting";
        }
    }
    require Clone;
    my $new = Clone::clone( $self->mgr->currentConf );
    foreach my $key ( keys %pairs ) {
        $self->_setKey( $new, $key, $pairs{$key} );
    }
80 81 82 83 84 85 86 87
    return $self->_save($new);
}

sub addKey {
    my $self = shift;
    unless ( @_ % 3 == 0 ) {
        die 'usage: "addKey (?:rootKey newKey newValue)+';
    }
88
    my $sep = $self->sep;
89 90 91 92 93
    my @list;
    while (@_) {
        my $root   = shift;
        my $newKey = shift;
        my $value  = shift;
94
        unless ( $root =~ /$simpleHashKeys$/o or $root =~ /$sep/o ) {
95
            die "$root is not a simple hash. Aborting";
Xavier Guimard's avatar
Xavier Guimard committed
96
        }
97
        push @list, [ $root, $newKey, $value ];
Xavier Guimard's avatar
Xavier Guimard committed
98
    }
99 100 101
    require Clone;
    my $new = Clone::clone( $self->mgr->currentConf );
    foreach my $el (@list) {
102 103 104 105 106 107 108
        my @path = split $sep, $el->[0];
        if ( $#path == 0 ) {
            $new->{ $path[0] }->{ $el->[1] } = $el->[2];
        }
        elsif ( $#path == 1 ) {
            $new->{ $path[0] }->{ $path[1] }->{ $el->[1] } = $el->[2];
        }
109 110 111 112 113 114 115 116
        elsif ( $#path == 2 ) {
            $new->{ $path[0] }->{ $path[1] }->{ $path[2] }->{ $el->[1] } =
              $el->[2];
        }
        elsif ( $#path == 3 ) {
            $new->{ $path[0] }->{ $path[1] }->{ $path[2] }->{ $path[3] }
              ->{ $el->[1] } = $el->[2];
        }
117 118 119
        else {
            die $el->[0] . " has too many levels. Aborting";
        }
Xavier Guimard's avatar
Xavier Guimard committed
120
    }
121 122 123 124 125 126 127
    return $self->_save($new);
}

sub delKey {
    my $self = shift;
    unless ( @_ % 2 == 0 ) {
        die 'usage: "delKey (?:rootKey key)+';
Xavier Guimard's avatar
Xavier Guimard committed
128
    }
129
    my $sep = $self->sep;
130 131 132 133
    my @list;
    while (@_) {
        my $root = shift;
        my $key  = shift;
134
        unless ( $root =~ /$simpleHashKeys$/o or $root =~ /$sep/o ) {
135 136 137 138 139 140 141
            die "$root is not a simple hash. Aborting";
        }
        push @list, [ $root, $key ];
    }
    require Clone;
    my $new = Clone::clone( $self->mgr->currentConf );
    foreach my $el (@list) {
142 143
        my @path = split $sep, $el->[0];
        if ( $#path == 0 ) {
144 145 146 147 148 149
            if (   exists $new->{ $path[0] }
                && exists $new->{ $path[0] }->{ $el->[1] } )
            {
                delete $new->{ $path[0] }->{ $el->[1] }
                  if exists $new->{ $path[0] }->{ $el->[1] };
            }
150 151
        }
        elsif ( $#path == 1 ) {
152 153 154 155 156 157
            if (   exists $new->{ $path[0] }
                && exists $new->{ $path[0] }->{ $path[1] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $el->[1] } )
            {
                delete $new->{ $path[0] }->{ $path[1] }->{ $el->[1] };
            }
158
        }
159
        elsif ( $#path == 2 ) {
160 161 162 163 164 165 166 167 168
            if (   exists $new->{ $path[0] }
                && exists $new->{ $path[0] }->{ $path[1] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                ->{ $el->[1] } )
            {
                delete $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                  ->{ $el->[1] };
            }
169 170
        }
        elsif ( $#path == 3 ) {
171 172 173 174 175 176 177 178 179 180 181
            if (   exists $new->{ $path[0] }
                && exists $new->{ $path[0] }->{ $path[1] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                ->{ $path[3] }
                && exists $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                ->{ $path[3] }->{ $el->[1] } )
            {
                delete $new->{ $path[0] }->{ $path[1] }->{ $path[2] }
                  ->{ $path[3] }->{ $el->[1] };
            }
182
        }
183 184 185
        else {
            die $el->[0] . " has too many levels. Aborting";
        }
Xavier Guimard's avatar
Xavier Guimard committed
186
    }
187
    return $self->_save($new);
Xavier Guimard's avatar
Xavier Guimard committed
188 189
}

190 191 192 193 194
sub lastCfg {
    my ($self) = @_;
    return $self->jsonResponse('/confs/latest')->{cfgNum};
}

Xavier Guimard's avatar
Xavier Guimard committed
195 196 197 198 199 200 201 202
sub _getKey {
    my ( $self, $key ) = @_;
    my $sep = $self->sep;
    my ( $base, @path ) = split $sep, $key;
    unless ( $base =~ /^\w+$/ ) {
        warn "Malformed key $base";
        return ();
    }
203
    my $value = $self->mgr->getConfKey( $self->req, $base, noCache => 1 );
Xavier Guimard's avatar
Xavier Guimard committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    if ( $self->req->error ) {
        die $self->req->error;
    }
    if ( ref $value eq 'HASH' ) {
        while ( my $next = shift @path ) {
            unless ( exists $value->{$next} ) {
                warn "Unknown subkey $next for $key";
                next L;
            }
            $value = $value->{$next};
        }
    }
    elsif (@path) {
        warn "No subkeys for $base";
        return ();
    }
    return $value;
}

sub _setKey {
    my ( $self, $conf, $key, $value ) = @_;
    my $sep = $self->sep;
    my (@path) = split $sep, $key;
    my $last = pop @path;
    while ( my $next = shift @path ) {
        $conf = $conf->{$next};
    }
    $conf->{$last} = $value;
}

234 235
sub _save {
    my ( $self, $new ) = @_;
236
    require Lemonldap::NG::Manager::Conf::Parser;
Xavier Guimard's avatar
Xavier Guimard committed
237
    my $parser = Lemonldap::NG::Manager::Conf::Parser->new( {
238 239 240 241 242 243 244 245
            newConf => $new,
            refConf => $self->mgr->currentConf,
            req     => $self->req
        }
    );
    unless ( $parser->testNewConf() ) {
        printf STDERR "Modifications rejected: %s:\n", $parser->{message};
    }
246 247 248 249 250
    my $saveParams = { force => $self->force };
    if ( $self->force and $self->cfgNum ) {
        $saveParams->{cfgNum}      = $self->cfgNum;
        $saveParams->{cfgNumFixed} = 1;
    }
251 252 253 254 255 256
    $new->{cfgAuthor} = 'lmConfigEditor: ' . `whoami`;
    chomp $new->{cfgAuthor};
    $new->{cfgAuthorIP} = '';
    $new->{cfgDate}     = time;
    $new->{cfgVersion}  = $VERSION;
    $new->{cfgLog}      = '';
257 258 259
    $new->{key} ||= join( '',
        map { chr( int( ord( Crypt::URandom::urandom(1) ) * 94 / 256 ) + 33 ) }
          ( 1 .. 16 ) );
260

261
    my $s = $self->mgr->confAcc->saveConf( $new, %$saveParams );
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    if ( $s > 0 ) {
        print STDERR "Saved under number $s\n";
        $parser->{status} = [ $self->mgr->applyConf($new) ];
    }
    else {
        printf STDERR "Modifications rejected: %s:\n", $parser->{message};
        print STDERR Dumper($parser);
    }
    foreach (qw(errors warnings status)) {
        if ( $parser->{$_} and @{ $parser->{$_} } ) {
            my $s = Dumper( $parser->{$_} );
            $s =~ s/\$VAR1\s*=\s*//;
            printf STDERR "%-8s: %s", ucfirst($_), $s;
        }
    }
}

279 280
sub run {
    my $self = shift;
Xavier Guimard's avatar
Xavier Guimard committed
281 282

    # Options simply call corresponding accessor
283
    my $args = {};
Xavier Guimard's avatar
Xavier Guimard committed
284
    while ( $_[0] =~ s/^--?// ) {
Xavier Guimard's avatar
Xavier Guimard committed
285 286
        my $k = shift;
        my $v = shift;
287 288 289 290 291
        if ( ref $self ) {
            eval { $self->$k($v) };
            if ($@) {
                die "Unknown option -$k or bad value ($@)";
            }
Xavier Guimard's avatar
Xavier Guimard committed
292
        }
293 294 295 296 297 298
        else {
            $args->{$k} = $v;
        }
    }
    unless ( ref $self ) {
        $self = $self->new($args);
Xavier Guimard's avatar
Xavier Guimard committed
299
    }
300 301 302
    unless (@_) {
        die 'nothing to do, aborting';
    }
303
    $self->cfgNum( $self->lastCfg ) unless ( $self->cfgNum );
304
    my $action = shift;
Xavier Guimard's avatar
Xavier Guimard committed
305
    unless ( $action =~ /^(?:get|set|addKey|delKey)$/ ) {
Xavier Guimard's avatar
Xavier Guimard committed
306 307
        die
"unknown action $action. Only get, set, addKey or delKey are accepted";
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    }
    $self->$action(@_);
}

package Lemonldap::NG::Manager::Cli::Request;

use Mouse;

has cfgNum => ( is => 'rw' );

has error => ( is => 'rw' );

sub params {
    my ( $self, $key ) = @_;
    return $self->{$key};
}

1;
Xavier Guimard's avatar
Xavier Guimard committed
326 327 328 329 330 331
__END__

=head1 NAME

=encoding utf8

332 333
Lemonldap::NG::Manager::Cli - Command line manager for Lemonldap::NG web SSO
system.
Xavier Guimard's avatar
Xavier Guimard committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

=head1 SYNOPSIS

  #!/usr/bin/env perl
  
  use warnings;
  use strict;
  use Lemonldap::NG::Manager::Cli;
  
  # Optional: you can specify here some parameters
  my $cli = Lemonldap::NG::Manager::Cli->new(iniFile=>'t/lemonldap-ng.ini');
  
  $cli->run(@ARGV);

or use llng-manager-cli provides with this package.

  llng-manager-cli <options> <command> <keys>

=head1 DESCRIPTION

Lemonldap::NG::Manager provides a web interface to manage Lemonldap::NG Web-SSO
system.

357 358
Lemonldap::NG Manager::Cli provides a command line client to read or modify
configuration.
Xavier Guimard's avatar
Xavier Guimard committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

=head1 METHODS

=head2 ACCESSORS

All accessors can be set using the command line: just set a '-' before their
names. Example

  llng-manager-cli -sep ',' get macros,_whatToTrace

=head3 iniFile()

The lemonldap-ng.ini file to use is not default value.

=head3 sep()

The key separator, default to '/'. For example to read the value of macro
_whatToTrace using ',', use:

  llng-manager-cli -sep ',' get macros,_whatToTrace

=head3 cfgNum()

The configuration number. If not set, it will use the latest configuration.

Xavier Guimard's avatar
Xavier Guimard committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397
=head3 yes()

If set to 1, no confirmation is asked to save new values:

  llng-manager -yes 1 set portal http://somewhere/

=head3 force()

Set it to 1 to save a configuration earlier than latest

=head3 format()

Confirmation array line format. Default to "%-25s | %-25s | %-25s"

Xavier Guimard's avatar
Xavier Guimard committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
=head2 run()

The main method: it reads option, command and launch the corresponding
subroutine.

=head3 Commands

=head4 get

Using get, you can read several keys. Example:

  llng-manager-cli get portal cookieName domain

=head1 SEE ALSO

413 414 415
For other features of llng-cli, see L<Lemonldap::NG::Common::Cli>

Other links: L<Lemonldap::NG::Manager>, L<http://lemonldap-ng.org/>
Xavier Guimard's avatar
Xavier Guimard committed
416 417 418 419 420

=head1 AUTHORS

=over

Xavier Guimard's avatar
Xavier Guimard committed
421
=item Original idea from David Delassus in 2012
Xavier Guimard's avatar
Xavier Guimard committed
422

Xavier Guimard's avatar
Xavier Guimard committed
423
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
Xavier Guimard's avatar
Xavier Guimard committed
424 425 426 427 428 429

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
Xavier Guimard's avatar
Xavier Guimard committed
430
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Xavier Guimard's avatar
Xavier Guimard committed
431 432 433 434 435 436 437 438

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

Xavier Guimard's avatar
Xavier Guimard committed
439
See COPYING file for details.
Xavier Guimard's avatar
Xavier Guimard committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut