From nobody Tue Nov 26 13:39:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573483569; cv=none; d=zoho.com; s=zohoarc; b=HtTy5QhkzQjgU3GUg/aC6btIEqAKtgQU4cRVHXDlwmL00v6Hp0bz54RFMmpAdnk7wjlYD9M3/+kuArkUzpqX08zm7DgB/royVXAytC3czIqyffgKA5+8WaNSX/BJnF1W/zUsw8sUIhFOdIJFVCu7+8y4eSQQmCqmve4OwtpzawU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573483569; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iA8jVdombxEzaExUV2jFuWOjITfbOb9SGBeboD5zGHQ=; b=hvUQ9OTOdWQH5qJry7n9gaNLIEchDBqGLMRuU3IU47ob+YxT71+Z15lzzaqJambOIPrdWD0TXYg3bCAIwnwz07K+n1Yo2m2SIdWt8IRes1HWwo8x0apSVCbR9SYd3ju1etsFh/dgYTqCCYlhnPULdLoA3DqbxOT/s3bf0kQyeRE= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1573483569035928.7182914023036; Mon, 11 Nov 2019 06:46:09 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-429-gGLMVjwvNmq2SP74U5liJw-1; Mon, 11 Nov 2019 09:45:25 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CD1A985EE72; Mon, 11 Nov 2019 14:45:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9D9155DA7D; Mon, 11 Nov 2019 14:45:17 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 452AC18033C0; Mon, 11 Nov 2019 14:45:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xABEj1XL020458 for ; Mon, 11 Nov 2019 09:45:01 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1B7C35DA7D; Mon, 11 Nov 2019 14:45:01 +0000 (UTC) Received: from catbus.gsslab.fab.redhat.com (dhcp-32.gsslab.fab.redhat.com [10.33.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7F2C25D9C9; Mon, 11 Nov 2019 14:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573483567; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=iA8jVdombxEzaExUV2jFuWOjITfbOb9SGBeboD5zGHQ=; b=NO0s/au/kk5tKaaSiTJjHq9LzU8ORKKtJ4wa5xLlKfutlduKl+oBfRdLr24cai+u8DVtLO 3D7J+kCYiXAAc/YHs/9nKrrWQgasjWoirjY1Vaka5s6E195iYdeG3BAl/+0sW4NFNkEytj LY5KyN6lQZUBqk5UwGoEhsfViCKqlN4= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 11 Nov 2019 14:38:16 +0000 Message-Id: <20191111143826.16050-14-berrange@redhat.com> In-Reply-To: <20191111143826.16050-1-berrange@redhat.com> References: <20191111143826.16050-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v5 13/23] src: rewrite polkit ACL generator in Python X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: gGLMVjwvNmq2SP74U5liJw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" As part of an goal to eliminate Perl from libvirt build tools, rewrite the genpolkit.pl tool in Python. This was a straight conversion, manually going line-by-line to change the syntax from Perl to Python. Thus the overall structure of the file and approach is the same. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: J=C3=A1n Tomko Tested-by: Cole Robinson --- Makefile.am | 1 + scripts/genpolkit.py | 122 +++++++++++++++++++++++++++++++++++++ src/access/Makefile.inc.am | 6 +- src/access/genpolkit.pl | 119 ------------------------------------ 4 files changed, 126 insertions(+), 122 deletions(-) create mode 100755 scripts/genpolkit.py delete mode 100755 src/access/genpolkit.pl diff --git a/Makefile.am b/Makefile.am index f28b07d814..e7ebe7281a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,6 +54,7 @@ EXTRA_DIST =3D \ scripts/check-symfile.py \ scripts/check-symsorting.py \ scripts/dtrace2systemtap.py \ + scripts/genpolkit.py \ scripts/gensystemtap.py \ scripts/header-ifdef.py \ scripts/minimize-po.py \ diff --git a/scripts/genpolkit.py b/scripts/genpolkit.py new file mode 100755 index 0000000000..0cdba2bd3c --- /dev/null +++ b/scripts/genpolkit.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# +# Copyright (C) 2012-2019 Red Hat, Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see +# . +# + +from __future__ import print_function + +import re +import sys + +objects =3D [ + "CONNECT", "DOMAIN", "INTERFACE", "NETWORK_PORT", + "NETWORK", "NODE_DEVICE", "NWFILTER_BINDING", + "NWFILTER", "SECRET", "STORAGE_POOL", "STORAGE_VOL", +] + +objectstr =3D "|".join(objects) + +# Data we're going to be generating looks like this +# +# +# +# Monitor local virtualized systems +# System policy prevents monitoring of +# local virtualized systems +# +# yes +# yes +# yes +# +# +# ...more rules... +# + +opts =3D {} +in_opts =3D False + +perms =3D {} + +aclfile =3D sys.argv[1] +with open(aclfile, "r") as fh: + for line in fh: + if in_opts: + if line.find("*/") !=3D -1: + in_opts =3D False + else: + m =3D re.search(r'''\*\s*\@(\w+):\s*(.*?)\s*$''', line) + if m is not None: + opts[m.group(1)] =3D m.group(2) + elif line.find("**") !=3D -1: + in_opts =3D True + else: + m =3D re.search(r'''VIR_ACCESS_PERM_(%s)_((?:\w|_)+),''' % + objectstr, line) + if m is not None: + obj =3D m.group(1).lower() + perm =3D m.group(2).lower() + if perm =3D=3D "last": + continue + + obj =3D obj.replace("_", "-") + perm =3D perm.replace("_", "-") + + if obj not in perms: + perms[obj] =3D {} + perms[obj][perm] =3D { + "desc": opts.get("desc", None), + "message": opts.get("message", None), + "anonymous": opts.get("anonymous", None), + } + opts =3D {} + +print('') +print('') +print('') +print(' Libvirt Project') +print(' https://libvirt.org') + +for obj in sorted(perms.keys()): + for perm in sorted(perms[obj].keys()): + description =3D perms[obj][perm]["desc"] + message =3D perms[obj][perm]["message"] + anonymous =3D perms[obj][perm]["anonymous"] + + if description is None: + raise Exception("missing description for %s.%s" % (obj, perm)) + if message is None: + raise Exception("missing message for %s.%s" % (obj, perm)) + + allow_any =3D "no" + if anonymous: + allow_any =3D "yes" + allow_inactive =3D allow_any + allow_active =3D allow_any + + print(' ' % (obj, perm)) + print(' %s' % description) + print(' %s' % message) + print(' ') + print(' %s' % allow_any) + print(' %s' % allow_inactive) + print(' %s' % allow_active) + print(' ') + print(' ') + +print('') diff --git a/src/access/Makefile.inc.am b/src/access/Makefile.inc.am index fd0a5d8098..11f87c6aa7 100644 --- a/src/access/Makefile.inc.am +++ b/src/access/Makefile.inc.am @@ -43,7 +43,6 @@ ACCESS_DRIVER_POLKIT_POLICY =3D access/org.libvirt.api.po= licy GENERATED_SYM_FILES +=3D $(ACCESS_DRIVER_SYM_FILES) =20 EXTRA_DIST +=3D \ - access/genpolkit.pl \ $(NULL) =20 =20 @@ -66,8 +65,9 @@ libvirt_driver_access_la_LIBADD =3D \ =20 =20 $(ACCESS_DRIVER_POLKIT_POLICY): $(srcdir)/access/viraccessperm.h \ - $(srcdir)/access/genpolkit.pl Makefile.am - $(AM_V_GEN)$(PERL) $(srcdir)/access/genpolkit.pl < $< > $@ || rm -f $@ + $(top_srcdir)/scripts/genpolkit.py Makefile.am + $(AM_V_GEN)$(RUNUTF8) $(PYTHON) \ + $(top_srcdir)/scripts/genpolkit.py $< > $@ || rm -f $@ =20 if WITH_POLKIT libvirt_driver_access_la_SOURCES +=3D $(ACCESS_DRIVER_POLKIT_SOURCES) diff --git a/src/access/genpolkit.pl b/src/access/genpolkit.pl deleted file mode 100755 index f8f20caf65..0000000000 --- a/src/access/genpolkit.pl +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env perl -# -# Copyright (C) 2012-2013 Red Hat, Inc. -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see -# . -# - -use strict; -use warnings; - -my @objects =3D ( - "CONNECT", "DOMAIN", "INTERFACE", "NETWORK_PORT", - "NETWORK","NODE_DEVICE", "NWFILTER_BINDING", "NWFILTER", - "SECRET", "STORAGE_POOL", "STORAGE_VOL", - ); - -my $objects =3D join ("|", @objects); - -# Data we're going to be generating looks like this -# -# -# -# Monitor local virtualized systems -# System policy prevents monitoring of local virtualized syst= ems -# -# yes -# yes -# yes -# -# -# ...more rules... -# - -my %opts; -my $in_opts =3D 0; - -my %perms; - -while (<>) { - if ($in_opts) { - if (m,\*/,) { - $in_opts =3D 0; - } elsif (/\*\s*\@(\w+):\s*(.*?)\s*$/) { - $opts{$1} =3D $2; - } - } elsif (m,/\*\*,) { - $in_opts =3D 1; - } elsif (/VIR_ACCESS_PERM_($objects)_((?:\w|_)+),/) { - my $object =3D lc $1; - my $perm =3D lc $2; - next if $perm eq "last"; - - $object =3D~ s/_/-/g; - $perm =3D~ s/_/-/g; - - $perms{$object} =3D {} unless exists $perms{$object}; - $perms{$object}->{$perm} =3D { - desc =3D> $opts{desc}, - message =3D> $opts{message}, - anonymous =3D> $opts{anonymous} - }; - %opts =3D (); - } -} - -print < - - - Libvirt Project - https://libvirt.org -EOF - -foreach my $object (sort { $a cmp $b } keys %perms) { - foreach my $perm (sort { $a cmp $b } keys %{$perms{$object}}) { - my $description =3D $perms{$object}->{$perm}->{desc}; - my $message =3D $perms{$object}->{$perm}->{message}; - my $anonymous =3D $perms{$object}->{$perm}->{anonymous}; - - die "missing description for $object.$perm" unless - defined $description; - die "missing message for $object.$perm" unless - defined $message; - - my $allow_any =3D $anonymous ? "yes" : "no"; - my $allow_inactive =3D $allow_any; - my $allow_active =3D $allow_any; - - print < - $description - $message - - $allow_any - $allow_inactive - $allow_active - - -EOF - - } -} - -print < -EOF --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list