SetRootCertificate adds one or more root certificates to the client's TLS configuration.
(path string)
| 311 | |
| 312 | // SetRootCertificate adds one or more root certificates to the client's TLS configuration. |
| 313 | func (c *Client) SetRootCertificate(path string) *Client { |
| 314 | cleanPath := filepath.Clean(path) |
| 315 | file, err := os.Open(cleanPath) |
| 316 | if err != nil { |
| 317 | c.logger.Panicf("client: %v", err) |
| 318 | } |
| 319 | defer func() { |
| 320 | if closeErr := file.Close(); closeErr != nil { |
| 321 | c.logger.Panicf("client: failed to close file: %v", closeErr) |
| 322 | } |
| 323 | }() |
| 324 | |
| 325 | pem, err := io.ReadAll(file) |
| 326 | if err != nil { |
| 327 | c.logger.Panicf("client: %v", err) |
| 328 | } |
| 329 | |
| 330 | config := c.TLSConfig() |
| 331 | if config.RootCAs == nil { |
| 332 | config.RootCAs = x509.NewCertPool() |
| 333 | } |
| 334 | |
| 335 | if !config.RootCAs.AppendCertsFromPEM(pem) { |
| 336 | c.logger.Panicf("client: %v", ErrFailedToAppendCert) |
| 337 | } |
| 338 | |
| 339 | return c |
| 340 | } |
| 341 | |
| 342 | // SetRootCertificateFromString adds one or more root certificates from a string to the client's TLS configuration. |
| 343 | func (c *Client) SetRootCertificateFromString(pem string) *Client { |