]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/Subscription.pm
website: update external links to www.proxmox.com
[pve-manager.git] / PVE / API2 / Subscription.pm
CommitLineData
21ace8d3
DM
1package PVE::API2::Subscription;
2
3use strict;
4use warnings;
5use Digest::MD5 qw(md5_hex md5_base64);
6use MIME::Base64;
7use HTTP::Request;
8use LWP::UserAgent;
43fc27a4 9use JSON;
21ace8d3 10
d017de1f
FG
11use Proxmox::RS::Subscription;
12
21ace8d3 13use PVE::Tools;
8f9217dc 14use PVE::ProcFSTools;
21ace8d3
DM
15use PVE::Exception qw(raise_param_exc);
16use PVE::INotify;
17use PVE::Cluster qw (cfs_read_file cfs_write_file);
3ac3653e 18use PVE::DataCenterConfig;
21ace8d3
DM
19use PVE::AccessControl;
20use PVE::Storage;
21use PVE::JSONSchema qw(get_standard_option);
22
23use PVE::SafeSyslog;
24
25use PVE::API2Tools;
26use PVE::RESTHandler;
27
28use base qw(PVE::RESTHandler);
29
9fdfebf7 30my $subscription_pattern = 'pve([1248])([cbsp])-[0-9a-f]{10}';
d017de1f 31my $filename = "/etc/subscription";
21ace8d3 32
21ace8d3 33sub get_sockets {
8f9217dc
DM
34 my $info = PVE::ProcFSTools::read_cpuinfo();
35 return $info->{sockets};
21ace8d3
DM
36}
37
38sub parse_key {
43fc27a4 39 my ($key, $noerr) = @_;
21ace8d3 40
43fc27a4 41 if ($key =~ m/^${subscription_pattern}$/) {
16b69b6c 42 return wantarray ? ($1, $2) : $1; # number of sockets, level
21ace8d3 43 }
43fc27a4
DM
44 return undef if $noerr;
45
46 die "Wrong subscription key format\n";
21ace8d3
DM
47}
48
43fc27a4
DM
49sub check_key {
50 my ($key, $req_sockets) = @_;
21ace8d3 51
43fc27a4 52 my ($sockets, $level) = parse_key($key);
21ace8d3
DM
53 if ($sockets < $req_sockets) {
54 die "wrong number of sockets ($sockets < $req_sockets)\n";
55 }
43fc27a4 56 return ($sockets, $level);
21ace8d3
DM
57}
58
d017de1f 59sub read_etc_subscription {
43fc27a4
DM
60 my $req_sockets = get_sockets();
61 my $server_id = PVE::API2Tools::get_hwaddress();
21ace8d3 62
d017de1f 63 my $info = Proxmox::RS::Subscription::read_subscription($filename);
21ace8d3 64
25eaf729 65 return $info if !$info || $info->{status} ne 'active';
21ace8d3 66
43fc27a4
DM
67 my ($sockets, $level);
68 eval { ($sockets, $level) = check_key($info->{key}, $req_sockets); };
69 if (my $err = $@) {
70 chomp $err;
d017de1f 71 $info->{status} = 'invalid';
43fc27a4
DM
72 $info->{message} = $err;
73 } else {
16b69b6c
DM
74 $info->{level} = $level;
75 }
76
21ace8d3
DM
77 return $info;
78}
79
d017de1f
FG
80sub write_etc_subscription {
81 my ($info) = @_;
21ace8d3 82
2ba6d822 83 my $server_id = PVE::API2Tools::get_hwaddress();
d017de1f
FG
84 mkdir "/etc/apt/auth.conf.d";
85 Proxmox::RS::Subscription::write_subscription($filename, "/etc/apt/auth.conf.d/pve.conf", "enterprise.proxmox.com/debian/pve", $info);
31e5ac13
TL
86
87 # NOTE: preparation for easier upgrade to Proxmox VE 8, which introduced the ceph enterprise repo
88 my $ceph_auth = '';
89 for my $ceph_release ('quincy', 'reef') {
90 $ceph_auth .= "machine enterprise.proxmox.com/debian/ceph-${ceph_release}"
91 ." login $info->{key} password $info->{serverid}\n"
92 }
93 PVE::Tools::file_set_contents("/etc/apt/auth.conf.d/ceph.conf", $ceph_auth);
21ace8d3
DM
94}
95
96__PACKAGE__->register_method ({
43fc27a4
DM
97 name => 'get',
98 path => '',
21ace8d3
DM
99 method => 'GET',
100 description => "Read subscription info.",
101 proxyto => 'node',
e721a829 102 permissions => { user => 'all' },
21ace8d3
DM
103 parameters => {
104 additionalProperties => 0,
105 properties => {
106 node => get_standard_option('pve-node'),
107 },
108 },
109 returns => { type => 'object'},
110 code => sub {
111 my ($param) = @_;
112
7d6fba8f 113 my $node = $param->{node};
00a93a4b 114
2d2ed7ab
DM
115 my $rpcenv = PVE::RPCEnvironment::get();
116 my $authuser = $rpcenv->get_user();
d8645329 117 my $has_permission = $rpcenv->check($authuser, "/nodes/$node", ['Sys.Audit'], 1);
2d2ed7ab 118
7d762f4c 119 my $server_id = PVE::API2Tools::get_hwaddress();
bcab50eb 120 my $url = "https://www.proxmox.com/en/proxmox-virtual-environment/pricing";
7d762f4c 121
d017de1f 122 my $info = read_etc_subscription();
21ace8d3 123 if (!$info) {
2d2ed7ab 124 my $no_subscription_info = {
d017de1f 125 status => "notfound",
21ace8d3 126 message => "There is no subscription key",
2d2ed7ab
DM
127 url => $url,
128 };
129 $no_subscription_info->{serverid} = $server_id if $has_permission;
130 return $no_subscription_info;
131 }
132
133 if (!$has_permission) {
134 return {
135 status => $info->{status},
136 message => $info->{message},
43fc27a4 137 url => $url,
21ace8d3
DM
138 }
139 }
00a93a4b
DM
140
141 $info->{serverid} = $server_id;
142 $info->{sockets} = get_sockets();
43fc27a4 143 $info->{url} = $url;
00a93a4b 144
21ace8d3
DM
145 return $info
146 }});
147
148__PACKAGE__->register_method ({
43fc27a4
DM
149 name => 'update',
150 path => '',
21ace8d3 151 method => 'POST',
69bbb885
TL
152 permissions => {
153 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
154 },
21ace8d3
DM
155 description => "Update subscription info.",
156 proxyto => 'node',
157 protected => 1,
158 parameters => {
159 additionalProperties => 0,
160 properties => {
161 node => get_standard_option('pve-node'),
162 force => {
163 description => "Always connect to server, even if we have up to date info inside local cache.",
164 type => 'boolean',
165 optional => 1,
166 default => 0
167 }
168 },
169 },
170 returns => { type => 'null'},
171 code => sub {
172 my ($param) = @_;
173
d017de1f 174 my $info = read_etc_subscription();
21ace8d3
DM
175 return undef if !$info;
176
43fc27a4
DM
177 my $server_id = PVE::API2Tools::get_hwaddress();
178 my $key = $info->{key};
179
d4df1b14
FG
180 die "Updating offline key not possible - please remove and re-add subscription key to switch to online key.\n"
181 if $info->{signature};
182
183 # key has been recently checked
d017de1f
FG
184 return undef
185 if !$param->{force}
186 && $info->{status} eq 'active'
187 && Proxmox::RS::Subscription::check_age($info, 1)->{status} eq 'active';
21ace8d3 188
43fc27a4
DM
189 my $req_sockets = get_sockets();
190 check_key($key, $req_sockets);
191
192 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
193 my $proxy = $dccfg->{http_proxy};
194
d017de1f 195 $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox VE", $proxy);
21ace8d3 196
d017de1f 197 write_etc_subscription($info);
21ace8d3
DM
198
199 return undef;
200 }});
201
202__PACKAGE__->register_method ({
43fc27a4
DM
203 name => 'set',
204 path => '',
21ace8d3 205 method => 'PUT',
69bbb885
TL
206 permissions => {
207 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
208 },
21ace8d3
DM
209 description => "Set subscription key.",
210 proxyto => 'node',
211 protected => 1,
212 parameters => {
e82ed167 213 additionalProperties => 0,
21ace8d3
DM
214 properties => {
215 node => get_standard_option('pve-node'),
216 key => {
217 description => "Proxmox VE subscription key",
218 type => 'string',
43fc27a4
DM
219 pattern => $subscription_pattern,
220 maxLength => 32,
21ace8d3
DM
221 },
222 },
223 },
224 returns => { type => 'null'},
225 code => sub {
226 my ($param) = @_;
227
43fc27a4 228 my $key = PVE::Tools::trim($param->{key});
94cc9560 229
21ace8d3
DM
230 my $info = {
231 status => 'New',
43fc27a4 232 key => $key,
21ace8d3
DM
233 checktime => time(),
234 };
235
236 my $req_sockets = get_sockets();
2ba6d822 237 my $server_id = PVE::API2Tools::get_hwaddress();
21ace8d3 238
43fc27a4 239 check_key($key, $req_sockets);
21ace8d3 240
d017de1f 241 write_etc_subscription($info);
21ace8d3 242
43fc27a4
DM
243 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
244 my $proxy = $dccfg->{http_proxy};
245
d017de1f 246 $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox VE", $proxy);
21ace8d3 247
d017de1f 248 write_etc_subscription($info);
21ace8d3
DM
249
250 return undef;
251 }});
252
85222f82
MA
253__PACKAGE__->register_method ({
254 name => 'delete',
255 path => '',
256 method => 'DELETE',
257 permissions => {
258 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
259 },
bc12fba5 260 description => "Delete subscription key of this node.",
85222f82
MA
261 proxyto => 'node',
262 protected => 1,
263 parameters => {
e82ed167 264 additionalProperties => 0,
85222f82
MA
265 properties => {
266 node => get_standard_option('pve-node'),
267 },
268 },
269 returns => { type => 'null'},
270 code => sub {
271 my $subscription_file = '/etc/subscription';
e82ed167 272 return if ! -e $subscription_file;
85222f82
MA
273 unlink($subscription_file) or die "cannot delete subscription key: $!";
274 return undef;
275 }});
276
21ace8d3 2771;