|
| 1 | +package cyberarkdiscovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + _ "embed" |
| 7 | + "encoding/hex" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/jetstack/preflight/pkg/version" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed testdata/discovery_success.json |
| 19 | +var discoverySuccessResponse string |
| 20 | + |
| 21 | +func testHandler(w http.ResponseWriter, r *http.Request) { |
| 22 | + if r.Method != http.MethodGet { |
| 23 | + // This was observed by making a POST request to the integration environment |
| 24 | + // Normally, we'd expect 405 Method Not Allowed but we match the observed response here |
| 25 | + w.WriteHeader(http.StatusForbidden) |
| 26 | + _, _ = w.Write([]byte(`{"message":"Missing Authentication Token"}`)) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + if !strings.HasPrefix(r.URL.String(), "/services/subdomain/") { |
| 31 | + // This was observed by making a request to /api/v2/services/asd |
| 32 | + // Normally, we'd expect 404 Not Found but we match the observed response here |
| 33 | + w.WriteHeader(http.StatusForbidden) |
| 34 | + _, _ = w.Write([]byte(`{"message":"Missing Authentication Token"}`)) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if r.Header.Get("User-Agent") != version.UserAgent() { |
| 39 | + w.WriteHeader(http.StatusInternalServerError) |
| 40 | + _, _ = w.Write([]byte("should set user agent on all requests")) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + if r.Header.Get("Accept") != "application/json" { |
| 45 | + w.WriteHeader(http.StatusInternalServerError) |
| 46 | + _, _ = w.Write([]byte("should request JSON on all requests")) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + subdomain := strings.TrimPrefix(r.URL.String(), "/services/subdomain/") |
| 51 | + |
| 52 | + switch subdomain { |
| 53 | + case "venafi-test": |
| 54 | + _, _ = w.Write([]byte(discoverySuccessResponse)) |
| 55 | + |
| 56 | + case "no-identity": |
| 57 | + // return a snippet of valid service discovery JSON, but don't include the identity service |
| 58 | + _, _ = w.Write([]byte(`{"data_privacy": {"ui": "https://ui.dataprivacy.integration-cyberark.cloud/", "api": "https://us-east-1.dataprivacy.integration-cyberark.cloud/api", "bootstrap": "https://venafi-test-data_privacy.integration-cyberark.cloud", "region": "us-east-1"}}`)) |
| 59 | + |
| 60 | + case "bad-request": |
| 61 | + // test how the client handles a random unexpected response |
| 62 | + w.WriteHeader(http.StatusBadRequest) |
| 63 | + _, _ = w.Write([]byte("{}")) |
| 64 | + |
| 65 | + case "json-invalid": |
| 66 | + // test that the client correctly rejects handles invalid JSON |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + _, _ = w.Write([]byte(`{"a": a}`)) |
| 69 | + |
| 70 | + case "json-too-long": |
| 71 | + // test that the client correctly rejects JSON which is too long |
| 72 | + w.WriteHeader(http.StatusOK) |
| 73 | + |
| 74 | + // we'll hex encode the random bytes (doubling the size) |
| 75 | + longData := make([]byte, 1+maxDiscoverBodySize/2) |
| 76 | + _, _ = rand.Read(longData) |
| 77 | + |
| 78 | + longJSON, err := json.Marshal(map[string]string{"key": hex.EncodeToString(longData)}) |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + |
| 83 | + _, _ = w.Write(longJSON) |
| 84 | + |
| 85 | + default: |
| 86 | + w.WriteHeader(http.StatusNotFound) |
| 87 | + _, _ = w.Write([]byte("{}")) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func Test_DiscoverIdentityAPIURL(t *testing.T) { |
| 92 | + tests := map[string]struct { |
| 93 | + subdomain string |
| 94 | + expectedURL string |
| 95 | + expectedError error |
| 96 | + }{ |
| 97 | + "successful request": { |
| 98 | + subdomain: "venafi-test", |
| 99 | + expectedURL: "https://ajp5871.id.integration-cyberark.cloud", |
| 100 | + expectedError: nil, |
| 101 | + }, |
| 102 | + "subdomain not found": { |
| 103 | + subdomain: "something-random", |
| 104 | + expectedURL: "", |
| 105 | + expectedError: fmt.Errorf("got an HTTP 404 response from service discovery; maybe the subdomain %q is incorrect or does not exist?", "something-random"), |
| 106 | + }, |
| 107 | + "no identity service in response": { |
| 108 | + subdomain: "no-identity", |
| 109 | + expectedURL: "", |
| 110 | + expectedError: fmt.Errorf("didn't find %s in response from service discovery; unable to detect CyberArk Identity API URL", identityServiceName), |
| 111 | + }, |
| 112 | + "unexpected HTTP response": { |
| 113 | + subdomain: "bad-request", |
| 114 | + expectedURL: "", |
| 115 | + expectedError: fmt.Errorf("got unexpected status code 400 Bad Request from request to service discovery API"), |
| 116 | + }, |
| 117 | + "response JSON too long": { |
| 118 | + subdomain: "json-too-long", |
| 119 | + expectedURL: "", |
| 120 | + expectedError: fmt.Errorf("rejecting JSON response from server as it was too large or was truncated"), |
| 121 | + }, |
| 122 | + "response JSON invalid": { |
| 123 | + subdomain: "json-invalid", |
| 124 | + expectedURL: "", |
| 125 | + expectedError: fmt.Errorf("failed to parse JSON from otherwise successful request to service discovery endpoint: invalid character 'a' looking for beginning of value"), |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for name, testSpec := range tests { |
| 130 | + t.Run(name, func(t *testing.T) { |
| 131 | + ctx := context.Background() |
| 132 | + |
| 133 | + ts := httptest.NewServer(http.HandlerFunc(testHandler)) |
| 134 | + |
| 135 | + defer ts.Close() |
| 136 | + |
| 137 | + client := New(WithCustomEndpoint(ts.URL)) |
| 138 | + |
| 139 | + apiURL, err := client.DiscoverIdentityAPIURL(ctx, testSpec.subdomain) |
| 140 | + if err != nil { |
| 141 | + if err.Error() != testSpec.expectedError.Error() { |
| 142 | + t.Errorf("expectedError=%v\nobservedError=%v", testSpec.expectedError, err) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // NB: we don't exit here because we also want to check the API URL is empty in the event of an error |
| 147 | + |
| 148 | + if apiURL != testSpec.expectedURL { |
| 149 | + t.Errorf("expected API URL=%s\nobserved API URL=%s", testSpec.expectedURL, apiURL) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments