@@ -8,12 +8,12 @@ import (
8
8
"errors"
9
9
"fmt"
10
10
"io"
11
- "os"
12
11
"log"
13
12
"math"
14
13
"net/http"
15
14
"net/http/cookiejar"
16
15
"net/url"
16
+ "os"
17
17
"strings"
18
18
"time"
19
19
@@ -52,7 +52,8 @@ type apiClientOpt struct {
52
52
oauthEndpointParams url.Values
53
53
certFile string
54
54
keyFile string
55
- caCertsFile string
55
+ caCertsFile string
56
+ caCertsString string
56
57
certString string
57
58
keyString string
58
59
debug bool
@@ -64,6 +65,7 @@ type APIClient struct {
64
65
uri string
65
66
insecure bool
66
67
caCertsFile string
68
+ caCertsString string
67
69
username string
68
70
password string
69
71
zitiUsername string
@@ -141,18 +143,26 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
141
143
tlsConfig .Certificates = []tls.Certificate {cert }
142
144
}
143
145
144
- if tlsConfig .RootCAs == nil && opt .caCertsFile != "" {
146
+ if tlsConfig .RootCAs == nil && ( opt .caCertsFile != "" || opt . caCertsString != "" ) {
145
147
// create a Certificate pool to hold one or more CA certificates
146
148
rootCAPool := x509 .NewCertPool ()
147
149
148
150
// 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 )
156
166
}
157
167
158
168
// in the http client configuration, add TLS configuration and add the RootCAs
@@ -186,7 +196,8 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
186
196
rateLimiter : rateLimiter ,
187
197
uri : opt .uri ,
188
198
insecure : opt .insecure ,
189
- caCertsFile : opt .caCertsFile ,
199
+ caCertsFile : opt .caCertsFile ,
200
+ caCertsString : opt .caCertsString ,
190
201
username : opt .username ,
191
202
password : opt .password ,
192
203
zitiUsername : opt .zitiUsername ,
@@ -229,6 +240,7 @@ func (client *APIClient) toString() string {
229
240
buffer .WriteString (fmt .Sprintf ("uri: %s\n " , client .uri ))
230
241
buffer .WriteString (fmt .Sprintf ("insecure: %t\n " , client .insecure ))
231
242
buffer .WriteString (fmt .Sprintf ("cacerts_file: %s\n " , client .caCertsFile ))
243
+ buffer .WriteString (fmt .Sprintf ("cacerts_string: %s\n " , client .caCertsString ))
232
244
buffer .WriteString (fmt .Sprintf ("username: %s\n " , client .username ))
233
245
buffer .WriteString (fmt .Sprintf ("password: %s\n " , client .password ))
234
246
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
310
322
_ , _ = container .SetP (client .zitiPassword , "password" )
311
323
body := container .String ()
312
324
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
+ }
313
340
zitiLogin , err := zitiUtil .EdgeControllerLogin (client .uri , client .caCertsFile , body , log .Writer (), false , 30 , true )
314
341
if err != nil {
315
342
return "" , err
0 commit comments