Skip to content

Commit 63a86d5

Browse files
authored
Merge pull request #3 from qrkourier/add-cacert-string
add cacerts_string option
2 parents 45106e9 + a429fda commit 63a86d5

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

restapi/api_client.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"os"
1211
"log"
1312
"math"
1413
"net/http"
1514
"net/http/cookiejar"
1615
"net/url"
16+
"os"
1717
"strings"
1818
"time"
1919

@@ -52,7 +52,8 @@ type apiClientOpt struct {
5252
oauthEndpointParams url.Values
5353
certFile string
5454
keyFile string
55-
caCertsFile string
55+
caCertsFile string
56+
caCertsString string
5657
certString string
5758
keyString string
5859
debug bool
@@ -64,6 +65,7 @@ type APIClient struct {
6465
uri string
6566
insecure bool
6667
caCertsFile string
68+
caCertsString string
6769
username string
6870
password string
6971
zitiUsername string
@@ -141,18 +143,26 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
141143
tlsConfig.Certificates = []tls.Certificate{cert}
142144
}
143145

144-
if tlsConfig.RootCAs == nil && opt.caCertsFile != "" {
146+
if tlsConfig.RootCAs == nil && (opt.caCertsFile != "" || opt.caCertsString != "") {
145147
// create a Certificate pool to hold one or more CA certificates
146148
rootCAPool := x509.NewCertPool()
147149

148150
// read CA certificates and add to the Certificate Pool
149-
rootCA, err := os.ReadFile(opt.caCertsFile)
150-
if err != nil {
151-
log.Fatalf("reading CA certs file failed : %v", err)
152-
}
153-
rootCAPool.AppendCertsFromPEM(rootCA)
154-
if opt.debug {
155-
log.Printf("RootCA loaded from file: %s", opt.caCertsFile)
151+
if opt.caCertsFile != "" {
152+
rootCA, err := os.ReadFile(opt.caCertsFile)
153+
if err != nil {
154+
log.Fatalf("reading CA certs file failed : %v", err)
155+
}
156+
rootCAPool.AppendCertsFromPEM(rootCA)
157+
if opt.debug {
158+
log.Printf("RootCA loaded from file: %s", opt.caCertsFile)
159+
}
160+
} else {
161+
rootCA := []byte(opt.caCertsString)
162+
if opt.debug {
163+
log.Printf("RootCA loaded from string: %s", opt.caCertsString)
164+
}
165+
rootCAPool.AppendCertsFromPEM(rootCA)
156166
}
157167

158168
// in the http client configuration, add TLS configuration and add the RootCAs
@@ -186,7 +196,8 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
186196
rateLimiter: rateLimiter,
187197
uri: opt.uri,
188198
insecure: opt.insecure,
189-
caCertsFile: opt.caCertsFile,
199+
caCertsFile: opt.caCertsFile,
200+
caCertsString: opt.caCertsString,
190201
username: opt.username,
191202
password: opt.password,
192203
zitiUsername: opt.zitiUsername,
@@ -229,6 +240,7 @@ func (client *APIClient) toString() string {
229240
buffer.WriteString(fmt.Sprintf("uri: %s\n", client.uri))
230241
buffer.WriteString(fmt.Sprintf("insecure: %t\n", client.insecure))
231242
buffer.WriteString(fmt.Sprintf("cacerts_file: %s\n", client.caCertsFile))
243+
buffer.WriteString(fmt.Sprintf("cacerts_string: %s\n", client.caCertsString))
232244
buffer.WriteString(fmt.Sprintf("username: %s\n", client.username))
233245
buffer.WriteString(fmt.Sprintf("password: %s\n", client.password))
234246
buffer.WriteString(fmt.Sprintf("ziti_username: %s\n", client.zitiUsername))
@@ -310,6 +322,21 @@ func (client *APIClient) sendRequest(method string, path string, data string) (s
310322
_, _ = container.SetP(client.zitiPassword, "password")
311323
body := container.String()
312324

325+
if client.caCertsFile == "" && client.caCertsString != "" {
326+
// write string to temp file for login function that only takes a file
327+
tmpFile, err := os.CreateTemp("", "ctrl-plane-cas-*.crt")
328+
if err != nil {
329+
return "", fmt.Errorf("failed to allocate a temporary file to save the Ziti root CA PEM string: %s", err)
330+
}
331+
defer os.Remove(tmpFile.Name())
332+
if _, err := tmpFile.Write([]byte(client.caCertsString)); err != nil {
333+
return "", fmt.Errorf("failed to write Ziti root CA PEM string to temporary file", err)
334+
}
335+
client.caCertsFile = tmpFile.Name()
336+
if client.debug {
337+
log.Printf("wrote Ziti root CA PEM string to temporary file '%s'", client.caCertsFile)
338+
}
339+
}
313340
zitiLogin, err := zitiUtil.EdgeControllerLogin(client.uri, client.caCertsFile, body, log.Writer(), false, 30, true)
314341
if err != nil {
315342
return "", err

restapi/provider.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,17 @@ func Provider() *schema.Provider {
180180
},
181181
},
182182
},
183-
"cacerts_file": {
183+
"cacerts_string": {
184184
Type: schema.TypeString,
185185
Optional: true,
186186
DefaultFunc: schema.EnvDefaultFunc("REST_API_CACERTS_STRING", nil),
187-
Description: "One or more CA certs to trust as a PEM bundle.",
187+
Description: "One or more CA certs to trust as a PEM bundle in a string.",
188+
},
189+
"cacerts_file": {
190+
Type: schema.TypeString,
191+
Optional: true,
192+
DefaultFunc: schema.EnvDefaultFunc("REST_API_CACERTS_FILE", nil),
193+
Description: "One or more CA certs to trust as a PEM bundle in a file.",
188194
},
189195
"cert_string": {
190196
Type: schema.TypeString,
@@ -246,6 +252,7 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
246252
uri: d.Get("uri").(string),
247253
insecure: d.Get("insecure").(bool),
248254
caCertsFile: d.Get("cacerts_file").(string),
255+
caCertsString: d.Get("cacerts_string").(string),
249256
username: d.Get("username").(string),
250257
password: d.Get("password").(string),
251258
zitiUsername: d.Get("ziti_username").(string),
@@ -302,6 +309,9 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
302309
if v, ok := d.GetOk("cacerts_file"); ok {
303310
opt.caCertsFile = v.(string)
304311
}
312+
if v, ok := d.GetOk("cacerts_string"); ok {
313+
opt.caCertsString = v.(string)
314+
}
305315
if v, ok := d.GetOk("cert_string"); ok {
306316
opt.certString = v.(string)
307317
}

0 commit comments

Comments
 (0)