Skip to content

Commit 06eb949

Browse files
committed
updated creating acl code to make it more modularized
1 parent 4193874 commit 06eb949

File tree

4 files changed

+72
-54
lines changed

4 files changed

+72
-54
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,26 @@ func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
5959
return ipConfigsResp, err
6060
}
6161

62-
// ipConfigsResp has infra IP configs -> if defaultDenyACLbool is enabled, add the default deny acl's pn the infra IP configs
62+
// ipConfigsResp has infra IP configs -> if defaultDenyACLbool is enabled, add the default deny endpoint policies as a property in PodIpInfo
6363
for i := range ipConfigsResp.PodIPInfo {
6464
ipInfo := &ipConfigsResp.PodIPInfo[i]
65-
var defaultDenyEndpointPolicies []policy.Policy
6665
// there will be no pod connectivity to and from those pods
66+
var defaultDenyEngressPolicy, defaultDenyIngressPolicy policy.Policy
67+
6768
if defaultDenyACLbool && ipInfo.NICType == cns.InfraNIC {
68-
defaultDenyEndpointPolicies, err = addDefaultDenyACL()
69+
defaultDenyEngressPolicy, err = getEndpointPolicyL(string(policy.ACLPolicy), cns.ActionTypeBlock, cns.DirectionTypeOut, 10_000)
6970
if err != nil {
7071
logger.Errorf("failed to add default deny acl's for pod %v with err %v", podInfo.Name(), err)
7172
}
72-
ipInfo.EndpointPolicies = append(ipInfo.EndpointPolicies, defaultDenyEndpointPolicies...)
73+
74+
defaultDenyIngressPolicy, err = getEndpointPolicyL(string(policy.ACLPolicy), cns.ActionTypeBlock, cns.DirectionTypeIn, 10_000)
75+
if err != nil {
76+
logger.Errorf("failed to add default deny acl's for pod %v with err %v", podInfo.Name(), err)
77+
}
78+
79+
ipInfo.EndpointPolicies = append(ipInfo.EndpointPolicies, defaultDenyEngressPolicy, defaultDenyIngressPolicy)
80+
logger.Printf("Created endpoint policies for defaultDenyEngressPolicy and defaultDenyIngressPolicy")
81+
7382
break
7483
}
7584
}

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo,
105105

106106
func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo, string) {}
107107

108-
func addDefaultDenyACL() ([]policy.Policy, error) {
109-
return []policy.Policy{}, nil
108+
func getEndpointPolicyL(_, _, _ string, _ int) (policy.Policy, error) {
109+
return policy.Policy{}, nil
110110
}

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
88
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
99
"github.com/Azure/azure-container-networking/network/policy"
10-
"github.com/Microsoft/hcsshim/hcn"
1110
"github.com/pkg/errors"
1211
)
1312

@@ -63,52 +62,41 @@ func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gwIP st
6362
podIPInfo.Routes = append(podIPInfo.Routes, route)
6463
}
6564

66-
// append the default deny acl's to the list defaultDenyACL field in podIpInfo
67-
func addDefaultDenyACL() ([]policy.Policy, error) {
68-
blockEgressACL, err := getDefaultDenyACLPolicy(hcn.DirectionTypeOut)
65+
// get policy of type endpoint policy given the params
66+
func getEndpointPolicyL(policyType string, action string, direction string, priority int) (policy.Policy, error) {
67+
endpointPolicy, err := createEndpointPolicy(policyType, action, direction, priority)
6968
if err != nil {
70-
return []policy.Policy{}, errors.Wrap(err, "failed to create default deny ACL policy egress")
69+
return policy.Policy{}, errors.Wrap(err, "failed to create endpoint policy")
7170
}
7271

73-
blockIngressACL, err := getDefaultDenyACLPolicy(hcn.DirectionTypeIn)
74-
if err != nil {
75-
return []policy.Policy{}, errors.Wrap(err, "Failed to create default deny ACL policy ingress")
76-
}
77-
78-
additionalArgs := []policy.Policy{
79-
{
80-
Type: policy.EndpointPolicy,
81-
Data: blockEgressACL,
82-
},
83-
{
84-
Type: policy.EndpointPolicy,
85-
Data: blockIngressACL,
86-
},
72+
additionalArgs := policy.Policy{
73+
Type: policy.EndpointPolicy,
74+
Data: endpointPolicy,
8775
}
8876

8977
return additionalArgs, nil
9078
}
9179

92-
// create the default deny acl's that need to be added to the list defaultDenyACL field in podIpInfo
93-
func getDefaultDenyACLPolicy(direction hcn.DirectionType) ([]byte, error) {
94-
type DefaultDenyACL struct {
95-
Type string `json:"Type"`
96-
Action hcn.ActionType `json:"Action"`
97-
Direction hcn.DirectionType `json:"Direction"`
98-
Priority int `json:"Priority"`
80+
// create policy given the params
81+
func createEndpointPolicy(policyType string, action string, direction string, priority int) ([]byte, error) {
82+
type EndpointPolicy struct {
83+
Type string `json:"Type"`
84+
Action string `json:"Action"`
85+
Direction string `json:"Direction"`
86+
Priority int `json:"Priority"`
9987
}
10088

101-
denyACL := DefaultDenyACL{
102-
Type: "ACL", // policy type is ACL
103-
Action: hcn.ActionTypeBlock,
89+
policy := EndpointPolicy{
90+
Type: policyType,
91+
Action: action,
10492
Direction: direction,
105-
Priority: 10_000, // default deny priority will be 10_000
93+
Priority: priority,
10694
}
10795

108-
denyACLJSON, err := json.Marshal(denyACL)
96+
rawPolicy, err := json.Marshal(policy)
10997
if err != nil {
110-
return nil, errors.Wrap(err, "error marshalling default deny policy to json")
98+
return nil, errors.Wrap(err, "error marshalling policy to json")
11199
}
112100

113-
return denyACLJSON, nil
101+
return rawPolicy, nil
114102
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middlewares
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"reflect"
67
"testing"
78

@@ -105,19 +106,35 @@ func TestAddDefaultRoute(t *testing.T) {
105106
}
106107

107108
func TestAddDefaultDenyACL(t *testing.T) {
108-
valueOut := []byte(`{
109-
"Type": "ACL",
110-
"Action": "Block",
111-
"Direction": "Out",
112-
"Priority": 10000
113-
}`)
114-
115-
valueIn := []byte(`{
116-
"Type": "ACL",
117-
"Action": "Block",
118-
"Direction": "In",
119-
"Priority": 10000
120-
}`)
109+
const policyType = "ACL"
110+
const action = "Block"
111+
const ingressDir = "In"
112+
const egressDir = "Out"
113+
const priority = 10000
114+
115+
valueIn := []byte(fmt.Sprintf(`{
116+
"Type": "%s",
117+
"Action": "%s",
118+
"Direction": "%s",
119+
"Priority": %d
120+
}`,
121+
policyType,
122+
action,
123+
ingressDir,
124+
priority,
125+
))
126+
127+
valueOut := []byte(fmt.Sprintf(`{
128+
"Type": "%s",
129+
"Action": "%s",
130+
"Direction": "%s",
131+
"Priority": %d
132+
}`,
133+
policyType,
134+
action,
135+
egressDir,
136+
priority,
137+
))
121138

122139
expectedDefaultDenyEndpoint := []policy.Policy{
123140
{
@@ -129,13 +146,17 @@ func TestAddDefaultDenyACL(t *testing.T) {
129146
Data: valueIn,
130147
},
131148
}
149+
var allEndpoints []policy.Policy
132150

133-
defaultDenyEndpoint, err := addDefaultDenyACL()
151+
defaultDenyEngressPolicy, err := getEndpointPolicyL("ACL", "Block", "Out", 10000)
152+
defaultDenyIngressPolicy, err := getEndpointPolicyL("ACL", "Block", "In", 10000)
153+
154+
allEndpoints = append(allEndpoints, defaultDenyEngressPolicy, defaultDenyIngressPolicy)
134155
assert.Equal(t, err, nil)
135156

136157
// Normalize both slices so there is no extra spacing, new lines, etc
137158
normalizedExpected := normalizeKVPairs(t, expectedDefaultDenyEndpoint)
138-
normalizedActual := normalizeKVPairs(t, defaultDenyEndpoint)
159+
normalizedActual := normalizeKVPairs(t, allEndpoints)
139160
if !reflect.DeepEqual(normalizedExpected, normalizedActual) {
140161
t.Errorf("got '%+v', expected '%+v'", normalizedActual, normalizedExpected)
141162
}

0 commit comments

Comments
 (0)