Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SetClientRootCertificate method support clientCAs usage #826

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,46 @@
return c
}

// SetClientRootCertificate method helps to add one or more client's root certificates into Resty client
//
// client.SetClientRootCertificate("/path/to/root/pemFile.pem")
func (c *Client) SetClientRootCertificate(pemFilePath string) *Client {
rootPemData, err := os.ReadFile(pemFilePath)
if err != nil {
c.log.Errorf("%v", err)
return c
}

config, err := c.tlsConfig()
if err != nil {
c.log.Errorf("%v", err)
return c

Check warning on line 931 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L930-L931

Added lines #L930 - L931 were not covered by tests
}
if config.ClientCAs == nil {
config.ClientCAs = x509.NewCertPool()
}

config.ClientCAs.AppendCertsFromPEM(rootPemData)
return c
}

// SetClientRootCertificateFromString method helps to add one or more client's root certificates into Resty client
//
// client.SetClientRootCertificateFromString("pem file content")
func (c *Client) SetClientRootCertificateFromString(pemContent string) *Client {
config, err := c.tlsConfig()
if err != nil {
c.log.Errorf("%v", err)
return c
}
if config.ClientCAs == nil {
config.ClientCAs = x509.NewCertPool()
}

config.ClientCAs.AppendCertsFromPEM([]byte(pemContent))
return c
}

// SetOutputDirectory method sets output directory for saving HTTP response into file.
// If the output directory not exists then resty creates one. This setting is optional one,
// if you're planning using absolute path in `Request.SetOutput` and can used together.
Expand Down
50 changes: 50 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,56 @@ func TestClientSetRootCertificateFromStringErrorTls(t *testing.T) {
assertNil(t, transport)
}

func TestClientSetClientRootCertificate(t *testing.T) {
client := dc()
client.SetClientRootCertificate(filepath.Join(getTestDataPath(), "sample-root.pem"))

transport, err := client.Transport()

assertNil(t, err)
assertNotNil(t, transport.TLSClientConfig.ClientCAs)
}

func TestClientSetClientRootCertificateNotExists(t *testing.T) {
client := dc()
client.SetClientRootCertificate(filepath.Join(getTestDataPath(), "not-exists-sample-root.pem"))

transport, err := client.Transport()

assertNil(t, err)
assertNil(t, transport.TLSClientConfig)
}

func TestClientSetClientRootCertificateFromString(t *testing.T) {
client := dc()
rootPemData, err := os.ReadFile(filepath.Join(getTestDataPath(), "sample-root.pem"))
assertNil(t, err)

client.SetClientRootCertificateFromString(string(rootPemData))

transport, err := client.Transport()

assertNil(t, err)
assertNotNil(t, transport.TLSClientConfig.ClientCAs)
}

func TestClientSetClientRootCertificateFromStringErrorTls(t *testing.T) {
client := NewWithClient(&http.Client{})
client.outputLogTo(io.Discard)

rootPemData, err := os.ReadFile(filepath.Join(getTestDataPath(), "sample-root.pem"))
assertNil(t, err)
rt := &CustomRoundTripper{}
client.SetTransport(rt)
transport, err := client.Transport()

client.SetClientRootCertificateFromString(string(rootPemData))

assertNotNil(t, rt)
assertNotNil(t, err)
assertNil(t, transport)
}

func TestClientOnBeforeRequestModification(t *testing.T) {
tc := dc()
tc.OnBeforeRequest(func(c *Client, r *Request) error {
Expand Down