Skip to content

Commit 3806834

Browse files
committed
Convert data to the required format before uploading
Signed-off-by: Richard Wall <[email protected]>
1 parent 3d1f73d commit 3806834

File tree

4 files changed

+120
-29
lines changed

4 files changed

+120
-29
lines changed

agent.yaml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,43 @@ data-gatherers:
55
# gather k8s apiserver version information
66
- kind: "k8s-discovery"
77
name: "k8s-discovery"
8-
# pods data is used in the pods and application_versions packages
98
- kind: "k8s-dynamic"
10-
name: "k8s/pods"
9+
name: "k8s/serviceaccounts"
1110
config:
1211
resource-type:
13-
resource: pods
12+
resource: serviceaccounts
1413
version: v1
1514
- kind: "k8s-dynamic"
16-
name: "k8s/namespaces"
15+
name: "k8s/secrets"
1716
config:
1817
resource-type:
19-
resource: namespaces
2018
version: v1
19+
resource: secrets
20+
- kind: "k8s-dynamic"
21+
name: "k8s/roles"
22+
config:
23+
resource-type:
24+
version: v1
25+
group: rbac.authorization.k8s.io
26+
resource: roles
27+
- kind: "k8s-dynamic"
28+
name: "k8s/clusterroles"
29+
config:
30+
resource-type:
31+
version: v1
32+
group: rbac.authorization.k8s.io
33+
resource: clusterroles
34+
- kind: "k8s-dynamic"
35+
name: "k8s/rolebindings"
36+
config:
37+
resource-type:
38+
version: v1
39+
group: rbac.authorization.k8s.io
40+
resource: rolebindings
41+
- kind: "k8s-dynamic"
42+
name: "k8s/clusterrolebindings"
43+
config:
44+
resource-type:
45+
version: v1
46+
group: rbac.authorization.k8s.io
47+
resource: clusterrolebindings

api/datareading.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type DataReading struct {
2828
type GatheredResource struct {
2929
// Resource is a reference to a k8s object that was found by the informer
3030
// should be of type unstructured.Unstructured, raw Object
31-
Resource interface{}
32-
DeletedAt Time
31+
Resource interface{} `json: "resource"`
32+
DeletedAt Time `json:"deleted_at,omitempty"`
3333
}
3434

3535
func (v GatheredResource) MarshalJSON() ([]byte, error) {

pkg/agent/run.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030
"k8s.io/klog/v2"
3131
"sigs.k8s.io/controller-runtime/pkg/manager"
3232

33-
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
34-
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
3533
"github.com/jetstack/preflight/api"
3634
"github.com/jetstack/preflight/pkg/client"
3735
"github.com/jetstack/preflight/pkg/clusteruid"
3836
"github.com/jetstack/preflight/pkg/datagatherer"
3937
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
38+
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
39+
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
4040
"github.com/jetstack/preflight/pkg/kubeconfig"
4141
"github.com/jetstack/preflight/pkg/logs"
4242
"github.com/jetstack/preflight/pkg/version"
@@ -90,7 +90,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
9090

9191
const (
9292
discoveryContextServiceName = "inventory"
93-
separator = "."
93+
separator = "."
9494
)
9595

9696
// TODO(wallrj): Maybe get this URL via the service discovery API.
@@ -99,7 +99,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
9999

100100
var (
101101
identityClient *identity.Client
102-
err error
102+
err error
103103
)
104104
if platformDomain == "cyberark.cloud" {
105105
identityClient, err = identity.New(ctx, subdomain)
@@ -377,16 +377,16 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
377377
}
378378
}
379379

380+
clusterID := clusteruid.ClusterUIDFromContext(ctx)
381+
payload := api.DataReadingsPost{
382+
AgentMetadata: &api.AgentMetadata{
383+
Version: version.PreflightVersion,
384+
ClusterID: clusterID,
385+
},
386+
DataGatherTime: time.Now(),
387+
DataReadings: readings,
388+
}
380389
if config.OutputPath != "" {
381-
clusterID := clusteruid.ClusterUIDFromContext(ctx)
382-
payload := api.DataReadingsPost{
383-
AgentMetadata: &api.AgentMetadata{
384-
Version: version.PreflightVersion,
385-
ClusterID: clusterID,
386-
},
387-
DataGatherTime: time.Now(),
388-
DataReadings: readings,
389-
}
390390

391391
data, err := json.MarshalIndent(payload, "", " ")
392392
if err != nil {
@@ -409,14 +409,6 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
409409
log.Info("Warning: PushingErr: retrying", "in", t, "reason", err)
410410
})
411411
if config.MachineHubMode {
412-
clusterID := clusteruid.ClusterUIDFromContext(ctx)
413-
payload := api.DataReadingsPost{
414-
AgentMetadata: &api.AgentMetadata{
415-
Version: "",
416-
ClusterID: clusterID,
417-
},
418-
DataReadings: readings,
419-
}
420412
post := func() (any, error) {
421413
return struct{}{}, caClient.PostDataReadingsWithOptions(ctx, payload, client.CyberArkClientOptions{
422414
ClusterName: clusterID,

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,78 @@ type Options struct {
3737
ClusterDescription string
3838
}
3939

40+
type CyberarkPayload struct {
41+
AgentVersion string `json:"agent_version"`
42+
K8sVersion string `json:"k8s_version"`
43+
ClusterID string `json:"cluster_id"`
44+
Secrets []*api.GatheredResource `json:"secrets"` // kubectl output, sanitized
45+
ServiceAccounts []*api.GatheredResource `json:"service_accounts"` // kubectl output
46+
Roles []*api.GatheredResource `json:"roles"` // k8s native format
47+
RoleBindings []*api.GatheredResource `json:"role_bindings"` // k8s native format
48+
}
49+
50+
// You may want to define these constants based on your data-gatherer names
51+
const (
52+
Discovery = "k8s-discovery"
53+
SecretsGatherer = "k8s/secrets"
54+
ServiceAccountsGatherer = "k8s/serviceaccounts"
55+
RolesGatherer = "k8s/roles"
56+
RoleBindingsGatherer = "k8s/rolebindings"
57+
ClusterRolesGatherer = "k8s/clusterroles"
58+
ClusterRoleBindingsGatherer = "k8s/clusterrolebindings"
59+
)
60+
61+
// ConvertDataReadingsToCyberarkPayload converts jetstack-secure DataReadings into CyberarkPayload
62+
func ConvertDataReadingsToCyberarkPayload(
63+
input api.DataReadingsPost,
64+
) CyberarkPayload {
65+
var (
66+
k8sVersion []byte
67+
secrets, serviceAccounts, roles, roleBindings []*api.GatheredResource
68+
)
69+
70+
for _, reading := range input.DataReadings {
71+
switch reading.DataGatherer {
72+
case Discovery:
73+
k8sVersion, _ = json.Marshal(reading.Data)
74+
case SecretsGatherer:
75+
if data, ok := reading.Data.(map[string]interface{}); ok {
76+
if items, ok := data["items"].([]*api.GatheredResource); ok {
77+
secrets = append(secrets, items...)
78+
}
79+
}
80+
case ServiceAccountsGatherer:
81+
if data, ok := reading.Data.(map[string]interface{}); ok {
82+
if items, ok := data["items"].([]*api.GatheredResource); ok {
83+
serviceAccounts = append(serviceAccounts, items...)
84+
}
85+
}
86+
case RolesGatherer, ClusterRoleBindingsGatherer:
87+
if data, ok := reading.Data.(map[string]interface{}); ok {
88+
if items, ok := data["items"].([]*api.GatheredResource); ok {
89+
roles = append(roles, items...)
90+
}
91+
}
92+
case RoleBindingsGatherer, ClusterRolesGatherer:
93+
if data, ok := reading.Data.(map[string]interface{}); ok {
94+
if items, ok := data["items"].([]*api.GatheredResource); ok {
95+
roleBindings = append(roleBindings, items...)
96+
}
97+
}
98+
}
99+
}
100+
101+
return CyberarkPayload{
102+
AgentVersion: input.AgentMetadata.Version,
103+
K8sVersion: string(k8sVersion),
104+
ClusterID: input.AgentMetadata.ClusterID,
105+
Secrets: secrets,
106+
ServiceAccounts: serviceAccounts,
107+
Roles: roles,
108+
RoleBindings: roleBindings,
109+
}
110+
}
111+
40112
func NewCyberArkClient(trustedCAs *x509.CertPool, baseURL string, authenticateRequest func(req *http.Request) error) (*CyberArkClient, error) {
41113
cyberClient := &http.Client{}
42114
tr := http.DefaultTransport.(*http.Transport).Clone()
@@ -59,7 +131,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
59131

60132
encodedBody := &bytes.Buffer{}
61133
checksum := sha256.New()
62-
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(payload); err != nil {
134+
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(ConvertDataReadingsToCyberarkPayload(payload)); err != nil {
63135
return err
64136
}
65137

0 commit comments

Comments
 (0)