v2_curl_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "strings"
  19. "testing"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. )
  22. func TestV2CurlNoTLS(t *testing.T) { testCurlPutGet(t, &configNoTLS) }
  23. func TestV2CurlAutoTLS(t *testing.T) { testCurlPutGet(t, &configAutoTLS) }
  24. func TestV2CurlAllTLS(t *testing.T) { testCurlPutGet(t, &configTLS) }
  25. func TestV2CurlPeerTLS(t *testing.T) { testCurlPutGet(t, &configPeerTLS) }
  26. func TestV2CurlClientTLS(t *testing.T) { testCurlPutGet(t, &configClientTLS) }
  27. func TestV2CurlClientBoth(t *testing.T) { testCurlPutGet(t, &configClientBoth) }
  28. func testCurlPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
  29. defer testutil.AfterTest(t)
  30. // test doesn't use quorum gets, so ensure there are no followers to avoid
  31. // stale reads that will break the test
  32. cfg = configStandalone(*cfg)
  33. epc, err := newEtcdProcessCluster(cfg)
  34. if err != nil {
  35. t.Fatalf("could not start etcd process cluster (%v)", err)
  36. }
  37. defer func() {
  38. if err := epc.Close(); err != nil {
  39. t.Fatalf("error closing etcd processes (%v)", err)
  40. }
  41. }()
  42. var (
  43. expectPut = `{"action":"set","node":{"key":"/foo","value":"bar","`
  44. expectGet = `{"action":"get","node":{"key":"/foo","value":"bar","`
  45. )
  46. if err := cURLPut(epc, cURLReq{endpoint: "/v2/keys/foo", value: "bar", expected: expectPut}); err != nil {
  47. t.Fatalf("failed put with curl (%v)", err)
  48. }
  49. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo", expected: expectGet}); err != nil {
  50. t.Fatalf("failed get with curl (%v)", err)
  51. }
  52. if cfg.clientTLS == clientTLSAndNonTLS {
  53. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo", expected: expectGet, isTLS: true}); err != nil {
  54. t.Fatalf("failed get with curl (%v)", err)
  55. }
  56. }
  57. }
  58. func TestV2CurlIssue5182(t *testing.T) {
  59. defer testutil.AfterTest(t)
  60. epc := setupEtcdctlTest(t, &configNoTLS, false)
  61. defer func() {
  62. if err := epc.Close(); err != nil {
  63. t.Fatalf("error closing etcd processes (%v)", err)
  64. }
  65. }()
  66. expectPut := `{"action":"set","node":{"key":"/foo","value":"bar","`
  67. if err := cURLPut(epc, cURLReq{endpoint: "/v2/keys/foo", value: "bar", expected: expectPut}); err != nil {
  68. t.Fatal(err)
  69. }
  70. expectUserAdd := `{"user":"foo","roles":null}`
  71. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/users/foo", value: `{"user":"foo", "password":"pass"}`, expected: expectUserAdd}); err != nil {
  72. t.Fatal(err)
  73. }
  74. expectRoleAdd := `{"role":"foo","permissions":{"kv":{"read":["/foo/*"],"write":null}}`
  75. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/roles/foo", value: `{"role":"foo", "permissions": {"kv": {"read": ["/foo/*"]}}}`, expected: expectRoleAdd}); err != nil {
  76. t.Fatal(err)
  77. }
  78. expectUserUpdate := `{"user":"foo","roles":["foo"]}`
  79. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/users/foo", value: `{"user": "foo", "grant": ["foo"]}`, expected: expectUserUpdate}); err != nil {
  80. t.Fatal(err)
  81. }
  82. if err := etcdctlUserAdd(epc, "root", "a"); err != nil {
  83. t.Fatal(err)
  84. }
  85. if err := etcdctlAuthEnable(epc); err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "root", password: "a", expected: "bar"}); err != nil {
  89. t.Fatal(err)
  90. }
  91. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "foo", password: "pass", expected: "bar"}); err != nil {
  92. t.Fatal(err)
  93. }
  94. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "foo", password: "", expected: "bar"}); err != nil {
  95. if !strings.Contains(err.Error(), `The request requires user authentication`) {
  96. t.Fatalf("expected 'The request requires user authentication' error, got %v", err)
  97. }
  98. } else {
  99. t.Fatalf("expected 'The request requires user authentication' error")
  100. }
  101. }
  102. type cURLReq struct {
  103. username string
  104. password string
  105. isTLS bool
  106. timeout int
  107. endpoint string
  108. value string
  109. expected string
  110. header string
  111. metricsURLScheme string
  112. }
  113. // cURLPrefixArgs builds the beginning of a curl command for a given key
  114. // addressed to a random URL in the given cluster.
  115. func cURLPrefixArgs(clus *etcdProcessCluster, method string, req cURLReq) []string {
  116. var (
  117. cmdArgs = []string{"curl"}
  118. acurl = clus.procs[rand.Intn(clus.cfg.clusterSize)].Config().acurl
  119. )
  120. if req.metricsURLScheme != "https" {
  121. if req.isTLS {
  122. if clus.cfg.clientTLS != clientTLSAndNonTLS {
  123. panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
  124. }
  125. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  126. acurl = toTLS(clus.procs[rand.Intn(clus.cfg.clusterSize)].Config().acurl)
  127. } else if clus.cfg.clientTLS == clientTLS {
  128. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  129. }
  130. }
  131. if req.metricsURLScheme != "" {
  132. acurl = clus.procs[rand.Intn(clus.cfg.clusterSize)].EndpointsMetrics()[0]
  133. }
  134. ep := acurl + req.endpoint
  135. if req.username != "" || req.password != "" {
  136. cmdArgs = append(cmdArgs, "-L", "-u", fmt.Sprintf("%s:%s", req.username, req.password), ep)
  137. } else {
  138. cmdArgs = append(cmdArgs, "-L", ep)
  139. }
  140. if req.timeout != 0 {
  141. cmdArgs = append(cmdArgs, "-m", fmt.Sprintf("%d", req.timeout))
  142. }
  143. if req.header != "" {
  144. cmdArgs = append(cmdArgs, "-H", req.header)
  145. }
  146. switch method {
  147. case "POST", "PUT":
  148. dt := req.value
  149. if !strings.HasPrefix(dt, "{") { // for non-JSON value
  150. dt = "value=" + dt
  151. }
  152. cmdArgs = append(cmdArgs, "-X", method, "-d", dt)
  153. }
  154. return cmdArgs
  155. }
  156. func cURLPost(clus *etcdProcessCluster, req cURLReq) error {
  157. return spawnWithExpect(cURLPrefixArgs(clus, "POST", req), req.expected)
  158. }
  159. func cURLPut(clus *etcdProcessCluster, req cURLReq) error {
  160. return spawnWithExpect(cURLPrefixArgs(clus, "PUT", req), req.expected)
  161. }
  162. func cURLGet(clus *etcdProcessCluster, req cURLReq) error {
  163. return spawnWithExpect(cURLPrefixArgs(clus, "GET", req), req.expected)
  164. }