https_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package docker
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "github.com/dotcloud/docker/api"
  6. "io/ioutil"
  7. "testing"
  8. "time"
  9. )
  10. const (
  11. errBadCertificate = "remote error: bad certificate"
  12. errCaUnknown = "x509: certificate signed by unknown authority"
  13. )
  14. func getTlsConfig(certFile, keyFile string, t *testing.T) *tls.Config {
  15. certPool := x509.NewCertPool()
  16. file, err := ioutil.ReadFile("fixtures/https/ca.pem")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. certPool.AppendCertsFromPEM(file)
  21. cert, err := tls.LoadX509KeyPair("fixtures/https/"+certFile, "fixtures/https/"+keyFile)
  22. if err != nil {
  23. t.Fatalf("Couldn't load X509 key pair: %s", err)
  24. }
  25. tlsConfig := &tls.Config{
  26. RootCAs: certPool,
  27. Certificates: []tls.Certificate{cert},
  28. }
  29. return tlsConfig
  30. }
  31. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  32. func TestHttpsInfo(t *testing.T) {
  33. cli := api.NewDockerCli(nil, ioutil.Discard, ioutil.Discard, testDaemonProto,
  34. testDaemonHttpsAddr, getTlsConfig("client-cert.pem", "client-key.pem", t))
  35. setTimeout(t, "Reading command output time out", 10*time.Second, func() {
  36. if err := cli.CmdInfo(); err != nil {
  37. t.Fatal(err)
  38. }
  39. })
  40. }
  41. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  42. // by using a rogue client certificate and checks that it fails with the expected error.
  43. func TestHttpsInfoRogueCert(t *testing.T) {
  44. cli := api.NewDockerCli(nil, ioutil.Discard, ioutil.Discard, testDaemonProto,
  45. testDaemonHttpsAddr, getTlsConfig("client-rogue-cert.pem", "client-rogue-key.pem", t))
  46. setTimeout(t, "Reading command output time out", 10*time.Second, func() {
  47. err := cli.CmdInfo()
  48. if err == nil {
  49. t.Fatal("Expected error but got nil")
  50. }
  51. if err.Error() != errBadCertificate {
  52. t.Fatalf("Expected error: %s, got instead: %s", errBadCertificate, err)
  53. }
  54. })
  55. }
  56. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  57. // which provides a rogue server certificate and checks that it fails with the expected error
  58. func TestHttpsInfoRogueServerCert(t *testing.T) {
  59. cli := api.NewDockerCli(nil, ioutil.Discard, ioutil.Discard, testDaemonProto,
  60. testDaemonRogueHttpsAddr, getTlsConfig("client-cert.pem", "client-key.pem", t))
  61. setTimeout(t, "Reading command output time out", 10*time.Second, func() {
  62. err := cli.CmdInfo()
  63. if err == nil {
  64. t.Fatal("Expected error but got nil")
  65. }
  66. if err.Error() != errCaUnknown {
  67. t.Fatalf("Expected error: %s, got instead: %s", errBadCertificate, err)
  68. }
  69. })
  70. }