Skip to content

Commit 0b19866

Browse files
committed
Convert DataReadings to Snapshot format
Signed-off-by: Richard Wall <[email protected]>
1 parent 5ab45c7 commit 0b19866

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,113 @@ type Snapshot struct {
4141
RoleBindings []interface{} `json:"role_bindings"`
4242
}
4343

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+
)
54+
55+
// ConvertDataReadingsToCyberarkSnapshot converts jetstack-secure DataReadings into Cyberark Snapshot format.
56+
func ConvertDataReadingsToCyberarkSnapshot(
57+
input api.DataReadingsPost,
58+
) (snapshot Snapshot, err error) {
59+
var (
60+
k8sVersion string
61+
secrets, serviceAccounts, roles, roleBindings []interface{}
62+
)
63+
64+
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)
73+
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)
79+
}
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)
136+
}
137+
}
138+
}
139+
140+
return Snapshot{
141+
AgentVersion: input.AgentMetadata.Version,
142+
ClusterID: input.AgentMetadata.ClusterID,
143+
K8SVersion: k8sVersion,
144+
Secrets: secrets,
145+
ServiceAccounts: serviceAccounts,
146+
Roles: roles,
147+
RoleBindings: roleBindings,
148+
}, nil
149+
}
150+
44151
type CyberArkClient struct {
45152
baseURL string
46153
client *http.Client
@@ -75,9 +182,9 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
75182
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty")
76183
}
77184

78-
snapshot := Snapshot{
79-
ClusterID: payload.AgentMetadata.ClusterID,
80-
AgentVersion: version.PreflightVersion,
185+
snapshot, err := ConvertDataReadingsToCyberarkSnapshot(payload)
186+
if err != nil {
187+
return fmt.Errorf("while converting datareadings to Cyberark snapshot format: %s", err)
81188
}
82189

83190
encodedBody := &bytes.Buffer{}

0 commit comments

Comments
 (0)