/
cabundle.go
56 lines (48 loc) · 1.29 KB
/
cabundle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package tls
import (
"bytes"
"encoding/pem"
"github.com/openshift/installer/pkg/asset"
"github.com/pkg/errors"
)
// CertBundle contains a multiple certificates in a bundle.
type CertBundle struct {
BundleRaw []byte
FileList []*asset.File
}
// Cert returns the certificate bundle.
func (b *CertBundle) Cert() []byte {
return b.BundleRaw
}
// Generate generates the cert bundle from certs.
func (b *CertBundle) Generate(filename string, certs ...CertInterface) error {
if len(certs) < 1 {
return errors.New("atleast one certificate required for a bundle")
}
buf := bytes.Buffer{}
for _, c := range certs {
cert, err := PemToCertificate(c.Cert())
if err != nil {
return errors.Wrap(err, "decoding certificate from PEM")
}
if err := pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
return errors.Wrap(err, "encoding certificate to PEM")
}
}
b.BundleRaw = buf.Bytes()
b.FileList = []*asset.File{
{
Filename: assetFilePath(filename + ".crt"),
Data: b.BundleRaw,
},
}
return nil
}
// Files returns the files generated by the asset.
func (b *CertBundle) Files() []*asset.File {
return b.FileList
}
// Load is a no-op because TLS assets are not written to disk.
func (b *CertBundle) Load(asset.FileFetcher) (bool, error) {
return false, nil
}