]> git.proxmox.com Git - pve-manager.git/blame - PVE/NodeConfig.pm
api2: network: improve code readability
[pve-manager.git] / PVE / NodeConfig.pm
CommitLineData
c4f78bb7
FG
1package PVE::NodeConfig;
2
3use strict;
4use warnings;
5
6use PVE::CertHelpers;
7use PVE::JSONSchema qw(get_standard_option);
8use PVE::Tools qw(file_get_contents file_set_contents lock_file);
cc442d3e
WL
9use PVE::ACME;
10
de9aa678
TL
11use PVE::API2::ACMEPlugin;
12
667198c4
TL
13# register up to 5 domain names per node for now
14my $MAXDOMAINS = 5;
c4f78bb7
FG
15
16my $node_config_lock = '/var/lock/pvenode.lock';
17
18PVE::JSONSchema::register_format('pve-acme-domain', sub {
19 my ($domain, $noerr) = @_;
20
4dab82e3 21 my $label = qr/[a-z0-9][a-z0-9_-]*/i;
c4f78bb7
FG
22
23 return $domain if $domain =~ /^$label(?:\.$label)+$/;
24 return undef if $noerr;
85b275b1 25 die "value '$domain' does not look like a valid domain name!\n";
c4f78bb7
FG
26});
27
d1dd680b 28PVE::JSONSchema::register_format('pve-acme-alias', sub {
b3fbf2ac 29 my ($alias, $noerr) = @_;
d1dd680b
FM
30
31 my $label = qr/[a-z0-9_][a-z0-9_-]*/i;
32
b3fbf2ac 33 return $alias if $alias =~ /^$label(?:\.$label)+$/;
d1dd680b 34 return undef if $noerr;
b3fbf2ac 35 die "value '$alias' does not look like a valid alias name!\n";
d1dd680b
FM
36});
37
c4f78bb7
FG
38sub config_file {
39 my ($node) = @_;
40
41 return "/etc/pve/nodes/${node}/config";
42}
43
44sub load_config {
45 my ($node) = @_;
46
47 my $filename = config_file($node);
48 my $raw = eval { PVE::Tools::file_get_contents($filename); };
49 return {} if !$raw;
50
9556af61 51 return parse_node_config($raw, $filename);
c4f78bb7
FG
52}
53
54sub write_config {
55 my ($node, $conf) = @_;
56
57 my $filename = config_file($node);
58
59 my $raw = write_node_config($conf);
60
61 PVE::Tools::file_set_contents($filename, $raw);
62}
63
64sub lock_config {
3c194d9e
FG
65 my ($node, $realcode, @param) = @_;
66
67 # make sure configuration file is up-to-date
68 my $code = sub {
69 PVE::Cluster::cfs_update();
70 $realcode->(@_);
71 };
c4f78bb7
FG
72
73 my $res = lock_file($node_config_lock, 10, $code, @param);
74
75 die $@ if $@;
76
77 return $res;
78}
79
80my $confdesc = {
81 description => {
82 type => 'string',
e466f896
TL
83 description => "Description for the Node. Shown in the web-interface node notes panel."
84 ." This is saved as comment inside the configuration file.",
85 maxLength => 64 * 1024,
c4f78bb7
FG
86 optional => 1,
87 },
b3d84542
CE
88 wakeonlan => {
89 type => 'string',
90 description => 'MAC address for wake on LAN',
0f615ea9 91 format => 'mac-addr',
b3d84542
CE
92 optional => 1,
93 },
1ff3fef0
TL
94 'startall-onboot-delay' => {
95 description => 'Initial delay in seconds, before starting all the Virtual Guests with on-boot enabled.',
96 type => 'integer',
97 minimum => 0,
98 maximum => 300,
99 default => 0,
100 optional => 1,
101 },
c4f78bb7
FG
102};
103
ab53e139 104my $acme_domain_desc = {
cc442d3e
WL
105 domain => {
106 type => 'string',
107 format => 'pve-acme-domain',
108 format_description => 'domain',
109 description => 'domain for this node\'s ACME certificate',
dcfb9f2c 110 default_key => 1,
cc442d3e
WL
111 },
112 plugin => {
113 type => 'string',
114 format => 'pve-configid',
9d17b945 115 description => 'The ACME plugin ID',
cc442d3e 116 format_description => 'name of the plugin configuration',
9d17b945
FG
117 optional => 1,
118 default => 'standalone',
cc442d3e
WL
119 },
120 alias => {
121 type => 'string',
d1dd680b 122 format => 'pve-acme-alias',
cc442d3e
WL
123 format_description => 'domain',
124 description => 'Alias for the Domain to verify ACME Challenge over DNS',
125 optional => 1,
126 },
127};
cc442d3e 128
c4f78bb7
FG
129my $acmedesc = {
130 account => get_standard_option('pve-acme-account-name'),
131 domains => {
132 type => 'string',
133 format => 'pve-acme-domain-list',
134 format_description => 'domain[;domain;...]',
135 description => 'List of domains for this node\'s ACME certificate',
cc442d3e 136 optional => 1,
c4f78bb7
FG
137 },
138};
c4f78bb7
FG
139
140$confdesc->{acme} = {
141 type => 'string',
142 description => 'Node specific ACME settings.',
143 format => $acmedesc,
144 optional => 1,
145};
146
cc442d3e 147for my $i (0..$MAXDOMAINS) {
ab53e139 148 $confdesc->{"acmedomain$i"} = {
cc442d3e 149 type => 'string',
ab53e139
FG
150 description => 'ACME domain and validation plugin',
151 format => $acme_domain_desc,
cc442d3e
WL
152 optional => 1,
153 };
154};
155
9556af61
WB
156my $conf_schema = {
157 type => 'object',
158 properties => $confdesc,
159};
e79894eb 160
9556af61
WB
161sub parse_node_config : prototype($$) {
162 my ($content, $filename) = @_;
c4f78bb7
FG
163
164 return undef if !defined($content);
9556af61 165 my $digest = Digest::SHA::sha1_hex($content);
c4f78bb7 166
9556af61
WB
167 my $conf = PVE::JSONSchema::parse_config($conf_schema, $filename, $content, 'description');
168 $conf->{digest} = $digest;
c4f78bb7
FG
169
170 return $conf;
171}
172
173sub write_node_config {
174 my ($conf) = @_;
175
176 my $raw = '';
177 # add description as comment to top of file
178 my $descr = $conf->{description} || '';
179 foreach my $cl (split(/\n/, $descr)) {
180 $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
181 }
182
183 for my $key (sort keys %$conf) {
184 next if ($key eq 'description');
185 next if ($key eq 'digest');
186
187 my $value = $conf->{$key};
188 die "detected invalid newline inside property '$key'\n"
189 if $value =~ m/\n/;
190 $raw .= "$key: $value\n";
191 }
192
193 return $raw;
194}
195
4aa89cc0
FG
196# we always convert domain values to lower case, since DNS entries are not case
197# sensitive and ACME implementations might convert the ordered identifiers
198# to lower case
c30e112e 199sub get_acme_conf {
7ffd1550 200 my ($node_conf, $noerr) = @_;
c4f78bb7 201
7ffd1550 202 $node_conf //= {};
c4f78bb7 203
c30e112e 204 my $res = {};
7ffd1550 205 if (defined($node_conf->{acme})) {
b232807d 206 $res = eval {
7ffd1550 207 PVE::JSONSchema::parse_property_string($acmedesc, $node_conf->{acme})
c30e112e 208 };
7ffd1550 209 if (my $err = $@) {
c30e112e 210 return undef if $noerr;
7ffd1550 211 die $err;
c30e112e 212 }
b232807d 213 my $standalone_domains = delete($res->{domains}) // '';
b7def515 214 $res->{domains} = {};
7ffd1550 215 for my $domain (split(";", $standalone_domains)) {
4aa89cc0
FG
216 $domain = lc($domain);
217 die "duplicate domain '$domain' in ACME config properties\n"
218 if defined($res->{domains}->{$domain});
219
b232807d 220 $res->{domains}->{$domain}->{plugin} = 'standalone';
75afd54a 221 $res->{domains}->{$domain}->{_configkey} = 'acme';
b232807d 222 }
c4f78bb7 223 }
b232807d
FG
224
225 $res->{account} //= 'default';
c30e112e
WL
226
227 for my $index (0..$MAXDOMAINS) {
7ffd1550 228 my $domain_rec = $node_conf->{"acmedomain$index"};
c30e112e
WL
229 next if !defined($domain_rec);
230
b232807d 231 my $parsed = eval {
7ffd1550 232 PVE::JSONSchema::parse_property_string($acme_domain_desc, $domain_rec)
c30e112e 233 };
7ffd1550 234 if (my $err = $@) {
c30e112e 235 return undef if $noerr;
7ffd1550 236 die $err;
c30e112e 237 }
4aa89cc0 238 my $domain = lc(delete $parsed->{domain});
75afd54a 239 if (my $exists = $res->{domains}->{$domain}) {
b232807d 240 return undef if $noerr;
75afd54a
TL
241 die "duplicate domain '$domain' in ACME config properties"
242 ." 'acmedomain$index' and '$exists->{_configkey}'\n";
b232807d 243 }
9d17b945 244 $parsed->{plugin} //= 'standalone';
de9aa678
TL
245
246 my $plugin_id = $parsed->{plugin};
247 if ($plugin_id ne 'standalone') {
248 my $plugins = PVE::API2::ACMEPlugin::load_config();
249 die "plugin '$plugin_id' for domain '$domain' not found!\n"
250 if !$plugins->{ids}->{$plugin_id};
251 }
252
75afd54a 253 $parsed->{_configkey} = "acmedomain$index";
b232807d 254 $res->{domains}->{$domain} = $parsed;
c30e112e
WL
255 }
256
c4f78bb7
FG
257 return $res;
258}
259
75afd54a
TL
260# expects that basic format verification was already done, this is more higher
261# level verification
262sub verify_conf {
263 my ($node_conf) = @_;
264
265 # verify ACME domain uniqueness
266 my $tmp = get_acme_conf($node_conf);
267
268 # TODO: what else?
269
270 return 1; # OK
271}
272
c4f78bb7
FG
273sub get_nodeconfig_schema {
274 return $confdesc;
275}
276
2771;