Refactor json Marshal+Unmarshal in one function.

This commit is contained in:
Mariano Cano 2022-02-16 17:17:32 -08:00
parent a9550a746f
commit b3316c4a56

View file

@ -158,7 +158,7 @@ func getCertificateAndChain(certb certutil.CertBundle) (*Certificate, error) {
chains := append(certb.CAChain, []string{certb.Certificate}...) chains := append(certb.CAChain, []string{certb.Certificate}...)
for _, chain := range chains { for _, chain := range chains {
for _, cert := range parseCertificates(chain) { for _, cert := range parseCertificates(chain) {
if used[cert.SerialNumber.String()] == true { if used[cert.SerialNumber.String()] {
continue continue
} }
used[cert.SerialNumber.String()] = true used[cert.SerialNumber.String()] = true
@ -231,15 +231,8 @@ func (v *VaultCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.
} }
var certBundle certutil.CertBundle var certBundle certutil.CertBundle
if err := unmarshalMap(secret.Data, &certBundle); err != nil {
secretData, err := json.Marshal(secret.Data) return nil, nil, errors.Wrap(err, "error unmarshaling cert bundle")
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(secretData, &certBundle)
if err != nil {
return nil, nil, err
} }
cert, err := getCertificateAndChain(certBundle) cert, err := getCertificateAndChain(certBundle)
@ -339,15 +332,8 @@ func (v *VaultCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityReq
} }
var certBundle certutil.CertBundle var certBundle certutil.CertBundle
if err := unmarshalMap(secret.Data, &certBundle); err != nil {
secretData, err := json.Marshal(secret.Data) return nil, errors.Wrap(err, "error unmarshaling cert bundle")
if err != nil {
return nil, err
}
err = json.Unmarshal(secretData, &certBundle)
if err != nil {
return nil, err
} }
cert, err := getCertificateAndChain(certBundle) cert, err := getCertificateAndChain(certBundle)
@ -407,3 +393,12 @@ func (v *VaultCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv
CertificateChain: nil, CertificateChain: nil,
}, nil }, nil
} }
func unmarshalMap(m map[string]interface{}, v interface{}) error {
b, err := json.Marshal(m)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}