]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/NodeConfig.pm
api: nodeconfig: validate acme config before writing
[pmg-api.git] / src / PMG / API2 / NodeConfig.pm
1 package PMG::API2::NodeConfig;
2
3 use strict;
4 use warnings;
5
6 use PVE::JSONSchema qw(get_standard_option);
7 use PVE::Tools qw(extract_param);
8
9 use PMG::NodeConfig;
10
11 use base qw(PVE::RESTHandler);
12
13 __PACKAGE__->register_method ({
14 name => 'get_config',
15 path => '',
16 method => 'GET',
17 description => "Get node configuration options.",
18 protected => 1,
19 proxyto => 'node',
20 permissions => { check => [ 'admin', 'audit' ] },
21 parameters => {
22 additionalProperties => 0,
23 properties => {
24 node => get_standard_option('pve-node'),
25 },
26 },
27 returns => PMG::NodeConfig::acme_config_schema({
28 digest => {
29 type => 'string',
30 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
31 maxLength => 40,
32 optional => 1,
33 },
34 }),
35 code => sub {
36 my ($param) = @_;
37
38 return PMG::NodeConfig::load_config();
39 }});
40
41 __PACKAGE__->register_method ({
42 name => 'set_config',
43 path => '',
44 method => 'PUT',
45 description => "Set node configuration options.",
46 protected => 1,
47 proxyto => 'node',
48 permissions => { check => [ 'admin', 'audit' ] },
49 parameters => PMG::NodeConfig::acme_config_schema({
50 delete => {
51 type => 'string', format => 'pve-configid-list',
52 description => "A list of settings you want to delete.",
53 optional => 1,
54 },
55 digest => {
56 type => 'string',
57 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
58 maxLength => 40,
59 optional => 1,
60 },
61 node => get_standard_option('pve-node'),
62 }),
63 returns => { type => "null" },
64 code => sub {
65 my ($param) = @_;
66
67 my $node = extract_param($param, 'node');
68 my $delete = extract_param($param, 'delete');
69 my $digest = extract_param($param, 'digest');
70
71 PMG::NodeConfig::lock_config(sub {
72 my $conf = PMG::NodeConfig::load_config();
73
74 PVE::Tools::assert_if_modified($digest, delete $conf->{digest});
75
76 foreach my $opt (PVE::Tools::split_list($delete)) {
77 delete $conf->{$opt};
78 };
79
80 foreach my $opt (keys %$param) {
81 $conf->{$opt} = $param->{$opt};
82 }
83
84 #validate the acme config (check for duplicates)
85 PMG::NodeConfig::get_acme_conf($conf);
86
87 PMG::NodeConfig::write_config($conf);
88 });
89
90 return undef;
91 }});
92
93 1;