summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Cai <jcai@gitlab.com>2020-05-04 10:59:49 -0700
committerJohn Cai <jcai@gitlab.com>2020-05-04 14:19:47 -0700
commit91f45342c4ff29a24c61812d539ac745dbb1570a (patch)
treeaec9280d8c4e1c0d02515f1ca9d46a65182cfb14
parentf62a4b2fb89754372a346f24659212eb8da13601 (diff)
downloadgitlab-shell-jc-refactor-gitlabnet-client.tar.gz
Move gitlabnet client to client packagejc-refactor-gitlabnet-client
-rw-r--r--client/client_test.go (renamed from internal/gitlabnet/client_test.go)36
-rw-r--r--client/gitlabnet.go140
-rw-r--r--client/httpclient.go (renamed from internal/config/httpclient.go)51
-rw-r--r--client/httpclient_test.go (renamed from internal/gitlabnet/httpclient_test.go)27
-rw-r--r--client/httpsclient_test.go115
-rw-r--r--client/testserver/gitalyserver.go (renamed from internal/gitlabnet/testserver/gitalyserver.go)3
-rw-r--r--client/testserver/testserver.go (renamed from internal/gitlabnet/testserver/testserver.go)1
-rw-r--r--internal/command/authorizedkeys/authorized_keys_test.go2
-rw-r--r--internal/command/discover/discover_test.go2
-rw-r--r--internal/command/healthcheck/healthcheck_test.go2
-rw-r--r--internal/command/lfsauthenticate/lfsauthenticate_test.go2
-rw-r--r--internal/command/receivepack/gitalycall_test.go2
-rw-r--r--internal/command/receivepack/receivepack_test.go2
-rw-r--r--internal/command/shared/accessverifier/accessverifier_test.go2
-rw-r--r--internal/command/shared/customaction/customaction.go4
-rw-r--r--internal/command/shared/customaction/customaction_test.go2
-rw-r--r--internal/command/twofactorrecover/twofactorrecover_test.go2
-rw-r--r--internal/command/uploadarchive/gitalycall_test.go2
-rw-r--r--internal/command/uploadarchive/uploadarchive_test.go2
-rw-r--r--internal/command/uploadpack/gitalycall_test.go2
-rw-r--r--internal/command/uploadpack/uploadpack_test.go2
-rw-r--r--internal/config/config.go20
-rw-r--r--internal/config/httpclient_test.go22
-rw-r--r--internal/gitlabnet/accessverifier/client.go3
-rw-r--r--internal/gitlabnet/accessverifier/client_test.go7
-rw-r--r--internal/gitlabnet/authorizedkeys/client.go3
-rw-r--r--internal/gitlabnet/authorizedkeys/client_test.go7
-rw-r--r--internal/gitlabnet/client.go125
-rw-r--r--internal/gitlabnet/discover/client.go3
-rw-r--r--internal/gitlabnet/discover/client_test.go8
-rw-r--r--internal/gitlabnet/healthcheck/client.go3
-rw-r--r--internal/gitlabnet/healthcheck/client_test.go2
-rw-r--r--internal/gitlabnet/httpsclient_test.go125
-rw-r--r--internal/gitlabnet/lfsauthenticate/client.go3
-rw-r--r--internal/gitlabnet/lfsauthenticate/client_test.go2
-rw-r--r--internal/gitlabnet/twofactorrecover/client.go3
-rw-r--r--internal/gitlabnet/twofactorrecover/client_test.go7
-rw-r--r--internal/testhelper/requesthandlers/requesthandlers.go2
38 files changed, 379 insertions, 369 deletions
diff --git a/internal/gitlabnet/client_test.go b/client/client_test.go
index d6ca91d..dfb1ca3 100644
--- a/internal/gitlabnet/client_test.go
+++ b/client/client_test.go
@@ -1,4 +1,4 @@
-package gitlabnet
+package client
import (
"encoding/base64"
@@ -11,12 +11,9 @@ import (
"testing"
"github.com/sirupsen/logrus"
-
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
-
- "gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
)
@@ -74,24 +71,20 @@ func TestClients(t *testing.T) {
testCases := []struct {
desc string
- config *config.Config
+ caFile string
server func(*testing.T, []testserver.TestRequestHandler) (string, func())
}{
{
desc: "Socket client",
- config: &config.Config{},
server: testserver.StartSocketHttpServer,
},
{
desc: "Http client",
- config: &config.Config{},
server: testserver.StartHttpServer,
},
{
- desc: "Https client",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{CaFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt")},
- },
+ desc: "Https client",
+ caFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt"),
server: testserver.StartHttpsServer,
},
}
@@ -101,10 +94,11 @@ func TestClients(t *testing.T) {
url, cleanup := tc.server(t, requests)
defer cleanup()
- tc.config.GitlabUrl = url
- tc.config.Secret = "sssh, it's a secret"
+ secret := "sssh, it's a secret"
+
+ httpClient := NewHTTPClient(url, tc.caFile, "", false, 1)
- client, err := GetClient(tc.config)
+ client, err := NewGitlabNetClient("", "", secret, httpClient)
require.NoError(t, err)
testBrokenRequest(t, client)
@@ -117,7 +111,7 @@ func TestClients(t *testing.T) {
}
}
-func testSuccessfulGet(t *testing.T, client *GitlabClient) {
+func testSuccessfulGet(t *testing.T, client *GitlabNetClient) {
t.Run("Successful get", func(t *testing.T) {
hook := testhelper.SetupLogger()
response, err := client.Get("/hello")
@@ -137,7 +131,7 @@ func testSuccessfulGet(t *testing.T, client *GitlabClient) {
})
}
-func testSuccessfulPost(t *testing.T, client *GitlabClient) {
+func testSuccessfulPost(t *testing.T, client *GitlabNetClient) {
t.Run("Successful Post", func(t *testing.T) {
hook := testhelper.SetupLogger()
data := map[string]string{"key": "value"}
@@ -159,7 +153,7 @@ func testSuccessfulPost(t *testing.T, client *GitlabClient) {
})
}
-func testMissing(t *testing.T, client *GitlabClient) {
+func testMissing(t *testing.T, client *GitlabNetClient) {
t.Run("Missing error for GET", func(t *testing.T) {
hook := testhelper.SetupLogger()
response, err := client.Get("/missing")
@@ -185,7 +179,7 @@ func testMissing(t *testing.T, client *GitlabClient) {
})
}
-func testErrorMessage(t *testing.T, client *GitlabClient) {
+func testErrorMessage(t *testing.T, client *GitlabNetClient) {
t.Run("Error with message for GET", func(t *testing.T) {
response, err := client.Get("/error")
assert.EqualError(t, err, "Don't do that")
@@ -199,7 +193,7 @@ func testErrorMessage(t *testing.T, client *GitlabClient) {
})
}
-func testBrokenRequest(t *testing.T, client *GitlabClient) {
+func testBrokenRequest(t *testing.T, client *GitlabNetClient) {
t.Run("Broken request for GET", func(t *testing.T) {
response, err := client.Get("/broken")
assert.EqualError(t, err, "Internal API unreachable")
@@ -213,7 +207,7 @@ func testBrokenRequest(t *testing.T, client *GitlabClient) {
})
}
-func testAuthenticationHeader(t *testing.T, client *GitlabClient) {
+func testAuthenticationHeader(t *testing.T, client *GitlabNetClient) {
t.Run("Authentication headers for GET", func(t *testing.T) {
response, err := client.Get("/auth")
require.NoError(t, err)
diff --git a/client/gitlabnet.go b/client/gitlabnet.go
new file mode 100644
index 0000000..67c48c7
--- /dev/null
+++ b/client/gitlabnet.go
@@ -0,0 +1,140 @@
+package client
+
+import (
+ "bytes"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "time"
+
+ log "github.com/sirupsen/logrus"
+)
+
+const (
+ internalApiPath = "/api/v4/internal"
+ secretHeaderName = "Gitlab-Shared-Secret"
+)
+
+type ErrorResponse struct {
+ Message string `json:"message"`
+}
+
+type GitlabNetClient struct {
+ httpClient *HttpClient
+ user, password, secret string
+}
+
+func NewGitlabNetClient(
+ user,
+ password,
+ secret string,
+ httpClient *HttpClient,
+) (*GitlabNetClient, error) {
+
+ if httpClient == nil {
+ return nil, fmt.Errorf("Unsupported protocol")
+ }
+
+ return &GitlabNetClient{
+ httpClient: httpClient,
+ user: user,
+ password: password,
+ secret: secret,
+ }, nil
+}
+
+func normalizePath(path string) string {
+ if !strings.HasPrefix(path, "/") {
+ path = "/" + path
+ }
+
+ if !strings.HasPrefix(path, internalApiPath) {
+ path = internalApiPath + path
+ }
+ return path
+}
+
+func newRequest(method, host, path string, data interface{}) (*http.Request, error) {
+ var jsonReader io.Reader
+ if data != nil {
+ jsonData, err := json.Marshal(data)
+ if err != nil {
+ return nil, err
+ }
+
+ jsonReader = bytes.NewReader(jsonData)
+ }
+
+ request, err := http.NewRequest(method, host+path, jsonReader)
+ if err != nil {
+ return nil, err
+ }
+
+ return request, nil
+}
+
+func parseError(resp *http.Response) error {
+ if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
+ return nil
+ }
+ defer resp.Body.Close()
+ parsedResponse := &ErrorResponse{}
+
+ if err := json.NewDecoder(resp.Body).Decode(parsedResponse); err != nil {
+ return fmt.Errorf("Internal API error (%v)", resp.StatusCode)
+ } else {
+ return fmt.Errorf(parsedResponse.Message)
+ }
+
+}
+
+func (c *GitlabNetClient) Get(path string) (*http.Response, error) {
+ return c.DoRequest(http.MethodGet, normalizePath(path), nil)
+}
+
+func (c *GitlabNetClient) Post(path string, data interface{}) (*http.Response, error) {
+ return c.DoRequest(http.MethodPost, normalizePath(path), data)
+}
+
+func (c *GitlabNetClient) DoRequest(method, path string, data interface{}) (*http.Response, error) {
+ request, err := newRequest(method, c.httpClient.Host, path, data)
+ if err != nil {
+ return nil, err
+ }
+
+ user, password := c.user, c.password
+ if user != "" && password != "" {
+ request.SetBasicAuth(user, password)
+ }
+
+ encodedSecret := base64.StdEncoding.EncodeToString([]byte(c.secret))
+ request.Header.Set(secretHeaderName, encodedSecret)
+
+ request.Header.Add("Content-Type", "application/json")
+ request.Close = true
+
+ start := time.Now()
+ response, err := c.httpClient.Do(request)
+ fields := log.Fields{
+ "method": method,
+ "url": request.URL.String(),
+ "duration_ms": time.Since(start) / time.Millisecond,
+ }
+
+ if err != nil {
+ log.WithError(err).WithFields(fields).Error("Internal API unreachable")
+ return nil, fmt.Errorf("Internal API unreachable")
+ }
+
+ if err := parseError(response); err != nil {
+ log.WithError(err).WithFields(fields).Error("Internal API error")
+ return nil, err
+ }
+
+ log.WithFields(fields).Info("Finished HTTP request")
+
+ return response, nil
+}
diff --git a/internal/config/httpclient.go b/client/httpclient.go
index c71efad..ff0cc25 100644
--- a/internal/config/httpclient.go
+++ b/client/httpclient.go
@@ -1,4 +1,4 @@
-package config
+package client
import (
"context"
@@ -21,41 +21,36 @@ const (
)
type HttpClient struct {
- HttpClient *http.Client
- Host string
+ *http.Client
+ Host string
}
-func (c *Config) GetHttpClient() *HttpClient {
- if c.HttpClient != nil {
- return c.HttpClient
- }
+func NewHTTPClient(gitlabURL, caFile, caPath string, selfSignedCert bool, readTimeoutSeconds uint64) *HttpClient {
var transport *http.Transport
var host string
- if strings.HasPrefix(c.GitlabUrl, unixSocketProtocol) {
- transport, host = c.buildSocketTransport()
- } else if strings.HasPrefix(c.GitlabUrl, httpProtocol) {
- transport, host = c.buildHttpTransport()
- } else if strings.HasPrefix(c.GitlabUrl, httpsProtocol) {
- transport, host = c.buildHttpsTransport()
+ if strings.HasPrefix(gitlabURL, unixSocketProtocol) {
+ transport, host = buildSocketTransport(gitlabURL)
+ } else if strings.HasPrefix(gitlabURL, httpProtocol) {
+ transport, host = buildHttpTransport(gitlabURL)
+ } else if strings.HasPrefix(gitlabURL, httpsProtocol) {
+ transport, host = buildHttpsTransport(caFile, caPath, selfSignedCert, gitlabURL)
} else {
return nil
}
- httpClient := &http.Client{
+ c := &http.Client{
Transport: transport,
- Timeout: c.readTimeout(),
+ Timeout: readTimeout(readTimeoutSeconds),
}
- client := &HttpClient{HttpClient: httpClient, Host: host}
-
- c.HttpClient = client
+ client := &HttpClient{Client: c, Host: host}
return client
}
-func (c *Config) buildSocketTransport() (*http.Transport, string) {
- socketPath := strings.TrimPrefix(c.GitlabUrl, unixSocketProtocol)
+func buildSocketTransport(gitlabURL string) (*http.Transport, string) {
+ socketPath := strings.TrimPrefix(gitlabURL, unixSocketProtocol)
transport := &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
dialer := net.Dialer{}
@@ -66,19 +61,17 @@ func (c *Config) buildSocketTransport() (*http.Transport, string) {
return transport, socketBaseUrl
}
-func (c *Config) buildHttpsTransport() (*http.Transport, string) {
+func buildHttpsTransport(caFile, caPath string, selfSignedCert bool, gitlabURL string) (*http.Transport, string) {
certPool, err := x509.SystemCertPool()
if err != nil {
certPool = x509.NewCertPool()
}
- caFile := c.HttpSettings.CaFile
if caFile != "" {
addCertToPool(certPool, caFile)
}
- caPath := c.HttpSettings.CaPath
if caPath != "" {
fis, _ := ioutil.ReadDir(caPath)
for _, fi := range fis {
@@ -93,11 +86,11 @@ func (c *Config) buildHttpsTransport() (*http.Transport, string) {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
- InsecureSkipVerify: c.HttpSettings.SelfSignedCert,
+ InsecureSkipVerify: selfSignedCert,
},
}
- return transport, c.GitlabUrl
+ return transport, gitlabURL
}
func addCertToPool(certPool *x509.CertPool, fileName string) {
@@ -107,13 +100,11 @@ func addCertToPool(certPool *x509.CertPool, fileName string) {
}
}
-func (c *Config) buildHttpTransport() (*http.Transport, string) {
- return &http.Transport{}, c.GitlabUrl
+func buildHttpTransport(gitlabURL string) (*http.Transport, string) {
+ return &http.Transport{}, gitlabURL
}
-func (c *Config) readTimeout() time.Duration {
- timeoutSeconds := c.HttpSettings.ReadTimeoutSeconds
-
+func readTimeout(timeoutSeconds uint64) time.Duration {
if timeoutSeconds == 0 {
timeoutSeconds = defaultReadTimeoutSeconds
}
diff --git a/internal/gitlabnet/httpclient_test.go b/client/httpclient_test.go
index a40ab6d..1f0a4ed 100644
--- a/internal/gitlabnet/httpclient_test.go
+++ b/client/httpclient_test.go
@@ -1,4 +1,4 @@
-package gitlabnet
+package client
import (
"encoding/base64"
@@ -7,13 +7,22 @@ import (
"net/http"
"strings"
"testing"
+ "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
- "gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
)
+func TestReadTimeout(t *testing.T) {
+ expectedSeconds := uint64(300)
+
+ client := NewHTTPClient("http://localhost:3000", "", "", false, expectedSeconds)
+
+ require.NotNil(t, client)
+ assert.Equal(t, time.Duration(expectedSeconds)*time.Second, client.Client.Timeout)
+}
+
const (
username = "basic_auth_user"
password = "basic_auth_password"
@@ -38,9 +47,8 @@ func TestBasicAuthSettings(t *testing.T) {
},
},
}
- config := &config.Config{HttpSettings: config.HttpSettingsConfig{User: username, Password: password}}
- client, cleanup := setup(t, config, requests)
+ client, cleanup := setup(t, username, password, requests)
defer cleanup()
response, err := client.Get("/get_endpoint")
@@ -78,18 +86,19 @@ func TestEmptyBasicAuthSettings(t *testing.T) {
},
}
- client, cleanup := setup(t, &config.Config{}, requests)
+ client, cleanup := setup(t, "", "", requests)
defer cleanup()
_, err := client.Get("/empty_basic_auth")
require.NoError(t, err)
}
-func setup(t *testing.T, config *config.Config, requests []testserver.TestRequestHandler) (*GitlabClient, func()) {
+func setup(t *testing.T, username, password string, requests []testserver.TestRequestHandler) (*GitlabNetClient, func()) {
url, cleanup := testserver.StartHttpServer(t, requests)
- config.GitlabUrl = url
- client, err := GetClient(config)
+ httpClient := NewHTTPClient(url, "", "", false, 1)
+
+ client, err := NewGitlabNetClient(username, password, "", httpClient)
require.NoError(t, err)
return client, cleanup
diff --git a/client/httpsclient_test.go b/client/httpsclient_test.go
new file mode 100644
index 0000000..6c3ae08
--- /dev/null
+++ b/client/httpsclient_test.go
@@ -0,0 +1,115 @@
+package client
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "path"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
+)
+
+func TestSuccessfulRequests(t *testing.T) {
+ testCases := []struct {
+ desc string
+ caFile, caPath string
+ selfSigned bool
+ }{
+ {
+ desc: "Valid CaFile",
+ caFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt"),
+ },
+ {
+ desc: "Valid CaPath",
+ caPath: path.Join(testhelper.TestRoot, "certs/valid"),
+ },
+ {
+ desc: "Self signed cert option enabled",
+ selfSigned: true,
+ },
+ {
+ desc: "Invalid cert with self signed cert option enabled",
+ caFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt"),
+ selfSigned: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ client, cleanup := setupWithRequests(t, tc.caFile, tc.caPath, tc.selfSigned)
+ defer cleanup()
+
+ response, err := client.Get("/hello")
+ require.NoError(t, err)
+ require.NotNil(t, response)
+
+ defer response.Body.Close()
+
+ responseBody, err := ioutil.ReadAll(response.Body)
+ assert.NoError(t, err)
+ assert.Equal(t, string(responseBody), "Hello")
+ })
+ }
+}
+
+func TestFailedRequests(t *testing.T) {
+ testCases := []struct {
+ desc string
+ caFile string
+ caPath string
+ }{
+ {
+ desc: "Invalid CaFile",
+ caFile: path.Join(testhelper.TestRoot, "certs/invalid/server.crt"),
+ },
+ {
+ desc: "Invalid CaPath",
+ caPath: path.Join(testhelper.TestRoot, "certs/invalid"),
+ },
+ {
+ desc: "Empty config",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ client, cleanup := setupWithRequests(t, tc.caFile, tc.caPath, false)
+ defer cleanup()
+
+ _, err := client.Get("/hello")
+ require.Error(t, err)
+
+ assert.Equal(t, err.Error(), "Internal API unreachable")
+ })
+ }
+}
+
+func setupWithRequests(t *testing.T, caFile, caPath string, selfSigned bool) (*GitlabNetClient, func()) {
+ testDirCleanup, err := testhelper.PrepareTestRootDir()
+ require.NoError(t, err)
+ defer testDirCleanup()
+
+ requests := []testserver.TestRequestHandler{
+ {
+ Path: "/api/v4/internal/hello",
+ Handler: func(w http.ResponseWriter, r *http.Request) {
+ require.Equal(t, http.MethodGet, r.Method)
+
+ fmt.Fprint(w, "Hello")
+ },
+ },
+ }
+
+ url, cleanup := testserver.StartHttpsServer(t, requests)
+
+ httpClient := NewHTTPClient(url, caFile, caPath, selfSigned, 1)
+
+ client, err := NewGitlabNetClient("", "", "", httpClient)
+ require.NoError(t, err)
+
+ return client, cleanup
+}
diff --git a/internal/gitlabnet/testserver/gitalyserver.go b/client/testserver/gitalyserver.go
index 6d0c130..4bf14f3 100644
--- a/internal/gitlabnet/testserver/gitalyserver.go
+++ b/client/testserver/gitalyserver.go
@@ -9,10 +9,9 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
-
- pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
type TestGitalyServer struct{ ReceivedMD metadata.MD }
diff --git a/internal/gitlabnet/testserver/testserver.go b/client/testserver/testserver.go
index f3b7b71..377e331 100644
--- a/internal/gitlabnet/testserver/testserver.go
+++ b/client/testserver/testserver.go
@@ -13,7 +13,6 @@ import (
"testing"
"github.com/stretchr/testify/require"
-
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
)
diff --git a/internal/command/authorizedkeys/authorized_keys_test.go b/internal/command/authorizedkeys/authorized_keys_test.go
index 3bf4153..4aa7586 100644
--- a/internal/command/authorizedkeys/authorized_keys_test.go
+++ b/internal/command/authorizedkeys/authorized_keys_test.go
@@ -8,10 +8,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
diff --git a/internal/command/discover/discover_test.go b/internal/command/discover/discover_test.go
index 3878286..8edbcb9 100644
--- a/internal/command/discover/discover_test.go
+++ b/internal/command/discover/discover_test.go
@@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
diff --git a/internal/command/healthcheck/healthcheck_test.go b/internal/command/healthcheck/healthcheck_test.go
index e59c5a2..7479bcb 100644
--- a/internal/command/healthcheck/healthcheck_test.go
+++ b/internal/command/healthcheck/healthcheck_test.go
@@ -8,10 +8,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/healthcheck"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
diff --git a/internal/command/lfsauthenticate/lfsauthenticate_test.go b/internal/command/lfsauthenticate/lfsauthenticate_test.go
index c2f0fd3..a1c7aec 100644
--- a/internal/command/lfsauthenticate/lfsauthenticate_test.go
+++ b/internal/command/lfsauthenticate/lfsauthenticate_test.go
@@ -9,12 +9,12 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/lfsauthenticate"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/receivepack/gitalycall_test.go b/internal/command/receivepack/gitalycall_test.go
index ece9f1e..dd75176 100644
--- a/internal/command/receivepack/gitalycall_test.go
+++ b/internal/command/receivepack/gitalycall_test.go
@@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/receivepack/receivepack_test.go b/internal/command/receivepack/receivepack_test.go
index d464e35..a4632b4 100644
--- a/internal/command/receivepack/receivepack_test.go
+++ b/internal/command/receivepack/receivepack_test.go
@@ -6,10 +6,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/shared/accessverifier/accessverifier_test.go b/internal/command/shared/accessverifier/accessverifier_test.go
index cfcf4a8..998e622 100644
--- a/internal/command/shared/accessverifier/accessverifier_test.go
+++ b/internal/command/shared/accessverifier/accessverifier_test.go
@@ -9,11 +9,11 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
diff --git a/internal/command/shared/customaction/customaction.go b/internal/command/shared/customaction/customaction.go
index 801ad63..2ba1091 100644
--- a/internal/command/shared/customaction/customaction.go
+++ b/internal/command/shared/customaction/customaction.go
@@ -4,6 +4,8 @@ import (
"bytes"
"errors"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
+
"io"
"net/http"
@@ -93,7 +95,7 @@ func (c *Command) processApiEndpoints(response *accessverifier.Response) error {
return nil
}
-func (c *Command) performRequest(client *gitlabnet.GitlabClient, endpoint string, request *Request) (*Response, error) {
+func (c *Command) performRequest(client *client.GitlabNetClient, endpoint string, request *Request) (*Response, error) {
response, err := client.DoRequest(http.MethodPost, endpoint, request)
if err != nil {
return nil, err
diff --git a/internal/command/shared/customaction/customaction_test.go b/internal/command/shared/customaction/customaction_test.go
index 31044f9..46c5f32 100644
--- a/internal/command/shared/customaction/customaction_test.go
+++ b/internal/command/shared/customaction/customaction_test.go
@@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
func TestExecuteEOFSent(t *testing.T) {
diff --git a/internal/command/twofactorrecover/twofactorrecover_test.go b/internal/command/twofactorrecover/twofactorrecover_test.go
index 283c45a..d2f931b 100644
--- a/internal/command/twofactorrecover/twofactorrecover_test.go
+++ b/internal/command/twofactorrecover/twofactorrecover_test.go
@@ -10,10 +10,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/twofactorrecover"
)
diff --git a/internal/command/uploadarchive/gitalycall_test.go b/internal/command/uploadarchive/gitalycall_test.go
index 6e67571..95274ee 100644
--- a/internal/command/uploadarchive/gitalycall_test.go
+++ b/internal/command/uploadarchive/gitalycall_test.go
@@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/uploadarchive/uploadarchive_test.go b/internal/command/uploadarchive/uploadarchive_test.go
index 50f3f7e..7b03009 100644
--- a/internal/command/uploadarchive/uploadarchive_test.go
+++ b/internal/command/uploadarchive/uploadarchive_test.go
@@ -6,10 +6,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/uploadpack/gitalycall_test.go b/internal/command/uploadpack/gitalycall_test.go
index e5fe8b2..4421d41 100644
--- a/internal/command/uploadpack/gitalycall_test.go
+++ b/internal/command/uploadpack/gitalycall_test.go
@@ -10,10 +10,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/command/uploadpack/uploadpack_test.go b/internal/command/uploadpack/uploadpack_test.go
index 04fe2ba..7ea8e5d 100644
--- a/internal/command/uploadpack/uploadpack_test.go
+++ b/internal/command/uploadpack/uploadpack_test.go
@@ -6,10 +6,10 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers"
)
diff --git a/internal/config/config.go b/internal/config/config.go
index 2231851..deed74d 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
yaml "gopkg.in/yaml.v2"
)
@@ -34,7 +35,24 @@ type Config struct {
SecretFilePath string `yaml:"secret_file"`
Secret string `yaml:"secret"`
HttpSettings HttpSettingsConfig `yaml:"http_settings"`
- HttpClient *HttpClient
+ HttpClient *client.HttpClient
+}
+
+func (c *Config) GetHttpClient() *client.HttpClient {
+ if c.HttpClient != nil {
+ return c.HttpClient
+ }
+
+ client := client.NewHTTPClient(
+ c.GitlabUrl,
+ c.HttpSettings.CaFile,
+ c.HttpSettings.CaPath,
+ c.HttpSettings.SelfSignedCert,
+ c.HttpSettings.ReadTimeoutSeconds)
+
+ c.HttpClient = client
+
+ return client
}
func New() (*Config, error) {
diff --git a/internal/config/httpclient_test.go b/internal/config/httpclient_test.go
deleted file mode 100644
index 474deba..0000000
--- a/internal/config/httpclient_test.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package config
-
-import (
- "testing"
- "time"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-)
-
-func TestReadTimeout(t *testing.T) {
- expectedSeconds := uint64(300)
-
- config := &Config{
- GitlabUrl: "http://localhost:3000",
- HttpSettings: HttpSettingsConfig{ReadTimeoutSeconds: expectedSeconds},
- }
- client := config.GetHttpClient()
-
- require.NotNil(t, client)
- assert.Equal(t, time.Duration(expectedSeconds)*time.Second, client.HttpClient.Timeout)
-}
diff --git a/internal/gitlabnet/accessverifier/client.go b/internal/gitlabnet/accessverifier/client.go
index 302a9e1..a9c7d97 100644
--- a/internal/gitlabnet/accessverifier/client.go
+++ b/internal/gitlabnet/accessverifier/client.go
@@ -5,6 +5,7 @@ import (
"net/http"
pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
@@ -17,7 +18,7 @@ const (
)
type Client struct {
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
}
type Request struct {
diff --git a/internal/gitlabnet/accessverifier/client_test.go b/internal/gitlabnet/accessverifier/client_test.go
index 009dcc0..7ddbb5e 100644
--- a/internal/gitlabnet/accessverifier/client_test.go
+++ b/internal/gitlabnet/accessverifier/client_test.go
@@ -8,12 +8,11 @@ import (
"testing"
"github.com/stretchr/testify/require"
-
pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
)
@@ -214,7 +213,7 @@ func setup(t *testing.T, allowedPayload string) (*Client, func()) {
require.NoError(t, err)
case "2":
w.WriteHeader(http.StatusForbidden)
- errBody := &gitlabnet.ErrorResponse{
+ errBody := &client.ErrorResponse{
Message: "Not allowed!",
}
require.NoError(t, json.NewEncoder(w).Encode(errBody))
diff --git a/internal/gitlabnet/authorizedkeys/client.go b/internal/gitlabnet/authorizedkeys/client.go
index ac23a96..e4fec28 100644
--- a/internal/gitlabnet/authorizedkeys/client.go
+++ b/internal/gitlabnet/authorizedkeys/client.go
@@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
)
@@ -14,7 +15,7 @@ const (
type Client struct {
config *config.Config
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
}
type Response struct {
diff --git a/internal/gitlabnet/authorizedkeys/client_test.go b/internal/gitlabnet/authorizedkeys/client_test.go
index 965025f..c9c76a1 100644
--- a/internal/gitlabnet/authorizedkeys/client_test.go
+++ b/internal/gitlabnet/authorizedkeys/client_test.go
@@ -6,10 +6,9 @@ import (
"testing"
"github.com/stretchr/testify/require"
-
+ "gitlab.com/gitlab-org/gitlab-shell/client"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
@@ -29,7 +28,7 @@ func init() {
json.NewEncoder(w).Encode(body)
} else if r.URL.Query().Get("key") == "broken-message" {
w.WriteHeader(http.StatusForbidden)
- body := &gitlabnet.ErrorResponse{
+ body := &client.ErrorResponse{
Message: "Not allowed!",
}
json.NewEncoder(w).Encode(body)
diff --git a/internal/gitlabnet/client.go b/internal/gitlabnet/client.go
index 78ceb44..923b064 100644
--- a/internal/gitlabnet/client.go
+++ b/internal/gitlabnet/client.go
@@ -1,140 +1,27 @@
package gitlabnet
import (
- "bytes"
- "encoding/base64"
"encoding/json"
"fmt"
- "io"
"net/http"
- "strings"
- "time"
- log "github.com/sirupsen/logrus"
- "gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/logger"
-)
+ "gitlab.com/gitlab-org/gitlab-shell/client"
-const (
- internalApiPath = "/api/v4/internal"
- secretHeaderName = "Gitlab-Shared-Secret"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
)
var (
ParsingError = fmt.Errorf("Parsing failed")
)
-type ErrorResponse struct {
- Message string `json:"message"`
-}
-
-type GitlabClient struct {
- httpClient *http.Client
- config *config.Config
- host string
-}
+func GetClient(config *config.Config) (*client.GitlabNetClient, error) {
+ httpClient := config.GetHttpClient()
-func GetClient(config *config.Config) (*GitlabClient, error) {
- client := config.GetHttpClient()
-
- if client == nil {
+ if httpClient == nil {
return nil, fmt.Errorf("Unsupported protocol")
}
- return &GitlabClient{httpClient: client.HttpClient, config: config, host: client.Host}, nil
-}
-
-func normalizePath(path string) string {
- if !strings.HasPrefix(path, "/") {
- path = "/" + path
- }
-
- if !strings.HasPrefix(path, internalApiPath) {
- path = internalApiPath + path
- }
- return path
-}
-
-func newRequest(method, host, path string, data interface{}) (*http.Request, error) {
- var jsonReader io.Reader
- if data != nil {
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
-
- jsonReader = bytes.NewReader(jsonData)
- }
-
- request, err := http.NewRequest(method, host+path, jsonReader)
- if err != nil {
- return nil, err
- }
-
- return request, nil
-}
-
-func parseError(resp *http.Response) error {
- if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
- return nil
- }
- defer resp.Body.Close()
- parsedResponse := &ErrorResponse{}
-
- if err := json.NewDecoder(resp.Body).Decode(parsedResponse); err != nil {
- return fmt.Errorf("Internal API error (%v)", resp.StatusCode)
- } else {
- return fmt.Errorf(parsedResponse.Message)
- }
-
-}
-
-func (c *GitlabClient) Get(path string) (*http.Response, error) {
- return c.DoRequest(http.MethodGet, normalizePath(path), nil)
-}
-
-func (c *GitlabClient) Post(path string, data interface{}) (*http.Response, error) {
- return c.DoRequest(http.MethodPost, normalizePath(path), data)
-}
-
-func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.Response, error) {
- request, err := newRequest(method, c.host, path, data)
- if err != nil {
- return nil, err
- }
-
- user, password := c.config.HttpSettings.User, c.config.HttpSettings.Password
- if user != "" && password != "" {
- request.SetBasicAuth(user, password)
- }
-
- encodedSecret := base64.StdEncoding.EncodeToString([]byte(c.config.Secret))
- request.Header.Set(secretHeaderName, encodedSecret)
-
- request.Header.Add("Content-Type", "application/json")
- request.Close = true
-
- start := time.Now()
- response, err := c.httpClient.Do(request)
- fields := log.Fields{
- "method": method,
- "url": request.URL.String(),
- "duration_ms": logger.ElapsedTimeMs(start, time.Now()),
- }
-
- if err != nil {
- log.WithError(err).WithFields(fields).Error("Internal API unreachable")
- return nil, fmt.Errorf("Internal API unreachable")
- }
-
- if err := parseError(response); err != nil {
- log.WithError(err).WithFields(fields).Error("Internal API error")
- return nil, err
- }
-
- log.WithFields(fields).Info("Finished HTTP request")
-
- return response, nil
+ return client.NewGitlabNetClient(config.HttpSettings.User, config.HttpSettings.Password, config.SecretFilePath, httpClient)
}
func ParseJSON(hr *http.Response, response interface{}) error {
diff --git a/internal/gitlabnet/discover/client.go b/internal/gitlabnet/discover/client.go
index 3faef53..d1e1906 100644
--- a/internal/gitlabnet/discover/client.go
+++ b/internal/gitlabnet/discover/client.go
@@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
@@ -12,7 +13,7 @@ import (
type Client struct {
config *config.Config
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
}
type Response struct {
diff --git a/internal/gitlabnet/discover/client_test.go b/internal/gitlabnet/discover/client_test.go
index 66e234b..96b3162 100644
--- a/internal/gitlabnet/discover/client_test.go
+++ b/internal/gitlabnet/discover/client_test.go
@@ -7,12 +7,12 @@ import (
"net/url"
"testing"
- "gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
)
var (
@@ -40,7 +40,7 @@ func init() {
json.NewEncoder(w).Encode(body)
} else if r.URL.Query().Get("username") == "broken_message" {
w.WriteHeader(http.StatusForbidden)
- body := &gitlabnet.ErrorResponse{
+ body := &client.ErrorResponse{
Message: "Not allowed!",
}
json.NewEncoder(w).Encode(body)
diff --git a/internal/gitlabnet/healthcheck/client.go b/internal/gitlabnet/healthcheck/client.go
index 7db682a..09b45af 100644
--- a/internal/gitlabnet/healthcheck/client.go
+++ b/internal/gitlabnet/healthcheck/client.go
@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
)
@@ -14,7 +15,7 @@ const (
type Client struct {
config *config.Config
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
}
type Response struct {
diff --git a/internal/gitlabnet/healthcheck/client_test.go b/internal/gitlabnet/healthcheck/client_test.go
index d7212b0..c66ddbd 100644
--- a/internal/gitlabnet/healthcheck/client_test.go
+++ b/internal/gitlabnet/healthcheck/client_test.go
@@ -5,8 +5,8 @@ import (
"net/http"
"testing"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
"github.com/stretchr/testify/require"
)
diff --git a/internal/gitlabnet/httpsclient_test.go b/internal/gitlabnet/httpsclient_test.go
deleted file mode 100644
index 0acd425..0000000
--- a/internal/gitlabnet/httpsclient_test.go
+++ /dev/null
@@ -1,125 +0,0 @@
-package gitlabnet
-
-import (
- "fmt"
- "io/ioutil"
- "net/http"
- "path"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
- "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
-)
-
-func TestSuccessfulRequests(t *testing.T) {
- testCases := []struct {
- desc string
- config *config.Config
- }{
- {
- desc: "Valid CaFile",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{CaFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt")},
- },
- },
- {
- desc: "Valid CaPath",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{CaPath: path.Join(testhelper.TestRoot, "certs/valid")},
- },
- },
- {
- desc: "Self signed cert option enabled",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{SelfSignedCert: true},
- },
- },
- {
- desc: "Invalid cert with self signed cert option enabled",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{SelfSignedCert: true, CaFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt")},
- },
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.desc, func(t *testing.T) {
- client, cleanup := setupWithRequests(t, tc.config)
- defer cleanup()
-
- response, err := client.Get("/hello")
- require.NoError(t, err)
- require.NotNil(t, response)
-
- defer response.Body.Close()
-
- responseBody, err := ioutil.ReadAll(response.Body)
- assert.NoError(t, err)
- assert.Equal(t, string(responseBody), "Hello")
- })
- }
-}
-
-func TestFailedRequests(t *testing.T) {
- testCases := []struct {
- desc string
- config *config.Config
- }{
- {
- desc: "Invalid CaFile",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{CaFile: path.Join(testhelper.TestRoot, "certs/invalid/server.crt")},
- },
- },
- {
- desc: "Invalid CaPath",
- config: &config.Config{
- HttpSettings: config.HttpSettingsConfig{CaPath: path.Join(testhelper.TestRoot, "certs/invalid")},
- },
- },
- {
- desc: "Empty config",
- config: &config.Config{},
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.desc, func(t *testing.T) {
- client, cleanup := setupWithRequests(t, tc.config)
- defer cleanup()
-
- _, err := client.Get("/hello")
- require.Error(t, err)
-
- assert.Equal(t, err.Error(), "Internal API unreachable")
- })
- }
-}
-
-func setupWithRequests(t *testing.T, config *config.Config) (*GitlabClient, func()) {
- testDirCleanup, err := testhelper.PrepareTestRootDir()
- require.NoError(t, err)
- defer testDirCleanup()
-
- requests := []testserver.TestRequestHandler{
- {
- Path: "/api/v4/internal/hello",
- Handler: func(w http.ResponseWriter, r *http.Request) {
- require.Equal(t, http.MethodGet, r.Method)
-
- fmt.Fprint(w, "Hello")
- },
- },
- }
-
- url, cleanup := testserver.StartHttpsServer(t, requests)
-
- config.GitlabUrl = url
- client, err := GetClient(config)
- require.NoError(t, err)
-
- return client, cleanup
-}
diff --git a/internal/gitlabnet/lfsauthenticate/client.go b/internal/gitlabnet/lfsauthenticate/client.go
index d797321..fffc225 100644
--- a/internal/gitlabnet/lfsauthenticate/client.go
+++ b/internal/gitlabnet/lfsauthenticate/client.go
@@ -5,6 +5,7 @@ import (
"net/http"
"strings"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
@@ -12,7 +13,7 @@ import (
type Client struct {
config *config.Config
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
args *commandargs.Shell
}
diff --git a/internal/gitlabnet/lfsauthenticate/client_test.go b/internal/gitlabnet/lfsauthenticate/client_test.go
index 0a06960..82e364b 100644
--- a/internal/gitlabnet/lfsauthenticate/client_test.go
+++ b/internal/gitlabnet/lfsauthenticate/client_test.go
@@ -8,9 +8,9 @@ import (
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
const (
diff --git a/internal/gitlabnet/twofactorrecover/client.go b/internal/gitlabnet/twofactorrecover/client.go
index a3052f8..d22daca 100644
--- a/internal/gitlabnet/twofactorrecover/client.go
+++ b/internal/gitlabnet/twofactorrecover/client.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
+ "gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
@@ -13,7 +14,7 @@ import (
type Client struct {
config *config.Config
- client *gitlabnet.GitlabClient
+ client *client.GitlabNetClient
}
type Response struct {
diff --git a/internal/gitlabnet/twofactorrecover/client_test.go b/internal/gitlabnet/twofactorrecover/client_test.go
index d5073e3..372afec 100644
--- a/internal/gitlabnet/twofactorrecover/client_test.go
+++ b/internal/gitlabnet/twofactorrecover/client_test.go
@@ -8,12 +8,11 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
-
+ "gitlab.com/gitlab-org/gitlab-shell/client"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/discover"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
)
var (
@@ -48,7 +47,7 @@ func initialize(t *testing.T) {
json.NewEncoder(w).Encode(body)
case "2":
w.WriteHeader(http.StatusForbidden)
- body := &gitlabnet.ErrorResponse{
+ body := &client.ErrorResponse{
Message: "Not allowed!",
}
json.NewEncoder(w).Encode(body)
diff --git a/internal/testhelper/requesthandlers/requesthandlers.go b/internal/testhelper/requesthandlers/requesthandlers.go
index 75827fa..73acc26 100644
--- a/internal/testhelper/requesthandlers/requesthandlers.go
+++ b/internal/testhelper/requesthandlers/requesthandlers.go
@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
- "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
)
func BuildDisallowedByApiHandlers(t *testing.T) []testserver.TestRequestHandler {