|
| 1 | +package client_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/x509" |
| 6 | + "encoding/json" |
| 7 | + "encoding/pem" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/jetstack/preflight/api" |
| 18 | + "github.com/jetstack/preflight/pkg/client" |
| 19 | +) |
| 20 | + |
| 21 | +func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { |
| 22 | + type testCase struct { |
| 23 | + name string |
| 24 | + handler http.Handler |
| 25 | + readings []*api.DataReading |
| 26 | + opts client.Options |
| 27 | + assertion func(t *testing.T, err error) |
| 28 | + } |
| 29 | + |
| 30 | + defaultReadings := []*api.DataReading{ |
| 31 | + { |
| 32 | + ClusterID: "test-cluster", |
| 33 | + DataGatherer: "test-gatherer", |
| 34 | + Timestamp: api.Time{Time: time.Now()}, |
| 35 | + Data: map[string]interface{}{"test": "data"}, |
| 36 | + SchemaVersion: "v1", |
| 37 | + }, |
| 38 | + } |
| 39 | + defaultOpts := client.Options{ |
| 40 | + ClusterName: "test-cluster", |
| 41 | + ClusterDescription: "Test cluster description", |
| 42 | + } |
| 43 | + |
| 44 | + tests := []testCase{ |
| 45 | + { |
| 46 | + name: "successful upload", |
| 47 | + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | + switch r.URL.Path { |
| 49 | + case "/api/data/kubernetes/upload": |
| 50 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 51 | + assert.Equal(t, "Bearer TODO-token", r.Header.Get("Authorization")) |
| 52 | + body, err := io.ReadAll(r.Body) |
| 53 | + require.NoError(t, err) |
| 54 | + var req struct { |
| 55 | + ClusterID string `json:"cluster_id"` |
| 56 | + ClusterDescription string `json:"Cluster_description"` |
| 57 | + Checksum string `json:"checksum_sha256"` |
| 58 | + } |
| 59 | + err = json.Unmarshal(body, &req) |
| 60 | + require.NoError(t, err) |
| 61 | + assert.Equal(t, "test-cluster", req.ClusterID) |
| 62 | + assert.Equal(t, "Test cluster description", req.ClusterDescription) |
| 63 | + assert.Equal(t, "TODO-checksum", req.Checksum) |
| 64 | + presignedURL := "https://" + r.Host + "/presigned-upload" |
| 65 | + w.Header().Set("Content-Type", "application/json") |
| 66 | + require.NoError(t, json.NewEncoder(w).Encode(struct { |
| 67 | + URL string `json:"url"` |
| 68 | + }{presignedURL})) |
| 69 | + case "/presigned-upload": |
| 70 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 71 | + body, err := io.ReadAll(r.Body) |
| 72 | + require.NoError(t, err) |
| 73 | + var payload api.DataReadingsPost |
| 74 | + err = json.Unmarshal(body, &payload) |
| 75 | + require.NoError(t, err) |
| 76 | + assert.Nil(t, payload.AgentMetadata) |
| 77 | + assert.NotZero(t, payload.DataGatherTime) |
| 78 | + assert.Len(t, payload.DataReadings, 1) |
| 79 | + assert.Equal(t, "test-cluster", payload.DataReadings[0].ClusterID) |
| 80 | + assert.Equal(t, "test-gatherer", payload.DataReadings[0].DataGatherer) |
| 81 | + w.WriteHeader(http.StatusOK) |
| 82 | + default: |
| 83 | + t.Errorf("Unexpected request path: %s", r.URL.Path) |
| 84 | + w.WriteHeader(http.StatusNotFound) |
| 85 | + } |
| 86 | + }), |
| 87 | + readings: defaultReadings, |
| 88 | + opts: defaultOpts, |
| 89 | + assertion: func(t *testing.T, err error) { |
| 90 | + assert.NoError(t, err) |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "error when cluster name is empty", |
| 95 | + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), |
| 96 | + readings: defaultReadings, |
| 97 | + opts: client.Options{ClusterName: ""}, |
| 98 | + assertion: func(t *testing.T, err error) { |
| 99 | + assert.Error(t, err) |
| 100 | + assert.Contains(t, err.Error(), "programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty") |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "error when presigned URL request fails", |
| 105 | + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 106 | + if r.URL.Path == "/api/data/kubernetes/upload" { |
| 107 | + w.WriteHeader(http.StatusInternalServerError) |
| 108 | + _, _ = w.Write([]byte("Internal server error")) |
| 109 | + return |
| 110 | + } |
| 111 | + w.WriteHeader(http.StatusNotFound) |
| 112 | + }), |
| 113 | + readings: defaultReadings, |
| 114 | + opts: defaultOpts, |
| 115 | + assertion: func(t *testing.T, err error) { |
| 116 | + assert.Error(t, err) |
| 117 | + assert.Contains(t, err.Error(), "received response with status code 500. Body: [Internal server error]") |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "error when upload to presigned URL fails", |
| 122 | + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 123 | + if r.URL.Path == "/api/data/kubernetes/upload" { |
| 124 | + presignedURL := "https://" + r.Host + "/presigned-upload" |
| 125 | + require.NoError(t, json.NewEncoder(w).Encode(struct { |
| 126 | + URL string `json:"url"` |
| 127 | + }{presignedURL})) |
| 128 | + return |
| 129 | + } |
| 130 | + if r.URL.Path == "/presigned-upload" { |
| 131 | + w.WriteHeader(http.StatusBadRequest) |
| 132 | + _, _ = w.Write([]byte("Bad request")) |
| 133 | + return |
| 134 | + } |
| 135 | + w.WriteHeader(http.StatusNotFound) |
| 136 | + }), |
| 137 | + readings: defaultReadings, |
| 138 | + opts: defaultOpts, |
| 139 | + assertion: func(t *testing.T, err error) { |
| 140 | + assert.Error(t, err) |
| 141 | + assert.Contains(t, err.Error(), "received response with status code 400. Body: [Bad request]") |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "error when presigned URL response is invalid JSON", |
| 146 | + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 147 | + if r.URL.Path == "/api/data/kubernetes/upload" { |
| 148 | + w.Header().Set("Content-Type", "application/json") |
| 149 | + _, _ = w.Write([]byte("invalid json")) |
| 150 | + return |
| 151 | + } |
| 152 | + w.WriteHeader(http.StatusNotFound) |
| 153 | + }), |
| 154 | + readings: defaultReadings, |
| 155 | + opts: defaultOpts, |
| 156 | + assertion: func(t *testing.T, err error) { |
| 157 | + assert.Error(t, err) |
| 158 | + assert.Contains(t, err.Error(), "invalid character") |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + for _, tc := range tests { |
| 164 | + t.Run(tc.name, func(t *testing.T) { |
| 165 | + server := httptest.NewTLSServer(tc.handler) |
| 166 | + defer server.Close() |
| 167 | + |
| 168 | + certPool := x509.NewCertPool() |
| 169 | + require.True(t, certPool.AppendCertsFromPEM(pem.EncodeToMemory(&pem.Block{ |
| 170 | + Type: "CERTIFICATE", |
| 171 | + Bytes: server.TLS.Certificates[0].Certificate[0], |
| 172 | + }))) |
| 173 | + |
| 174 | + cyberArkClient, err := client.NewCyberArkClient(certPool, server.URL) |
| 175 | + require.NoError(t, err) |
| 176 | + |
| 177 | + postErr := cyberArkClient.PostDataReadingsWithOptions(context.TODO(), tc.readings, tc.opts) |
| 178 | + tc.assertion(t, postErr) |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments