hopefully finally fixed the TLS nil pointer

This commit is contained in:
Tobias Trabelsi 2022-11-12 22:22:14 +01:00
parent 13d3ed85ad
commit 3dd609864b
No known key found for this signature in database
GPG key ID: FF0C2839718CAF2E

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
@ -28,8 +29,7 @@ func (c *Config) Client() (interface{}, error) {
return nil, fmt.Errorf("either a token or a username needs to be used") return nil, fmt.Errorf("either a token or a username needs to be used")
} }
// Configure TLS/SSL // Configure TLS/SSL
tlsConfig := &tls.Config{} var tlsConfig tls.Config
// If a CACertFile has been specified, use that for cert validation // If a CACertFile has been specified, use that for cert validation
if c.CACertFile != "" { if c.CACertFile != "" {
caCert, err := ioutil.ReadFile(c.CACertFile) caCert, err := ioutil.ReadFile(c.CACertFile)
@ -43,13 +43,12 @@ func (c *Config) Client() (interface{}, error) {
} }
// If configured as insecure, turn off SSL verification // If configured as insecure, turn off SSL verification
if c.Insecure { tlsConfig.InsecureSkipVerify = c.Insecure
tlsConfig.InsecureSkipVerify = true
}
t := http.DefaultTransport.(*http.Transport).Clone() t := http.DefaultTransport.(*http.Transport).Clone()
t.TLSClientConfig = tlsConfig t.TLSClientConfig = &tlsConfig
t.MaxIdleConnsPerHost = 100 t.MaxIdleConnsPerHost = 100
t.TLSHandshakeTimeout = 10 * time.Second
httpClient := &http.Client{ httpClient := &http.Client{
Transport: logging.NewTransport("Gitea", t), Transport: logging.NewTransport("Gitea", t),
@ -60,16 +59,23 @@ func (c *Config) Client() (interface{}, error) {
} }
var client *gitea.Client var client *gitea.Client
var err error
if c.Token != "" { if c.Token != "" {
client, _ = gitea.NewClient(c.BaseURL, gitea.SetToken(c.Token), gitea.SetHTTPClient(httpClient)) client, err = gitea.NewClient(c.BaseURL, gitea.SetToken(c.Token), gitea.SetHTTPClient(httpClient))
if err != nil {
return nil, err
}
} }
if c.Username != "" { if c.Username != "" {
client, _ = gitea.NewClient(c.BaseURL, gitea.SetBasicAuth(c.Username, c.Password), gitea.SetHTTPClient(httpClient)) client, err = gitea.NewClient(c.BaseURL, gitea.SetBasicAuth(c.Username, c.Password), gitea.SetHTTPClient(httpClient))
if err != nil {
return nil, err
}
} }
// Test the credentials by checking we can get information about the authenticated user. // Test the credentials by checking we can get information about the authenticated user.
_, _, err := client.GetMyUserInfo() _, _, err = client.GetMyUserInfo()
return client, err return client, err
} }