]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/API2/Domains.pm
parse_sync_opts: code cleanup
[pve-access-control.git] / PVE / API2 / Domains.pm
index fa6ab782449880080e13644d1107b8fcffda3860..d2d9a2b786403e2ba724f12828c581c583e6f115 100644 (file)
@@ -2,6 +2,7 @@ package PVE::API2::Domains;
 
 use strict;
 use warnings;
+
 use PVE::Exception qw(raise_param_exc);
 use PVE::Tools qw(extract_param);
 use PVE::Cluster qw (cfs_read_file cfs_write_file);
@@ -87,6 +88,9 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
+       # always extract, add it with hook
+       my $password = extract_param($param, 'password');
+
        PVE::Auth::Plugin::lock_domain_config(
            sub {
 
@@ -116,6 +120,13 @@ __PACKAGE__->register_method ({
 
                $ids->{$realm} = $config;
 
+               my $opts = $plugin->options();
+               if (defined($password) && !defined($opts->{password})) {
+                   $password = undef;
+                   warn "ignoring password parameter";
+               }
+               $plugin->on_add_hook($realm, $config, password => $password);
+
                cfs_write_file($domainconfigfile, $cfg);
            }, "add auth server failed");
 
@@ -136,6 +147,9 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
+       # always extract, update in hook
+       my $password = extract_param($param, 'password');
+
        PVE::Auth::Plugin::lock_domain_config(
            sub {
 
@@ -153,8 +167,10 @@ __PACKAGE__->register_method ({
                my $delete_str = extract_param($param, 'delete');
                die "no options specified\n" if !$delete_str && !scalar(keys %$param);
 
+               my $delete_pw = 0;
                foreach my $opt (PVE::Tools::split_list($delete_str)) {
                    delete $ids->{$realm}->{$opt};
+                   $delete_pw = 1 if $opt eq 'password';
                }
 
                my $plugin = PVE::Auth::Plugin->lookup($ids->{$realm}->{type});
@@ -170,6 +186,13 @@ __PACKAGE__->register_method ({
                    $ids->{$realm}->{$p} = $config->{$p};
                }
 
+               my $opts = $plugin->options();
+               if ($delete_pw || defined($password)) {
+                   $plugin->on_update_hook($realm, $config, password => $password);
+               } else {
+                   $plugin->on_update_hook($realm, $config);
+               }
+
                cfs_write_file($domainconfigfile, $cfg);
            }, "update auth server failed");
 
@@ -232,10 +255,13 @@ __PACKAGE__->register_method ({
 
                my $cfg = cfs_read_file($domainconfigfile);
                my $ids = $cfg->{ids};
-
                my $realm = $param->{realm};
 
-               die "domain '$realm' does not exist\n" if !$ids->{$realm};
+               die "authentication domain '$realm' does not exist\n" if !$ids->{$realm};
+
+               my $plugin = PVE::Auth::Plugin->lookup($ids->{$realm}->{type});
+
+               $plugin->on_delete_hook($realm, $ids->{$realm});
 
                delete $ids->{$realm};
 
@@ -276,7 +302,7 @@ my $update_users = sub {
            my $olduser = $oldusers->{$userid} // {};
            if (defined(my $enabled = $olduser->{enable})) {
                $user->{enable} = $enabled;
-           } elsif ($opts->{enable}) {
+           } elsif ($opts->{'enable-new'}) {
                $user->{enable} = 1;
            }
 
@@ -340,6 +366,29 @@ my $update_groups = sub {
     }
 };
 
+my $parse_sync_opts = sub {
+    my ($param, $realmconfig) = @_;
+
+    my $sync_opts_fmt = PVE::JSONSchema::get_format('realm-sync-options');
+
+    my $cfg_defaults = {};
+    if (defined(my $cfg_opts = $realmconfig->{'sync-defaults-options'})) {
+       $cfg_defaults = PVE::JSONSchema::parse_property_string($sync_opts_fmt, $cfg_opts);
+    }
+
+    my $res = {};
+    for my $opt (sort keys %$sync_opts_fmt) {
+       my $fmt = $sync_opts_fmt->{$opt};
+
+       $res->{$opt} = $param->{$opt} // $cfg_defaults->{$opt} // $fmt->{default};
+
+       raise_param_exc({
+           "$opt" => 'Not passed as parameter and not defined in realm default sync options.'
+       }) if !defined($res->{$opt});
+    }
+    return $res;
+};
+
 __PACKAGE__->register_method ({
     name => 'sync',
     path => '{realm}/sync',
@@ -358,28 +407,15 @@ __PACKAGE__->register_method ({
     protected => 1,
     parameters => {
        additionalProperties => 0,
-       properties => {
-           realm =>  get_standard_option('realm'),
-           scope => {
-               description => "Select what to sync.",
-               type => 'string',
-               enum => [qw(users groups both)],
-           },
-           full => {
-               description => "If set, uses the LDAP Directory as source of truth, ".
-                              "deleting all information not contained there. ".
-                              "Otherwise only syncs information set explicitly.",
+       properties => get_standard_option('realm-sync-options', {
+           realm => get_standard_option('realm'),
+           'dry-run' => {
+               description => "If set, does not write anything.",
                type => 'boolean',
+               optional => 1,
+               default => 0,
            },
-           enable => {
-               description => "Enable newly synced users.",
-               type => 'boolean',
-           },
-           purge => {
-               description => "Remove ACLs for users/groups that were removed from the config.",
-               type => 'boolean',
-           },
-       }
+       }),
     },
     returns => {
        description => 'Worker Task-UPID',
@@ -391,6 +427,9 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
+       my $write = !(extract_param($param, 'dry-run'));
+       my $dryrunstring = $write ? '' : ' (dry run)';
+
        my $realm = $param->{realm};
        my $cfg = cfs_read_file($domainconfigfile);
        my $realmconfig = $cfg->{ids}->{$realm};
@@ -402,14 +441,15 @@ __PACKAGE__->register_method ({
            die "Cannot sync realm type '$type'! Only LDAP/AD realms can be synced.\n";
        }
 
+       my $opts = $parse_sync_opts->($param, $realmconfig); # can throw up
 
-       my $scope = $param->{scope};
+       my $scope = $opts->{scope};
        my $whatstring = $scope eq 'both' ? "users and groups" : $scope;
 
        my $plugin = PVE::Auth::Plugin->lookup($type);
 
        my $worker = sub {
-           print "starting sync for realm $realm\n";
+           print "starting sync$dryrunstring for realm $realm\n";
 
            my ($synced_users, $dnmap) = $plugin->get_users($realmconfig, $realm);
            my $synced_groups = {};
@@ -422,19 +462,24 @@ __PACKAGE__->register_method ({
                print "got data from server, updating $whatstring\n";
 
                if ($scope eq 'users' || $scope eq 'both') {
-                   $update_users->($usercfg, $realm, $synced_users, $param);
+                   $update_users->($usercfg, $realm, $synced_users, $opts);
                }
 
                if ($scope eq 'groups' || $scope eq 'both') {
-                   $update_groups->($usercfg, $realm, $synced_groups, $param);
+                   $update_groups->($usercfg, $realm, $synced_groups, $opts);
                }
 
-               cfs_write_file("user.cfg", $usercfg);
-               print "successfully updated $whatstring configuration\n";
+               if ($write) {
+                   cfs_write_file("user.cfg", $usercfg) if $write;
+                   print "successfully updated $whatstring configuration\n" if $write;
+               } else {
+                   print "NOTE: This is just a dry run. No actual data was written.\n";
+               }
            }, "syncing $whatstring failed");
        };
 
-       return $rpcenv->fork_worker('auth-realm-sync', $realm, $authuser, $worker);
+       my $workerid = $write ? 'auth-realm-sync' : 'auth-realm-sync-test';
+       return $rpcenv->fork_worker($workerid, $realm, $authuser, $worker);
     }});
 
 1;