55package gitlab
66
77import (
8+ "context"
89 "crypto/tls"
910 "crypto/x509"
1011 "fmt"
1112 "io"
1213 "io/ioutil"
13- "net"
1414 "net/http"
1515 "os"
1616 "path/filepath"
@@ -69,49 +69,47 @@ func UserID() (int, error) {
6969 return u .ID , nil
7070}
7171
72- // Init initializes a gitlab client for use throughout lab.
73- func Init (_host , _user , _token string , allowInsecure bool ) {
72+ func initGitlabClient (ctx context.Context , _host , _user , _token string , tlsConfig * tls.Config ) {
7473 if len (_host ) > 0 && _host [len (_host )- 1 ] == '/' {
7574 _host = _host [:len (_host )- 1 ]
7675 }
7776 host = _host
7877 user = _user
7978 token = _token
8079
80+ tp := http .DefaultTransport .(* http.Transport ).Clone ()
81+ tp .TLSClientConfig = tlsConfig
8182 httpClient := & http.Client {
82- Transport : & http.Transport {
83- Proxy : http .ProxyFromEnvironment ,
84- DialContext : (& net.Dialer {
85- Timeout : 30 * time .Second ,
86- KeepAlive : 30 * time .Second ,
87- DualStack : true ,
88- }).DialContext ,
89- ForceAttemptHTTP2 : true ,
90- MaxIdleConns : 100 ,
91- IdleConnTimeout : 90 * time .Second ,
92- TLSHandshakeTimeout : 10 * time .Second ,
93- ExpectContinueTimeout : 1 * time .Second ,
94- TLSClientConfig : & tls.Config {
95- InsecureSkipVerify : allowInsecure ,
96- },
97- },
83+ Transport : tp ,
9884 }
9985
100- lab , _ = gitlab .NewClient (token , gitlab .WithHTTPClient (httpClient ), gitlab .WithBaseURL (host + "/api/v4" ), gitlab .WithCustomLeveledLogger (log ))
86+ lab , _ = gitlab .NewClient (token ,
87+ gitlab .WithHTTPClient (httpClient ),
88+ gitlab .WithBaseURL (host + "/api/v4" ),
89+ gitlab .WithCustomLeveledLogger (log ),
90+ gitlab .WithRequestOptions (gitlab .WithContext (ctx )),
91+ )
92+ }
93+
94+ // Init initializes a gitlab client for use throughout lab.
95+ func Init (ctx context.Context , _host , _user , _token string , allowInsecure bool ) {
96+ initGitlabClient (ctx , _host , _user , _token , & tls.Config {
97+ InsecureSkipVerify : allowInsecure ,
98+ })
10199}
102100
103101// InitWithCustomCA open the HTTP client using a custom CA file (a self signed
104102// one for instance) instead of relying only on those installed in the current
105103// system database
106- func InitWithCustomCA (_host , _user , _token , caFile string ) error {
104+ func InitWithCustomCA (ctx context. Context , _host , _user , _token , caFile string ) error {
107105 if len (_host ) > 0 && _host [len (_host )- 1 ] == '/' {
108106 _host = _host [:len (_host )- 1 ]
109107 }
110108 host = _host
111109 user = _user
112110 token = _token
113111
114- caCert , err := ioutil .ReadFile (caFile )
112+ caCert , err := os .ReadFile (caFile )
115113 if err != nil {
116114 return err
117115 }
@@ -122,26 +120,9 @@ func InitWithCustomCA(_host, _user, _token, caFile string) error {
122120 }
123121 caCertPool .AppendCertsFromPEM (caCert )
124122
125- httpClient := & http.Client {
126- Transport : & http.Transport {
127- Proxy : http .ProxyFromEnvironment ,
128- DialContext : (& net.Dialer {
129- Timeout : 30 * time .Second ,
130- KeepAlive : 30 * time .Second ,
131- DualStack : true ,
132- }).DialContext ,
133- ForceAttemptHTTP2 : true ,
134- MaxIdleConns : 100 ,
135- IdleConnTimeout : 90 * time .Second ,
136- TLSHandshakeTimeout : 10 * time .Second ,
137- ExpectContinueTimeout : 1 * time .Second ,
138- TLSClientConfig : & tls.Config {
139- RootCAs : caCertPool ,
140- },
141- },
142- }
143-
144- lab , _ = gitlab .NewClient (token , gitlab .WithHTTPClient (httpClient ), gitlab .WithBaseURL (host + "/api/v4" ))
123+ initGitlabClient (ctx , _host , _user , _token , & tls.Config {
124+ RootCAs : caCertPool ,
125+ })
145126 return nil
146127}
147128
0 commit comments