@@ -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,43 +31,60 @@ 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}
6974
75+ func extractClusterUIDFromReading (reading * api.DataReading ) (string , error ) {
76+ resources , err := extractResourceListFromReading (reading )
77+ if err != nil {
78+ return "" , err
79+ }
80+ for _ , resource := range resources {
81+ if resource .GetName () == "kube-system" {
82+ return string (resource .GetUID ()), nil
83+ }
84+ }
85+ return "" , fmt .Errorf ("kube-system namespace UID not found in data reading: %v" , reading )
86+ }
87+
7088func extractServerVersionFromReading (reading * api.DataReading ) (string , error ) {
7189 data , ok := reading .Data .(* k8s.DiscoveryData )
7290 if ! ok {
@@ -87,14 +105,20 @@ func ConvertDataReadingsToCyberarkSnapshot(
87105 clusterID := ""
88106 resourceData := ResourceData {}
89107 for _ , reading := range readings {
90- if reading .DataGatherer == "k8s- discovery" {
108+ if reading .DataGatherer == "ark/ discovery" {
91109 k8sVersion , err = extractServerVersionFromReading (reading )
92110 if err != nil {
93111 return nil , fmt .Errorf ("while extracting server version from data-reading: %s" , err )
94112 }
95113 }
114+ if reading .DataGatherer == "ark/namespaces" {
115+ clusterID , err = extractClusterUIDFromReading (reading )
116+ if err != nil {
117+ return nil , fmt .Errorf ("while extracting cluster UID from data-reading: %s" , err )
118+ }
119+ }
96120 if key , found := gathererNameToresourceDataKeyMap [reading .DataGatherer ]; found {
97- var resources []interface {}
121+ var resources []* unstructured. Unstructured
98122 resources , err = extractResourceListFromReading (reading )
99123 if err != nil {
100124 return nil , fmt .Errorf ("while extracting resource list from data-reading: %s" , err )
0 commit comments