]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/API2/Firewall/Cluster.pm
API fix: allow aliases in IPSets
[pve-firewall.git] / src / PVE / API2 / Firewall / Cluster.pm
CommitLineData
b4366f00
DM
1package PVE::API2::Firewall::Cluster;
2
3use strict;
4use warnings;
1df4ba7e 5use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
b4366f00
DM
6use PVE::JSONSchema qw(get_standard_option);
7
8use PVE::Firewall;
81d574a7 9use PVE::API2::Firewall::Aliases;
86791289 10use PVE::API2::Firewall::Rules;
b4366f00 11use PVE::API2::Firewall::Groups;
009ee3ac 12use PVE::API2::Firewall::IPSet;
b4366f00 13
1df4ba7e
DM
14#fixme: locking?
15
b4366f00
DM
16use Data::Dumper; # fixme: remove
17
18use 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
74my $option_properties = {
75 enable => {
76 type => 'boolean',
77 optional => 1,
78 },
79 policy_in => {
80 description => "Input policy.",
81 type => 'string',
82 optional => 1,
83 enum => ['ACCEPT', 'REJECT', 'DROP'],
84 },
947d6ea2 85 policy_out => {
271f287b
DM
86 description => "Output policy.",
87 type => 'string',
88 optional => 1,
89 enum => ['ACCEPT', 'REJECT', 'DROP'],
90 },
91};
92
93my $add_option_properties = sub {
94 my ($properties) = @_;
95
96 foreach my $k (keys %$option_properties) {
97 $properties->{$k} = $option_properties->{$k};
98 }
947d6ea2 99
271f287b
DM
100 return $properties;
101};
102
103
1df4ba7e
DM
104__PACKAGE__->register_method({
105 name => 'get_options',
106 path => 'options',
107 method => 'GET',
108 description => "Get Firewall options.",
109 parameters => {
110 additionalProperties => 0,
111 },
112 returns => {
113 type => "object",
114 #additionalProperties => 1,
271f287b 115 properties => $option_properties,
1df4ba7e
DM
116 },
117 code => sub {
118 my ($param) = @_;
119
120 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
121
5d38d64f 122 return PVE::Firewall::copy_opject_with_digest($cluster_conf->{options});
1df4ba7e
DM
123 }});
124
1df4ba7e
DM
125
126__PACKAGE__->register_method({
127 name => 'set_options',
128 path => 'options',
129 method => 'PUT',
130 description => "Set Firewall options.",
68c90e21 131 protected => 1,
1df4ba7e
DM
132 parameters => {
133 additionalProperties => 0,
134 properties => &$add_option_properties({
135 delete => {
136 type => 'string', format => 'pve-configid-list',
137 description => "A list of settings you want to delete.",
138 optional => 1,
139 },
5d38d64f 140 digest => get_standard_option('pve-config-digest'),
1df4ba7e
DM
141 }),
142 },
143 returns => { type => "null" },
144 code => sub {
145 my ($param) = @_;
146
147 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
148
5d38d64f
DM
149 my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($cluster_conf->{options});
150 PVE::Tools::assert_if_modified($digest, $param->{digest});
151
1df4ba7e
DM
152 if ($param->{delete}) {
153 foreach my $opt (PVE::Tools::split_list($param->{delete})) {
947d6ea2 154 raise_param_exc({ delete => "no such option '$opt'" })
1df4ba7e
DM
155 if !$option_properties->{$opt};
156 delete $cluster_conf->{options}->{$opt};
157 }
158 }
159
160 if (defined($param->{enable})) {
271f287b
DM
161 $param->{enable} = $param->{enable} ? 1 : 0;
162 }
163
164 foreach my $k (keys %$option_properties) {
165 next if !defined($param->{$k});
947d6ea2 166 $cluster_conf->{options}->{$k} = $param->{$k};
1df4ba7e
DM
167 }
168
1df4ba7e
DM
169 PVE::Firewall::save_clusterfw_conf($cluster_conf);
170
171 return undef;
172 }});
ebd54ae9
DM
173
174__PACKAGE__->register_method({
175 name => 'get_macros',
176 path => 'macros',
177 method => 'GET',
178 description => "List available macros",
179 parameters => {
180 additionalProperties => 0,
181 },
182 returns => {
183 type => 'array',
184 items => {
185 type => "object",
186 properties => {
187 macro => {
188 description => "Macro name.",
189 type => 'string',
190 },
191 descr => {
192 description => "More verbose description (if available).",
193 type => 'string',
194 }
195 },
196 },
197 },
198 code => sub {
199 my ($param) = @_;
200
201 my $res = [];
202
203 my ($macros, $descr) = PVE::Firewall::get_macros();
204
205 foreach my $macro (keys %$macros) {
206 push @$res, { macro => $macro, descr => $descr->{$macro} || $macro };
207 }
208
209 return $res;
210 }});
211
947d6ea2
DM
212__PACKAGE__->register_method({
213 name => 'refs',
214 path => 'refs',
215 method => 'GET',
216 description => "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
217 parameters => {
218 additionalProperties => 0,
219 properties => {},
220 },
221 returns => {
222 type => 'array',
223 items => {
224 type => "object",
225 properties => {
226 type => {
227 type => 'string',
228 enum => ['alias', 'ipset'],
229 },
230 name => {
231 type => 'string',
232 },
233 ref => {
234 type => 'string',
235 },
236 comment => {
237 type => 'string',
238 optional => 1,
239 },
240 },
241 },
242 },
243 code => sub {
244 my ($param) = @_;
245
246 my $conf = PVE::Firewall::load_clusterfw_conf();
247
248 my $res = [];
249
250 foreach my $name (keys %{$conf->{ipset}}) {
251 my $data = {
252 type => 'ipset',
253 name => $name,
254 ref => "+$name",
255 };
256 if (my $comment = $conf->{ipset_comments}->{$name}) {
257 $data->{comment} = $comment;
258 }
259 push @$res, $data;
260 }
261
262 foreach my $name (keys %{$conf->{aliases}}) {
263 my $e = $conf->{aliases}->{$name};
264 my $data = {
265 type => 'alias',
266 name => $name,
267 ref => $name,
268 };
269 $data->{comment} = $e->{comment} if $e->{comment};
270 push @$res, $data;
271 }
272
273 return $res;
274 }});
275
ebd54ae9 2761;