]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/Groups.pm
Firewall/Groups: add permissions
[pve-firewall.git] / src / PVE / API2 / Firewall / Groups.pm
1 package PVE::API2::Firewall::Groups;
2
3 use strict;
4 use warnings;
5 use PVE::JSONSchema qw(get_standard_option);
6 use PVE::Exception qw(raise raise_param_exc);
7
8 use PVE::Firewall;
9 use PVE::API2::Firewall::Rules;
10
11 use Data::Dumper; # fixme: remove
12
13 use base qw(PVE::RESTHandler);
14
15 my $get_security_group_list = sub {
16 my ($cluster_conf) = @_;
17
18 my $res = [];
19 foreach my $group (keys %{$cluster_conf->{groups}}) {
20 my $data = {
21 group => $group,
22 };
23 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
24 $data->{comment} = $comment;
25 }
26 push @$res, $data;
27 }
28
29 my ($list, $digest) = PVE::Firewall::copy_list_with_digest($res);
30
31 return wantarray ? ($list, $digest) : $list;
32 };
33
34 __PACKAGE__->register_method({
35 name => 'list_security_groups',
36 path => '',
37 method => 'GET',
38 description => "List security groups.",
39 permissions => { user => 'all' },
40 parameters => {
41 additionalProperties => 0,
42 properties => {},
43 },
44 returns => {
45 type => 'array',
46 items => {
47 type => "object",
48 properties => {
49 group => get_standard_option('pve-security-group-name'),
50 digest => get_standard_option('pve-config-digest', { optional => 0} ),
51 comment => {
52 type => 'string',
53 optional => 1,
54 }
55 },
56 },
57 links => [ { rel => 'child', href => "{group}" } ],
58 },
59 code => sub {
60 my ($param) = @_;
61
62 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
63
64 return &$get_security_group_list($cluster_conf);
65 }});
66
67 __PACKAGE__->register_method({
68 name => 'create_security_group',
69 path => '',
70 method => 'POST',
71 description => "Create new security group.",
72 protected => 1,
73 permissions => {
74 check => ['perm', '/', [ 'Sys.Modify' ]],
75 },
76 parameters => {
77 additionalProperties => 0,
78 properties => {
79 group => get_standard_option('pve-security-group-name'),
80 comment => {
81 type => 'string',
82 optional => 1,
83 },
84 rename => get_standard_option('pve-security-group-name', {
85 description => "Rename/update an existing security group. You can set 'rename' to the same value as 'name' to update the 'comment' of an existing group.",
86 optional => 1,
87 }),
88 digest => get_standard_option('pve-config-digest'),
89 },
90 },
91 returns => { type => 'null' },
92 code => sub {
93 my ($param) = @_;
94
95 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
96
97 if ($param->{rename}) {
98 my (undef, $digest) = &$get_security_group_list($cluster_conf);
99 PVE::Tools::assert_if_modified($digest, $param->{digest});
100
101 raise_param_exc({ group => "Security group '$param->{rename}' does not exists" })
102 if !$cluster_conf->{groups}->{$param->{rename}};
103
104 my $data = delete $cluster_conf->{groups}->{$param->{rename}};
105 $cluster_conf->{groups}->{$param->{group}} = $data;
106 if (my $comment = delete $cluster_conf->{group_comments}->{$param->{rename}}) {
107 $cluster_conf->{group_comments}->{$param->{group}} = $comment;
108 }
109 $cluster_conf->{group_comments}->{$param->{group}} = $param->{comment} if defined($param->{comment});
110 } else {
111 foreach my $name (keys %{$cluster_conf->{groups}}) {
112 raise_param_exc({ group => "Security group '$name' already exists" })
113 if $name eq $param->{group};
114 }
115
116 $cluster_conf->{groups}->{$param->{group}} = [];
117 $cluster_conf->{group_comments}->{$param->{group}} = $param->{comment} if defined($param->{comment});
118 }
119
120 PVE::Firewall::save_clusterfw_conf($cluster_conf);
121
122 return undef;
123 }});
124
125 __PACKAGE__->register_method ({
126 subclass => "PVE::API2::Firewall::GroupRules",
127 path => '{group}',
128 });
129
130 1;