| 1 | package WWW::Hetzner::CLI::Cmd::Firewall::Cmd::AddRule; | ||||||||||||
| 2 | # ABSTRACT: Add a rule to a firewall | ||||||||||||
| 3 | |||||||||||||
| 4 | 1 | our $VERSION = '0.102'; | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use MooX::Cmd; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use MooX::Options protect_argv => 0, usage_string => 'USAGE: hcloud.pl firewall add-rule <id> --direction <in|out> --protocol <tcp|udp|icmp|gre|esp> --port <port> [--source-ips <ips>]'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | with 'WWW::Hetzner::CLI::Role::WaitsForAction'; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | option direction => ( | |||||||||||
| 12 | is => 'ro', | ||||||||||||
| 13 | format => 's', | ||||||||||||
| 14 | required => 1, | ||||||||||||
| 15 | doc => 'Direction: in or out', | ||||||||||||
| 16 | ); | ||||||||||||
| 17 | |||||||||||||
| 18 | 1 | option protocol => ( | |||||||||||
| 19 | is => 'ro', | ||||||||||||
| 20 | format => 's', | ||||||||||||
| 21 | required => 1, | ||||||||||||
| 22 | doc => 'Protocol: tcp, udp, icmp, gre, esp', | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | 1 | option port => ( | |||||||||||
| 26 | is => 'ro', | ||||||||||||
| 27 | format => 's', | ||||||||||||
| 28 | doc => 'Port or port range (e.g. 22 or 80-443)', | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | |||||||||||||
| 31 | option 'source_ips' => ( | ||||||||||||
| 32 | is => 'ro', | ||||||||||||
| 33 | format => 's@', | ||||||||||||
| 34 | long_doc => 'source-ips', | ||||||||||||
| 35 | doc => 'Source IPs (can specify multiple)', | ||||||||||||
| 36 | 0 | ▶ | default => sub { ['0.0.0.0/0', '::/0'] }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | 1 | ); | |||||||||||
| 38 | |||||||||||||
| 39 | 1 | ▶ | sub execute { | ||||||||||
|
Subroutine
execute: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||
| 40 | 1 | my ($self, $args, $chain) = @_; | |||||||||||
| 41 | 1 | ▶ | my $id = $args->[0] or die "Usage: hcloud.pl firewall add-rule <id> --direction <in|out> --protocol <tcp|udp|icmp> --port <port>\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | |||||||||||||
| 43 | 1 | my $main = $chain->[0]; | |||||||||||
| 44 | 1 | my $cloud = $main->cloud; | |||||||||||
| 45 | |||||||||||||
| 46 | # Get existing firewall | ||||||||||||
| 47 | 1 | my $fw = $cloud->firewalls->get($id); | |||||||||||
| 48 | 1 | ▶ | my @rules = @{ $fw->rules // [] }; | ||||||||||
|
Condition50%1/2
$fw->rules // []
MC/DC0%0/1
$fw->rules // []
$fw->rules
Truth table50%1/2
A
A= $fw->rules
| |||||||||||||
| 49 | |||||||||||||
| 50 | # Build new rule | ||||||||||||
| 51 | 1 | my $rule = { | |||||||||||
| 52 | direction => $self->direction, | ||||||||||||
| 53 | protocol => $self->protocol, | ||||||||||||
| 54 | }; | ||||||||||||
| 55 | 1 | ▶ | $rule->{port} = $self->port if $self->port; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 1 | ▶ | if ($self->direction eq 'in') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 1 | $rule->{source_ips} = $self->source_ips; | |||||||||||
| 59 | } else { | ||||||||||||
| 60 | 0 | $rule->{destination_ips} = $self->source_ips; # reuse for simplicity | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 1 | push @rules, $rule; | |||||||||||
| 64 | |||||||||||||
| 65 | 1 | print "Adding rule to firewall $id...\n"; | |||||||||||
| 66 | 1 | my $actions = $cloud->firewalls->set_rules($id, \@rules); | |||||||||||
| 67 | 1 | $self->handle_action($actions); | |||||||||||
| 68 | 1 | ▶ | print $self->no_wait ? "Rule add requested.\n" : "Rule added.\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 1 | 1; | |||||||||||
| 72 | |||||||||||||
| 73 | __END__ | ||||||||||||