]>
Commit | Line | Data |
---|---|---|
b4366f00 DM |
1 | package PVE::API2::Firewall::Cluster; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
1df4ba7e | 5 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); |
b4366f00 DM |
6 | use PVE::JSONSchema qw(get_standard_option); |
7 | ||
8 | use PVE::Firewall; | |
81d574a7 | 9 | use PVE::API2::Firewall::Aliases; |
86791289 | 10 | use PVE::API2::Firewall::Rules; |
b4366f00 | 11 | use PVE::API2::Firewall::Groups; |
009ee3ac | 12 | use PVE::API2::Firewall::IPSet; |
b4366f00 | 13 | |
1df4ba7e DM |
14 | #fixme: locking? |
15 | ||
b4366f00 DM |
16 | use Data::Dumper; # fixme: remove |
17 | ||
18 | use base qw(PVE::RESTHandler); | |
19 | ||
20 | __PACKAGE__->register_method ({ | |
947d6ea2 | 21 | subclass => "PVE::API2::Firewall::Groups", |
b4366f00 DM |
22 | path => 'groups', |
23 | }); | |
24 | ||
86791289 | 25 | __PACKAGE__->register_method ({ |
947d6ea2 | 26 | subclass => "PVE::API2::Firewall::ClusterRules", |
86791289 DM |
27 | path => 'rules', |
28 | }); | |
29 | ||
c85c87f9 | 30 | __PACKAGE__->register_method ({ |
947d6ea2 | 31 | subclass => "PVE::API2::Firewall::ClusterIPSetList", |
c85c87f9 DM |
32 | path => 'ipset', |
33 | }); | |
34 | ||
81d574a7 | 35 | __PACKAGE__->register_method ({ |
947d6ea2 | 36 | subclass => "PVE::API2::Firewall::ClusterAliases", |
81d574a7 DM |
37 | path => 'aliases', |
38 | }); | |
39 | ||
40 | ||
b4366f00 DM |
41 | __PACKAGE__->register_method({ |
42 | name => 'index', | |
43 | path => '', | |
44 | method => 'GET', | |
45 | permissions => { user => 'all' }, | |
46 | description => "Directory index.", | |
47 | parameters => { | |
48 | additionalProperties => 0, | |
49 | }, | |
50 | returns => { | |
51 | type => 'array', | |
52 | items => { | |
53 | type => "object", | |
54 | properties => {}, | |
55 | }, | |
56 | links => [ { rel => 'child', href => "{name}" } ], | |
57 | }, | |
58 | code => sub { | |
59 | my ($param) = @_; | |
60 | ||
61 | my $result = [ | |
81d574a7 | 62 | { name => 'aliases' }, |
b4366f00 DM |
63 | { name => 'rules' }, |
64 | { name => 'options' }, | |
65 | { name => 'groups' }, | |
9d6f90e6 | 66 | { name => 'ipset' }, |
ebd54ae9 | 67 | { name => 'macros' }, |
947d6ea2 | 68 | { name => 'refs' }, |
b4366f00 DM |
69 | ]; |
70 | ||
71 | return $result; | |
72 | }}); | |
1df4ba7e | 73 | |
271f287b DM |
74 | my $option_properties = { |
75 | enable => { | |
72d055fc AG |
76 | type => 'integer', |
77 | minimum => 0, | |
271f287b DM |
78 | optional => 1, |
79 | }, | |
80 | policy_in => { | |
81 | description => "Input policy.", | |
82 | type => 'string', | |
83 | optional => 1, | |
84 | enum => ['ACCEPT', 'REJECT', 'DROP'], | |
85 | }, | |
947d6ea2 | 86 | policy_out => { |
271f287b DM |
87 | description => "Output policy.", |
88 | type => 'string', | |
89 | optional => 1, | |
90 | enum => ['ACCEPT', 'REJECT', 'DROP'], | |
91 | }, | |
92 | }; | |
93 | ||
94 | my $add_option_properties = sub { | |
95 | my ($properties) = @_; | |
96 | ||
97 | foreach my $k (keys %$option_properties) { | |
98 | $properties->{$k} = $option_properties->{$k}; | |
99 | } | |
947d6ea2 | 100 | |
271f287b DM |
101 | return $properties; |
102 | }; | |
103 | ||
104 | ||
1df4ba7e DM |
105 | __PACKAGE__->register_method({ |
106 | name => 'get_options', | |
107 | path => 'options', | |
108 | method => 'GET', | |
109 | description => "Get Firewall options.", | |
0ec56841 DM |
110 | permissions => { |
111 | check => ['perm', '/', [ 'Sys.Audit' ]], | |
112 | }, | |
1df4ba7e DM |
113 | parameters => { |
114 | additionalProperties => 0, | |
115 | }, | |
116 | returns => { | |
117 | type => "object", | |
118 | #additionalProperties => 1, | |
271f287b | 119 | properties => $option_properties, |
1df4ba7e DM |
120 | }, |
121 | code => sub { | |
122 | my ($param) = @_; | |
123 | ||
124 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); | |
125 | ||
5d38d64f | 126 | return PVE::Firewall::copy_opject_with_digest($cluster_conf->{options}); |
1df4ba7e DM |
127 | }}); |
128 | ||
1df4ba7e DM |
129 | |
130 | __PACKAGE__->register_method({ | |
131 | name => 'set_options', | |
132 | path => 'options', | |
133 | method => 'PUT', | |
134 | description => "Set Firewall options.", | |
68c90e21 | 135 | protected => 1, |
0ec56841 DM |
136 | permissions => { |
137 | check => ['perm', '/', [ 'Sys.Modify' ]], | |
138 | }, | |
1df4ba7e DM |
139 | parameters => { |
140 | additionalProperties => 0, | |
141 | properties => &$add_option_properties({ | |
142 | delete => { | |
143 | type => 'string', format => 'pve-configid-list', | |
144 | description => "A list of settings you want to delete.", | |
145 | optional => 1, | |
146 | }, | |
5d38d64f | 147 | digest => get_standard_option('pve-config-digest'), |
1df4ba7e DM |
148 | }), |
149 | }, | |
150 | returns => { type => "null" }, | |
151 | code => sub { | |
152 | my ($param) = @_; | |
153 | ||
154 | my $cluster_conf = PVE::Firewall::load_clusterfw_conf(); | |
155 | ||
5d38d64f DM |
156 | my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($cluster_conf->{options}); |
157 | PVE::Tools::assert_if_modified($digest, $param->{digest}); | |
158 | ||
1df4ba7e DM |
159 | if ($param->{delete}) { |
160 | foreach my $opt (PVE::Tools::split_list($param->{delete})) { | |
947d6ea2 | 161 | raise_param_exc({ delete => "no such option '$opt'" }) |
1df4ba7e DM |
162 | if !$option_properties->{$opt}; |
163 | delete $cluster_conf->{options}->{$opt}; | |
164 | } | |
165 | } | |
166 | ||
72d055fc AG |
167 | if (defined($param->{enable}) && ($param->{enable} > 1)) { |
168 | $param->{enable} = time(); | |
271f287b DM |
169 | } |
170 | ||
171 | foreach my $k (keys %$option_properties) { | |
172 | next if !defined($param->{$k}); | |
947d6ea2 | 173 | $cluster_conf->{options}->{$k} = $param->{$k}; |
1df4ba7e DM |
174 | } |
175 | ||
1df4ba7e DM |
176 | PVE::Firewall::save_clusterfw_conf($cluster_conf); |
177 | ||
c05492d6 AG |
178 | # instant firewall update when using double (anti-lockout) API call |
179 | # -> not waiting for a firewall update at the first (timestamp enable) set | |
180 | if (defined($param->{enable}) && ($param->{enable} > 1)) { | |
181 | PVE::Firewall::update(); | |
182 | } | |
183 | ||
1df4ba7e DM |
184 | return undef; |
185 | }}); | |
ebd54ae9 DM |
186 | |
187 | __PACKAGE__->register_method({ | |
188 | name => 'get_macros', | |
189 | path => 'macros', | |
190 | method => 'GET', | |
191 | description => "List available macros", | |
0ec56841 | 192 | permissions => { user => 'all' }, |
ebd54ae9 DM |
193 | parameters => { |
194 | additionalProperties => 0, | |
195 | }, | |
196 | returns => { | |
197 | type => 'array', | |
198 | items => { | |
199 | type => "object", | |
200 | properties => { | |
201 | macro => { | |
202 | description => "Macro name.", | |
203 | type => 'string', | |
204 | }, | |
205 | descr => { | |
206 | description => "More verbose description (if available).", | |
207 | type => 'string', | |
208 | } | |
209 | }, | |
210 | }, | |
211 | }, | |
212 | code => sub { | |
213 | my ($param) = @_; | |
214 | ||
215 | my $res = []; | |
216 | ||
217 | my ($macros, $descr) = PVE::Firewall::get_macros(); | |
218 | ||
219 | foreach my $macro (keys %$macros) { | |
220 | push @$res, { macro => $macro, descr => $descr->{$macro} || $macro }; | |
221 | } | |
222 | ||
223 | return $res; | |
224 | }}); | |
225 | ||
947d6ea2 DM |
226 | __PACKAGE__->register_method({ |
227 | name => 'refs', | |
228 | path => 'refs', | |
229 | method => 'GET', | |
230 | description => "Lists possible IPSet/Alias reference which are allowed in source/dest properties.", | |
0ec56841 DM |
231 | permissions => { |
232 | check => ['perm', '/', [ 'Sys.Audit' ]], | |
233 | }, | |
947d6ea2 DM |
234 | parameters => { |
235 | additionalProperties => 0, | |
f2c0865c DM |
236 | properties => { |
237 | type => { | |
238 | description => "Only list references of specified type.", | |
239 | type => 'string', | |
240 | enum => ['alias', 'ipset'], | |
241 | optional => 1, | |
242 | }, | |
243 | }, | |
947d6ea2 DM |
244 | }, |
245 | returns => { | |
246 | type => 'array', | |
247 | items => { | |
248 | type => "object", | |
249 | properties => { | |
250 | type => { | |
251 | type => 'string', | |
252 | enum => ['alias', 'ipset'], | |
253 | }, | |
254 | name => { | |
255 | type => 'string', | |
256 | }, | |
257 | ref => { | |
258 | type => 'string', | |
259 | }, | |
260 | comment => { | |
261 | type => 'string', | |
262 | optional => 1, | |
263 | }, | |
264 | }, | |
265 | }, | |
266 | }, | |
267 | code => sub { | |
268 | my ($param) = @_; | |
269 | ||
270 | my $conf = PVE::Firewall::load_clusterfw_conf(); | |
271 | ||
272 | my $res = []; | |
273 | ||
f2c0865c DM |
274 | if (!$param->{type} || $param->{type} eq 'ipset') { |
275 | foreach my $name (keys %{$conf->{ipset}}) { | |
276 | my $data = { | |
277 | type => 'ipset', | |
278 | name => $name, | |
279 | ref => "+$name", | |
280 | }; | |
281 | if (my $comment = $conf->{ipset_comments}->{$name}) { | |
282 | $data->{comment} = $comment; | |
283 | } | |
284 | push @$res, $data; | |
947d6ea2 | 285 | } |
947d6ea2 DM |
286 | } |
287 | ||
f2c0865c DM |
288 | if (!$param->{type} || $param->{type} eq 'alias') { |
289 | foreach my $name (keys %{$conf->{aliases}}) { | |
290 | my $e = $conf->{aliases}->{$name}; | |
291 | my $data = { | |
292 | type => 'alias', | |
293 | name => $name, | |
294 | ref => $name, | |
295 | }; | |
296 | $data->{comment} = $e->{comment} if $e->{comment}; | |
297 | push @$res, $data; | |
298 | } | |
947d6ea2 DM |
299 | } |
300 | ||
301 | return $res; | |
302 | }}); | |
303 | ||
ebd54ae9 | 304 | 1; |