|
7 | 7 | "github.com/Azure/azure-container-networking/cns/middlewares/utils" |
8 | 8 | "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1" |
9 | 9 | "github.com/Azure/azure-container-networking/network/policy" |
10 | | - "github.com/Microsoft/hcsshim/hcn" |
11 | 10 | "github.com/pkg/errors" |
12 | 11 | ) |
13 | 12 |
|
@@ -63,52 +62,41 @@ func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gwIP st |
63 | 62 | podIPInfo.Routes = append(podIPInfo.Routes, route) |
64 | 63 | } |
65 | 64 |
|
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) |
69 | 68 | 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") |
71 | 70 | } |
72 | 71 |
|
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, |
87 | 75 | } |
88 | 76 |
|
89 | 77 | return additionalArgs, nil |
90 | 78 | } |
91 | 79 |
|
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"` |
99 | 87 | } |
100 | 88 |
|
101 | | - denyACL := DefaultDenyACL{ |
102 | | - Type: "ACL", // policy type is ACL |
103 | | - Action: hcn.ActionTypeBlock, |
| 89 | + policy := EndpointPolicy{ |
| 90 | + Type: policyType, |
| 91 | + Action: action, |
104 | 92 | Direction: direction, |
105 | | - Priority: 10_000, // default deny priority will be 10_000 |
| 93 | + Priority: priority, |
106 | 94 | } |
107 | 95 |
|
108 | | - denyACLJSON, err := json.Marshal(denyACL) |
| 96 | + rawPolicy, err := json.Marshal(policy) |
109 | 97 | 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") |
111 | 99 | } |
112 | 100 |
|
113 | | - return denyACLJSON, nil |
| 101 | + return rawPolicy, nil |
114 | 102 | } |
0 commit comments