diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/gitaly/hook/access.go | 14 | ||||
-rw-r--r-- | internal/gitaly/hook/check.go | 53 | ||||
-rw-r--r-- | internal/testhelper/testserver.go | 12 |
3 files changed, 71 insertions, 8 deletions
diff --git a/internal/gitaly/hook/access.go b/internal/gitaly/hook/access.go index f3bf5f1ad..c47e68261 100644 --- a/internal/gitaly/hook/access.go +++ b/internal/gitaly/hook/access.go @@ -56,6 +56,8 @@ func marshallGitObjectDirs(gitObjectDirRel string, gitAltObjectDirsRel []string) type GitlabAPI interface { // Allowed queries the gitlab internal api /allowed endpoint to determine if a ref change for a given repository and user is allowed Allowed(ctx context.Context, repo *gitalypb.Repository, glRepository, glID, glProtocol, changes string) (bool, string, error) + // Check verifies that GitLab can be reached, and authenticated to + Check(ctx context.Context) (*CheckInfo, error) // PreReceive queries the gitlab internal api /pre_receive to increase the reference counter PreReceive(ctx context.Context, glRepository string) (bool, error) // PostReceive queries the gitlab internal api /post_receive to decrease the reference counter @@ -279,13 +281,21 @@ func (a *AllowedRequest) parseAndSetGLID(glID string) error { } // mockAPI is a noop gitlab API client -type mockAPI struct { -} +type mockAPI struct{} func (m *mockAPI) Allowed(ctx context.Context, repo *gitalypb.Repository, glRepository, glID, glProtocol, changes string) (bool, string, error) { return true, "", nil } +func (m *mockAPI) Check(ctx context.Context) (*CheckInfo, error) { + return &CheckInfo{ + Version: "v13.5.0", + Revision: "deadbeef", + APIVersion: "v4", + RedisReachable: true, + }, nil +} + func (m *mockAPI) PreReceive(ctx context.Context, glRepository string) (bool, error) { return true, nil } diff --git a/internal/gitaly/hook/check.go b/internal/gitaly/hook/check.go new file mode 100644 index 000000000..97b963be4 --- /dev/null +++ b/internal/gitaly/hook/check.go @@ -0,0 +1,53 @@ +package hook + +import ( + "context" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" +) + +// CheckInfo represents the response of GitLabs `check` API endpoint +type CheckInfo struct { + // GitLab Server version + Version string `json:"gitlab_version"` + // Revision of the Git object of the running GitLab + Revision string `json:"gitlab_revision"` + // The version of the API, expected to be v4 + APIVersion string `json:"api_version"` + // GitLab needs a working Redis, even if the check result is successful + // GitLab might still not be able to handle hook API calls without it + RedisReachable bool `json:"redis"` +} + +// Check performs an HTTP request to the internal/check API endpoint to verify +// the connection and tokens. It returns basic information of the installed +// GitLab +func (a *gitlabAPI) Check(ctx context.Context) (*CheckInfo, error) { + resp, err := a.client.Get(ctx, "/check") + if err != nil { + return nil, fmt.Errorf("HTTP GET to GitLab endpoint /check failed: %w", err) + } + + defer func() { + io.Copy(ioutil.Discard, resp.Body) + resp.Body.Close() + }() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("Check HTTP request failed with status: %d", resp.StatusCode) + } + + var info CheckInfo + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { + return nil, fmt.Errorf("failed to decode response from /check endpoint: %w", err) + } + + return &info, nil +} + +func (m *GitLabHookManager) Check(ctx context.Context) (*CheckInfo, error) { + return m.gitlabAPI.Check(ctx) +} diff --git a/internal/testhelper/testserver.go b/internal/testhelper/testserver.go index ce2e85f82..741b69870 100644 --- a/internal/testhelper/testserver.go +++ b/internal/testhelper/testserver.go @@ -874,16 +874,16 @@ func WriteTemporaryGitlabShellConfigFile(t FatalLogger, dir string, config Gitla // WriteTemporaryGitalyConfigFile writes a gitaly toml file into a temporary directory. It returns the path to // the file as well as a cleanup function -func WriteTemporaryGitalyConfigFile(t testing.TB, tempDir, gitlabURL, user, password string) (string, func()) { +func WriteTemporaryGitalyConfigFile(t testing.TB, tempDir, gitlabURL, user, password, secretFile string) (string, func()) { path := filepath.Join(tempDir, "config.toml") contents := fmt.Sprintf(` -[gitlab-shell] - dir = "%s/gitlab-shell" - gitlab_url = %q - [gitlab-shell.http-settings] +[gitlab] + url = "%s" + secret_file = "%s" + [gitlab.http-settings] user = %q password = %q -`, tempDir, gitlabURL, user, password) +`, gitlabURL, secretFile, user, password) require.NoError(t, ioutil.WriteFile(path, []byte(contents), 0644)) return path, func() { |