@@ -15,6 +15,7 @@ import (
15
15
"k8s.io/client-go/transport"
16
16
17
17
"github.com/jetstack/preflight/api"
18
+ "github.com/jetstack/preflight/pkg/datagatherer/k8s"
18
19
"github.com/jetstack/preflight/pkg/version"
19
20
)
20
21
@@ -29,6 +30,8 @@ const (
29
30
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
30
31
)
31
32
33
+ type ResourceData map [string ][]interface {}
34
+
32
35
// Snapshot is the JSON that the CyberArk Discovery and Context API expects to
33
36
// be uploaded to the AWS presigned URL.
34
37
type Snapshot struct {
@@ -41,110 +44,81 @@ type Snapshot struct {
41
44
RoleBindings []interface {} `json:"role_bindings"`
42
45
}
43
46
44
- // The names of Datagatherer configs which have the data to populate the Cyberark Snapshot
45
- const (
46
- Discovery = "k8s-discovery"
47
- SecretsGatherer = "k8s/secrets"
48
- ServiceAccountsGatherer = "k8s/serviceaccounts"
49
- RolesGatherer = "k8s/roles"
50
- RoleBindingsGatherer = "k8s/rolebindings"
51
- ClusterRolesGatherer = "k8s/clusterroles"
52
- ClusterRoleBindingsGatherer = "k8s/clusterrolebindings"
53
- )
47
+ // The names of Datagatherers which have the data to populate the Cyberark Snapshot mapped to the key in the Cyberark snapshot.
48
+ 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" ,
55
+ }
56
+
57
+ func extractResourceListFromReading (reading * api.DataReading ) ([]interface {}, error ) {
58
+ data , ok := reading .Data .(* k8s.DynamicData )
59
+ if ! ok {
60
+ return nil , fmt .Errorf ("failed to convert data: %s" , reading .DataGatherer )
61
+ }
62
+ items := data .Items
63
+ resources := make ([]interface {}, len (items ))
64
+ for i , resource := range items {
65
+ resources [i ] = resource .Resource
66
+ }
67
+ return resources , nil
68
+ }
69
+
70
+ // TODO(wallj): Use k8s version.Info struct here
71
+ func extractServerVersionFromReading (reading * api.DataReading ) (string , error ) {
72
+ data , ok := reading .Data .(map [string ]interface {})
73
+ if ! ok {
74
+ return "" , fmt .Errorf ("failed to convert data: %s" , reading .DataGatherer )
75
+ }
76
+ serverVersion , ok := data ["server_version" ]
77
+ if ! ok {
78
+ return "" , fmt .Errorf ("server_version key not found in data: %v" , data )
79
+ }
80
+ serverVersionBytes , err := json .Marshal (serverVersion )
81
+ if err != nil {
82
+ return "" , fmt .Errorf ("while marshalling server_version: %s" , err )
83
+ }
84
+ var serverVersionInfo map [string ]string
85
+ if err := json .Unmarshal (serverVersionBytes , & serverVersionInfo ); err != nil {
86
+ return "" , fmt .Errorf ("while un-marshalling server_version bytes: %s" , err )
87
+ }
88
+ return serverVersionInfo ["gitVersion" ], nil
89
+ }
54
90
55
91
// ConvertDataReadingsToCyberarkSnapshot converts jetstack-secure DataReadings into Cyberark Snapshot format.
56
92
func ConvertDataReadingsToCyberarkSnapshot (
57
93
input api.DataReadingsPost ,
58
- ) (snapshot Snapshot , err error ) {
59
- var (
60
- k8sVersion string
61
- secrets , serviceAccounts , roles , roleBindings []interface {}
62
- )
63
-
94
+ ) (_ * Snapshot , err error ) {
95
+ k8sVersion := ""
96
+ resourceData := ResourceData {}
64
97
for _ , reading := range input .DataReadings {
65
- switch reading .DataGatherer {
66
- case Discovery :
67
- data , ok := reading .Data .(map [string ]interface {})
68
- if ! ok {
69
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
70
- }
71
- serverVersion := data ["server_version" ]
72
- serverVersionBytes , err := json .Marshal (serverVersion )
98
+ if reading .DataGatherer == "k8s/discovery" {
99
+ k8sVersion , err = extractServerVersionFromReading (reading )
73
100
if err != nil {
74
- return snapshot , fmt .Errorf ("while marshalling server_version: %s" , err )
75
- }
76
- var serverVersionInfo map [string ]string
77
- if err := json .Unmarshal (serverVersionBytes , & serverVersionInfo ); err != nil {
78
- return snapshot , fmt .Errorf ("while un-marshalling server_version bytes: %s" , err )
101
+ return nil , fmt .Errorf ("while extracting server version from data-reading: %s" , err )
79
102
}
80
- k8sVersion = serverVersionInfo ["gitVersion" ]
81
- case SecretsGatherer :
82
- if data , ok := reading .Data .(map [string ]interface {}); ok {
83
- if items , ok := data ["items" ].([]* api.GatheredResource ); ok {
84
- resources := make ([]interface {}, len (items ))
85
- for i , resource := range items {
86
- resources [i ] = resource .Resource
87
- }
88
- secrets = append (secrets , resources ... )
89
- } else {
90
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
91
- }
92
- } else {
93
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
94
- }
95
- case ServiceAccountsGatherer :
96
- if data , ok := reading .Data .(map [string ]interface {}); ok {
97
- if items , ok := data ["items" ].([]* api.GatheredResource ); ok {
98
- resources := make ([]interface {}, len (items ))
99
- for i , resource := range items {
100
- resources [i ] = resource .Resource
101
- }
102
- serviceAccounts = append (serviceAccounts , resources ... )
103
- } else {
104
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
105
- }
106
- } else {
107
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
108
- }
109
- case RolesGatherer , ClusterRoleBindingsGatherer :
110
- if data , ok := reading .Data .(map [string ]interface {}); ok {
111
- if items , ok := data ["items" ].([]* api.GatheredResource ); ok {
112
- resources := make ([]interface {}, len (items ))
113
- for i , resource := range items {
114
- resources [i ] = resource .Resource
115
- }
116
- roles = append (roles , resources ... )
117
- } else {
118
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
119
- }
120
- } else {
121
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
122
- }
123
- case RoleBindingsGatherer , ClusterRolesGatherer :
124
- if data , ok := reading .Data .(map [string ]interface {}); ok {
125
- if items , ok := data ["items" ].([]* api.GatheredResource ); ok {
126
- resources := make ([]interface {}, len (items ))
127
- for i , resource := range items {
128
- resources [i ] = resource .Resource
129
- }
130
- roleBindings = append (roleBindings , resources ... )
131
- } else {
132
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
133
- }
134
- } else {
135
- return snapshot , fmt .Errorf ("failed to convert: %s" , reading .DataGatherer )
103
+ }
104
+ if key , found := gathererNameToresourceDataKeyMap [reading .DataGatherer ]; found {
105
+ var resources []interface {}
106
+ resources , err = extractResourceListFromReading (reading )
107
+ if err != nil {
108
+ return nil , fmt .Errorf ("while extracting resource list from data-reading: %s" , err )
136
109
}
110
+ resourceData [key ] = append (resourceData [key ], resources ... )
137
111
}
138
112
}
139
113
140
- return Snapshot {
114
+ return & Snapshot {
141
115
AgentVersion : input .AgentMetadata .Version ,
142
116
ClusterID : input .AgentMetadata .ClusterID ,
143
117
K8SVersion : k8sVersion ,
144
- Secrets : secrets ,
145
- ServiceAccounts : serviceAccounts ,
146
- Roles : roles ,
147
- RoleBindings : roleBindings ,
118
+ Secrets : resourceData [ " secrets" ] ,
119
+ ServiceAccounts : resourceData [ "serviceaccounts" ] ,
120
+ Roles : resourceData [ " roles" ] ,
121
+ RoleBindings : resourceData [ "rolebindings" ] ,
148
122
}, nil
149
123
}
150
124
0 commit comments