Skip to content

Commit 4a6502f

Browse files
Merge pull request #5 from richardc/specify_https_options
Add `cacert_file` and `insecure` options to the provider.
2 parents c13fcd7 + b08ec90 commit 4a6502f

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

gitlab/config.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
package gitlab
22

33
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"io/ioutil"
7+
"net/http"
8+
49
"github.com/xanzy/go-gitlab"
510
)
611

712
// Config is per-provider, specifies where to connect to gitlab
813
type Config struct {
9-
Token string
10-
BaseURL string
14+
Token string
15+
BaseURL string
16+
Insecure bool
17+
CACertFile string
1118
}
1219

1320
// Client returns a *gitlab.Client to interact with the configured gitlab instance
1421
func (c *Config) Client() (interface{}, error) {
15-
client := gitlab.NewClient(nil, c.Token)
22+
// Configure TLS/SSL
23+
tlsConfig := &tls.Config{}
24+
25+
// If a CACertFile has been specified, use that for cert validation
26+
if c.CACertFile != "" {
27+
caCert, err := ioutil.ReadFile(c.CACertFile)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
caCertPool := x509.NewCertPool()
33+
caCertPool.AppendCertsFromPEM(caCert)
34+
tlsConfig.RootCAs = caCertPool
35+
}
36+
37+
// If configured as insecure, turn off SSL verification
38+
if c.Insecure {
39+
tlsConfig.InsecureSkipVerify = true
40+
}
41+
42+
transport := &http.Transport{TLSClientConfig: tlsConfig}
43+
44+
httpClient := &http.Client{Transport: transport}
45+
46+
client := gitlab.NewClient(httpClient, c.Token)
1647
if c.BaseURL != "" {
1748
err := client.SetBaseURL(c.BaseURL)
1849
if err != nil {

gitlab/provider.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ func Provider() terraform.ResourceProvider {
2323
DefaultFunc: schema.EnvDefaultFunc("GITLAB_BASE_URL", ""),
2424
Description: descriptions["base_url"],
2525
},
26+
"cacert_file": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Default: "",
30+
Description: descriptions["cacert_file"],
31+
},
32+
"insecure": {
33+
Type: schema.TypeBool,
34+
Optional: true,
35+
Default: false,
36+
Description: descriptions["insecure"],
37+
},
2638
},
2739
ResourcesMap: map[string]*schema.Resource{
2840
"gitlab_group": resourceGitlabGroup(),
@@ -42,13 +54,19 @@ func init() {
4254
"token": "The OAuth token used to connect to GitLab.",
4355

4456
"base_url": "The GitLab Base API URL",
57+
58+
"cacert_file": "A file containing the ca certificate to use in case ssl certificate is not from a standard chain",
59+
60+
"insecure": "Disable SSL verification of API calls",
4561
}
4662
}
4763

4864
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
4965
config := Config{
50-
Token: d.Get("token").(string),
51-
BaseURL: d.Get("base_url").(string),
66+
Token: d.Get("token").(string),
67+
BaseURL: d.Get("base_url").(string),
68+
CACertFile: d.Get("cacert_file").(string),
69+
Insecure: d.Get("insecure").(bool),
5270
}
5371

5472
return config.Client()

website/docs/index.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ The following arguments are supported in the `provider` block:
6565
requirement when working with GitLab CE or GitLab Enterprise e.g. https://my.gitlab.server/api/v3/.
6666
It is optional to provide this value and it can also be sourced from the `GITLAB_BASE_URL` environment variable.
6767
The value must end with a slash.
68+
69+
* `cacert_file` - (Optional) This is a file containing the ca cert to verify the gitlab instance. This is available
70+
for use when working with GitLab CE or Gitlab Enterprise with a locally-issued or self-signed certificate chain.
71+
72+
* `insecure` - (Optional; boolean, defaults to false) When set to true this disables SSL verification of the connection to the
73+
GitLab instance.

0 commit comments

Comments
 (0)