Skip to content

Commit 931901c

Browse files
committed
fix: generate firewall script
1 parent 07ca2dd commit 931901c

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

internal/shadeform/v1/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (c *ShadeformClient) CreateInstance(ctx context.Context, attrs v1.CreateIns
7777
tags = append(tags, createdTag)
7878
}
7979

80-
base64Script, err := c.generateFirewallScript(attrs.FirewallRules)
80+
base64Script, err := c.GenerateFirewallScript(attrs.FirewallRules)
8181
if err != nil {
8282
return nil, err
8383
}

internal/shadeform/v1/ufw.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4141
func (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

6066
func (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
}

internal/shadeform/v1/validation_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,30 @@ func TestInstanceTypeFilter(t *testing.T) {
7474
FirewallRules: v1.FirewallRules{
7575
EgressRules: []v1.FirewallRule{
7676
{
77-
ID: "test-rule",
77+
ID: "test-rule1",
7878
FromPort: 80,
7979
ToPort: 8080,
80-
IPRanges: []string{"127.0.0.1", "10.0.0.0/32"},
80+
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
81+
},
82+
{
83+
ID: "test-rule2",
84+
FromPort: 5432,
85+
ToPort: 5432,
86+
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
8187
},
8288
},
8389
IngressRules: []v1.FirewallRule{
8490
{
85-
ID: "test-rule",
91+
ID: "test-rule3",
92+
FromPort: 80,
93+
ToPort: 8080,
94+
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
95+
},
96+
{
97+
ID: "test-rule4",
8698
FromPort: 5432,
8799
ToPort: 5432,
88-
IPRanges: []string{"127.0.0.1", "10.0.0.0/32"},
100+
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
89101
},
90102
},
91103
},

0 commit comments

Comments
 (0)