github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/api/groups.go (about) 1 // Copyright 2018 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 "net" 20 "sync" 21 22 "github.com/cilium/cilium/pkg/ip" 23 ) 24 25 const ( 26 AWSProvider = "AWS" // AWS provider key 27 ) 28 29 var ( 30 providers = sync.Map{} // map with the list of providers to callback to retrive info from. 31 ) 32 33 // GroupProviderFunc is a func that need to be register to be able to 34 // register a new provider in the platform. 35 type GroupProviderFunc func(*ToGroups) ([]net.IP, error) 36 37 // ToGroups structure to store all kinds of new integrations that needs a new 38 // derivative policy. 39 type ToGroups struct { 40 AWS *AWSGroup `json:"aws,omitempty"` 41 } 42 43 // AWSGroup is an structure that can be used to whitelisting information from AWS integration 44 type AWSGroup struct { 45 Labels map[string]string `json:"labels,omitempty"` 46 SecurityGroupsIds []string `json:"securityGroupsIds,omitempty"` 47 SecurityGroupsNames []string `json:"securityGroupsNames,omitempty"` 48 Region string `json:"region,omitempty"` 49 } 50 51 // RegisterToGroupsProvider it will register a new callback that will be used 52 // when a new ToGroups rule is added. 53 func RegisterToGroupsProvider(providerName string, callback GroupProviderFunc) { 54 providers.Store(providerName, callback) 55 } 56 57 // GetCidrSet will return the CIDRRule for the rule using the callbacks that 58 // are register in the platform. 59 func (group *ToGroups) GetCidrSet() ([]CIDRRule, error) { 60 61 var ips []net.IP 62 // Get per provider CIDRSet 63 if group.AWS != nil { 64 callbackInterface, ok := providers.Load(AWSProvider) 65 if !ok { 66 return nil, fmt.Errorf("Provider %s is not registered", AWSProvider) 67 } 68 callback, ok := callbackInterface.(GroupProviderFunc) 69 if !ok { 70 return nil, fmt.Errorf("Provider callback for %s is not a valid instance", AWSProvider) 71 } 72 awsIPs, err := callback(group) 73 if err != nil { 74 return nil, fmt.Errorf( 75 "Cannot retrieve data from %s provider: %s", 76 AWSProvider, err) 77 } 78 ips = append(ips, awsIPs...) 79 } 80 81 resultIps := ip.KeepUniqueIPs(ips) 82 return IPsToCIDRRules(resultIps), nil 83 }