@@ -12,6 +12,7 @@ import (
12
12
"net/http"
13
13
"net/url"
14
14
15
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15
16
"k8s.io/client-go/transport"
16
17
17
18
"github.com/jetstack/preflight/api"
@@ -30,43 +31,60 @@ const (
30
31
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
31
32
)
32
33
33
- type ResourceData map [string ][]interface {}
34
+ type ResourceData map [string ][]* unstructured. Unstructured
34
35
35
36
// Snapshot is the JSON that the CyberArk Discovery and Context API expects to
36
37
// be uploaded to the AWS presigned URL.
37
38
type 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"`
45
46
}
46
47
47
48
// The names of Datagatherers which have the data to populate the Cyberark Snapshot mapped to the key in the Cyberark snapshot.
48
49
var 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" ,
55
56
}
56
57
57
- func extractResourceListFromReading (reading * api.DataReading ) ([]interface {} , error ) {
58
+ func extractResourceListFromReading (reading * api.DataReading ) ([]* unstructured. Unstructured , error ) {
58
59
data , ok := reading .Data .(* k8s.DynamicData )
59
60
if ! ok {
60
61
return nil , fmt .Errorf ("failed to convert data: %s" , reading .DataGatherer )
61
62
}
62
63
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
+ }
66
71
}
67
72
return resources , nil
68
73
}
69
74
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
+
70
88
func extractServerVersionFromReading (reading * api.DataReading ) (string , error ) {
71
89
data , ok := reading .Data .(* k8s.DiscoveryData )
72
90
if ! ok {
@@ -87,14 +105,20 @@ func ConvertDataReadingsToCyberarkSnapshot(
87
105
clusterID := ""
88
106
resourceData := ResourceData {}
89
107
for _ , reading := range readings {
90
- if reading .DataGatherer == "k8s- discovery" {
108
+ if reading .DataGatherer == "ark/ discovery" {
91
109
k8sVersion , err = extractServerVersionFromReading (reading )
92
110
if err != nil {
93
111
return nil , fmt .Errorf ("while extracting server version from data-reading: %s" , err )
94
112
}
95
113
}
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
+ }
96
120
if key , found := gathererNameToresourceDataKeyMap [reading .DataGatherer ]; found {
97
- var resources []interface {}
121
+ var resources []* unstructured. Unstructured
98
122
resources , err = extractResourceListFromReading (reading )
99
123
if err != nil {
100
124
return nil , fmt .Errorf ("while extracting resource list from data-reading: %s" , err )
0 commit comments