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(refactor): for pr #826 #848

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
67 changes: 25 additions & 42 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,34 +884,15 @@ func (c *Client) SetRootCertificate(pemFilePath string) *Client {
c.log.Errorf("%v", err)
return c
}

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

config.RootCAs.AppendCertsFromPEM(rootPemData)
c.handleCAs("root", rootPemData)
return c
}

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

config.RootCAs.AppendCertsFromPEM([]byte(pemContent))
// client.SetRootCertificateFromString("pem certs content")
func (c *Client) SetRootCertificateFromString(pemCerts string) *Client {
c.handleCAs("root", []byte(pemCerts))
return c
}

Expand All @@ -924,35 +905,37 @@ func (c *Client) SetClientRootCertificate(pemFilePath string) *Client {
c.log.Errorf("%v", err)
return c
}

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(rootPemData)
c.handleCAs("client", 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 {
// client.SetClientRootCertificateFromString("pem certs content")
func (c *Client) SetClientRootCertificateFromString(pemCerts string) *Client {
c.handleCAs("client", []byte(pemCerts))
return c
}

func (c *Client) handleCAs(scope string, permCerts []byte) {
config, err := c.tlsConfig()
if err != nil {
c.log.Errorf("%v", err)
return c
}
if config.ClientCAs == nil {
config.ClientCAs = x509.NewCertPool()
return
}

config.ClientCAs.AppendCertsFromPEM([]byte(pemContent))
return c
switch scope {
case "root":
if config.RootCAs == nil {
config.RootCAs = x509.NewCertPool()
}
config.RootCAs.AppendCertsFromPEM(permCerts)
case "client":
if config.ClientCAs == nil {
config.ClientCAs = x509.NewCertPool()
}
config.ClientCAs.AppendCertsFromPEM(permCerts)
}
}

// SetOutputDirectory method sets output directory for saving HTTP response into file.
Expand Down