77 "crypto/x509"
88 "crypto/x509/pkix"
99 "encoding/pem"
10- "fmt"
1110 "io/ioutil"
1211 "math/big"
1312 "net"
@@ -16,18 +15,41 @@ import (
1615
1716 "errors"
1817
18+ "github.com/docker/machine/libmachine/auth"
1919 "github.com/docker/machine/libmachine/log"
2020)
2121
22- type ErrValidatingCert struct {
23- wrappedErr error
22+ var defaultGenerator = NewX509CertGenerator ()
23+
24+ type CertGenerator interface {
25+ GenerateCACertificate (certFile , keyFile , org string , bits int ) error
26+ GenerateCert (hosts []string , certFile , keyFile , caFile , caKeyFile , org string , bits int ) error
27+ ValidateCertificate (addr string , authOptions * auth.AuthOptions ) (bool , error )
28+ }
29+
30+ type X509CertGenerator struct {}
31+
32+ func NewX509CertGenerator () CertGenerator {
33+ return & X509CertGenerator {}
2434}
2535
26- func (e ErrValidatingCert ) Error () string {
27- return fmt .Sprintf ("There was an error validating the cert: %s" , e .wrappedErr )
36+ func GenerateCACertificate (certFile , keyFile , org string , bits int ) error {
37+ return defaultGenerator .GenerateCACertificate (certFile , keyFile , org , bits )
38+ }
39+
40+ func GenerateCert (hosts []string , certFile , keyFile , caFile , caKeyFile , org string , bits int ) error {
41+ return defaultGenerator .GenerateCert (hosts , certFile , keyFile , caFile , caKeyFile , org , bits )
2842}
2943
30- func getTLSConfig (caCert , cert , key []byte , allowInsecure bool ) (* tls.Config , error ) {
44+ func ValidateCertificate (addr string , authOptions * auth.AuthOptions ) (bool , error ) {
45+ return defaultGenerator .ValidateCertificate (addr , authOptions )
46+ }
47+
48+ func SetCertGenerator (cg CertGenerator ) {
49+ defaultGenerator = cg
50+ }
51+
52+ func (xcg * X509CertGenerator ) getTLSConfig (caCert , cert , key []byte , allowInsecure bool ) (* tls.Config , error ) {
3153 // TLS config
3254 var tlsConfig tls.Config
3355 tlsConfig .InsecureSkipVerify = allowInsecure
@@ -48,7 +70,7 @@ func getTLSConfig(caCert, cert, key []byte, allowInsecure bool) (*tls.Config, er
4870 return & tlsConfig , nil
4971}
5072
51- func newCertificate (org string ) (* x509.Certificate , error ) {
73+ func ( xcg * X509CertGenerator ) newCertificate (org string ) (* x509.Certificate , error ) {
5274 now := time .Now ()
5375 // need to set notBefore slightly in the past to account for time
5476 // skew in the VMs otherwise the certs sometimes are not yet valid
@@ -78,8 +100,8 @@ func newCertificate(org string) (*x509.Certificate, error) {
78100// GenerateCACertificate generates a new certificate authority from the specified org
79101// and bit size and stores the resulting certificate and key file
80102// in the arguments.
81- func GenerateCACertificate (certFile , keyFile , org string , bits int ) error {
82- template , err := newCertificate (org )
103+ func ( xcg * X509CertGenerator ) GenerateCACertificate (certFile , keyFile , org string , bits int ) error {
104+ template , err := xcg . newCertificate (org )
83105 if err != nil {
84106 return err
85107 }
@@ -123,8 +145,8 @@ func GenerateCACertificate(certFile, keyFile, org string, bits int) error {
123145// certificate authority files and stores the result in the certificate
124146// file and key provided. The provided host names are set to the
125147// appropriate certificate fields.
126- func GenerateCert (hosts []string , certFile , keyFile , caFile , caKeyFile , org string , bits int ) error {
127- template , err := newCertificate (org )
148+ func ( xcg * X509CertGenerator ) GenerateCert (hosts []string , certFile , keyFile , caFile , caKeyFile , org string , bits int ) error {
149+ template , err := xcg . newCertificate (org )
128150 if err != nil {
129151 return err
130152 }
@@ -183,28 +205,32 @@ func GenerateCert(hosts []string, certFile, keyFile, caFile, caKeyFile, org stri
183205}
184206
185207// ValidateCertificate validate the certificate installed on the vm.
186- func ValidateCertificate (addr , caCertPath , serverCertPath , serverKeyPath string ) (bool , error ) {
208+ func (xcg * X509CertGenerator ) ValidateCertificate (addr string , authOptions * auth.AuthOptions ) (bool , error ) {
209+ caCertPath := authOptions .CaCertPath
210+ serverCertPath := authOptions .ServerCertPath
211+ serverKeyPath := authOptions .ServerKeyPath
212+
187213 log .Debugf ("Reading CA certificate from %s" , caCertPath )
188214 caCert , err := ioutil .ReadFile (caCertPath )
189215 if err != nil {
190- return false , ErrValidatingCert { err }
216+ return false , err
191217 }
192218
193219 log .Debugf ("Reading server certificate from %s" , serverCertPath )
194220 serverCert , err := ioutil .ReadFile (serverCertPath )
195221 if err != nil {
196- return false , ErrValidatingCert { err }
222+ return false , err
197223 }
198224
199225 log .Debugf ("Reading server key from %s" , serverKeyPath )
200226 serverKey , err := ioutil .ReadFile (serverKeyPath )
201227 if err != nil {
202- return false , ErrValidatingCert { err }
228+ return false , err
203229 }
204230
205- tlsConfig , err := getTLSConfig (caCert , serverCert , serverKey , false )
231+ tlsConfig , err := xcg . getTLSConfig (caCert , serverCert , serverKey , false )
206232 if err != nil {
207- return false , ErrValidatingCert { err }
233+ return false , err
208234 }
209235
210236 dialer := & net.Dialer {
@@ -213,8 +239,7 @@ func ValidateCertificate(addr, caCertPath, serverCertPath, serverKeyPath string)
213239
214240 _ , err = tls .DialWithDialer (dialer , "tcp" , addr , tlsConfig )
215241 if err != nil {
216- log .Debugf ("Certificates are not valid: %s" , err )
217- return false , nil
242+ return false , err
218243 }
219244
220245 return true , nil
0 commit comments