diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-01-16 07:08:25 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-01-16 07:08:25 +0300 |
commit | 9e8252199cfe9968c23008b8154b394f03f025e1 (patch) | |
tree | bd6e5bdedd5d96df05841ce1e93ee7de29f25b67 | |
parent | 23745fb62dda042b989989637a3035a4b76d1d99 (diff) |
Add http client connection timeout and JWT token expiry as config parameters
-rw-r--r-- | internal/source/gitlab/client/client.go | 25 | ||||
-rw-r--r-- | internal/source/gitlab/client/config.go | 4 |
2 files changed, 18 insertions, 11 deletions
diff --git a/internal/source/gitlab/client/client.go b/internal/source/gitlab/client/client.go index f5ecf355..bfce9bbd 100644 --- a/internal/source/gitlab/client/client.go +++ b/internal/source/gitlab/client/client.go @@ -19,18 +19,15 @@ import ( // Client is a HTTP client to access Pages internal API type Client struct { - secretKey []byte - baseURL *url.URL - httpClient *http.Client + secretKey []byte + baseURL *url.URL + httpClient *http.Client + jwtTokenExpiry time.Duration } -// TODO make these values configurable https://gitlab.com/gitlab-org/gitlab-pages/issues/274 -var tokenTimeout = 30 * time.Second -var connectionTimeout = 10 * time.Second - // NewClient initializes and returns new Client baseUrl is // appConfig.GitLabServer secretKey is appConfig.GitLabAPISecretKey -func NewClient(baseURL string, secretKey []byte) (*Client, error) { +func NewClient(baseURL string, secretKey []byte, connectionTimeout, jwtTokenExpiry time.Duration) (*Client, error) { if len(baseURL) == 0 || len(secretKey) == 0 { return nil, errors.New("GitLab API URL or API secret has not been provided") } @@ -39,7 +36,12 @@ func NewClient(baseURL string, secretKey []byte) (*Client, error) { if err != nil { return nil, err } - + if connectionTimeout == 0 { + return nil, errors.New("GitLab HTTP client connection timeout has not been provided") + } + if jwtTokenExpiry == 0 { + return nil, errors.New("GitLab JWT token expiry has not been provided") + } return &Client{ secretKey: secretKey, baseURL: url, @@ -47,12 +49,13 @@ func NewClient(baseURL string, secretKey []byte) (*Client, error) { Timeout: connectionTimeout, Transport: httptransport.Transport, }, + jwtTokenExpiry: jwtTokenExpiry, }, nil } // NewFromConfig creates a new client from Config struct func NewFromConfig(config Config) (*Client, error) { - return NewClient(config.GitlabServerURL(), config.GitlabAPISecret()) + return NewClient(config.GitlabServerURL(), config.GitlabAPISecret(), config.GitlabClientConnectionTimeout(), config.GitlabJWTTokenExpiry()) } // Resolve returns a VirtualDomain configuration wrapped into a Lookup for a @@ -151,7 +154,7 @@ func (gc *Client) request(ctx context.Context, method string, endpoint *url.URL) func (gc *Client) token() (string, error) { claims := jwt.StandardClaims{ Issuer: "gitlab-pages", - ExpiresAt: time.Now().Add(tokenTimeout).Unix(), + ExpiresAt: time.Now().Add(gc.jwtTokenExpiry).Unix(), } token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(gc.secretKey) diff --git a/internal/source/gitlab/client/config.go b/internal/source/gitlab/client/config.go index 49c13a60..4ed14267 100644 --- a/internal/source/gitlab/client/config.go +++ b/internal/source/gitlab/client/config.go @@ -1,8 +1,12 @@ package client +import "time" + // Config represents an interface that is configuration provider for client // capable of comunicating with GitLab type Config interface { GitlabServerURL() string GitlabAPISecret() []byte + GitlabClientConnectionTimeout() time.Duration + GitlabJWTTokenExpiry() time.Duration } |