2018-02-05 21:05:59 +00:00
|
|
|
package authorization // import "github.com/docker/docker/pkg/authorization"
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"math/big"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-15 08:05:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeerCertificateMarshalJSON(t *testing.T) {
|
|
|
|
template := &x509.Certificate{
|
2020-02-07 13:39:24 +00:00
|
|
|
IsCA: true,
|
2017-06-15 08:05:40 +00:00
|
|
|
BasicConstraintsValid: true,
|
|
|
|
SubjectKeyId: []byte{1, 2, 3},
|
|
|
|
SerialNumber: big.NewInt(1234),
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Country: []string{"Earth"},
|
|
|
|
Organization: []string{"Mother Nature"},
|
|
|
|
},
|
|
|
|
NotBefore: time.Now(),
|
|
|
|
NotAfter: time.Now().AddDate(5, 5, 5),
|
|
|
|
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
}
|
|
|
|
// generate private key
|
|
|
|
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
publickey := &privatekey.PublicKey
|
|
|
|
|
|
|
|
// create a self-signed certificate. template = parent
|
|
|
|
var parent = template
|
|
|
|
raw, err := x509.CreateCertificate(rand.Reader, template, parent, publickey, privatekey)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(raw)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
var certs = []*x509.Certificate{cert}
|
|
|
|
addr := "www.authz.com/auth"
|
2019-10-12 18:43:46 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, addr, nil)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
req.RequestURI = addr
|
|
|
|
req.TLS = &tls.ConnectionState{}
|
|
|
|
req.TLS.PeerCertificates = certs
|
|
|
|
req.Header.Add("header", "value")
|
|
|
|
|
|
|
|
for _, c := range req.TLS.PeerCertificates {
|
|
|
|
pcObj := PeerCertificate(*c)
|
|
|
|
|
|
|
|
t.Run("Marshalling :", func(t *testing.T) {
|
|
|
|
raw, err = pcObj.MarshalJSON()
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, raw != nil)
|
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnMarshalling :", func(t *testing.T) {
|
|
|
|
err := pcObj.UnmarshalJSON(raw)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, is.Nil(err))
|
|
|
|
assert.Equal(t, "Earth", pcObj.Subject.Country[0])
|
|
|
|
assert.Equal(t, true, pcObj.IsCA)
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|