|
| 1 | +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. |
| 2 | +// See LICENSE.txt for license information. |
| 3 | +// |
| 4 | + |
| 5 | +package utility |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/tls" |
| 9 | + "encoding/base64" |
| 10 | + "encoding/json" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "net/url" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | + "sync/atomic" |
| 17 | + "testing" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/mattermost/mattermost-cloud/model" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +// withGitlabConfig temporarily sets the package-level GitLab token and |
| 26 | +// gitops repo URL used by fetchFromGitlabIfNecessary, restoring previous |
| 27 | +// values on cleanup. |
| 28 | +func withGitlabConfig(t *testing.T, token, repoURL string) { |
| 29 | + t.Helper() |
| 30 | + prevToken := model.GetGitlabToken() |
| 31 | + prevURL := model.GetGitopsRepoURL() |
| 32 | + model.SetGitlabToken(token) |
| 33 | + model.SetGitopsRepoURL(repoURL) |
| 34 | + t.Cleanup(func() { |
| 35 | + model.SetGitlabToken(prevToken) |
| 36 | + model.SetGitopsRepoURL(prevURL) |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +// withFetchClient swaps the package-level gitlabFetchClient for a test |
| 41 | +// client that trusts httptest's TLS server and counts outbound calls. |
| 42 | +func withFetchClient(t *testing.T) *atomic.Int64 { |
| 43 | + t.Helper() |
| 44 | + prev := gitlabFetchClient |
| 45 | + var hits atomic.Int64 |
| 46 | + gitlabFetchClient = &http.Client{ |
| 47 | + Timeout: 5 * time.Second, |
| 48 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 49 | + return http.ErrUseLastResponse |
| 50 | + }, |
| 51 | + Transport: &countingTransport{ |
| 52 | + counter: &hits, |
| 53 | + next: &http.Transport{ |
| 54 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + t.Cleanup(func() { gitlabFetchClient = prev }) |
| 59 | + return &hits |
| 60 | +} |
| 61 | + |
| 62 | +type countingTransport struct { |
| 63 | + counter *atomic.Int64 |
| 64 | + next http.RoundTripper |
| 65 | +} |
| 66 | + |
| 67 | +func (c *countingTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| 68 | + c.counter.Add(1) |
| 69 | + return c.next.RoundTrip(r) |
| 70 | +} |
| 71 | + |
| 72 | +func TestFetchFromGitlabIfNecessary_NoTokenReturnsPathUnchanged(t *testing.T) { |
| 73 | + withGitlabConfig(t, "", "https://gitlab.example.com") |
| 74 | + hits := withFetchClient(t) |
| 75 | + |
| 76 | + in := "https://gitlab.example.com/api/v4/projects/1/repository/files/values.yaml?ref=main" |
| 77 | + out, cleanup, err := fetchFromGitlabIfNecessary(in) |
| 78 | + require.NoError(t, err) |
| 79 | + assert.Equal(t, in, out) |
| 80 | + assert.Nil(t, cleanup) |
| 81 | + assert.Zero(t, hits.Load()) |
| 82 | +} |
| 83 | + |
| 84 | +func TestFetchFromGitlabIfNecessary_NonConfiguredHostReturnsPathUnchanged(t *testing.T) { |
| 85 | + withGitlabConfig(t, "secret-token", "https://gitlab.example.com") |
| 86 | + hits := withFetchClient(t) |
| 87 | + |
| 88 | + in := "https://example.org/values.yaml" |
| 89 | + out, cleanup, err := fetchFromGitlabIfNecessary(in) |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, in, out) |
| 92 | + assert.Nil(t, cleanup) |
| 93 | + assert.Zero(t, hits.Load()) |
| 94 | +} |
| 95 | + |
| 96 | +// TestFetchFromGitlabIfNecessary_HostMatchIsExact covers a range of URL |
| 97 | +// shapes whose host is not equal to the configured GitLab host. All of |
| 98 | +// them must be treated as non-GitLab URLs and returned unchanged. |
| 99 | +func TestFetchFromGitlabIfNecessary_HostMatchIsExact(t *testing.T) { |
| 100 | + withGitlabConfig(t, "secret-token", "https://gitlab.example.com") |
| 101 | + hits := withFetchClient(t) |
| 102 | + |
| 103 | + nonConfiguredURLs := []string{ |
| 104 | + "https://git.example.com/values.yaml?ref=main", |
| 105 | + "https://github.com/x?a=1", |
| 106 | + "https://gitea.example.org/y?z=1", |
| 107 | + "https://gitlab.example.com.other.org/values.yaml?ref=main", |
| 108 | + "https://other.org/gitlab.example.com/values.yaml", |
| 109 | + "https://gitlab.example.com@other.org/values.yaml", |
| 110 | + } |
| 111 | + |
| 112 | + for _, u := range nonConfiguredURLs { |
| 113 | + out, cleanup, err := fetchFromGitlabIfNecessary(u) |
| 114 | + require.NoError(t, err, "url=%s", u) |
| 115 | + assert.Equal(t, u, out, "url=%s should be returned unchanged", u) |
| 116 | + assert.Nil(t, cleanup, "url=%s should not produce a cleanup", u) |
| 117 | + } |
| 118 | + assert.Zero(t, hits.Load()) |
| 119 | +} |
| 120 | + |
| 121 | +func TestFetchFromGitlabIfNecessary_RejectsHTTPForConfiguredHost(t *testing.T) { |
| 122 | + withGitlabConfig(t, "secret-token", "https://gitlab.example.com") |
| 123 | + hits := withFetchClient(t) |
| 124 | + |
| 125 | + in := "http://gitlab.example.com/api/v4/projects/1/repository/files/values.yaml?ref=main" |
| 126 | + _, _, err := fetchFromGitlabIfNecessary(in) |
| 127 | + require.Error(t, err) |
| 128 | + assert.Contains(t, err.Error(), "HTTPS") |
| 129 | + assert.Zero(t, hits.Load()) |
| 130 | +} |
| 131 | + |
| 132 | +func TestFetchFromGitlabIfNecessary_SendsTokenViaPrivateTokenHeader(t *testing.T) { |
| 133 | + var ( |
| 134 | + seenPrivateTokenHeader string |
| 135 | + seenRawQuery string |
| 136 | + ) |
| 137 | + |
| 138 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | + seenPrivateTokenHeader = r.Header.Get("PRIVATE-TOKEN") |
| 140 | + seenRawQuery = r.URL.RawQuery |
| 141 | + body, _ := json.Marshal(gitlabValuesFileResponse{ |
| 142 | + Content: base64.StdEncoding.EncodeToString([]byte("key: value\n")), |
| 143 | + }) |
| 144 | + w.Header().Set("Content-Type", "application/json") |
| 145 | + _, _ = w.Write(body) |
| 146 | + })) |
| 147 | + t.Cleanup(srv.Close) |
| 148 | + |
| 149 | + srvURL, err := url.Parse(srv.URL) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + withGitlabConfig(t, "secret-token", "https://"+srvURL.Host) |
| 153 | + _ = withFetchClient(t) |
| 154 | + |
| 155 | + in := "https://" + srvURL.Host + "/api/v4/projects/1/repository/files/values.yaml?ref=main" |
| 156 | + out, cleanup, err := fetchFromGitlabIfNecessary(in) |
| 157 | + require.NoError(t, err) |
| 158 | + require.NotNil(t, cleanup) |
| 159 | + t.Cleanup(func() { cleanup(out) }) |
| 160 | + |
| 161 | + assert.Equal(t, "secret-token", seenPrivateTokenHeader) |
| 162 | + assert.NotContains(t, seenRawQuery, "private_token") |
| 163 | + assert.NotContains(t, seenRawQuery, "secret-token") |
| 164 | + |
| 165 | + assert.True(t, strings.HasPrefix(out, os.TempDir())) |
| 166 | + content, err := os.ReadFile(out) |
| 167 | + require.NoError(t, err) |
| 168 | + assert.Equal(t, "key: value\n", string(content)) |
| 169 | +} |
| 170 | + |
| 171 | +// TestFetchFromGitlabIfNecessary_DoesNotFollowRedirects ensures the |
| 172 | +// fetch client surfaces a redirect response as an error rather than |
| 173 | +// silently following it. |
| 174 | +func TestFetchFromGitlabIfNecessary_DoesNotFollowRedirects(t *testing.T) { |
| 175 | + var downstreamHit atomic.Bool |
| 176 | + downstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 177 | + downstreamHit.Store(true) |
| 178 | + w.WriteHeader(http.StatusOK) |
| 179 | + })) |
| 180 | + t.Cleanup(downstream.Close) |
| 181 | + |
| 182 | + gitlab := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 183 | + http.Redirect(w, r, downstream.URL+"/v", http.StatusFound) |
| 184 | + })) |
| 185 | + t.Cleanup(gitlab.Close) |
| 186 | + |
| 187 | + gitlabURL, err := url.Parse(gitlab.URL) |
| 188 | + require.NoError(t, err) |
| 189 | + |
| 190 | + withGitlabConfig(t, "secret-token", "https://"+gitlabURL.Host) |
| 191 | + _ = withFetchClient(t) |
| 192 | + |
| 193 | + in := "https://" + gitlabURL.Host + "/api/v4/projects/1/repository/files/values.yaml?ref=main" |
| 194 | + _, _, err = fetchFromGitlabIfNecessary(in) |
| 195 | + require.Error(t, err) |
| 196 | + assert.False(t, downstreamHit.Load()) |
| 197 | +} |
0 commit comments