@@ -1488,7 +1488,52 @@ func (c *Client) RemoveProxy() *Client {
1488
1488
return c
1489
1489
}
1490
1490
1491
- // SetCertificates method helps to conveniently set client certificates into Resty.
1491
+ // SetCertificateFromString method helps to set client certificates into Resty
1492
+ // from cert and key files to perform SSL client authentication
1493
+ //
1494
+ // client.SetCertificateFromFile("certs/client.pem", "certs/client.key")
1495
+ func (c * Client ) SetCertificateFromFile (certFilePath , certKeyFilePath string ) * Client {
1496
+ cert , err := tls .LoadX509KeyPair (certFilePath , certKeyFilePath )
1497
+ if err != nil {
1498
+ c .Logger ().Errorf ("client certificate/key parsing error: %v" , err )
1499
+ return c
1500
+ }
1501
+ c .SetCertificates (cert )
1502
+ return c
1503
+ }
1504
+
1505
+ // SetCertificateFromString method helps to set client certificates into Resty
1506
+ // from string to perform SSL client authentication
1507
+ //
1508
+ // myClientCertStr := `-----BEGIN CERTIFICATE-----
1509
+ // ... cert content ...
1510
+ // -----END CERTIFICATE-----`
1511
+ //
1512
+ // myClientCertKeyStr := `-----BEGIN PRIVATE KEY-----
1513
+ // ... cert key content ...
1514
+ // -----END PRIVATE KEY-----`
1515
+ //
1516
+ // client.SetCertificateFromString(myClientCertStr, myClientCertKeyStr)
1517
+ func (c * Client ) SetCertificateFromString (certStr , certKeyStr string ) * Client {
1518
+ cert , err := tls .X509KeyPair ([]byte (certStr ), []byte (certKeyStr ))
1519
+ if err != nil {
1520
+ c .Logger ().Errorf ("client certificate/key parsing error: %v" , err )
1521
+ return c
1522
+ }
1523
+ c .SetCertificates (cert )
1524
+ return c
1525
+ }
1526
+
1527
+ // SetCertificates method helps to conveniently set client certificates into Resty
1528
+ // to perform SSL client authentication
1529
+ //
1530
+ // cert, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
1531
+ // if err != nil {
1532
+ // log.Printf("ERROR client certificate/key parsing error: %v", err)
1533
+ // return
1534
+ // }
1535
+ //
1536
+ // client.SetCertificates(cert)
1492
1537
func (c * Client ) SetCertificates (certs ... tls.Certificate ) * Client {
1493
1538
config , err := c .tlsConfig ()
1494
1539
if err != nil {
@@ -1502,72 +1547,142 @@ func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
1502
1547
return c
1503
1548
}
1504
1549
1505
- // SetRootCertificate method helps to add one or more root certificates into the Resty client
1550
+ // SetRootCertificates method helps to add one or more root certificates into the Resty client
1506
1551
//
1507
- // client.SetRootCertificate("/path/to/root/pemFile.pem")
1508
- func (c * Client ) SetRootCertificate (pemFilePath string ) * Client {
1509
- rootPemData , err := os .ReadFile (pemFilePath )
1510
- if err != nil {
1511
- c .Logger ().Errorf ("%v" , err )
1512
- return c
1552
+ // // one pem file path
1553
+ // client.SetRootCertificates("/path/to/root/pemFile.pem")
1554
+ //
1555
+ // // one or more pem file path(s)
1556
+ // client.SetRootCertificates(
1557
+ // "/path/to/root/pemFile1.pem",
1558
+ // "/path/to/root/pemFile2.pem"
1559
+ // "/path/to/root/pemFile3.pem"
1560
+ // )
1561
+ //
1562
+ // // if you happen to have string slices
1563
+ // client.SetRootCertificates(certs...)
1564
+ func (c * Client ) SetRootCertificates (pemFilePaths ... string ) * Client {
1565
+ for _ , fp := range pemFilePaths {
1566
+ rootPemData , err := os .ReadFile (fp )
1567
+ if err != nil {
1568
+ c .Logger ().Errorf ("%v" , err )
1569
+ return c
1570
+ }
1571
+ c .handleCAs ("root" , rootPemData )
1513
1572
}
1514
- c .handleCAs ("root" , rootPemData )
1515
1573
return c
1516
1574
}
1517
1575
1518
- // SetRootCertificateWatcher enables dynamic reloading of one or more root certificates.
1576
+ // SetRootCertificatesWatcher method enables dynamic reloading of one or more root certificates.
1519
1577
// It is designed for scenarios involving long-running Resty clients where certificates may be renewed.
1520
- // The caller is responsible for calling Close to stop the watcher.
1521
- //
1522
- // client.SetRootCertificateWatcher("root-ca.crt", &CertWatcherOptions{
1523
- // PoolInterval: time.Hour * 24,
1524
- // })
1525
1578
//
1526
- // defer client.Close()
1527
- func (c * Client ) SetRootCertificateWatcher (pemFilePath string , options * CertWatcherOptions ) * Client {
1528
- c .SetRootCertificate (pemFilePath )
1529
- c .initCertWatcher (pemFilePath , "root" , options )
1579
+ // client.SetRootCertificatesWatcher(
1580
+ // &resty.CertWatcherOptions{
1581
+ // PoolInterval: 24 * time.Hour,
1582
+ // },
1583
+ // "root-ca.crt",
1584
+ // )
1585
+ func (c * Client ) SetRootCertificatesWatcher (options * CertWatcherOptions , pemFilePaths ... string ) * Client {
1586
+ c .SetRootCertificates (pemFilePaths ... )
1587
+ for _ , fp := range pemFilePaths {
1588
+ c .initCertWatcher (fp , "root" , options )
1589
+ }
1530
1590
return c
1531
1591
}
1532
1592
1533
1593
// SetRootCertificateFromString method helps to add one or more root certificates
1534
1594
// into the Resty client
1535
1595
//
1536
- // client.SetRootCertificateFromString("pem certs content")
1596
+ // myRootCertStr := `-----BEGIN CERTIFICATE-----
1597
+ // ... cert content ...
1598
+ // -----END CERTIFICATE-----`
1599
+ //
1600
+ // client.SetRootCertificateFromString(myRootCertStr)
1537
1601
func (c * Client ) SetRootCertificateFromString (pemCerts string ) * Client {
1538
1602
c .handleCAs ("root" , []byte (pemCerts ))
1539
1603
return c
1540
1604
}
1541
1605
1542
- // SetClientRootCertificate method helps to add one or more client's root
1606
+ // SetClientRootCertificates method helps to add one or more client's root
1543
1607
// certificates into the Resty client
1544
1608
//
1545
- // client.SetClientRootCertificate("/path/to/root/pemFile.pem")
1546
- func (c * Client ) SetClientRootCertificate (pemFilePath string ) * Client {
1547
- rootPemData , err := os .ReadFile (pemFilePath )
1548
- if err != nil {
1549
- c .Logger ().Errorf ("%v" , err )
1550
- return c
1609
+ // // one pem file path
1610
+ // client.SetClientCertificates("/path/to/client/pemFile.pem")
1611
+ //
1612
+ // // one or more pem file path(s)
1613
+ // client.SetClientCertificates(
1614
+ // "/path/to/client/pemFile1.pem",
1615
+ // "/path/to/client/pemFile2.pem"
1616
+ // "/path/to/client/pemFile3.pem"
1617
+ // )
1618
+ //
1619
+ // // if you happen to have string slices
1620
+ // client.SetClientCertificates(certs...)
1621
+ func (c * Client ) SetClientRootCertificates (pemFilePaths ... string ) * Client {
1622
+ for _ , fp := range pemFilePaths {
1623
+ pemData , err := os .ReadFile (fp )
1624
+ if err != nil {
1625
+ c .Logger ().Errorf ("%v" , err )
1626
+ return c
1627
+ }
1628
+ c .handleCAs ("client-root" , pemData )
1551
1629
}
1552
- c .handleCAs ("client" , rootPemData )
1553
1630
return c
1554
1631
}
1555
1632
1556
- // SetClientRootCertificateWatcher enables dynamic reloading of one or more client root certificates.
1633
+ // SetClientRootCertificatesWatcher method enables dynamic reloading of one or more client root certificates.
1557
1634
// It is designed for scenarios involving long-running Resty clients where certificates may be renewed.
1558
- // The caller is responsible for calling Close to stop the watcher.
1559
1635
//
1560
- // client.SetClientRootCertificateWatcher("root-ca.crt", &CertWatcherOptions{
1561
- // PoolInterval: time.Hour * 24,
1562
- // })
1563
- // defer client.Close()
1564
- func (c * Client ) SetClientRootCertificateWatcher (pemFilePath string , options * CertWatcherOptions ) * Client {
1565
- c .SetClientRootCertificate (pemFilePath )
1566
- c .initCertWatcher (pemFilePath , "client" , options )
1636
+ // client.SetClientRootCertificatesWatcher(
1637
+ // &resty.CertWatcherOptions{
1638
+ // PoolInterval: 24 * time.Hour,
1639
+ // },
1640
+ // "root-ca.crt",
1641
+ // )
1642
+ func (c * Client ) SetClientRootCertificatesWatcher (options * CertWatcherOptions , pemFilePaths ... string ) * Client {
1643
+ c .SetClientRootCertificates (pemFilePaths ... )
1644
+ for _ , fp := range pemFilePaths {
1645
+ c .initCertWatcher (fp , "client-root" , options )
1646
+ }
1647
+ return c
1648
+ }
1567
1649
1650
+ // SetClientRootCertificateFromString method helps to add one or more clients
1651
+ // root certificates into the Resty client
1652
+ //
1653
+ // myClientRootCertStr := `-----BEGIN CERTIFICATE-----
1654
+ // ... cert content ...
1655
+ // -----END CERTIFICATE-----`
1656
+ //
1657
+ // client.SetClientRootCertificateFromString(myClientRootCertStr)
1658
+ func (c * Client ) SetClientRootCertificateFromString (pemCerts string ) * Client {
1659
+ c .handleCAs ("client-root" , []byte (pemCerts ))
1568
1660
return c
1569
1661
}
1570
1662
1663
+ func (c * Client ) handleCAs (scope string , permCerts []byte ) {
1664
+ config , err := c .tlsConfig ()
1665
+ if err != nil {
1666
+ c .Logger ().Errorf ("%v" , err )
1667
+ return
1668
+ }
1669
+
1670
+ c .lock .Lock ()
1671
+ defer c .lock .Unlock ()
1672
+ switch scope {
1673
+ case "root" :
1674
+ if config .RootCAs == nil {
1675
+ config .RootCAs = x509 .NewCertPool ()
1676
+ }
1677
+ config .RootCAs .AppendCertsFromPEM (permCerts )
1678
+ case "client-root" :
1679
+ if config .ClientCAs == nil {
1680
+ config .ClientCAs = x509 .NewCertPool ()
1681
+ }
1682
+ config .ClientCAs .AppendCertsFromPEM (permCerts )
1683
+ }
1684
+ }
1685
+
1571
1686
func (c * Client ) initCertWatcher (pemFilePath , scope string , options * CertWatcherOptions ) {
1572
1687
tickerDuration := defaultWatcherPoolingInterval
1573
1688
if options != nil && options .PoolInterval > 0 {
@@ -1611,9 +1726,9 @@ func (c *Client) initCertWatcher(pemFilePath, scope string, options *CertWatcher
1611
1726
1612
1727
switch scope {
1613
1728
case "root" :
1614
- c .SetRootCertificate (pemFilePath )
1615
- case "client" :
1616
- c .SetClientRootCertificate (pemFilePath )
1729
+ c .SetRootCertificates (pemFilePath )
1730
+ case "client-root " :
1731
+ c .SetClientRootCertificates (pemFilePath )
1617
1732
}
1618
1733
1619
1734
c .debugf ("Cert %s reloaded." , pemFilePath )
@@ -1622,38 +1737,6 @@ func (c *Client) initCertWatcher(pemFilePath, scope string, options *CertWatcher
1622
1737
}()
1623
1738
}
1624
1739
1625
- // SetClientRootCertificateFromString method helps to add one or more clients
1626
- // root certificates into the Resty client
1627
- //
1628
- // client.SetClientRootCertificateFromString("pem certs content")
1629
- func (c * Client ) SetClientRootCertificateFromString (pemCerts string ) * Client {
1630
- c .handleCAs ("client" , []byte (pemCerts ))
1631
- return c
1632
- }
1633
-
1634
- func (c * Client ) handleCAs (scope string , permCerts []byte ) {
1635
- config , err := c .tlsConfig ()
1636
- if err != nil {
1637
- c .Logger ().Errorf ("%v" , err )
1638
- return
1639
- }
1640
-
1641
- c .lock .Lock ()
1642
- defer c .lock .Unlock ()
1643
- switch scope {
1644
- case "root" :
1645
- if config .RootCAs == nil {
1646
- config .RootCAs = x509 .NewCertPool ()
1647
- }
1648
- config .RootCAs .AppendCertsFromPEM (permCerts )
1649
- case "client" :
1650
- if config .ClientCAs == nil {
1651
- config .ClientCAs = x509 .NewCertPool ()
1652
- }
1653
- config .ClientCAs .AppendCertsFromPEM (permCerts )
1654
- }
1655
- }
1656
-
1657
1740
// OutputDirectory method returns the output directory value from the client.
1658
1741
func (c * Client ) OutputDirectory () string {
1659
1742
c .lock .RLock ()
0 commit comments