Skip to content

Commit

Permalink
ensure gitlab API requests are canceled if user hits control-c
Browse files Browse the repository at this point in the history
  • Loading branch information
sding3 authored and prarit committed Aug 14, 2023
1 parent f4cc7b5 commit 644d19a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
3 changes: 2 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"context"
"io"
"math/rand"
"os"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatal(err)
}
lab.Init(host, u.Username, token, false)
lab.Init(context.Background(), host, u.Username, token, false)

// Make "origin" the default remote for test cases calling
// cmd.Run() directly, instead of launching the labBinaryPath
Expand Down
65 changes: 23 additions & 42 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package gitlab

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -69,49 +69,47 @@ func UserID() (int, error) {
return u.ID, nil
}

// Init initializes a gitlab client for use throughout lab.
func Init(_host, _user, _token string, allowInsecure bool) {
func initGitlabClient(ctx context.Context, _host, _user, _token string, tlsConfig *tls.Config) {
if len(_host) > 0 && _host[len(_host)-1] == '/' {
_host = _host[:len(_host)-1]
}
host = _host
user = _user
token = _token

tp := http.DefaultTransport.(*http.Transport).Clone()
tp.TLSClientConfig = tlsConfig
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: allowInsecure,
},
},
Transport: tp,
}

lab, _ = gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4"), gitlab.WithCustomLeveledLogger(log))
lab, _ = gitlab.NewClient(token,
gitlab.WithHTTPClient(httpClient),
gitlab.WithBaseURL(host+"/api/v4"),
gitlab.WithCustomLeveledLogger(log),
gitlab.WithRequestOptions(gitlab.WithContext(ctx)),
)
}

// Init initializes a gitlab client for use throughout lab.
func Init(ctx context.Context, _host, _user, _token string, allowInsecure bool) {
initGitlabClient(ctx, _host, _user, _token, &tls.Config{
InsecureSkipVerify: allowInsecure,
})
}

// InitWithCustomCA open the HTTP client using a custom CA file (a self signed
// one for instance) instead of relying only on those installed in the current
// system database
func InitWithCustomCA(_host, _user, _token, caFile string) error {
func InitWithCustomCA(ctx context.Context, _host, _user, _token, caFile string) error {
if len(_host) > 0 && _host[len(_host)-1] == '/' {
_host = _host[:len(_host)-1]
}
host = _host
user = _user
token = _token

caCert, err := ioutil.ReadFile(caFile)
caCert, err := os.ReadFile(caFile)
if err != nil {
return err
}
Expand All @@ -122,26 +120,9 @@ func InitWithCustomCA(_host, _user, _token, caFile string) error {
}
caCertPool.AppendCertsFromPEM(caCert)

httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
},
},
}

lab, _ = gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4"))
initGitlabClient(ctx, _host, _user, _token, &tls.Config{
RootCAs: caCertPool,
})
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion internal/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitlab

import (
"context"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestMain(m *testing.M) {
log.Fatal(err)
}

Init(host, u.Username, token, false)
Init(context.Background(), host, u.Username, token, false)

code := m.Run()

Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"context"
"log"
"os"
"os/signal"

"github.com/rsteube/carapace"
"github.com/zaquestion/lab/cmd"
Expand All @@ -13,15 +16,20 @@ import (
var version = "master"

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

cmd.Version = version
initSkipped := skipInit()
if !initSkipped {
h, u, t, ca, skipVerify := config.LoadMainConfig()

if ca != "" {
lab.InitWithCustomCA(h, u, t, ca)
if err := lab.InitWithCustomCA(ctx, h, u, t, ca); err != nil {
log.Fatal(err)
}
} else {
lab.Init(h, u, t, skipVerify)
lab.Init(ctx, h, u, t, skipVerify)
}
}
cmd.Execute(initSkipped)
Expand Down

0 comments on commit 644d19a

Please sign in to comment.