Skip to content

Commit 9069b01

Browse files
committed
modified code to add default deny acl policies only when default deny has been enabled to true in pni
1 parent 4bba37f commit 9069b01

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

cns/networkcontainers/networkcontainers.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func DeleteLoopbackAdapter(adapterName string) error {
106106
}
107107

108108
// This function gets the flattened network configuration (compliant with azure cni) in byte array format
109-
func getNetworkConfig(configFilePath string) ([]byte, error) {
109+
func getNetworkConfig(configFilePath string, defaultDenyACL bool) ([]byte, error) {
110110
content, err := os.ReadFile(configFilePath)
111111
if err != nil {
112112
return nil, err
@@ -134,34 +134,35 @@ func getNetworkConfig(configFilePath string) ([]byte, error) {
134134
flatNetConfigMap[versionStr] = configMap[versionStr].(string)
135135
flatNetConfigMap[nameStr] = configMap[nameStr].(string)
136136

137-
// TODO Check if default deny bool is enabled to true
138-
// insert default dent policy here
139-
defaultDenyOutACL := map[string]interface{}{
140-
"Name": "EndpointPolicy",
141-
"Value": map[string]interface{}{
142-
"Type": "ACL",
143-
"Action": "Block",
144-
"Direction": "Out",
145-
"Priority": 300,
146-
},
147-
}
137+
if defaultDenyACL {
138+
// insert default dent policy here
139+
defaultDenyOutACL := map[string]interface{}{
140+
"Name": "EndpointPolicy",
141+
"Value": map[string]interface{}{
142+
"Type": "ACL",
143+
"Action": "Block",
144+
"Direction": "Out",
145+
"Priority": 300,
146+
},
147+
}
148148

149-
defaultDenyInACL := map[string]interface{}{
150-
"Name": "EndpointPolicy",
151-
"Value": map[string]interface{}{
152-
"Type": "ACL",
153-
"Action": "Block",
154-
"Direction": "In",
155-
"Priority": 300,
156-
},
157-
}
158-
additionalArgsKey := "AdditionalArgs"
159-
if _, exists := flatNetConfigMap[additionalArgsKey]; !exists {
160-
flatNetConfigMap[additionalArgsKey] = []interface{}{}
161-
}
149+
defaultDenyInACL := map[string]interface{}{
150+
"Name": "EndpointPolicy",
151+
"Value": map[string]interface{}{
152+
"Type": "ACL",
153+
"Action": "Block",
154+
"Direction": "In",
155+
"Priority": 300,
156+
},
157+
}
158+
additionalArgsKey := "AdditionalArgs"
159+
if _, exists := flatNetConfigMap[additionalArgsKey]; !exists {
160+
flatNetConfigMap[additionalArgsKey] = []interface{}{}
161+
}
162162

163-
flatNetConfigMap[additionalArgsKey] = append(flatNetConfigMap[additionalArgsKey].([]interface{}), defaultDenyOutACL)
164-
flatNetConfigMap[additionalArgsKey] = append(flatNetConfigMap[additionalArgsKey].([]interface{}), defaultDenyInACL)
163+
flatNetConfigMap[additionalArgsKey] = append(flatNetConfigMap[additionalArgsKey].([]interface{}), defaultDenyOutACL)
164+
flatNetConfigMap[additionalArgsKey] = append(flatNetConfigMap[additionalArgsKey].([]interface{}), defaultDenyInACL)
165+
}
165166

166167
// convert into bytes format
167168
netConfig, err := json.Marshal(flatNetConfigMap)
@@ -227,17 +228,17 @@ func execPlugin(rt *libcni.RuntimeConf, netconf []byte, operation, path string)
227228
}
228229

229230
// Attach - attaches network container to network.
230-
func (cn *NetworkContainers) Attach(podInfo cns.PodInfo, dockerContainerid string, netPluginConfig *NetPluginConfiguration) error {
231+
func (cn *NetworkContainers) Attach(podInfo cns.PodInfo, dockerContainerid string, netPluginConfig *NetPluginConfiguration, defaultDenyACL bool) error {
231232
logger.Printf("[Azure CNS] NetworkContainers.Attach called")
232-
err := configureNetworkContainerNetworking(cniAdd, podInfo.Name(), podInfo.Namespace(), dockerContainerid, netPluginConfig)
233+
err := configureNetworkContainerNetworking(cniAdd, podInfo.Name(), podInfo.Namespace(), dockerContainerid, netPluginConfig, defaultDenyACL)
233234
logger.Printf("[Azure CNS] NetworkContainers.Attach finished")
234235
return err
235236
}
236237

237238
// Detach - detaches network container from network.
238-
func (cn *NetworkContainers) Detach(podInfo cns.PodInfo, dockerContainerid string, netPluginConfig *NetPluginConfiguration) error {
239+
func (cn *NetworkContainers) Detach(podInfo cns.PodInfo, dockerContainerid string, netPluginConfig *NetPluginConfiguration, defaultDenyACL bool) error {
239240
logger.Printf("[Azure CNS] NetworkContainers.Detach called")
240-
err := configureNetworkContainerNetworking(cniDelete, podInfo.Name(), podInfo.Namespace(), dockerContainerid, netPluginConfig)
241+
err := configureNetworkContainerNetworking(cniDelete, podInfo.Name(), podInfo.Namespace(), dockerContainerid, netPluginConfig, defaultDenyACL)
241242
logger.Printf("[Azure CNS] NetworkContainers.Detach finished")
242243
return err
243244
}

cns/networkcontainers/networkcontainers_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func updateInterface(createNetworkContainerRequest cns.CreateNetworkContainerReq
6464

6565
logger.Printf("[Azure CNS] run time configuration for CNI plugin info %+v", rt)
6666

67-
netConfig, err := getNetworkConfig(netpluginConfig.networkConfigPath)
67+
netConfig, err := getNetworkConfig(netpluginConfig.networkConfigPath, false)
6868
if err != nil {
6969
logger.Printf("[Azure CNS] Failed to build network configuration with error %v", err)
7070
return err
@@ -85,7 +85,7 @@ func deleteInterface(networkContainerID string) error {
8585
return nil
8686
}
8787

88-
func configureNetworkContainerNetworking(operation, podName, podNamespace, dockerContainerid string, netPluginConfig *NetPluginConfiguration) (err error) {
88+
func configureNetworkContainerNetworking(operation, podName, podNamespace, dockerContainerid string, netPluginConfig *NetPluginConfiguration, defaultDenyACL bool) (err error) {
8989
return fmt.Errorf("[Azure CNS] Operation is not supported in linux.")
9090
}
9191

cns/networkcontainers/networkcontainers_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func deleteInterface(interfaceName string) error {
219219
return err
220220
}
221221

222-
func configureNetworkContainerNetworking(operation, podName, podNamespace, dockerContainerid string, netPluginConfig *NetPluginConfiguration) (err error) {
222+
func configureNetworkContainerNetworking(operation, podName, podNamespace, dockerContainerid string, netPluginConfig *NetPluginConfiguration, defaultDenyACL bool) (err error) {
223223
cniRtConf := &libcni.RuntimeConf{
224224
ContainerID: dockerContainerid,
225225
NetNS: "none",
@@ -231,7 +231,7 @@ func configureNetworkContainerNetworking(operation, podName, podNamespace, docke
231231
}
232232
logger.Printf("[Azure CNS] run time conf info %+v", cniRtConf)
233233

234-
netConfig, err := getNetworkConfig(netPluginConfig.networkConfigPath)
234+
netConfig, err := getNetworkConfig(netPluginConfig.networkConfigPath, defaultDenyACL)
235235
if err != nil {
236236
logger.Printf("[Azure CNS] Failed to build network configuration with error %v", err)
237237
return err

cns/restserver/util.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN
676676

677677
var returnCode types.ResponseCode
678678
var returnMessage string
679+
nc := service.state.ContainerStatus[req.NetworkContainerid]
680+
defaultDenyACL := nc.CreateNetworkContainerRequest.DefaultDenyACL
681+
679682
switch service.state.OrchestratorType {
680683
case cns.Batch:
681684
podInfo, err := cns.UnmarshalPodInfo(existing.CreateNetworkContainerRequest.OrchestratorContext)
@@ -687,9 +690,9 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN
687690
netPluginConfig := service.getNetPluginDetails()
688691
switch operation {
689692
case attach:
690-
err = nc.Attach(podInfo, req.Containerid, netPluginConfig)
693+
err = nc.Attach(podInfo, req.Containerid, netPluginConfig, defaultDenyACL)
691694
case detach:
692-
err = nc.Detach(podInfo, req.Containerid, netPluginConfig)
695+
err = nc.Detach(podInfo, req.Containerid, netPluginConfig, defaultDenyACL)
693696
}
694697
if err != nil {
695698
returnCode = types.UnexpectedError

crd/multitenancy/manifests/multitenancy.acn.azure.com_podnetworkinstances.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ spec:
5757
default: 0
5858
description: Deprecated - use PodNetworks
5959
type: integer
60+
DefaultDenyACL:
61+
default: false
62+
description: indicates whether default deny policy will be present on the pods upon pod creation
63+
type: bool
6064
podNetworkConfigs:
6165
description: |-
6266
PodNetworkConfigs describes each PodNetwork to attach to a single Pod

0 commit comments

Comments
 (0)