This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathpki.go
81 lines (71 loc) · 1.99 KB
/
pki.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
package credential
import (
"fmt"
"io/ioutil"
"os"
"github.com/kubernetes-incubator/kube-aws/logger"
"github.com/kubernetes-incubator/kube-aws/pkg/api"
"github.com/kubernetes-incubator/kube-aws/pki"
)
type ProtectedPKI struct {
Encryptor
*pki.PKI
}
func NewProtectedPKI(enc Encryptor) *ProtectedPKI {
return &ProtectedPKI{
Encryptor: enc,
PKI: pki.NewPKI(),
}
}
func (ppki *ProtectedPKI) CreateKeyaPair(spec api.KeyPairSpec) error {
var signer *pki.KeyPair
if spec.Signer != "" {
signerCert, err := ioutil.ReadFile(spec.SignerCertPath())
if err != nil {
return fmt.Errorf("failed to read signer certificate %s for creating %s: %v", spec.SignerCertPath(), spec.Name, err)
}
signerKey, err := ioutil.ReadFile(spec.SignerKeyPath())
if err != nil {
return fmt.Errorf("failed to read signer key %s for creating %s: %v", spec.SignerKeyPath(), spec.Name, err)
}
signer, err = pki.KeyPairFromPEMs(spec.Signer, signerCert, signerKey)
}
keypair, err := ppki.GenerateKeyPair(spec, signer)
if err != nil {
return err
}
keypath := spec.KeyPath()
keypem := keypair.KeyInPEM()
logger.Infof("Writing key pem file %s", keypath)
if err := ioutil.WriteFile(keypath, keypem, 0644); err != nil {
return err
}
crtpath := spec.CertPath()
crtpem := keypair.CertInPEM()
logger.Infof("Writing certificate pem file %s", crtpath)
if err := ioutil.WriteFile(crtpath, crtpem, 0644); err != nil {
return err
}
return nil
}
func (ppki *ProtectedPKI) EnsureKeyPairsCreated(specs []api.KeyPairSpec) error {
for _, spec := range specs {
keypath := spec.KeyPath()
shapath := spec.KeyPath() + ".fingerprint"
encpath := spec.EncryptedKeyPath()
crtpath := spec.CertPath()
if !fileExists(keypath) && !fileExists(encpath) && !fileExists(shapath) && !fileExists(crtpath) {
if err := ppki.CreateKeyaPair(spec); err != nil {
return err
}
}
}
return nil
}
func fileExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}