1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/certutils/certutils.go
Johannes Dillmann 4f23507bb7
Support custom tls certs in cnbBuild (#3103)
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
Co-authored-by: Pavel Busko <pavel.busko@sap.com>
2021-10-01 13:48:24 +02:00

38 lines
1.0 KiB
Go

package certutils
import (
"io/ioutil"
"net/http"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/pkg/errors"
)
func CertificateUpdate(certLinks []string, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils, caCertsFile string) error {
caCerts, err := fileUtils.FileRead(caCertsFile)
if err != nil {
return errors.Wrapf(err, "failed to load file '%v'", caCertsFile)
}
for _, link := range certLinks {
response, err := httpClient.SendRequest(http.MethodGet, link, nil, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to load certificate from url")
}
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "error reading response")
}
_ = response.Body.Close()
content = append(content, []byte("\n")...)
caCerts = append(caCerts, content...)
}
err = fileUtils.FileWrite(caCertsFile, caCerts, 0644)
if err != nil {
return errors.Wrapf(err, "failed to update file '%v'", caCertsFile)
}
return nil
}