Skip to content

Commit 9097b96

Browse files
MagHErmitVladislav Kakurin
and
Vladislav Kakurin
authored
feat: add SetClientRootCertificate method support clientCAs usage (#826)
Co-authored-by: Vladislav Kakurin <[email protected]>
1 parent 370d744 commit 9097b96

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

client.go

+40
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,46 @@ func (c *Client) SetRootCertificateFromString(pemContent string) *Client {
915915
return c
916916
}
917917

918+
// SetClientRootCertificate method helps to add one or more client's root certificates into Resty client
919+
//
920+
// client.SetClientRootCertificate("/path/to/root/pemFile.pem")
921+
func (c *Client) SetClientRootCertificate(pemFilePath string) *Client {
922+
rootPemData, err := os.ReadFile(pemFilePath)
923+
if err != nil {
924+
c.log.Errorf("%v", err)
925+
return c
926+
}
927+
928+
config, err := c.tlsConfig()
929+
if err != nil {
930+
c.log.Errorf("%v", err)
931+
return c
932+
}
933+
if config.ClientCAs == nil {
934+
config.ClientCAs = x509.NewCertPool()
935+
}
936+
937+
config.ClientCAs.AppendCertsFromPEM(rootPemData)
938+
return c
939+
}
940+
941+
// SetClientRootCertificateFromString method helps to add one or more client's root certificates into Resty client
942+
//
943+
// client.SetClientRootCertificateFromString("pem file content")
944+
func (c *Client) SetClientRootCertificateFromString(pemContent string) *Client {
945+
config, err := c.tlsConfig()
946+
if err != nil {
947+
c.log.Errorf("%v", err)
948+
return c
949+
}
950+
if config.ClientCAs == nil {
951+
config.ClientCAs = x509.NewCertPool()
952+
}
953+
954+
config.ClientCAs.AppendCertsFromPEM([]byte(pemContent))
955+
return c
956+
}
957+
918958
// SetOutputDirectory method sets output directory for saving HTTP response into file.
919959
// If the output directory not exists then resty creates one. This setting is optional one,
920960
// if you're planning using absolute path in `Request.SetOutput` and can used together.

client_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,56 @@ func TestClientSetRootCertificateFromStringErrorTls(t *testing.T) {
309309
assertNil(t, transport)
310310
}
311311

312+
func TestClientSetClientRootCertificate(t *testing.T) {
313+
client := dc()
314+
client.SetClientRootCertificate(filepath.Join(getTestDataPath(), "sample-root.pem"))
315+
316+
transport, err := client.Transport()
317+
318+
assertNil(t, err)
319+
assertNotNil(t, transport.TLSClientConfig.ClientCAs)
320+
}
321+
322+
func TestClientSetClientRootCertificateNotExists(t *testing.T) {
323+
client := dc()
324+
client.SetClientRootCertificate(filepath.Join(getTestDataPath(), "not-exists-sample-root.pem"))
325+
326+
transport, err := client.Transport()
327+
328+
assertNil(t, err)
329+
assertNil(t, transport.TLSClientConfig)
330+
}
331+
332+
func TestClientSetClientRootCertificateFromString(t *testing.T) {
333+
client := dc()
334+
rootPemData, err := os.ReadFile(filepath.Join(getTestDataPath(), "sample-root.pem"))
335+
assertNil(t, err)
336+
337+
client.SetClientRootCertificateFromString(string(rootPemData))
338+
339+
transport, err := client.Transport()
340+
341+
assertNil(t, err)
342+
assertNotNil(t, transport.TLSClientConfig.ClientCAs)
343+
}
344+
345+
func TestClientSetClientRootCertificateFromStringErrorTls(t *testing.T) {
346+
client := NewWithClient(&http.Client{})
347+
client.outputLogTo(io.Discard)
348+
349+
rootPemData, err := os.ReadFile(filepath.Join(getTestDataPath(), "sample-root.pem"))
350+
assertNil(t, err)
351+
rt := &CustomRoundTripper{}
352+
client.SetTransport(rt)
353+
transport, err := client.Transport()
354+
355+
client.SetClientRootCertificateFromString(string(rootPemData))
356+
357+
assertNotNil(t, rt)
358+
assertNotNil(t, err)
359+
assertNil(t, transport)
360+
}
361+
312362
func TestClientOnBeforeRequestModification(t *testing.T) {
313363
tc := dc()
314364
tc.OnBeforeRequest(func(c *Client, r *Request) error {

0 commit comments

Comments
 (0)