Skip to content

Commit 4c32273

Browse files
committed
Use unstructured instead of interface
Signed-off-by: Richard Wall <[email protected]>
1 parent 310bf58 commit 4c32273

File tree

6 files changed

+85
-35
lines changed

6 files changed

+85
-35
lines changed

api/datareading.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package api
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"time"
7+
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
69
)
710

811
// DataReadingsPost is the payload in the upload request.
@@ -48,3 +51,20 @@ func (v GatheredResource) MarshalJSON() ([]byte, error) {
4851

4952
return json.Marshal(data)
5053
}
54+
55+
func (v *GatheredResource) UnmarshalJSON(data []byte) error {
56+
var tmpResource struct {
57+
Resource *unstructured.Unstructured `json:"resource"`
58+
DeletedAt Time `json:"deleted_at,omitempty"`
59+
}
60+
61+
d := json.NewDecoder(bytes.NewReader(data))
62+
d.DisallowUnknownFields()
63+
64+
if err := d.Decode(&tmpResource); err != nil {
65+
return err
66+
}
67+
v.Resource = tmpResource.Resource
68+
v.DeletedAt = tmpResource.DeletedAt
69+
return nil
70+
}

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3738
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"`
4546
}
4647

4748
// The names of Datagatherers which have the data to populate the Cyberark Snapshot mapped to the key in the Cyberark snapshot.
4849
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",
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+
7088
func 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)

pkg/internal/cyberark/dataupload/testdata/example-1/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ go run . agent \
1414
--install-namespace venafi \
1515
--log-level 6 \
1616
--one-shot \
17-
--agent-config pkg/internal/cyberark/dataupload/testdata/example-1/agent.yaml \
17+
--agent-config-file pkg/internal/cyberark/dataupload/testdata/example-1/agent.yaml \
1818
--output-path pkg/internal/cyberark/dataupload/testdata/example-1/datareadings.json
1919
gzip pkg/internal/cyberark/dataupload/testdata/example-1/datareadings.json
2020
```

pkg/internal/cyberark/dataupload/testdata/example-1/agent.yaml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ cluster_id: example-cluster-id
22
organization_id: example-organization-id
33
data-gatherers:
44
# gather k8s apiserver version information
5-
- kind: "k8s-discovery"
6-
name: "k8s-discovery"
7-
- kind: "k8s-dynamic"
8-
name: "k8s/serviceaccounts"
5+
- kind: k8s-discovery
6+
name: ark/discovery
7+
- kind: k8s-dynamic
8+
name: ark/namespaces
9+
config:
10+
resource-type:
11+
version: v1
12+
resource: namespaces
13+
- kind: k8s-dynamic
14+
name: ark/serviceaccounts
915
config:
1016
resource-type:
1117
resource: serviceaccounts
1218
version: v1
13-
- kind: "k8s-dynamic"
14-
name: "k8s/secrets"
19+
- kind: k8s-dynamic
20+
name: ark/secrets
1521
config:
1622
resource-type:
1723
version: v1
@@ -24,29 +30,29 @@ data-gatherers:
2430
- type!=kubernetes.io/ssh-auth
2531
- type!=bootstrap.kubernetes.io/token
2632
- type!=helm.sh/release.v1
27-
- kind: "k8s-dynamic"
28-
name: "k8s/roles"
33+
- kind: k8s-dynamic
34+
name: ark/roles
2935
config:
3036
resource-type:
3137
version: v1
3238
group: rbac.authorization.k8s.io
3339
resource: roles
34-
- kind: "k8s-dynamic"
35-
name: "k8s/clusterroles"
40+
- kind: k8s-dynamic
41+
name: ark/clusterroles
3642
config:
3743
resource-type:
3844
version: v1
3945
group: rbac.authorization.k8s.io
4046
resource: clusterroles
41-
- kind: "k8s-dynamic"
42-
name: "k8s/rolebindings"
47+
- kind: k8s-dynamic
48+
name: ark/rolebindings
4349
config:
4450
resource-type:
4551
version: v1
4652
group: rbac.authorization.k8s.io
4753
resource: rolebindings
48-
- kind: "k8s-dynamic"
49-
name: "k8s/clusterrolebindings"
54+
- kind: k8s-dynamic
55+
name: ark/clusterrolebindings
5056
config:
5157
resource-type:
5258
version: v1
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)