|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cortex |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/base64" |
| 19 | + "io/ioutil" |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// TestAuthentication checks whether http requests are properly authenticated with either |
| 29 | +// bearer tokens or basic authentication in the addHeaders method. |
| 30 | +func TestAuthentication(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + testName string |
| 33 | + basicAuth map[string]string |
| 34 | + basicAuthPasswordFileContents []byte |
| 35 | + bearerToken string |
| 36 | + bearerTokenFile string |
| 37 | + bearerTokenFileContents []byte |
| 38 | + expectedAuthHeaderValue string |
| 39 | + expectedError error |
| 40 | + }{ |
| 41 | + { |
| 42 | + testName: "Basic Auth with password", |
| 43 | + basicAuth: map[string]string{ |
| 44 | + "username": "TestUser", |
| 45 | + "password": "TestPassword", |
| 46 | + }, |
| 47 | + expectedAuthHeaderValue: "Basic " + base64.StdEncoding.EncodeToString( |
| 48 | + []byte("TestUser:TestPassword"), |
| 49 | + ), |
| 50 | + expectedError: nil, |
| 51 | + }, |
| 52 | + { |
| 53 | + testName: "Basic Auth with password file", |
| 54 | + basicAuth: map[string]string{ |
| 55 | + "username": "TestUser", |
| 56 | + "password_file": "passwordFile", |
| 57 | + }, |
| 58 | + basicAuthPasswordFileContents: []byte("TestPassword"), |
| 59 | + expectedAuthHeaderValue: "Basic " + base64.StdEncoding.EncodeToString( |
| 60 | + []byte("TestUser:TestPassword"), |
| 61 | + ), |
| 62 | + expectedError: nil, |
| 63 | + }, |
| 64 | + { |
| 65 | + testName: "Basic Auth with bad password file", |
| 66 | + basicAuth: map[string]string{ |
| 67 | + "username": "TestUser", |
| 68 | + "password_file": "missingPasswordFile", |
| 69 | + }, |
| 70 | + expectedAuthHeaderValue: "", |
| 71 | + expectedError: ErrFailedToReadFile, |
| 72 | + }, |
| 73 | + { |
| 74 | + testName: "Bearer Token", |
| 75 | + bearerToken: "testToken", |
| 76 | + expectedAuthHeaderValue: "Bearer testToken", |
| 77 | + expectedError: nil, |
| 78 | + }, |
| 79 | + { |
| 80 | + testName: "Bearer Token with bad bearer token file", |
| 81 | + bearerTokenFile: "missingBearerTokenFile", |
| 82 | + expectedAuthHeaderValue: "", |
| 83 | + expectedError: ErrFailedToReadFile, |
| 84 | + }, |
| 85 | + { |
| 86 | + testName: "Bearer Token with bearer token file", |
| 87 | + bearerTokenFile: "bearerTokenFile", |
| 88 | + expectedAuthHeaderValue: "Bearer testToken", |
| 89 | + bearerTokenFileContents: []byte("testToken"), |
| 90 | + expectedError: nil, |
| 91 | + }, |
| 92 | + } |
| 93 | + for _, test := range tests { |
| 94 | + t.Run(test.testName, func(t *testing.T) { |
| 95 | + // Set up a test server that runs a handler function when it receives a http |
| 96 | + // request. The server writes the request's Authorization header to the |
| 97 | + // response body. |
| 98 | + handler := func(rw http.ResponseWriter, req *http.Request) { |
| 99 | + authHeaderValue := req.Header.Get("Authorization") |
| 100 | + _, err := rw.Write([]byte(authHeaderValue)) |
| 101 | + require.Nil(t, err) |
| 102 | + } |
| 103 | + server := httptest.NewServer(http.HandlerFunc(handler)) |
| 104 | + defer server.Close() |
| 105 | + |
| 106 | + // Create the necessary files for tests. |
| 107 | + if test.basicAuth != nil { |
| 108 | + passwordFile := test.basicAuth["password_file"] |
| 109 | + if passwordFile != "" && test.basicAuthPasswordFileContents != nil { |
| 110 | + filepath := "./" + test.basicAuth["password_file"] |
| 111 | + err := createFile(test.basicAuthPasswordFileContents, filepath) |
| 112 | + require.Nil(t, err) |
| 113 | + defer os.Remove(filepath) |
| 114 | + } |
| 115 | + } |
| 116 | + if test.bearerTokenFile != "" && test.bearerTokenFileContents != nil { |
| 117 | + filepath := "./" + test.bearerTokenFile |
| 118 | + err := createFile(test.bearerTokenFileContents, filepath) |
| 119 | + require.Nil(t, err) |
| 120 | + defer os.Remove(filepath) |
| 121 | + } |
| 122 | + |
| 123 | + // Create a HTTP request and add headers to it through an Exporter. Since the |
| 124 | + // Exporter has an empty Headers map, authentication methods will be called. |
| 125 | + exporter := Exporter{ |
| 126 | + Config{ |
| 127 | + BasicAuth: test.basicAuth, |
| 128 | + BearerToken: test.bearerToken, |
| 129 | + BearerTokenFile: test.bearerTokenFile, |
| 130 | + }, |
| 131 | + } |
| 132 | + req, err := http.NewRequest(http.MethodPost, server.URL, nil) |
| 133 | + require.Nil(t, err) |
| 134 | + err = exporter.addHeaders(req) |
| 135 | + |
| 136 | + // Verify the error and if the Authorization header was correctly set. |
| 137 | + if err != nil { |
| 138 | + require.Equal(t, err.Error(), test.expectedError.Error()) |
| 139 | + } else { |
| 140 | + require.Nil(t, test.expectedError) |
| 141 | + authHeaderValue := req.Header.Get("Authorization") |
| 142 | + require.Equal(t, authHeaderValue, test.expectedAuthHeaderValue) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// createFile writes a file with a slice of bytes at a specified filepath. |
| 149 | +func createFile(bytes []byte, filepath string) error { |
| 150 | + err := ioutil.WriteFile(filepath, bytes, 0644) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
0 commit comments