https_test.go 2.8 KB

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