main_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // +build !windows
  2. package authz // import "github.com/docker/docker/integration/plugin/authz"
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "strings"
  11. "testing"
  12. "github.com/docker/docker/internal/test/daemon"
  13. "github.com/docker/docker/internal/test/environment"
  14. "github.com/docker/docker/pkg/authorization"
  15. "github.com/docker/docker/pkg/plugins"
  16. "github.com/gotestyourself/gotestyourself/skip"
  17. )
  18. var (
  19. testEnv *environment.Execution
  20. d *daemon.Daemon
  21. server *httptest.Server
  22. )
  23. func TestMain(m *testing.M) {
  24. var err error
  25. testEnv, err = environment.New()
  26. if err != nil {
  27. fmt.Println(err)
  28. os.Exit(1)
  29. }
  30. err = environment.EnsureFrozenImagesLinux(testEnv)
  31. if err != nil {
  32. fmt.Println(err)
  33. os.Exit(1)
  34. }
  35. testEnv.Print()
  36. setupSuite()
  37. exitCode := m.Run()
  38. teardownSuite()
  39. os.Exit(exitCode)
  40. }
  41. func setupTest(t *testing.T) func() {
  42. skip.IfCondition(t, testEnv.IsRemoteDaemon(), "cannot run daemon when remote daemon")
  43. environment.ProtectAll(t, testEnv)
  44. d = daemon.New(t, daemon.WithExperimental)
  45. return func() {
  46. if d != nil {
  47. d.Stop(t)
  48. }
  49. testEnv.Clean(t)
  50. }
  51. }
  52. func setupSuite() {
  53. mux := http.NewServeMux()
  54. server = httptest.NewServer(mux)
  55. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  56. b, err := json.Marshal(plugins.Manifest{Implements: []string{authorization.AuthZApiImplements}})
  57. if err != nil {
  58. panic("could not marshal json for /Plugin.Activate: " + err.Error())
  59. }
  60. w.Write(b)
  61. })
  62. mux.HandleFunc("/AuthZPlugin.AuthZReq", func(w http.ResponseWriter, r *http.Request) {
  63. defer r.Body.Close()
  64. body, err := ioutil.ReadAll(r.Body)
  65. if err != nil {
  66. panic("could not read body for /AuthZPlugin.AuthZReq: " + err.Error())
  67. }
  68. authReq := authorization.Request{}
  69. err = json.Unmarshal(body, &authReq)
  70. if err != nil {
  71. panic("could not unmarshal json for /AuthZPlugin.AuthZReq: " + err.Error())
  72. }
  73. assertBody(authReq.RequestURI, authReq.RequestHeaders, authReq.RequestBody)
  74. assertAuthHeaders(authReq.RequestHeaders)
  75. // Count only server version api
  76. if strings.HasSuffix(authReq.RequestURI, serverVersionAPI) {
  77. ctrl.versionReqCount++
  78. }
  79. ctrl.requestsURIs = append(ctrl.requestsURIs, authReq.RequestURI)
  80. reqRes := ctrl.reqRes
  81. if isAllowed(authReq.RequestURI) {
  82. reqRes = authorization.Response{Allow: true}
  83. }
  84. if reqRes.Err != "" {
  85. w.WriteHeader(http.StatusInternalServerError)
  86. }
  87. b, err := json.Marshal(reqRes)
  88. if err != nil {
  89. panic("could not marshal json for /AuthZPlugin.AuthZReq: " + err.Error())
  90. }
  91. ctrl.reqUser = authReq.User
  92. w.Write(b)
  93. })
  94. mux.HandleFunc("/AuthZPlugin.AuthZRes", func(w http.ResponseWriter, r *http.Request) {
  95. defer r.Body.Close()
  96. body, err := ioutil.ReadAll(r.Body)
  97. if err != nil {
  98. panic("could not read body for /AuthZPlugin.AuthZRes: " + err.Error())
  99. }
  100. authReq := authorization.Request{}
  101. err = json.Unmarshal(body, &authReq)
  102. if err != nil {
  103. panic("could not unmarshal json for /AuthZPlugin.AuthZRes: " + err.Error())
  104. }
  105. assertBody(authReq.RequestURI, authReq.ResponseHeaders, authReq.ResponseBody)
  106. assertAuthHeaders(authReq.ResponseHeaders)
  107. // Count only server version api
  108. if strings.HasSuffix(authReq.RequestURI, serverVersionAPI) {
  109. ctrl.versionResCount++
  110. }
  111. resRes := ctrl.resRes
  112. if isAllowed(authReq.RequestURI) {
  113. resRes = authorization.Response{Allow: true}
  114. }
  115. if resRes.Err != "" {
  116. w.WriteHeader(http.StatusInternalServerError)
  117. }
  118. b, err := json.Marshal(resRes)
  119. if err != nil {
  120. panic("could not marshal json for /AuthZPlugin.AuthZRes: " + err.Error())
  121. }
  122. ctrl.resUser = authReq.User
  123. w.Write(b)
  124. })
  125. }
  126. func teardownSuite() {
  127. if server == nil {
  128. return
  129. }
  130. server.Close()
  131. }
  132. // assertAuthHeaders validates authentication headers are removed
  133. func assertAuthHeaders(headers map[string]string) error {
  134. for k := range headers {
  135. if strings.Contains(strings.ToLower(k), "auth") || strings.Contains(strings.ToLower(k), "x-registry") {
  136. panic(fmt.Sprintf("Found authentication headers in request '%v'", headers))
  137. }
  138. }
  139. return nil
  140. }
  141. // assertBody asserts that body is removed for non text/json requests
  142. func assertBody(requestURI string, headers map[string]string, body []byte) {
  143. if strings.Contains(strings.ToLower(requestURI), "auth") && len(body) > 0 {
  144. panic("Body included for authentication endpoint " + string(body))
  145. }
  146. for k, v := range headers {
  147. if strings.EqualFold(k, "Content-Type") && strings.HasPrefix(v, "text/") || v == "application/json" {
  148. return
  149. }
  150. }
  151. if len(body) > 0 {
  152. panic(fmt.Sprintf("Body included while it should not (Headers: '%v')", headers))
  153. }
  154. }