summaryrefslogtreecommitdiff
path: root/internal/config
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go20
-rw-r--r--internal/config/httpclient.go122
-rw-r--r--internal/config/httpclient_test.go22
3 files changed, 19 insertions, 145 deletions
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.go b/internal/config/httpclient.go
deleted file mode 100644
index c71efad..0000000
--- a/internal/config/httpclient.go
+++ /dev/null
@@ -1,122 +0,0 @@
-package config
-
-import (
- "context"
- "crypto/tls"
- "crypto/x509"
- "io/ioutil"
- "net"
- "net/http"
- "path/filepath"
- "strings"
- "time"
-)
-
-const (
- socketBaseUrl = "http://unix"
- unixSocketProtocol = "http+unix://"
- httpProtocol = "http://"
- httpsProtocol = "https://"
- defaultReadTimeoutSeconds = 300
-)
-
-type HttpClient struct {
- HttpClient *http.Client
- Host string
-}
-
-func (c *Config) GetHttpClient() *HttpClient {
- if c.HttpClient != nil {
- return c.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()
- } else {
- return nil
- }
-
- httpClient := &http.Client{
- Transport: transport,
- Timeout: c.readTimeout(),
- }
-
- client := &HttpClient{HttpClient: httpClient, Host: host}
-
- c.HttpClient = client
-
- return client
-}
-
-func (c *Config) buildSocketTransport() (*http.Transport, string) {
- socketPath := strings.TrimPrefix(c.GitlabUrl, unixSocketProtocol)
- transport := &http.Transport{
- DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
- dialer := net.Dialer{}
- return dialer.DialContext(ctx, "unix", socketPath)
- },
- }
-
- return transport, socketBaseUrl
-}
-
-func (c *Config) buildHttpsTransport() (*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 {
- if fi.IsDir() {
- continue
- }
-
- addCertToPool(certPool, filepath.Join(caPath, fi.Name()))
- }
- }
-
- transport := &http.Transport{
- TLSClientConfig: &tls.Config{
- RootCAs: certPool,
- InsecureSkipVerify: c.HttpSettings.SelfSignedCert,
- },
- }
-
- return transport, c.GitlabUrl
-}
-
-func addCertToPool(certPool *x509.CertPool, fileName string) {
- cert, err := ioutil.ReadFile(fileName)
- if err == nil {
- certPool.AppendCertsFromPEM(cert)
- }
-}
-
-func (c *Config) buildHttpTransport() (*http.Transport, string) {
- return &http.Transport{}, c.GitlabUrl
-}
-
-func (c *Config) readTimeout() time.Duration {
- timeoutSeconds := c.HttpSettings.ReadTimeoutSeconds
-
- if timeoutSeconds == 0 {
- timeoutSeconds = defaultReadTimeoutSeconds
- }
-
- return time.Duration(timeoutSeconds) * time.Second
-}
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)
-}