Add method to just write the identity certificate.

This commit is contained in:
Mariano Cano 2019-12-18 14:39:01 -08:00
parent 839fe6b952
commit 0b5d37b284

View file

@ -81,22 +81,12 @@ func WriteDefaultIdentity(certChain []api.Certificate, key crypto.PrivateKey) er
keyFilename := filepath.Join(identityDir, "identity_key") keyFilename := filepath.Join(identityDir, "identity_key")
// Write certificate // Write certificate
buf := new(bytes.Buffer) if err := WriteIdentityCertificate(certChain); err != nil {
for _, crt := range certChain { return err
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Raw,
}
if err := pem.Encode(buf, block); err != nil {
return errors.Wrap(err, "error encoding identity certificate")
}
}
if err := ioutil.WriteFile(certFilename, buf.Bytes(), 0600); err != nil {
return errors.Wrap(err, "error writing identity certificate")
} }
// Write key // Write key
buf.Reset() buf := new(bytes.Buffer)
block, err := pemutil.Serialize(key) block, err := pemutil.Serialize(key)
if err != nil { if err != nil {
return err return err
@ -126,6 +116,27 @@ func WriteDefaultIdentity(certChain []api.Certificate, key crypto.PrivateKey) er
return nil return nil
} }
// WriteIdentityCertificate writes the identity certificate in disk.
func WriteIdentityCertificate(certChain []api.Certificate) error {
buf := new(bytes.Buffer)
certFilename := filepath.Join(identityDir, "identity.crt")
for _, crt := range certChain {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Raw,
}
if err := pem.Encode(buf, block); err != nil {
return errors.Wrap(err, "error encoding identity certificate")
}
}
if err := ioutil.WriteFile(certFilename, buf.Bytes(), 0600); err != nil {
return errors.Wrap(err, "error writing identity certificate")
}
return nil
}
// Kind returns the type for the given identity. // Kind returns the type for the given identity.
func (i *Identity) Kind() Type { func (i *Identity) Kind() Type {
switch strings.ToLower(i.Type) { switch strings.ToLower(i.Type) {