1
0
mirror of https://github.com/docker/docs.git synced 2026-10-29 20:39:05 +03:00

Add cryptoservice.GenerateTestingCertificate

Various tests have been calling trustmanager.NewCertificate and
open-coding most of cryptoservice.GenerateCertificate.  So, add
cryptoservice.GenerateTestingCertificate.  It differs only by using
crypto.Signer instead of data.PrivateKey because the tests
have a crypto.Signer more frequently available, and converting
from data.PrivateKey to crypto.Signer is easier than the other way.

This will make it easier to add policy parameters which the tests don't
care about to trustmanager.NewCertificate and
cryptoservice.GenerateCertificate in the future.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2015-12-02 17:05:07 +01:00
parent 74d327f273
commit d5c080ae9c
4 changed files with 15 additions and 19 deletions

View File

@@ -242,7 +242,7 @@ func filestoreWithTwoCerts(t *testing.T, gun, keyAlg string) (
key, _, err := fileKeyStore.GetKey(pubKey.ID())
assert.NoError(t, err)
cert, err := cryptoservice.GenerateCertificate(key, gun)
cert, err := cryptoservice.GenerateTestingCertificate(key.CryptoSigner(), gun)
assert.NoError(t, err)
certificates[i] = cert

View File

@@ -1,6 +1,7 @@
package cryptoservice
import (
"crypto"
"crypto/rand"
"crypto/x509"
"fmt"
@@ -16,6 +17,15 @@ func GenerateCertificate(rootKey data.PrivateKey, gun string) (*x509.Certificate
return nil, fmt.Errorf("key type not supported for Certificate generation: %s\n", rootKey.Algorithm())
}
return generateCertificate(signer, gun)
}
// GenerateTestingCertificate generates an X509 Certificate from a template, given a GUN.
func GenerateTestingCertificate(signer crypto.Signer, gun string) (*x509.Certificate, error) {
return generateCertificate(signer, gun)
}
func generateCertificate(signer crypto.Signer, gun string) (*x509.Certificate, error) {
template, err := trustmanager.NewCertificate(gun)
if err != nil {
return nil, fmt.Errorf("failed to create the certificate template for: %s (%v)", gun, err)

View File

@@ -2,11 +2,11 @@ package signed
import (
"crypto/rand"
"crypto/x509"
"encoding/pem"
"io"
"testing"
"github.com/docker/notary/cryptoservice"
"github.com/docker/notary/trustmanager"
"github.com/docker/notary/tuf/data"
"github.com/stretchr/testify/assert"
@@ -254,15 +254,7 @@ func TestSignWithX509(t *testing.T) {
assert.NoError(t, err)
// make a RSA x509 key
template, err := trustmanager.NewCertificate("test")
assert.NoError(t, err)
signer := privKey.CryptoSigner()
derBytes, err := x509.CreateCertificate(
rand.Reader, template, template, signer.Public(), signer)
assert.NoError(t, err)
cert, err := x509.ParseCertificate(derBytes)
cert, err := cryptoservice.GenerateTestingCertificate(privKey.CryptoSigner(), "test")
assert.NoError(t, err)
tufRSAx509Key := trustmanager.CertToKey(cert)

View File

@@ -7,11 +7,11 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"
"testing"
"github.com/docker/notary/cryptoservice"
"github.com/docker/notary/trustmanager"
"github.com/stretchr/testify/assert"
)
@@ -33,15 +33,9 @@ func generateMultiCert(t *testing.T) string {
assert.NoError(t, err)
ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
template, err := trustmanager.NewCertificate("gun")
assert.NoError(t, err)
for _, key := range []crypto.Signer{rsaKey, ecKey} {
derBytes, err := x509.CreateCertificate(
rand.Reader, template, template, key.Public(), key)
assert.NoError(t, err)
cert, err := x509.ParseCertificate(derBytes)
cert, err := cryptoservice.GenerateTestingCertificate(key, "gun")
assert.NoError(t, err)
pemBytes := trustmanager.CertToPEM(cert)