]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/Host.pm
add API for firewall log
[pve-firewall.git] / src / PVE / API2 / Firewall / Host.pm
1 package PVE::API2::Firewall::Host;
2
3 use strict;
4 use warnings;
5 use PVE::JSONSchema qw(get_standard_option);
6 use PVE::RPCEnvironment;
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 __PACKAGE__->register_method ({
16 subclass => "PVE::API2::Firewall::HostRules",
17 path => 'rules',
18 });
19
20 __PACKAGE__->register_method({
21 name => 'index',
22 path => '',
23 method => 'GET',
24 permissions => { user => 'all' },
25 description => "Directory index.",
26 parameters => {
27 additionalProperties => 0,
28 properties => {
29 node => get_standard_option('pve-node'),
30 },
31 },
32 returns => {
33 type => 'array',
34 items => {
35 type => "object",
36 properties => {},
37 },
38 links => [ { rel => 'child', href => "{name}" } ],
39 },
40 code => sub {
41 my ($param) = @_;
42
43 my $result = [
44 { name => 'rules' },
45 { name => 'options' },
46 { name => 'log' },
47 ];
48
49 return $result;
50 }});
51
52 __PACKAGE__->register_method({
53 name => 'get_options',
54 path => 'options',
55 method => 'GET',
56 description => "Get host firewall options.",
57 proxyto => 'node',
58 parameters => {
59 additionalProperties => 0,
60 properties => {
61 node => get_standard_option('pve-node'),
62 },
63 },
64 returns => {
65 type => "object",
66 properties => {},
67 },
68 code => sub {
69 my ($param) = @_;
70
71 my $hostfw_conf = PVE::Firewall::load_hostfw_conf();
72
73 return PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
74 }});
75
76 __PACKAGE__->register_method({
77 name => 'log',
78 path => 'log',
79 method => 'GET',
80 description => "Read firewall log",
81 proxyto => 'node',
82 permissions => {
83 check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
84 },
85 protected => 1,
86 parameters => {
87 additionalProperties => 0,
88 properties => {
89 node => get_standard_option('pve-node'),
90 start => {
91 type => 'integer',
92 minimum => 0,
93 optional => 1,
94 },
95 limit => {
96 type => 'integer',
97 minimum => 0,
98 optional => 1,
99 },
100 },
101 },
102 returns => {
103 type => 'array',
104 items => {
105 type => "object",
106 properties => {
107 n => {
108 description=> "Line number",
109 type=> 'integer',
110 },
111 t => {
112 description=> "Line text",
113 type => 'string',
114 }
115 }
116 }
117 },
118 code => sub {
119 my ($param) = @_;
120
121 my $rpcenv = PVE::RPCEnvironment::get();
122 my $user = $rpcenv->get_user();
123 my $node = $param->{node};
124
125 my ($count, $lines) = PVE::Tools::dump_logfile("/var/log/pve-firewall.log", $param->{start}, $param->{limit});
126
127 $rpcenv->set_result_attrib('total', $count);
128
129 return $lines;
130 }});
131
132 1;