v2_curl_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "os"
  19. "strings"
  20. "testing"
  21. "go.etcd.io/etcd/pkg/testutil"
  22. )
  23. func TestV2CurlNoTLS(t *testing.T) { testCurlPutGet(t, &configNoTLS) }
  24. func TestV2CurlAutoTLS(t *testing.T) { testCurlPutGet(t, &configAutoTLS) }
  25. func TestV2CurlAllTLS(t *testing.T) { testCurlPutGet(t, &configTLS) }
  26. func TestV2CurlPeerTLS(t *testing.T) { testCurlPutGet(t, &configPeerTLS) }
  27. func TestV2CurlClientTLS(t *testing.T) { testCurlPutGet(t, &configClientTLS) }
  28. func TestV2CurlClientBoth(t *testing.T) { testCurlPutGet(t, &configClientBoth) }
  29. func testCurlPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
  30. defer testutil.AfterTest(t)
  31. // test doesn't use quorum gets, so ensure there are no followers to avoid
  32. // stale reads that will break the test
  33. cfg = configStandalone(*cfg)
  34. epc, err := newEtcdProcessCluster(cfg)
  35. if err != nil {
  36. t.Fatalf("could not start etcd process cluster (%v)", err)
  37. }
  38. defer func() {
  39. if err := epc.Close(); err != nil {
  40. t.Fatalf("error closing etcd processes (%v)", err)
  41. }
  42. }()
  43. var (
  44. expectPut = `{"action":"set","node":{"key":"/foo","value":"bar","`
  45. expectGet = `{"action":"get","node":{"key":"/foo","value":"bar","`
  46. )
  47. if err := cURLPut(epc, cURLReq{endpoint: "/v2/keys/foo", value: "bar", expected: expectPut}); err != nil {
  48. t.Fatalf("failed put with curl (%v)", err)
  49. }
  50. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo", expected: expectGet}); err != nil {
  51. t.Fatalf("failed get with curl (%v)", err)
  52. }
  53. if cfg.clientTLS == clientTLSAndNonTLS {
  54. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo", expected: expectGet, isTLS: true}); err != nil {
  55. t.Fatalf("failed get with curl (%v)", err)
  56. }
  57. }
  58. }
  59. func TestV2CurlIssue5182(t *testing.T) {
  60. os.Setenv("ETCDCTL_API", "2")
  61. defer os.Unsetenv("ETCDCTL_API")
  62. defer testutil.AfterTest(t)
  63. epc := setupEtcdctlTest(t, &configNoTLS, false)
  64. defer func() {
  65. if err := epc.Close(); err != nil {
  66. t.Fatalf("error closing etcd processes (%v)", err)
  67. }
  68. }()
  69. expectPut := `{"action":"set","node":{"key":"/foo","value":"bar","`
  70. if err := cURLPut(epc, cURLReq{endpoint: "/v2/keys/foo", value: "bar", expected: expectPut}); err != nil {
  71. t.Fatal(err)
  72. }
  73. expectUserAdd := `{"user":"foo","roles":null}`
  74. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/users/foo", value: `{"user":"foo", "password":"pass"}`, expected: expectUserAdd}); err != nil {
  75. t.Fatal(err)
  76. }
  77. expectRoleAdd := `{"role":"foo","permissions":{"kv":{"read":["/foo/*"],"write":null}}`
  78. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/roles/foo", value: `{"role":"foo", "permissions": {"kv": {"read": ["/foo/*"]}}}`, expected: expectRoleAdd}); err != nil {
  79. t.Fatal(err)
  80. }
  81. expectUserUpdate := `{"user":"foo","roles":["foo"]}`
  82. if err := cURLPut(epc, cURLReq{endpoint: "/v2/auth/users/foo", value: `{"user": "foo", "grant": ["foo"]}`, expected: expectUserUpdate}); err != nil {
  83. t.Fatal(err)
  84. }
  85. if err := etcdctlUserAdd(epc, "root", "a"); err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := etcdctlAuthEnable(epc); err != nil {
  89. t.Fatal(err)
  90. }
  91. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "root", password: "a", expected: "bar"}); err != nil {
  92. t.Fatal(err)
  93. }
  94. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "foo", password: "pass", expected: "bar"}); err != nil {
  95. t.Fatal(err)
  96. }
  97. if err := cURLGet(epc, cURLReq{endpoint: "/v2/keys/foo/", username: "foo", password: "", expected: "bar"}); err != nil {
  98. if !strings.Contains(err.Error(), `The request requires user authentication`) {
  99. t.Fatalf("expected 'The request requires user authentication' error, got %v", err)
  100. }
  101. } else {
  102. t.Fatalf("expected 'The request requires user authentication' error")
  103. }
  104. }
  105. type cURLReq struct {
  106. username string
  107. password string
  108. isTLS bool
  109. timeout int
  110. endpoint string
  111. value string
  112. expected string
  113. header string
  114. metricsURLScheme string
  115. ciphers string
  116. }
  117. // cURLPrefixArgs builds the beginning of a curl command for a given key
  118. // addressed to a random URL in the given cluster.
  119. func cURLPrefixArgs(clus *etcdProcessCluster, method string, req cURLReq) []string {
  120. var (
  121. cmdArgs = []string{"curl"}
  122. acurl = clus.procs[rand.Intn(clus.cfg.clusterSize)].Config().acurl
  123. )
  124. if req.metricsURLScheme != "https" {
  125. if req.isTLS {
  126. if clus.cfg.clientTLS != clientTLSAndNonTLS {
  127. panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
  128. }
  129. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  130. acurl = toTLS(clus.procs[rand.Intn(clus.cfg.clusterSize)].Config().acurl)
  131. } else if clus.cfg.clientTLS == clientTLS {
  132. if !clus.cfg.noCN {
  133. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  134. } else {
  135. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath3, "--key", privateKeyPath3)
  136. }
  137. }
  138. }
  139. if req.metricsURLScheme != "" {
  140. acurl = clus.procs[rand.Intn(clus.cfg.clusterSize)].EndpointsMetrics()[0]
  141. }
  142. ep := acurl + req.endpoint
  143. if req.username != "" || req.password != "" {
  144. cmdArgs = append(cmdArgs, "-L", "-u", fmt.Sprintf("%s:%s", req.username, req.password), ep)
  145. } else {
  146. cmdArgs = append(cmdArgs, "-L", ep)
  147. }
  148. if req.timeout != 0 {
  149. cmdArgs = append(cmdArgs, "-m", fmt.Sprintf("%d", req.timeout))
  150. }
  151. if req.header != "" {
  152. cmdArgs = append(cmdArgs, "-H", req.header)
  153. }
  154. if req.ciphers != "" {
  155. cmdArgs = append(cmdArgs, "--ciphers", req.ciphers)
  156. }
  157. switch method {
  158. case "POST", "PUT":
  159. dt := req.value
  160. if !strings.HasPrefix(dt, "{") { // for non-JSON value
  161. dt = "value=" + dt
  162. }
  163. cmdArgs = append(cmdArgs, "-X", method, "-d", dt)
  164. }
  165. return cmdArgs
  166. }
  167. func cURLPost(clus *etcdProcessCluster, req cURLReq) error {
  168. return spawnWithExpect(cURLPrefixArgs(clus, "POST", req), req.expected)
  169. }
  170. func cURLPut(clus *etcdProcessCluster, req cURLReq) error {
  171. return spawnWithExpect(cURLPrefixArgs(clus, "PUT", req), req.expected)
  172. }
  173. func cURLGet(clus *etcdProcessCluster, req cURLReq) error {
  174. return spawnWithExpect(cURLPrefixArgs(clus, "GET", req), req.expected)
  175. }