]> git.proxmox.com Git - pmg-api.git/commitdiff
subscription: switch to rust, add offline key support
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 21 Jul 2022 10:02:49 +0000 (12:02 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 6 Sep 2022 09:02:25 +0000 (11:02 +0200)
mimicing changes in pve-manager

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
src/PMG/API2/APT.pm
src/PMG/API2/Nodes.pm
src/PMG/API2/Subscription.pm
src/PMG/CLI/pmgsubscription.pm
src/bin/pmg-daily

index d6f050f7d9446beb740e8ae7986316ddf6af0b38..c65fd19cb0300e5a31345322714aeadc8de2d6d5 100644 (file)
@@ -451,8 +451,8 @@ __PACKAGE__->register_method({
        my $pw;
 
        if ($pkgfile->{Origin} eq 'Proxmox' && $pkgfile->{Component} eq 'pmg-enterprise') {
-           my $info = PVE::INotify::read_file('subscription');
-           if ($info->{status} eq 'Active') {
+           my $info = PMG::API2::Subscription::read_etc_subscription();
+           if ($info->{status} eq 'active') {
                $username = $info->{key};
                $pw = PMG::Utils::get_hwaddress();
                $ua->credentials("enterprise.proxmox.com:443", 'pmg-enterprise-repository',
index 472a5f3a45e22d5224d73200049a00b8c027afe1..873434b6d55ebd46063fda71d98158c51d63f6b9 100644 (file)
@@ -772,7 +772,7 @@ __PACKAGE__->register_method({
            free => $dinfo->{blocks} - $dinfo->{used},
        };
 
-       if (my $subinfo = PVE::INotify::read_file('subscription')) {
+       if (my $subinfo = eval { PMG::API2::Subscription::read_etc_subscription() } ) {
            if (my $level = $subinfo->{level}) {
                $res->{level} = $level;
            }
index a231cbd3cebb360268d59f014153f0a7585b4b0c..c30718c709d7d2343bfbedb5cb3fefca493aad05 100644 (file)
@@ -3,6 +3,8 @@ package PMG::API2::Subscription;
 use strict;
 use warnings;
 
+use Proxmox::RS::Subscription;
+
 use PVE::Tools;
 use PVE::SafeSyslog;
 use PVE::INotify;
@@ -10,18 +12,14 @@ use PVE::Exception qw(raise_param_exc);
 use PVE::RESTHandler;
 use PMG::RESTEnvironment;
 use PVE::JSONSchema qw(get_standard_option);
-use PVE::Subscription;
 
 use PMG::Utils;
 use PMG::Config;
 
 use base qw(PVE::RESTHandler);
 
-PVE::INotify::register_file('subscription', "/etc/pmg/subscription",
-                           \&read_etc_pmg_subscription,
-                           \&write_etc_pmg_subscription);
-
 my $subscription_pattern = 'pmg([cbsp])-[0-9a-f]{10}';
+my $filename = "/etc/pmg/subscription";
 
 sub parse_key {
     my ($key, $noerr) = @_;
@@ -34,27 +32,26 @@ sub parse_key {
     die "Wrong subscription key format\n";
 }
 
-sub read_etc_pmg_subscription {
-    my ($filename, $fh) = @_;
-
+sub read_etc_subscription {
     my $server_id = PMG::Utils::get_hwaddress();
 
-    my $info = PVE::Subscription::read_subscription($server_id, $filename, $fh);
+    my $info = Proxmox::RS::Subscription::read_subscription($filename);
+
     my $level = parse_key($info->{key});
 
-    if ($info->{status} eq 'Active') {
+    if ($info->{status} eq 'active') {
        $info->{level} = $level;
     }
 
     return $info;
 };
 
-sub write_etc_pmg_subscription {
-    my ($filename, $fh, $info) = @_;
+sub write_etc_subscription {
+    my ($info) = @_;
 
     my $server_id = PMG::Utils::get_hwaddress();
 
-    PVE::Subscription::write_subscription($server_id, $filename, $fh, $info);
+    Proxmox::RS::Subscription::write_subscription($filename, "/etc/apt/auth.conf.d/pmg.conf", "enterprise.proxmox.com/debian/pmg", $info);
 }
 
 __PACKAGE__->register_method ({
@@ -76,10 +73,10 @@ __PACKAGE__->register_method ({
 
        my $server_id = PMG::Utils::get_hwaddress();
        my $url = "https://www.proxmox.com/proxmox-mail-gateway/pricing";
-       my $info = PVE::INotify::read_file('subscription');
+       my $info = read_etc_subscription();
        if (!$info) {
            return {
-               status => "NotFound",
+               status => "notfound",
                message => "There is no subscription key",
                serverid => $server_id,
                url => $url,
@@ -116,27 +113,24 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $info = PVE::INotify::read_file('subscription');
+       my $info = read_etc_subscription();
        return undef if !$info;
 
        my $server_id = PMG::Utils::get_hwaddress();
        my $key = $info->{key};
 
-       if ($key) {
-           PVE::Subscription::update_apt_auth($key, $server_id);
-       }
-
-       if (!$param->{force} && $info->{status} eq 'Active') {
-           my $age = time() -  $info->{checktime};
-           return undef if $age < $PVE::Subscription::localkeydays*60*60*24;
-       }
+       # key has been recently checked
+       return undef
+           if !$param->{force}
+               && $info->{status} eq 'active'
+               && Proxmox::RS::Subscription::check_age($info, 1)->{status} eq 'active';
 
        my $pmg_cfg = PMG::Config->new();
        my $proxy = $pmg_cfg->get('admin', 'http_proxy');
 
-       $info = PVE::Subscription::check_subscription($key, $server_id, $proxy);
+       $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox Mail Gateway", $proxy);
 
-       PVE::INotify::write_file('subscription', $info);
+       write_etc_subscription($info);
 
        return undef;
     }});
@@ -169,21 +163,21 @@ __PACKAGE__->register_method ({
        my $level = parse_key($key);
 
        my $info = {
-           status => 'New',
+           status => 'new',
            key => $key,
            checktime => time(),
        };
 
        my $server_id = PMG::Utils::get_hwaddress();
 
-       PVE::INotify::write_file('subscription', $info);
+       write_etc_subscription($info);
 
        my $pmg_cfg = PMG::Config->new();
        my $proxy = $pmg_cfg->get('admin', 'http_proxy');
 
-       $info = PVE::Subscription::check_subscription($key, $server_id, $proxy);
+       $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox Mail Gateway", $proxy);
 
-       PVE::INotify::write_file('subscription', $info);
+       write_etc_subscription($info);
 
        return undef;
     }});
@@ -203,9 +197,8 @@ __PACKAGE__->register_method ({
     },
     returns => { type => 'null'},
     code => sub {
-       my $subscription_file = '/etc/pmg/subscription';
-       return if ! -e $subscription_file;
-       unlink($subscription_file) or die "cannot delete subscription key: $!";
+       return if ! -e $filename;
+       unlink($filename) or die "cannot delete subscription key: $!";
        return undef;
     }});
 
index a4d5b86f1c60789d1c184fcb6650df80fa32fa20..373a46eded50ed653c856586bf87f3ce1ea5142b 100755 (executable)
@@ -3,6 +3,9 @@ package PMG::CLI::pmgsubscription;
 use strict;
 use warnings;
 
+use MIME::Base64;
+use JSON qw(decode_json);
+
 use PVE::Tools;
 use PVE::SafeSyslog;
 use PVE::INotify;
@@ -15,6 +18,32 @@ use base qw(PVE::CLIHandler);
 
 my $nodename = PVE::INotify::nodename();
 
+__PACKAGE__->register_method({
+    name => 'set_offline_key',
+    path => 'set_offline_key',
+    method => 'POST',
+    description => "(Internal use only!) Set a signed subscription info blob as offline key",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           data => {
+                type => "string",
+           },
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $info = decode_json(decode_base64($param->{data}));
+
+       $info = Proxmox::RS::Subscription::check_signature($info);
+       $info = Proxmox::RS::Subscription::check_server_id($info);
+       $info = Proxmox::RS::Subscription::check_age($info, 0);
+
+       PMG::API2::Subscription::write_etc_subscription($info);
+ }});
+
 sub setup_environment {
     PMG::RESTEnvironment->setup_default_cli_env();
 }
@@ -29,6 +58,7 @@ our $cmddef = {
                 }
             }],
     set => [ 'PMG::API2::Subscription', 'set', ['key'], { node => $nodename } ],
+    "set-offline-key" => [ __PACKAGE__, 'set_offline_key', ['data'] ],
     delete => [ 'PMG::API2::Subscription', 'delete', undef, { node => $nodename } ],
 };
 
index d67a88ac8aad0df8226e0c3a2e9793497af64036..e66b66d92d11ab80473b2e6a57999d433ede69d0 100755 (executable)
@@ -61,8 +61,8 @@ if (my $statlifetime = $cfg->get ('admin', 'statlifetime')) {
 # check for available updates
 # We assume that users with subscriptions want information
 # about new packages.
-my $info = PVE::INotify::read_file('subscription');
-my $notify = ($info && $info->{status} eq 'Active') ? 1 : 0;
+my $info = eval { PMG::API2::Subscription::read_etc_subscription() };
+my $notify = ($info && $info->{status} eq 'active') ? 1 : 0;
 eval { PMG::API2::APT->update_database({ node => $nodename, notify => $notify, quiet => 1 }); };
 if (my $err = $@) {
     syslog ('err', "update apt database failed: $err");