@@ -15,7 +15,7 @@ const (
1515 ufwForceEnable = "ufw --force enable"
1616)
1717
18- func (c * ShadeformClient ) generateFirewallScript (firewallRules v1.FirewallRules ) (string , error ) {
18+ func (c * ShadeformClient ) GenerateFirewallScript (firewallRules v1.FirewallRules ) (string , error ) {
1919 commands := []string {ufwForceReset , ufwDefaultDropIncoming , ufwDefaultAllowOutgoing , ufwDefaultAllowPort22 , ufwDefaultAllowPort2222 }
2020
2121 for _ , firewallRule := range firewallRules .IngressRules {
@@ -40,38 +40,50 @@ func (c *ShadeformClient) generateFirewallScript(firewallRules v1.FirewallRules)
4040
4141func (c * ShadeformClient ) convertIngressFirewallRuleToUfwCommand (firewallRule v1.FirewallRule ) []string {
4242 cmds := []string {}
43- portSpec := ""
43+ portSpecs := [] string {}
4444 if firewallRule .FromPort == firewallRule .ToPort {
45- portSpec = fmt .Sprintf ("port %d" , firewallRule .FromPort )
45+ portSpecs = append ( portSpecs , fmt .Sprintf ("port %d" , firewallRule .FromPort ) )
4646 } else {
47- portSpec = fmt .Sprintf ("port %d:%d" , firewallRule .FromPort , firewallRule .ToPort )
47+ // port ranges require two separate rules for tcp and udp
48+ portSpecs = append (portSpecs , fmt .Sprintf ("port %d:%d proto tcp" , firewallRule .FromPort , firewallRule .ToPort ))
49+ portSpecs = append (portSpecs , fmt .Sprintf ("port %d:%d proto udp" , firewallRule .FromPort , firewallRule .ToPort ))
4850 }
4951
5052 if len (firewallRule .IPRanges ) == 0 {
51- cmds = append (cmds , fmt .Sprintf ("ufw allow in from any to any port %s" , portSpec ))
53+ for _ , portSpec := range portSpecs {
54+ cmds = append (cmds , fmt .Sprintf ("ufw allow in from any to any %s" , portSpec ))
55+ }
5256 }
5357
5458 for _ , ipRange := range firewallRule .IPRanges {
55- cmds = append (cmds , fmt .Sprintf ("ufw allow in from %s to any port %s" , ipRange , portSpec ))
59+ for _ , portSpec := range portSpecs {
60+ cmds = append (cmds , fmt .Sprintf ("ufw allow in from %s to any %s" , ipRange , portSpec ))
61+ }
5662 }
5763 return cmds
5864}
5965
6066func (c * ShadeformClient ) convertEgressFirewallRuleToUfwCommand (firewallRule v1.FirewallRule ) []string {
6167 cmds := []string {}
62- portSpec := ""
68+ portSpecs := [] string {}
6369 if firewallRule .FromPort == firewallRule .ToPort {
64- portSpec = fmt .Sprintf ("port %d" , firewallRule .FromPort )
70+ portSpecs = append ( portSpecs , fmt .Sprintf ("port %d" , firewallRule .FromPort ) )
6571 } else {
66- portSpec = fmt .Sprintf ("port %d:%d" , firewallRule .FromPort , firewallRule .ToPort )
72+ // port ranges require two separate rules for tcp and udp
73+ portSpecs = append (portSpecs , fmt .Sprintf ("port %d:%d proto tcp" , firewallRule .FromPort , firewallRule .ToPort ))
74+ portSpecs = append (portSpecs , fmt .Sprintf ("port %d:%d proto udp" , firewallRule .FromPort , firewallRule .ToPort ))
6775 }
6876
6977 if len (firewallRule .IPRanges ) == 0 {
70- cmds = append (cmds , fmt .Sprintf ("ufw allow out to any port %s" , portSpec ))
78+ for _ , portSpec := range portSpecs {
79+ cmds = append (cmds , fmt .Sprintf ("ufw allow out to any %s" , portSpec ))
80+ }
7181 }
7282
7383 for _ , ipRange := range firewallRule .IPRanges {
74- cmds = append (cmds , fmt .Sprintf ("ufw allow out to %s port %s" , ipRange , portSpec ))
84+ for _ , portSpec := range portSpecs {
85+ cmds = append (cmds , fmt .Sprintf ("ufw allow out to %s %s" , ipRange , portSpec ))
86+ }
7587 }
7688 return cmds
7789}
0 commit comments