-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager.go
137 lines (108 loc) · 3.09 KB
/
manager.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package profile
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"os"
"os/user"
"time"
"github.com/dpb587/ssoca/service/openvpn/api"
"github.com/dpb587/ssoca/service/openvpn/httpclient"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Manager struct {
client *httpclient.Client
service string
privateKey *rsa.PrivateKey
profile string
certificate *x509.Certificate
certificateBytes []byte
}
func NewManager(client *httpclient.Client, service string, privateKey *rsa.PrivateKey) Manager {
return Manager{
client: client,
service: service,
privateKey: privateKey,
}
}
func CreateManagerAndPrivateKey(client *httpclient.Client, service string) (Manager, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return Manager{}, bosherr.WrapError(err, "Generating private key")
}
return NewManager(client, service, privateKey), nil
}
func (m Manager) Sign(data []byte) ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, m.privateKey, 0, data)
}
func (m *Manager) GetProfile() (Profile, error) {
if m.IsExpired() {
err := m.Renew()
if err != nil {
return Profile{}, bosherr.WrapError(err, "Renewing certificate")
}
}
return NewProfile(m.profile, m.privateKey, m.certificateBytes), nil
}
func (m Manager) IsExpired() bool {
return m.certificate == nil || time.Now().After(m.certificate.NotAfter)
}
func (m *Manager) Renew() error {
csrBytes, err := m.createCSR()
if err != nil {
return bosherr.WrapError(err, "Creating CSR")
}
response, err := m.client.SignUserCSR(api.SignUserCSRRequest{
CSR: string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})),
})
if err != nil {
return bosherr.WrapError(err, "Requesting signed profile")
}
m.profile = response.Profile
m.certificateBytes = []byte(response.Certificate)
pem, _ := pem.Decode(m.certificateBytes)
if pem == nil {
return errors.New("Failed to decode PEM from certificate")
}
certificate, err := x509.ParseCertificate(pem.Bytes)
if err != nil {
panic(err)
}
m.certificate = certificate
return nil
}
func (m Manager) createCSR() ([]byte, error) {
localuser, err := user.Current()
if err != nil {
return nil, bosherr.WrapError(err, "Checking local user")
}
localhost, err := os.Hostname()
if err != nil {
return nil, bosherr.WrapError(err, "Checking local host")
}
emailAddress := fmt.Sprintf("%s@%s", localuser.Username, localhost)
subj := pkix.Name{CommonName: m.service}
rawSubj := subj.ToRDNSequence()
rawSubj = append(rawSubj, []pkix.AttributeTypeAndValue{
{
Type: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1},
Value: emailAddress,
},
})
asn1Subj, _ := asn1.Marshal(rawSubj)
template := x509.CertificateRequest{
RawSubject: asn1Subj,
EmailAddresses: []string{emailAddress},
SignatureAlgorithm: x509.SHA256WithRSA,
}
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &template, m.privateKey)
if err != nil {
return nil, bosherr.WrapError(err, "Creating certificate request")
}
return csrBytes, nil
}