Skip to content

Commit 919c73d

Browse files
committed
Use a namespaces datagatherer to get the cluster ID
Remove obsolete clusteruid package Signed-off-by: Richard Wall <[email protected]>
1 parent 5196fc9 commit 919c73d

File tree

10 files changed

+99
-157
lines changed

10 files changed

+99
-157
lines changed

pkg/agent/run.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232

3333
"github.com/jetstack/preflight/api"
3434
"github.com/jetstack/preflight/pkg/client"
35-
"github.com/jetstack/preflight/pkg/clusteruid"
3635
"github.com/jetstack/preflight/pkg/datagatherer"
3736
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
3837
"github.com/jetstack/preflight/pkg/kubeconfig"
@@ -79,28 +78,6 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7978
return fmt.Errorf("While evaluating configuration: %v", err)
8079
}
8180

82-
// We need the cluster UID before we progress further so it can be sent along with other data readings
83-
84-
{
85-
restCfg, err := kubeconfig.LoadRESTConfig("")
86-
if err != nil {
87-
return err
88-
}
89-
90-
clientset, err := kubernetes.NewForConfig(restCfg)
91-
if err != nil {
92-
return err
93-
}
94-
95-
ctx, err = clusteruid.GetClusterUID(ctx, clientset)
96-
if err != nil {
97-
return fmt.Errorf("failed to get cluster UID: %v", err)
98-
}
99-
100-
clusterUID := clusteruid.ClusterUIDFromContext(ctx)
101-
log.V(logs.Debug).Info("Retrieved cluster UID", "clusterUID", clusterUID)
102-
}
103-
10481
group, gctx := errgroup.WithContext(ctx)
10582
defer func() {
10683
cancel()

pkg/clusteruid/clusteruid.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/clusteruid/clusteruid_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ type CyberArkClient struct {
3636
authenticateRequest func(req *http.Request) error
3737
}
3838

39-
type Options struct {
40-
ClusterName string
41-
}
42-
4339
func NewCyberArkClient(trustedCAs *x509.CertPool, baseURL string, authenticateRequest func(req *http.Request) error) (*CyberArkClient, error) {
4440
cyberClient := &http.Client{}
4541
tr := http.DefaultTransport.(*http.Transport).Clone()
@@ -55,14 +51,10 @@ func NewCyberArkClient(trustedCAs *x509.CertPool, baseURL string, authenticateRe
5551
}, nil
5652
}
5753

58-
// PostDataReadingsWithOptions PUTs the supplied payload to an [AWS presigned URL] which it obtains via the CyberArk inventory API.
54+
// PostDataReadings PUTs the supplied payload to an [AWS presigned URL] which it obtains via the CyberArk inventory API.
5955
//
6056
// [AWS presigned URL]: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
61-
func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, readings []*api.DataReading, opts Options) error {
62-
if opts.ClusterName == "" {
63-
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty")
64-
}
65-
57+
func (c *CyberArkClient) PostDataReadings(ctx context.Context, readings []*api.DataReading) error {
6658
snapshot, err := convertDataReadingsToCyberarkSnapshot(readings)
6759
if err != nil {
6860
return fmt.Errorf("while converting datareadings to Cyberark snapshot format: %s", err)
@@ -74,7 +66,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, readin
7466
return err
7567
}
7668

77-
presignedUploadURL, err := c.retrievePresignedUploadURL(ctx, hex.EncodeToString(checksum.Sum(nil)), opts)
69+
presignedUploadURL, err := c.retrievePresignedUploadURL(ctx, hex.EncodeToString(checksum.Sum(nil)), snapshot.ClusterID)
7870
if err != nil {
7971
return fmt.Errorf("while retrieving snapshot upload URL: %s", err)
8072
}
@@ -104,7 +96,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, readin
10496
return nil
10597
}
10698

107-
func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksum string, opts Options) (string, error) {
99+
func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksum string, clusterID string) (string, error) {
108100
uploadURL, err := url.JoinPath(c.baseURL, apiPathSnapshotLinks)
109101
if err != nil {
110102
return "", err
@@ -115,7 +107,7 @@ func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksu
115107
Checksum string `json:"checksum_sha3"`
116108
AgentVersion string `json:"agent_version"`
117109
}{
118-
ClusterID: opts.ClusterName,
110+
ClusterID: clusterID,
119111
Checksum: checksum,
120112
AgentVersion: version.PreflightVersion,
121113
}

pkg/internal/cyberark/dataupload/dataupload_test.go

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"net/http"
99
"os"
1010
"testing"
11-
"time"
1211

1312
"github.com/stretchr/testify/require"
13+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14+
"k8s.io/apimachinery/pkg/types"
1415
"k8s.io/klog/v2"
1516
"k8s.io/klog/v2/ktesting"
1617

@@ -23,20 +24,41 @@ import (
2324
_ "k8s.io/klog/v2/ktesting/init"
2425
)
2526

26-
func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
27-
fakeTime := time.Unix(123, 0)
28-
defaultDataReadings := []*api.DataReading{
29-
{
30-
ClusterID: "success-cluster-id",
31-
DataGatherer: "test-gatherer",
32-
Timestamp: api.Time{Time: fakeTime},
33-
Data: map[string]interface{}{"test": "data"},
34-
SchemaVersion: "v1",
27+
func genNamespace(name string) *unstructured.Unstructured {
28+
o := &unstructured.Unstructured{}
29+
o.SetAPIVersion("")
30+
o.SetKind("Namespace")
31+
o.SetName(name)
32+
return o
33+
}
34+
func genArkNamespacesDataReading(clusterID types.UID) *api.DataReading {
35+
kubeSystemNamespace := genNamespace("kube-system")
36+
kubeSystemNamespace.SetUID(clusterID)
37+
return &api.DataReading{
38+
ClusterID: "ignored-tlspk-cluster-id",
39+
DataGatherer: "ark/namespaces",
40+
Data: &api.DynamicData{
41+
Items: []*api.GatheredResource{
42+
{
43+
Resource: kubeSystemNamespace,
44+
},
45+
{
46+
Resource: genNamespace("kube-public"),
47+
},
48+
{
49+
Resource: genNamespace("venafi"),
50+
},
51+
{
52+
Resource: genNamespace("cert-manager"),
53+
},
54+
},
3555
},
56+
SchemaVersion: "v1",
3657
}
37-
38-
defaultOpts := dataupload.Options{
39-
ClusterName: "success-cluster-id",
58+
}
59+
func TestCyberArkClient_PostDataReadings_MockAPI(t *testing.T) {
60+
defaultDataReadings := []*api.DataReading{
61+
genArkNamespacesDataReading("success-cluster-id"),
4062
}
4163

4264
setToken := func(token string) func(*http.Request) error {
@@ -50,31 +72,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
5072
name string
5173
readings []*api.DataReading
5274
authenticate func(req *http.Request) error
53-
opts dataupload.Options
5475
requireFn func(t *testing.T, err error)
5576
}{
5677
{
5778
name: "successful upload",
5879
readings: defaultDataReadings,
59-
opts: defaultOpts,
6080
authenticate: setToken("success-token"),
6181
requireFn: func(t *testing.T, err error) {
6282
require.NoError(t, err)
6383
},
6484
},
6585
{
66-
name: "error when cluster name is empty",
67-
readings: defaultDataReadings,
68-
opts: dataupload.Options{ClusterName: ""},
86+
name: "error when cluster ID not found among data readings",
87+
readings: nil,
6988
authenticate: setToken("success-token"),
7089
requireFn: func(t *testing.T, err error) {
71-
require.ErrorContains(t, err, "programmer mistake: the cluster name")
90+
require.ErrorContains(t, err, "while converting datareadings to Cyberark snapshot format: failed to compute a clusterID from the data-readings")
7291
},
7392
},
7493
{
7594
name: "error when bearer token is incorrect",
7695
readings: defaultDataReadings,
77-
opts: defaultOpts,
7896
authenticate: setToken("fail-token"),
7997
requireFn: func(t *testing.T, err error) {
8098
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token")
@@ -83,7 +101,6 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
83101
{
84102
name: "error contains authenticate error",
85103
readings: defaultDataReadings,
86-
opts: defaultOpts,
87104
authenticate: func(_ *http.Request) error {
88105
return errors.New("simulated-authenticate-error")
89106
},
@@ -92,18 +109,20 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
92109
},
93110
},
94111
{
95-
name: "invalid JSON from server (RetrievePresignedUploadURL step)",
96-
readings: defaultDataReadings,
97-
opts: dataupload.Options{ClusterName: "invalid-json-retrieve-presigned"},
112+
name: "invalid JSON from server (RetrievePresignedUploadURL step)",
113+
readings: []*api.DataReading{
114+
genArkNamespacesDataReading("invalid-json-retrieve-presigned"),
115+
},
98116
authenticate: setToken("success-token"),
99117
requireFn: func(t *testing.T, err error) {
100118
require.ErrorContains(t, err, "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated")
101119
},
102120
},
103121
{
104-
name: "500 from server (RetrievePresignedUploadURL step)",
105-
readings: defaultDataReadings,
106-
opts: dataupload.Options{ClusterName: "invalid-response-post-data"},
122+
name: "500 from server (RetrievePresignedUploadURL step)",
123+
readings: []*api.DataReading{
124+
genArkNamespacesDataReading("invalid-response-post-data"),
125+
},
107126
authenticate: setToken("success-token"),
108127
requireFn: func(t *testing.T, err error) {
109128
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: mock error")
@@ -128,22 +147,22 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
128147
cyberArkClient, err := dataupload.NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
129148
require.NoError(t, err)
130149

131-
err = cyberArkClient.PostDataReadingsWithOptions(ctx, tc.readings, tc.opts)
150+
err = cyberArkClient.PostDataReadings(ctx, tc.readings)
132151
tc.requireFn(t, err)
133152
})
134153
}
135154
}
136155

137-
// TestCyberArkClient_PostDataReadingsWithOptions_RealAPI demonstrates that the dataupload code works with the real inventory API.
156+
// TestCyberArkClient_PostDataReadings_RealAPI demonstrates that the dataupload code works with the real inventory API.
138157
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
139158
// ARK_SUBDOMAIN should be your tenant subdomain.
140159
// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
141160
//
142161
// To enable verbose request logging:
143162
//
144163
// go test ./pkg/internal/cyberark/dataupload/... \
145-
// -v -count 1 -run TestCyberArkClient_PostDataReadingsWithOptions_RealAPI -args -testing.v 6
146-
func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
164+
// -v -count 1 -run TestCyberArkClient_PostDataReadings_RealAPI -args -testing.v 6
165+
func TestCyberArkClient_PostDataReadings_RealAPI(t *testing.T) {
147166
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
148167
subdomain := os.Getenv("ARK_SUBDOMAIN")
149168
username := os.Getenv("ARK_USERNAME")
@@ -183,12 +202,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
183202
require.NoError(t, err)
184203

185204
dataReadings := testutil.ParseDataReadings(t, testutil.ReadGZIP(t, "testdata/example-1/datareadings.json.gz"))
186-
err = cyberArkClient.PostDataReadingsWithOptions(
205+
err = cyberArkClient.PostDataReadings(
187206
ctx,
188207
dataReadings,
189-
dataupload.Options{
190-
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
191-
},
192208
)
193209
require.NoError(t, err)
194210
}

pkg/internal/cyberark/dataupload/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r
109109
}
110110

111111
if req.ClusterID != successClusterID {
112-
http.Error(w, "post body contains cluster ID", http.StatusInternalServerError)
112+
http.Error(w, "post body does not contain cluster ID", http.StatusInternalServerError)
113113
return
114114
}
115115

0 commit comments

Comments
 (0)