github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/api/utils.go (about) 1 // Copyright 2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package api 16 17 import ( 18 "fmt" 19 "strings" 20 ) 21 22 // Exists returns true if the HTTP rule already exists in the list of rules 23 func (h *PortRuleHTTP) Exists(rules L7Rules) bool { 24 for _, existingRule := range rules.HTTP { 25 if h.Equal(existingRule) { 26 return true 27 } 28 } 29 30 return false 31 } 32 33 // Equal returns true if both HTTP rules are equal 34 func (h *PortRuleHTTP) Equal(o PortRuleHTTP) bool { 35 if h.Path != o.Path || 36 h.Method != o.Method || 37 h.Host != o.Host || 38 len(h.Headers) != len(o.Headers) { 39 return false 40 } 41 42 for i, value := range h.Headers { 43 if o.Headers[i] != value { 44 return false 45 } 46 } 47 return true 48 } 49 50 // Exists returns true if the HTTP rule already exists in the list of rules 51 func (k *PortRuleKafka) Exists(rules L7Rules) bool { 52 for _, existingRule := range rules.Kafka { 53 if k.Equal(existingRule) { 54 return true 55 } 56 } 57 58 return false 59 } 60 61 // Exists returns true if the DNS rule already exists in the list of rules 62 func (d *PortRuleDNS) Exists(rules L7Rules) bool { 63 for _, existingRule := range rules.DNS { 64 if d.Equal(existingRule) { 65 return true 66 } 67 } 68 69 return false 70 } 71 72 // Equal returns true if both rules are equal 73 func (k *PortRuleKafka) Equal(o PortRuleKafka) bool { 74 return k.APIVersion == o.APIVersion && k.APIKey == o.APIKey && 75 k.Topic == o.Topic && k.ClientID == o.ClientID && k.Role == o.Role 76 } 77 78 // Exists returns true if the L7 rule already exists in the list of rules 79 func (h *PortRuleL7) Exists(rules L7Rules) bool { 80 for _, existingRule := range rules.L7 { 81 if h.Equal(existingRule) { 82 return true 83 } 84 } 85 86 return false 87 } 88 89 // Equal returns true if both rules are equal 90 func (d *PortRuleDNS) Equal(o PortRuleDNS) bool { 91 return d != nil && d.MatchName == o.MatchName && d.MatchPattern == o.MatchPattern 92 } 93 94 // Equal returns true if both L7 rules are equal 95 func (h *PortRuleL7) Equal(o PortRuleL7) bool { 96 if len(*h) != len(o) { 97 return false 98 } 99 for k, v := range *h { 100 if v2, ok := o[k]; !ok || v2 != v { 101 return false 102 } 103 } 104 return true 105 } 106 107 // Validate returns an error if the layer 4 protocol is not valid 108 func (l4 L4Proto) Validate() error { 109 switch l4 { 110 case ProtoAny, ProtoTCP, ProtoUDP: 111 default: 112 return fmt.Errorf("invalid protocol %q, must be { tcp | udp | any }", l4) 113 } 114 115 return nil 116 } 117 118 // ParseL4Proto parses a string as layer 4 protocol 119 func ParseL4Proto(proto string) (L4Proto, error) { 120 if proto == "" { 121 return ProtoAny, nil 122 } 123 124 p := L4Proto(strings.ToUpper(proto)) 125 return p, p.Validate() 126 }