From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 772701FF16F
	for <inbox@lore.proxmox.com>; Thu, 27 Mar 2025 02:57:16 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 0A5D52207;
	Thu, 27 Mar 2025 02:57:11 +0100 (CET)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1743040591; x=1743645391;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=oAy/CQNLlaL7d8iy6eS5LcNijhZHmMBTNzU+ei+fxTI=;
 b=s6EznoSpvyNWQBt+iHtzjiFSbi3zOKgS2JwqUNFqVUhySRoRALymPcHFJS0j7zfK2p
 H4bEeYmWsvjkyXq6o/RD9Bj3MJ5rMxyGMw2pO2mcWuj3miMV1ZQRhemN6GOEmhGUDVco
 sHkgCRQ1jDBOHDLPAQ7LEbrk12RoWQvF3KPVupAwp8mesDWXf4B58v4UABCD1/l6vmsr
 rgD1ir3DTR3TXzzdI2UawOfr5A9LbcQLIzH1M2JVJgJCRiGqjlWB/o2wQ9utzZxHWrDB
 hClY4ofk4TW+JjND9kv2vFkFwknS5VpWfXHUfVDxs1lBhrOVJ1BX0/tL8L7GoGoP+UsS
 F8eg==
X-Gm-Message-State: AOJu0Yw4ABRZQ0xnuiP337U41EnPoejI+MR90ZOgXHyvMNpdyVBwTqgT
 HM2HT9/V7v01Wgd26wuJQbGS5dyqsmEsUqyq+UocYk0b1vmrQK6xJP7gv0Ms
X-Gm-Gg: ASbGncvLOE5TjcDz1jeVbn1Fhjpr4GiwbFuM5fQpaBrYzf6OH60Vwn6Zv4T6/jsCxCI
 FSG6BaeJCF7Qk/TkAFhdkKmQJDUtc0Bs+OKY/rGXjlWBJzzcsgy6KpsYNFErjYHYclM9SpEAiEd
 7nbooqNqowCF1KTVyLhbRi+VM0gS3E4oJDCBahwmWoV037wPsk7crazyi2lGS3DjdacY5X8FqlV
 8UrYQ4ch0VD2AmJXSPLdnXBF/LpjogSKhS+bkMMKn4bGqHGZ1dtTgUmVSHGrGWWGB2oqm0WkxO5
 Ij1Yr0xSBhXNsaJVvqHVGr/DYhfMak7ZLFtRPd6H4An62/BeiIVXAghTawtmVc3lA7w5Qm0YKOB
 68q7LLaqecBE0gg03aK60o2UMlpxT96bWD/ypFO3ISBEjJIOEcNyWC37GBA==
X-Google-Smtp-Source: AGHT+IELg/FCNShjqCSkLkhZQCbH5N4PEKR1omMMBhDGMmq887C5GX0Uv9ysH/DmxdrlqAYt8jTfZQ==
X-Received: by 2002:a05:690c:23c1:b0:6f9:8845:cfbf with SMTP id
 00721157ae682-70224ff95aamr25003977b3.22.1743040214115; 
 Wed, 26 Mar 2025 18:50:14 -0700 (PDT)
From: Thomas Skinner <thomas@atskinner.net>
To: pve-devel@lists.proxmox.com
Date: Wed, 26 Mar 2025 20:50:01 -0500
Message-Id: <20250327015002.1982876-4-thomas@atskinner.net>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250327015002.1982876-1-thomas@atskinner.net>
References: <20250327015002.1982876-1-thomas@atskinner.net>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.008 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 FREEMAIL_FORGED_FROMDOMAIN 0.001 2nd level domains in From and EnvelopeFrom
 freemail headers are different
 FREEMAIL_FROM 0.001 Sender email is commonly abused enduser mail provider
 HEADER_FROM_DIFFERENT_DOMAINS 0.007 From and EnvelopeFrom 2nd level mail
 domains are different
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_NONE     -0.0001 Sender listed at https://www.dnswl.org/,
 no trust RCVD_IN_MSPIKE_H2       0.001 Average reputation (+2)
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH access-control v5 1/1] fix #4411: openid: add
 logic for openid groups support
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Cc: Thomas Skinner <thomas@atskinner.net>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

Signed-off-by: Thomas Skinner <thomas@atskinner.net>
---
 src/PVE/API2/OpenId.pm   | 83 ++++++++++++++++++++++++++++++++++++++++
 src/PVE/AccessControl.pm |  2 +-
 src/PVE/Auth/OpenId.pm   | 25 ++++++++++++
 src/PVE/Auth/Plugin.pm   |  1 +
 4 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/src/PVE/API2/OpenId.pm b/src/PVE/API2/OpenId.pm
index 77410e6..baa4dbc 100644
--- a/src/PVE/API2/OpenId.pm
+++ b/src/PVE/API2/OpenId.pm
@@ -13,6 +13,7 @@ use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::AccessControl;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Auth::Plugin;
+use PVE::Auth::OpenId;
 
 use PVE::RESTHandler;
 
@@ -220,6 +221,88 @@ __PACKAGE__->register_method ({
 		$rpcenv->check_user_enabled($username);
 	    }
 
+	    if (defined(my $groups_claim = $config->{'groups-claim'})) {
+		if (defined(my $groups_list = $info->{$groups_claim})) {
+		    if (ref($groups_list) eq 'ARRAY') {
+			PVE::AccessControl::lock_user_config(sub {
+			    my $usercfg = cfs_read_file("user.cfg");
+
+			    my $oidc_groups;
+			    for my $group (@$groups_list) {
+				if (PVE::AccessControl::verify_groupname($group, 1)) {
+				    # add realm name as suffix to group
+				    $oidc_groups->{"$group-$realm"} = 1;
+				} else {
+				    # ignore any groups in the list that have invalid characters
+				    syslog(
+					'warn', 
+					"openid group '$group' contains invalid characters"
+				    );
+				}
+			    }
+			    
+			    # get groups that exist in OIDC and PVE
+			    my $groups_intersect;
+			    for my $group (keys %$oidc_groups) {
+				$groups_intersect->{$group} = 1 if $usercfg->{groups}->{$group};
+			    }
+
+			    if ($config->{'groups-autocreate'}) {
+                                # populate all groups in claim
+                                $groups_intersect = $oidc_groups;
+				my $groups_to_create;
+				for my $group (keys %$oidc_groups) {
+                                    $groups_to_create->{$group} = 1 if !$usercfg->{groups}->{$group};
+			    	}
+				if ($groups_to_create) {
+				    # log a messages about created groups here
+				    my $groups_to_create_string = join(', ', sort keys %$groups_to_create);
+				    syslog(
+					'info', 
+					"groups created automatically from openid claim: $groups_to_create_string"
+				    );
+				}
+                            }
+
+			    # if groups should be overwritten, delete all the users groups first
+			    if ( $config->{'groups-overwrite'} ) {
+				PVE::AccessControl::delete_user_group(
+				    $username, 
+				    $usercfg, 
+				);
+				syslog(
+				    'info', 
+				    "openid overwrite groups enabled; user '$username' removed from all groups"
+			    	);
+			    }
+			    
+			    if (keys %$groups_intersect) {
+				# ensure user is a member of the groups
+				for my $group (keys %$groups_intersect) {
+				    PVE::AccessControl::add_user_group(
+					$username,
+					$usercfg,
+					$group
+				    );
+				}
+
+				my $groups_intersect_string = join(', ', sort keys %$groups_intersect);
+				syslog(
+				    'info', 
+				    "openid user '$username' added to groups: $groups_intersect_string"
+				);
+			    }
+
+			    cfs_write_file("user.cfg", $usercfg);
+			}, "openid group mapping failed");
+		    } else {
+			syslog('err', "openid groups list is not an array; groups will not be updated");
+		    }
+		} else {
+		    syslog('err', "openid groups claim '$groups_claim' is not found in claims");
+		}
+	    }
+
 	    my $ticket = PVE::AccessControl::assemble_ticket($username);
 	    my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username);
 	    my $cap = $rpcenv->compute_api_permission($username);
diff --git a/src/PVE/AccessControl.pm b/src/PVE/AccessControl.pm
index 47f2d38..7493c57 100644
--- a/src/PVE/AccessControl.pm
+++ b/src/PVE/AccessControl.pm
@@ -1293,7 +1293,7 @@ PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname);
 sub verify_groupname {
     my ($groupname, $noerr) = @_;
 
-    if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) {
+    if ($groupname !~ m/^[$PVE::Auth::Plugin::groupname_regex_chars]+$/) {
 
 	die "group name '$groupname' contains invalid characters\n" if !$noerr;
 
diff --git a/src/PVE/Auth/OpenId.pm b/src/PVE/Auth/OpenId.pm
index c8e4db9..4c52adc 100755
--- a/src/PVE/Auth/OpenId.pm
+++ b/src/PVE/Auth/OpenId.pm
@@ -9,6 +9,9 @@ use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file
 
 use base qw(PVE::Auth::Plugin);
 
+# include all printable ascii characters
+my $openid_claim_regex = qr/[ -~]+/;
+
 sub type {
     return 'openid';
 }
@@ -42,6 +45,25 @@ sub properties {
 	    type => 'string',
 	    optional => 1,
 	},
+	"groups-claim" => {
+	    description => "OpenID claim used to retrieve groups with.",
+	    type => 'string',
+	    pattern => $openid_claim_regex,
+	    maxLength => 256,
+	    optional => 1,
+	},
+	"groups-autocreate" => {
+	    description => "Automatically create groups if they do not exist.",
+	    optional => 1,
+	    type => 'boolean',
+	    default => 0,
+	},
+	"groups-overwrite" => {
+	    description => "All groups will be overwritten for the user on login.",
+	    type => 'boolean',
+	    default => 0,
+	    optional => 1,
+	},
 	prompt => {
 	    description => "Specifies whether the Authorization Server prompts the End-User for"
 	        ." reauthentication and consent.",
@@ -73,6 +95,9 @@ sub options {
 	"client-key" => { optional => 1 },
 	autocreate => { optional => 1 },
 	"username-claim" => { optional => 1, fixed => 1 },
+	"groups-claim" => { optional => 1 },
+	"groups-autocreate" => { optional => 1 },
+	"groups-overwrite" => { optional => 1 },
 	prompt => { optional => 1 },
 	scopes => { optional => 1 },
 	"acr-values" => { optional => 1 },
diff --git a/src/PVE/Auth/Plugin.pm b/src/PVE/Auth/Plugin.pm
index 763239f..7617044 100755
--- a/src/PVE/Auth/Plugin.pm
+++ b/src/PVE/Auth/Plugin.pm
@@ -31,6 +31,7 @@ sub lock_domain_config {
 
 our $realm_regex = qr/[A-Za-z][A-Za-z0-9\.\-_]+/;
 our $user_regex = qr![^\s:/]+!;
+our $groupname_regex_chars = qr/A-Za-z0-9\.\-_/;
 
 PVE::JSONSchema::register_format('pve-realm', \&pve_verify_realm);
 sub pve_verify_realm {
-- 
2.39.5


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel