|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/Microsoft/hcsshim" |
| 9 | +) |
| 10 | + |
| 11 | +// SerializePolicies serializes policies to json. |
| 12 | +func SerializePolicies(policyType CNIPolicyType, policies []Policy, epInfoData map[string]interface{}) []json.RawMessage { |
| 13 | + var jsonPolicies []json.RawMessage |
| 14 | + for _, policy := range policies { |
| 15 | + if policy.Type == policyType { |
| 16 | + if isPolicyTypeOutBoundNAT := IsPolicyTypeOutBoundNAT(policy); isPolicyTypeOutBoundNAT { |
| 17 | + if serializedOutboundNatPolicy, err := SerializeOutBoundNATPolicy(policies, epInfoData); err != nil { |
| 18 | + log.Printf("Failed to serialize OutBoundNAT policy") |
| 19 | + } else { |
| 20 | + jsonPolicies = append(jsonPolicies, serializedOutboundNatPolicy) |
| 21 | + } |
| 22 | + } else { |
| 23 | + jsonPolicies = append(jsonPolicies, policy.Data) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + return jsonPolicies |
| 28 | +} |
| 29 | + |
| 30 | +// GetOutBoundNatExceptionList returns exception list for outbound nat policy |
| 31 | +func GetOutBoundNatExceptionList(policies []Policy) ([]string, error) { |
| 32 | + type KVPair struct { |
| 33 | + Type CNIPolicyType `json:"Type"` |
| 34 | + ExceptionList json.RawMessage `json:"ExceptionList"` |
| 35 | + } |
| 36 | + |
| 37 | + for _, policy := range policies { |
| 38 | + if policy.Type == EndpointPolicy { |
| 39 | + var data KVPair |
| 40 | + if err := json.Unmarshal(policy.Data, &data); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + if data.Type == OutBoundNatPolicy { |
| 45 | + var exceptionList []string |
| 46 | + if err := json.Unmarshal(data.ExceptionList, &exceptionList); err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return exceptionList, nil |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + log.Printf("OutBoundNAT policy not set") |
| 56 | + return nil, nil |
| 57 | +} |
| 58 | + |
| 59 | +// IsPolicyTypeOutBoundNAT return true if the policy type is OutBoundNAT |
| 60 | +func IsPolicyTypeOutBoundNAT(policy Policy) bool { |
| 61 | + if policy.Type == EndpointPolicy { |
| 62 | + type KVPair struct { |
| 63 | + Type CNIPolicyType `json:"Type"` |
| 64 | + ExceptionList json.RawMessage `json:"ExceptionList"` |
| 65 | + } |
| 66 | + var data KVPair |
| 67 | + if err := json.Unmarshal(policy.Data, &data); err != nil { |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + if data.Type == OutBoundNatPolicy { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +// SerializeOutBoundNATPolicy formulates OutBoundNAT policy and returns serialized json |
| 80 | +func SerializeOutBoundNATPolicy(policies []Policy, epInfoData map[string]interface{}) (json.RawMessage, error) { |
| 81 | + outBoundNatPolicy := hcsshim.OutboundNatPolicy{} |
| 82 | + outBoundNatPolicy.Policy.Type = hcsshim.OutboundNat |
| 83 | + |
| 84 | + exceptionList, err := GetOutBoundNatExceptionList(policies) |
| 85 | + if err != nil { |
| 86 | + log.Printf("Failed to parse outbound NAT policy %v", err) |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + if exceptionList != nil { |
| 91 | + for _, ipAddress := range exceptionList { |
| 92 | + outBoundNatPolicy.Exceptions = append(outBoundNatPolicy.Exceptions, ipAddress) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if epInfoData["cnetAddressSpace"] != nil { |
| 97 | + if cnetAddressSpace := epInfoData["cnetAddressSpace"].([]string); cnetAddressSpace != nil { |
| 98 | + for _, ipAddress := range cnetAddressSpace { |
| 99 | + outBoundNatPolicy.Exceptions = append(outBoundNatPolicy.Exceptions, ipAddress) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if outBoundNatPolicy.Exceptions != nil { |
| 105 | + serializedOutboundNatPolicy, _ := json.Marshal(outBoundNatPolicy) |
| 106 | + return serializedOutboundNatPolicy, nil |
| 107 | + } |
| 108 | + |
| 109 | + return nil, fmt.Errorf("OutBoundNAT policy not set") |
| 110 | +} |
0 commit comments