mirror of
https://github.com/docker/docs.git
synced 2026-04-05 10:48:55 +07:00
Add explicit startTime and endTime parameters to cryptoservice.GenerateCertificate and trustmanager.NewCertificate. trustmanager.NewCertificate as a low-level data manipulation function should not be hard-coding policy (10-year expiration); that policy belongs to its callers, or one more level higher to callers of cryptoservice.GenerateCertificate. These places hard-coding policy now also have an explict comment to that effect. In addition to conceptual cleanliness, this will allow writing tests of certificate expiry by generating appropriate expired or nearly-expired certificates. Tests which don't care about the policy much will continue to use the just added cryptoservice.GenerateTestingCertificate. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package cryptoservice
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/docker/notary/trustmanager"
|
|
"github.com/docker/notary/tuf/data"
|
|
)
|
|
|
|
// GenerateCertificate generates an X509 Certificate from a template, given a GUN and validity interval
|
|
func GenerateCertificate(rootKey data.PrivateKey, gun string, startTime, endTime time.Time) (*x509.Certificate, error) {
|
|
signer := rootKey.CryptoSigner()
|
|
if signer == nil {
|
|
return nil, fmt.Errorf("key type not supported for Certificate generation: %s\n", rootKey.Algorithm())
|
|
}
|
|
|
|
return generateCertificate(signer, gun, startTime, endTime)
|
|
}
|
|
|
|
// GenerateTestingCertificate generates a non-expired X509 Certificate from a template, given a GUN.
|
|
// Good enough for tests where expiration does not really matter; do not use if you care about the policy.
|
|
func GenerateTestingCertificate(signer crypto.Signer, gun string) (*x509.Certificate, error) {
|
|
startTime := time.Now()
|
|
return generateCertificate(signer, gun, startTime, startTime.AddDate(10, 0, 0))
|
|
}
|
|
|
|
func generateCertificate(signer crypto.Signer, gun string, startTime, endTime time.Time) (*x509.Certificate, error) {
|
|
template, err := trustmanager.NewCertificate(gun, startTime, endTime)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create the certificate template for: %s (%v)", gun, err)
|
|
}
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, signer.Public(), signer)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create the certificate for: %s (%v)", gun, err)
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(derBytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse the certificate for key: %s (%v)", gun, err)
|
|
}
|
|
|
|
return cert, nil
|
|
}
|