To establish the rules it is important to take into account that;
Rules are not removed, they are replaced
The rules cannot be obtained directly, the rules are obtained from the Rules property of the Firewall object
If there are no ingress rules, it will allow all incoming traffic
If there are no outbound rules, it will allow all outbound traffic
For the first example we are going to allow access to ports 80 and 22
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");// Get the objectFirewall firewall =awaithetznerCloudClient.Firewall.Get(1012861);// List rulesList<Rule> listRules =newList<Rule>();// Enable port 80 / tcp / in / All traffic ipv4 and ipv6listRules.Add(newRule{ Direction =Direction.@in, Protocol =Protocol.tcp, Port ="80", Description ="Port 80 for http", SourceIps =newList<string> { "0.0.0.0/0","::/0" }});// Enable port 22 / tcp / in / All traffic ipv4 and ipv6listRules.Add(newRule{ Direction =Direction.@in, Protocol =Protocol.tcp, Port ="22", Description ="Port 22 for https", SourceIps =newList<string> { "0.0.0.0/0","::/0" }});// Send rulesList<Action> listAction =awaithetznerCloudClient.FirewallAction.SetRulesTask(firewall, listRules);
Now, we are going to allow all outgoing traffic (although it is redundant because not specifying this will allow all traffic)
Notice how we use List<Rule> listRules = firewall.Rules; this is to keep the current rules and add a new one. If we don't do this, we would be replacing the existing rules with the new ones.
In other words, if we don't do this, the new rules will replace the old ones, and the old ones will be deleted.
// Get the objectFirewall firewall =awaithetznerCloudClient.Firewall.Get(1012861);// Get pre-existing rulesList<Rule> listRules =firewall.Rules;//Add new rulelistRules.Add(newRule(){ Direction =Direction.@out, Protocol =Protocol.tcp, Port ="any", Description ="All port out open", DestinationIps =newList<string> { "0.0.0.0/0","::/0" }});// Set rulesList<Action> listAction =awaithetznerCloudClient.FirewallAction.SetRules(firewall, listRules);
Get all Actions for a Firewall
Returns all Action objects for a Firewall.
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");long firewallId =1012861;List<Action> list =awaithetznerCloudClient.FirewallAction.GetAllActions(firewallId);