-
Notifications
You must be signed in to change notification settings - Fork 25
[VC-43753] CyberArk Discovery and Context: Upload data in the JSON format required by the API #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9afa248
c5edd24
8d1365d
42f5861
b344e46
80a73e7
5ab45c7
0b19866
ca693d4
d54d435
f76274a
667a065
5fce793
369494d
a5a375c
65ca6d0
a361867
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I deleted this file because it was causing import cycle.
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/version" | ||
"k8s.io/client-go/discovery" | ||
|
||
"github.com/jetstack/preflight/pkg/datagatherer" | ||
|
@@ -57,17 +58,18 @@ func (g *DataGathererDiscovery) WaitForCacheSync(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
type DiscoveryData struct { | ||
ServerVersion *version.Info `json:"server_version"` | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a data type instead of the ad-hoc map that was previously returned by this data gatherer, |
||
// Fetch will fetch discovery data from the apiserver, or return an error | ||
func (g *DataGathererDiscovery) Fetch() (interface{}, int, error) { | ||
data, err := g.cl.ServerVersion() | ||
serverVersion, err := g.cl.ServerVersion() | ||
if err != nil { | ||
return nil, -1, fmt.Errorf("failed to get server version: %v", err) | ||
} | ||
|
||
response := map[string]interface{}{ | ||
// data has type Info: https://godoc.org/k8s.io/apimachinery/pkg/version#Info | ||
"server_version": data, | ||
} | ||
|
||
return response, len(response), nil | ||
return &DiscoveryData{ | ||
ServerVersion: serverVersion, | ||
}, 1, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,14 +307,17 @@ func (g *DataGathererDynamic) WaitForCacheSync(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
type DynamicData struct { | ||
Items []*api.GatheredResource `json:"items"` | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a data type instead of the ad-hoc map that was previously returned by this data gatherer, |
||
|
||
// Fetch will fetch the requested data from the apiserver, or return an error | ||
// if fetching the data fails. | ||
func (g *DataGathererDynamic) Fetch() (interface{}, int, error) { | ||
if g.groupVersionResource.String() == "" { | ||
return nil, -1, fmt.Errorf("resource type must be specified") | ||
} | ||
|
||
var list = map[string]interface{}{} | ||
var items = []*api.GatheredResource{} | ||
|
||
fetchNamespaces := g.namespaces | ||
|
@@ -344,10 +347,9 @@ func (g *DataGathererDynamic) Fetch() (interface{}, int, error) { | |
return nil, -1, err | ||
} | ||
|
||
// add gathered resources to items | ||
list["items"] = items | ||
|
||
return list, len(items), nil | ||
return &DynamicData{ | ||
Items: items, | ||
}, len(items), nil | ||
} | ||
|
||
func redactList(list []*api.GatheredResource, excludeAnnotKeys, excludeLabelKeys []*regexp.Regexp) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ var SecretSelectedFields = []FieldPath{ | |
{"metadata", "ownerReferences"}, | ||
{"metadata", "selfLink"}, | ||
{"metadata", "uid"}, | ||
{"metadata", "creationTimestamp"}, | ||
{"metadata", "deletionTimestamp"}, | ||
{"metadata", "resourceVersion"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Cyberark backend needs this extra metadata to produce its reports. |
||
|
||
{"type"}, | ||
{"data", "tls.crt"}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"k8s.io/client-go/transport" | ||
|
||
"github.com/jetstack/preflight/api" | ||
"github.com/jetstack/preflight/pkg/datagatherer/k8s" | ||
"github.com/jetstack/preflight/pkg/version" | ||
) | ||
|
||
|
@@ -29,6 +30,88 @@ const ( | |
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links" | ||
) | ||
|
||
type ResourceData map[string][]interface{} | ||
|
||
// Snapshot is the JSON that the CyberArk Discovery and Context API expects to | ||
// be uploaded to the AWS presigned URL. | ||
type Snapshot struct { | ||
AgentVersion string `json:"agent_version"` | ||
ClusterID string `json:"cluster_id"` | ||
K8SVersion string `json:"k8s_version"` | ||
Secrets []interface{} `json:"secrets"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this into a concrete type? I think it is a |
||
ServiceAccounts []interface{} `json:"service_accounts"` | ||
Roles []interface{} `json:"roles"` | ||
RoleBindings []interface{} `json:"role_bindings"` | ||
} | ||
|
||
// The names of Datagatherers which have the data to populate the Cyberark Snapshot mapped to the key in the Cyberark snapshot. | ||
var gathererNameToresourceDataKeyMap = map[string]string{ | ||
"k8s/secrets": "secrets", | ||
"k8s/serviceaccounts": "serviceaccounts", | ||
"k8s/roles": "roles", | ||
"k8s/clusterroles": "roles", | ||
"k8s/rolebindings": "rolebindings", | ||
"k8s/clusterrolebindings": "rolebindings", | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API currently requires roles and clusterroles, rolebindings and clusterrolebindings, to be combined. Hence the duplicate key names here. |
||
|
||
func extractResourceListFromReading(reading *api.DataReading) ([]interface{}, error) { | ||
data, ok := reading.Data.(*k8s.DynamicData) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to convert data: %s", reading.DataGatherer) | ||
} | ||
items := data.Items | ||
resources := make([]interface{}, len(items)) | ||
for i, resource := range items { | ||
resources[i] = resource.Resource | ||
} | ||
return resources, nil | ||
} | ||
|
||
func extractServerVersionFromReading(reading *api.DataReading) (string, error) { | ||
data, ok := reading.Data.(*k8s.DiscoveryData) | ||
if !ok { | ||
return "", fmt.Errorf("failed to convert data: %s", reading.DataGatherer) | ||
} | ||
if data.ServerVersion == nil { | ||
return "unknown", nil | ||
} | ||
return data.ServerVersion.GitVersion, nil | ||
} | ||
|
||
// ConvertDataReadingsToCyberarkSnapshot converts jetstack-secure DataReadings into Cyberark Snapshot format. | ||
func ConvertDataReadingsToCyberarkSnapshot( | ||
input api.DataReadingsPost, | ||
) (_ *Snapshot, err error) { | ||
k8sVersion := "" | ||
resourceData := ResourceData{} | ||
for _, reading := range input.DataReadings { | ||
if reading.DataGatherer == "k8s-discovery" { | ||
k8sVersion, err = extractServerVersionFromReading(reading) | ||
if err != nil { | ||
return nil, fmt.Errorf("while extracting server version from data-reading: %s", err) | ||
} | ||
} | ||
if key, found := gathererNameToresourceDataKeyMap[reading.DataGatherer]; found { | ||
var resources []interface{} | ||
resources, err = extractResourceListFromReading(reading) | ||
if err != nil { | ||
return nil, fmt.Errorf("while extracting resource list from data-reading: %s", err) | ||
} | ||
resourceData[key] = append(resourceData[key], resources...) | ||
} | ||
} | ||
|
||
return &Snapshot{ | ||
AgentVersion: input.AgentMetadata.Version, | ||
ClusterID: input.AgentMetadata.ClusterID, | ||
K8SVersion: k8sVersion, | ||
Secrets: resourceData["secrets"], | ||
ServiceAccounts: resourceData["serviceaccounts"], | ||
Roles: resourceData["roles"], | ||
RoleBindings: resourceData["rolebindings"], | ||
}, nil | ||
} | ||
|
||
type CyberArkClient struct { | ||
baseURL string | ||
client *http.Client | ||
|
@@ -63,9 +146,14 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa | |
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty") | ||
} | ||
|
||
snapshot, err := ConvertDataReadingsToCyberarkSnapshot(payload) | ||
if err != nil { | ||
return fmt.Errorf("while converting datareadings to Cyberark snapshot format: %s", err) | ||
} | ||
|
||
encodedBody := &bytes.Buffer{} | ||
checksum := sha256.New() | ||
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(payload); err != nil { | ||
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(snapshot); err != nil { | ||
return err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added json annotations here so that I can unmarshal date readings from a file, for testing.
The agent already has an
--input-file
option, but stops decoding the input atapi.DataReading.Data
, leaving the actual data asinterface{}
.In the test in this PR I need to decode the Data, so that it has the same types as the DataGatherer.Fetch return values.