Skip to content

[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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/datareading.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type DataReading struct {
type GatheredResource struct {
// Resource is a reference to a k8s object that was found by the informer
// should be of type unstructured.Unstructured, raw Object
Resource interface{}
DeletedAt Time
Resource interface{} `json:"resource"`
DeletedAt Time `json:"deleted_at,omitempty"`
}
Copy link
Contributor Author

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 at api.DataReading.Data, leaving the actual data as interface{}.

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.


func (v GatheredResource) MarshalJSON() ([]byte, error) {
Expand Down
9 changes: 0 additions & 9 deletions pkg/client/client_cyberark.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted this file because it was causing import cycle.

$ go test -v ./pkg/datagatherer/...
# github.com/jetstack/preflight/pkg/datagatherer/k8s
package github.com/jetstack/preflight/pkg/datagatherer/k8s
        imports github.com/jetstack/preflight/pkg/testutil from fieldfilter_test.go
        imports github.com/jetstack/preflight/pkg/client from envtest.go
        imports github.com/jetstack/preflight/pkg/internal/cyberark/dataupload from client_cyberark.go
        imports github.com/jetstack/preflight/pkg/datagatherer/k8s from dataupload.go: import cycle not allowed in test
FAIL    github.com/jetstack/preflight/pkg/datagatherer/k8s [setup failed]
?       github.com/jetstack/preflight/pkg/datagatherer  [no test files]
?       github.com/jetstack/preflight/pkg/datagatherer/local    [no test files]
FAIL

This file was deleted.

16 changes: 9 additions & 7 deletions pkg/datagatherer/k8s/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"

"github.com/jetstack/preflight/pkg/datagatherer"
Expand Down Expand Up @@ -57,17 +58,18 @@ func (g *DataGathererDiscovery) WaitForCacheSync(ctx context.Context) error {
return nil
}

type DiscoveryData struct {
ServerVersion *version.Info `json:"server_version"`
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
to make it easier to do a type conversion in ConverDatareadingsToCyberarkSnapshot function.

// 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
}
12 changes: 7 additions & 5 deletions pkg/datagatherer/k8s/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,17 @@ func (g *DataGathererDynamic) WaitForCacheSync(ctx context.Context) error {
return nil
}

type DynamicData struct {
Items []*api.GatheredResource `json:"items"`
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
to make it easier to do a type conversion in ConverDatareadingsToCyberarkSnapshot function.


// 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
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions pkg/datagatherer/k8s/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,12 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
}

if tc.expected != nil {
items, ok := res.(map[string]interface{})
data, ok := res.(*DynamicData)
if !ok {
t.Errorf("expected result be an map[string]interface{} but wasn't")
t.Errorf("expected result be *DynamicData but wasn't")
}

list, ok := items["items"].([]*api.GatheredResource)
if !ok {
t.Errorf("expected result be an []*api.GatheredResource but wasn't")
}
list := data.Items
// sorting list of results by name
sortGatheredResources(list)
// sorting list of expected results by name
Expand Down Expand Up @@ -1045,10 +1042,9 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
}

if tc.expected != nil {
res, ok := rawRes.(map[string]interface{})
require.Truef(t, ok, "expected result be an map[string]interface{} but wasn't")
actual := res["items"].([]*api.GatheredResource)
require.Truef(t, ok, "expected result be an []*api.GatheredResource but wasn't")
res, ok := rawRes.(*DynamicData)
require.Truef(t, ok, "expected result be an *DynamicData but wasn't")
actual := res.Items

// sorting list of results by name
sortGatheredResources(actual)
Expand Down
3 changes: 3 additions & 0 deletions pkg/datagatherer/k8s/fieldfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var SecretSelectedFields = []FieldPath{
{"metadata", "ownerReferences"},
{"metadata", "selfLink"},
{"metadata", "uid"},
{"metadata", "creationTimestamp"},
{"metadata", "deletionTimestamp"},
{"metadata", "resourceVersion"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Cyberark backend needs this extra metadata to produce its reports.
I think it makes sense to upload this metadata for TLSPK too.
I can't see any harm in it and it will be difficult to implement different field filters for the TLSPK vs CyberArk uploaded data.


{"type"},
{"data", "tls.crt"},
Expand Down
90 changes: 89 additions & 1 deletion pkg/internal/cyberark/dataupload/dataupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"`
Copy link
Contributor

Choose a reason for hiding this comment

The 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 *api.GatheredResource, right?

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",
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
}

Expand Down
Loading