https_test.go 2.5 KB

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