Skip to content

Commit

Permalink
Refactoring of transform rule API
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCazade committed Mar 4, 2024
1 parent ab65bc4 commit 72a489d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
33 changes: 25 additions & 8 deletions pkg/api/transform_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,42 @@ func TransformNetworkOperationName(operation string) string {
}

type NetworkTransformRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
Parameters string `yaml:"parameters,omitempty" json:"parameters,omitempty" doc:"parameters specific to type"`
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
KubernetesInfra *K8sInfraRule `yaml:"kubernetes_infra,omitempty" json:"kubernetes_infra,omitempty" doc:"Kubernetes infra rule specific configuration"`
Kubernetes *K8sRule `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty" doc:"Kubernetes rule specific configuration"`
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
KubernetesInfra *K8sInfraRule `yaml:"kubernetes_infra,omitempty" json:"kubernetes_infra,omitempty" doc:"Kubernetes infra rule specific configuration"`
Kubernetes *K8sRule `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty" doc:"Kubernetes rule specific configuration"`
AddSubnet *NetworkAddSubnetRule `yaml:"add_subnet,omitempty" json:"add_subnet,omitempty" doc:"Add subnet rule specific configuration"`
AddLocation *NetworkGenericRule `yaml:"add_location,omitempty" json:"add_location,omitempty" doc:"Add location rule specific configuration"`
AddService *NetworkAddServiceRule `yaml:"add_service,omitempty" json:"add_service,omitempty" doc:"Add service rule specific configuration"`
}

type K8sInfraRule struct {
Inputs []string `yaml:"inputs,omitempty" json:"inputs,omitempty" doc:"entry inputs fields"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
InfraPrefix string `yaml:"infra_prefixes,omitempty" json:"infra_prefixes,omitempty" doc:"Namespace prefixes that will be tagged as infra"`
InfraPrefix []string `yaml:"infra_prefixes,omitempty" json:"infra_prefixes,omitempty" doc:"Namespace prefixes that will be tagged as infra"`
}

type K8sRule struct {
AddZone bool `yaml:"add_zone,omitempty" json:"add_zone,omitempty" doc:"If true the rule will add the zone"`
}

type NetworkGenericRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
}

type NetworkAddSubnetRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
SubnetMask string `yaml:"protocol,omitempty" json:"protocol,omitempty" doc:"entry protocol field"`
}

type NetworkAddServiceRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty" doc:"entry protocol field"`
}

type NetworkTransformDirectionInfo struct {
ReporterIPField string `yaml:"reporterIPField,omitempty" json:"reporterIPField,omitempty" doc:"field providing the reporter (agent) host IP"`
SrcHostField string `yaml:"srcHostField,omitempty" json:"srcHostField,omitempty" doc:"source host field"`
Expand Down
42 changes: 27 additions & 15 deletions pkg/pipeline/transform/transform_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,42 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
for _, rule := range n.Rules {
switch rule.Type {
case api.OpAddSubnet:
_, ipv4Net, err := net.ParseCIDR(fmt.Sprintf("%v%s", outputEntry[rule.Input], rule.Parameters))
if rule.AddSubnet == nil {
log.Errorf("Missing add subnet configuration")
continue
}
_, ipv4Net, err := net.ParseCIDR(fmt.Sprintf("%v%s", outputEntry[rule.AddSubnet.Input], rule.AddSubnet.SubnetMask))
if err != nil {
log.Warningf("Can't find subnet for IP %v and prefix length %s - err %v", outputEntry[rule.Input], rule.Parameters, err)
log.Warningf("Can't find subnet for IP %v and prefix length %s - err %v", outputEntry[rule.AddSubnet.Input], rule.AddSubnet.SubnetMask, err)
continue
}
outputEntry[rule.Output] = ipv4Net.String()
outputEntry[rule.AddSubnet.Output] = ipv4Net.String()
case api.OpAddLocation:
if rule.AddLocation == nil {
log.Errorf("Missing add location configuration")
continue
}
var locationInfo *location.Info
err, locationInfo := location.GetLocation(fmt.Sprintf("%s", outputEntry[rule.Input]))
err, locationInfo := location.GetLocation(fmt.Sprintf("%s", outputEntry[rule.AddLocation.Input]))
if err != nil {
log.Warningf("Can't find location for IP %v err %v", outputEntry[rule.Input], err)
log.Warningf("Can't find location for IP %v err %v", outputEntry[rule.AddLocation.Input], err)
continue
}
outputEntry[rule.Output+"_CountryName"] = locationInfo.CountryName
outputEntry[rule.Output+"_CountryLongName"] = locationInfo.CountryLongName
outputEntry[rule.Output+"_RegionName"] = locationInfo.RegionName
outputEntry[rule.Output+"_CityName"] = locationInfo.CityName
outputEntry[rule.Output+"_Latitude"] = locationInfo.Latitude
outputEntry[rule.Output+"_Longitude"] = locationInfo.Longitude
outputEntry[rule.AddLocation.Output+"_CountryName"] = locationInfo.CountryName
outputEntry[rule.AddLocation.Output+"_CountryLongName"] = locationInfo.CountryLongName
outputEntry[rule.AddLocation.Output+"_RegionName"] = locationInfo.RegionName
outputEntry[rule.AddLocation.Output+"_CityName"] = locationInfo.CityName
outputEntry[rule.AddLocation.Output+"_Latitude"] = locationInfo.Latitude
outputEntry[rule.AddLocation.Output+"_Longitude"] = locationInfo.Longitude
case api.OpAddService:
if rule.AddService == nil {
log.Errorf("Missing add service configuration")
continue
}
protocol := fmt.Sprintf("%v", outputEntry[rule.Parameters])
portNumber, err := strconv.Atoi(fmt.Sprintf("%v", outputEntry[rule.Input]))
portNumber, err := strconv.Atoi(fmt.Sprintf("%v", outputEntry[rule.AddService.Input]))
if err != nil {
log.Errorf("Can't convert port to int: Port %v - err %v", outputEntry[rule.Input], err)
log.Errorf("Can't convert port to int: Port %v - err %v", outputEntry[rule.AddService.Input], err)
continue
}
var serviceName string
Expand All @@ -92,11 +104,11 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
}
if serviceName == "" {
if err != nil {
log.Debugf("Can't find service name for Port %v and protocol %v - err %v", outputEntry[rule.Input], protocol, err)
log.Debugf("Can't find service name for Port %v and protocol %v - err %v", outputEntry[rule.AddService.Input], protocol, err)
continue
}
}
outputEntry[rule.Output] = serviceName
outputEntry[rule.AddService.Output] = serviceName
case api.OpAddKubernetes:
kubernetes.Enrich(outputEntry, rule)
case api.OpAddKubernetesInfra:
Expand Down

0 comments on commit 72a489d

Please sign in to comment.