forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_uploader.go
56 lines (47 loc) · 1.32 KB
/
certificate_uploader.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 iam
import (
"fmt"
"io/ioutil"
"strings"
"github.com/aws/aws-sdk-go/aws"
awsiam "github.com/aws/aws-sdk-go/service/iam"
)
type CertificateUploader struct {
iamClientProvider iamClientProvider
}
func NewCertificateUploader(iamClientProvider iamClientProvider) CertificateUploader {
return CertificateUploader{
iamClientProvider: iamClientProvider,
}
}
func (c CertificateUploader) Upload(certificatePath, privateKeyPath, chainPath, certificateName string) error {
if strings.ContainsAny(certificateName, ":") {
return fmt.Errorf("%q is an invalid certificate name, it must not contain %q", certificateName, ":")
}
certificate, err := ioutil.ReadFile(certificatePath)
if err != nil {
return err
}
privateKey, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return err
}
var chain *string
if chainPath != "" {
chainContents, err := ioutil.ReadFile(chainPath)
chain = aws.String(string(chainContents))
if err != nil {
return err
}
}
_, err = c.iamClientProvider.GetIAMClient().UploadServerCertificate(&awsiam.UploadServerCertificateInput{
CertificateBody: aws.String(string(certificate)),
PrivateKey: aws.String(string(privateKey)),
CertificateChain: chain,
ServerCertificateName: aws.String(certificateName),
})
if err != nil {
return err
}
return nil
}