@@ -12,6 +12,7 @@ import (
1212 "net/http"
1313 "net/url"
1414
15+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1516 "k8s.io/client-go/transport"
1617
1718 "github.com/jetstack/preflight/api"
@@ -30,39 +31,43 @@ const (
3031 apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
3132)
3233
33- type ResourceData map [string ][]interface {}
34+ type ResourceData map [string ][]* unstructured. Unstructured
3435
3536// Snapshot is the JSON that the CyberArk Discovery and Context API expects to
3637// be uploaded to the AWS presigned URL.
3738type Snapshot struct {
38- AgentVersion string `json:"agent_version"`
39- ClusterID string `json:"cluster_id"`
40- K8SVersion string `json:"k8s_version"`
41- Secrets []interface {} `json:"secrets"`
42- ServiceAccounts []interface {} `json:"service_accounts"`
43- Roles []interface {} `json:"roles"`
44- RoleBindings []interface {} `json:"role_bindings"`
39+ AgentVersion string `json:"agent_version"`
40+ ClusterID string `json:"cluster_id"`
41+ K8SVersion string `json:"k8s_version"`
42+ Secrets []* unstructured. Unstructured `json:"secrets"`
43+ ServiceAccounts []* unstructured. Unstructured `json:"service_accounts"`
44+ Roles []* unstructured. Unstructured `json:"roles"`
45+ RoleBindings []* unstructured. Unstructured `json:"role_bindings"`
4546}
4647
4748// The names of Datagatherers which have the data to populate the Cyberark Snapshot mapped to the key in the Cyberark snapshot.
4849var gathererNameToresourceDataKeyMap = map [string ]string {
49- "k8s /secrets" : "secrets" ,
50- "k8s /serviceaccounts" : "serviceaccounts" ,
51- "k8s /roles" : "roles" ,
52- "k8s /clusterroles" : "roles" ,
53- "k8s /rolebindings" : "rolebindings" ,
54- "k8s /clusterrolebindings" : "rolebindings" ,
50+ "ark /secrets" : "secrets" ,
51+ "ark /serviceaccounts" : "serviceaccounts" ,
52+ "ark /roles" : "roles" ,
53+ "ark /clusterroles" : "roles" ,
54+ "ark /rolebindings" : "rolebindings" ,
55+ "ark /clusterrolebindings" : "rolebindings" ,
5556}
5657
57- func extractResourceListFromReading (reading * api.DataReading ) ([]interface {} , error ) {
58+ func extractResourceListFromReading (reading * api.DataReading ) ([]* unstructured. Unstructured , error ) {
5859 data , ok := reading .Data .(* k8s.DynamicData )
5960 if ! ok {
6061 return nil , fmt .Errorf ("failed to convert data: %s" , reading .DataGatherer )
6162 }
6263 items := data .Items
63- resources := make ([]interface {}, len (items ))
64- for i , resource := range items {
65- resources [i ] = resource .Resource
64+ resources := make ([]* unstructured.Unstructured , len (items ))
65+ for i , item := range items {
66+ if resource , ok := item .Resource .(* unstructured.Unstructured ); ok {
67+ resources [i ] = resource
68+ } else {
69+ return nil , fmt .Errorf ("failed to convert resource: %#v" , item )
70+ }
6671 }
6772 return resources , nil
6873}
@@ -80,19 +85,19 @@ func extractServerVersionFromReading(reading *api.DataReading) (string, error) {
8085
8186// ConvertDataReadingsToCyberarkSnapshot converts jetstack-secure DataReadings into Cyberark Snapshot format.
8287func ConvertDataReadingsToCyberarkSnapshot (
83- input api.DataReadingsPost ,
88+ payload api.DataReadingsPost ,
8489) (_ * Snapshot , err error ) {
8590 k8sVersion := ""
8691 resourceData := ResourceData {}
87- for _ , reading := range input .DataReadings {
88- if reading .DataGatherer == "k8s- discovery" {
92+ for _ , reading := range payload .DataReadings {
93+ if reading .DataGatherer == "ark/ discovery" {
8994 k8sVersion , err = extractServerVersionFromReading (reading )
9095 if err != nil {
9196 return nil , fmt .Errorf ("while extracting server version from data-reading: %s" , err )
9297 }
9398 }
9499 if key , found := gathererNameToresourceDataKeyMap [reading .DataGatherer ]; found {
95- var resources []interface {}
100+ var resources []* unstructured. Unstructured
96101 resources , err = extractResourceListFromReading (reading )
97102 if err != nil {
98103 return nil , fmt .Errorf ("while extracting resource list from data-reading: %s" , err )
@@ -102,8 +107,8 @@ func ConvertDataReadingsToCyberarkSnapshot(
102107 }
103108
104109 return & Snapshot {
105- AgentVersion : input .AgentMetadata .Version ,
106- ClusterID : input .AgentMetadata .ClusterID ,
110+ AgentVersion : payload .AgentMetadata .Version ,
111+ ClusterID : payload .AgentMetadata .ClusterID ,
107112 K8SVersion : k8sVersion ,
108113 Secrets : resourceData ["secrets" ],
109114 ServiceAccounts : resourceData ["serviceaccounts" ],
0 commit comments