v3_curl_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. "encoding/json"
  17. "path"
  18. "testing"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "github.com/grpc-ecosystem/grpc-gateway/runtime"
  22. )
  23. // TODO: remove /v3alpha tests in 3.4 release
  24. func TestV3CurlPutGetNoTLSAlpha(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS, "/v3alpha") }
  25. func TestV3CurlPutGetNoTLSBeta(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS, "/v3beta") }
  26. func TestV3CurlPutGetAutoTLSAlpha(t *testing.T) {
  27. testCurlPutGetGRPCGateway(t, &configAutoTLS, "/v3alpha")
  28. }
  29. func TestV3CurlPutGetAutoTLSBeta(t *testing.T) {
  30. testCurlPutGetGRPCGateway(t, &configAutoTLS, "/v3beta")
  31. }
  32. func TestV3CurlPutGetAllTLSAlpha(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS, "/v3alpha") }
  33. func TestV3CurlPutGetAllTLSBeta(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS, "/v3beta") }
  34. func TestV3CurlPutGetPeerTLSAlpha(t *testing.T) {
  35. testCurlPutGetGRPCGateway(t, &configPeerTLS, "/v3alpha")
  36. }
  37. func TestV3CurlPutGetPeerTLSBeta(t *testing.T) {
  38. testCurlPutGetGRPCGateway(t, &configPeerTLS, "/v3beta")
  39. }
  40. func TestV3CurlPutGetClientTLSAlpha(t *testing.T) {
  41. testCurlPutGetGRPCGateway(t, &configClientTLS, "/v3alpha")
  42. }
  43. func TestV3CurlPutGetClientTLSBeta(t *testing.T) {
  44. testCurlPutGetGRPCGateway(t, &configClientTLS, "/v3beta")
  45. }
  46. func testCurlPutGetGRPCGateway(t *testing.T, cfg *etcdProcessClusterConfig, pathPrefix string) {
  47. defer testutil.AfterTest(t)
  48. epc, err := newEtcdProcessCluster(cfg)
  49. if err != nil {
  50. t.Fatalf("could not start etcd process cluster (%v)", err)
  51. }
  52. defer func() {
  53. if cerr := epc.Close(); err != nil {
  54. t.Fatalf("error closing etcd processes (%v)", cerr)
  55. }
  56. }()
  57. var (
  58. key = []byte("foo")
  59. value = []byte("bar") // this will be automatically base64-encoded by Go
  60. expectPut = `"revision":"`
  61. expectGet = `"value":"`
  62. )
  63. putData, err := json.Marshal(&pb.PutRequest{
  64. Key: key,
  65. Value: value,
  66. })
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. rangeData, err := json.Marshal(&pb.RangeRequest{
  71. Key: key,
  72. })
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putData), expected: expectPut}); err != nil {
  77. t.Fatalf("failed put with curl (%v)", err)
  78. }
  79. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/range"), value: string(rangeData), expected: expectGet}); err != nil {
  80. t.Fatalf("failed get with curl (%v)", err)
  81. }
  82. if cfg.clientTLS == clientTLSAndNonTLS {
  83. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/range"), value: string(rangeData), expected: expectGet, isTLS: true}); err != nil {
  84. t.Fatalf("failed get with curl (%v)", err)
  85. }
  86. }
  87. }
  88. func TestV3CurlWatchAlpha(t *testing.T) { testV3CurlWatch(t, "/v3alpha") }
  89. func TestV3CurlWatchBeta(t *testing.T) { testV3CurlWatch(t, "/v3beta") }
  90. func testV3CurlWatch(t *testing.T, pathPrefix string) {
  91. defer testutil.AfterTest(t)
  92. epc, err := newEtcdProcessCluster(&configNoTLS)
  93. if err != nil {
  94. t.Fatalf("could not start etcd process cluster (%v)", err)
  95. }
  96. defer func() {
  97. if cerr := epc.Close(); err != nil {
  98. t.Fatalf("error closing etcd processes (%v)", cerr)
  99. }
  100. }()
  101. // store "bar" into "foo"
  102. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), expected: "revision"}); err != nil {
  107. t.Fatalf("failed put with curl (%v)", err)
  108. }
  109. // watch for first update to "foo"
  110. wcr := &pb.WatchCreateRequest{Key: []byte("foo"), StartRevision: 1}
  111. wreq, err := json.Marshal(wcr)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. // marshaling the grpc to json gives:
  116. // "{"RequestUnion":{"CreateRequest":{"key":"Zm9v","start_revision":1}}}"
  117. // but the gprc-gateway expects a different format..
  118. wstr := `{"create_request" : ` + string(wreq) + "}"
  119. // expects "bar", timeout after 2 seconds since stream waits forever
  120. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/watch"), value: wstr, expected: `"YmFy"`, timeout: 2}); err != nil {
  121. t.Fatal(err)
  122. }
  123. }
  124. func TestV3CurlTxnAlpha(t *testing.T) { testV3CurlTxn(t, "/v3alpha") }
  125. func TestV3CurlTxnBeta(t *testing.T) { testV3CurlTxn(t, "/v3beta") }
  126. func testV3CurlTxn(t *testing.T, pathPrefix string) {
  127. defer testutil.AfterTest(t)
  128. epc, err := newEtcdProcessCluster(&configNoTLS)
  129. if err != nil {
  130. t.Fatalf("could not start etcd process cluster (%v)", err)
  131. }
  132. defer func() {
  133. if cerr := epc.Close(); err != nil {
  134. t.Fatalf("error closing etcd processes (%v)", cerr)
  135. }
  136. }()
  137. txn := &pb.TxnRequest{
  138. Compare: []*pb.Compare{
  139. {
  140. Key: []byte("foo"),
  141. Result: pb.Compare_EQUAL,
  142. Target: pb.Compare_CREATE,
  143. TargetUnion: &pb.Compare_CreateRevision{0},
  144. },
  145. },
  146. Success: []*pb.RequestOp{
  147. {
  148. Request: &pb.RequestOp_RequestPut{
  149. RequestPut: &pb.PutRequest{
  150. Key: []byte("foo"),
  151. Value: []byte("bar"),
  152. },
  153. },
  154. },
  155. },
  156. }
  157. m := &runtime.JSONPb{}
  158. jsonDat, jerr := m.Marshal(txn)
  159. if jerr != nil {
  160. t.Fatal(jerr)
  161. }
  162. expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]`
  163. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/txn"), value: string(jsonDat), expected: expected}); err != nil {
  164. t.Fatalf("failed txn with curl (%v)", err)
  165. }
  166. // was crashing etcd server
  167. malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}`
  168. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/txn"), value: malformed, expected: "error"}); err != nil {
  169. t.Fatalf("failed put with curl (%v)", err)
  170. }
  171. }
  172. func TestV3CurlAuthAlpha(t *testing.T) { testV3CurlAuth(t, "/v3alpha") }
  173. func TestV3CurlAuthBeta(t *testing.T) { testV3CurlAuth(t, "/v3beta") }
  174. func testV3CurlAuth(t *testing.T, pathPrefix string) {
  175. defer testutil.AfterTest(t)
  176. epc, err := newEtcdProcessCluster(&configNoTLS)
  177. if err != nil {
  178. t.Fatalf("could not start etcd process cluster (%v)", err)
  179. }
  180. defer func() {
  181. if cerr := epc.Close(); err != nil {
  182. t.Fatalf("error closing etcd processes (%v)", cerr)
  183. }
  184. }()
  185. // create root user
  186. userreq, err := json.Marshal(&pb.AuthUserAddRequest{Name: string("root"), Password: string("toor")})
  187. testutil.AssertNil(t, err)
  188. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/add"), value: string(userreq), expected: "revision"}); err != nil {
  189. t.Fatalf("failed add user with curl (%v)", err)
  190. }
  191. // create root role
  192. rolereq, err := json.Marshal(&pb.AuthRoleAddRequest{Name: string("root")})
  193. testutil.AssertNil(t, err)
  194. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/role/add"), value: string(rolereq), expected: "revision"}); err != nil {
  195. t.Fatalf("failed create role with curl (%v)", err)
  196. }
  197. // grant root role
  198. grantrolereq, err := json.Marshal(&pb.AuthUserGrantRoleRequest{User: string("root"), Role: string("root")})
  199. testutil.AssertNil(t, err)
  200. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/grant"), value: string(grantrolereq), expected: "revision"}); err != nil {
  201. t.Fatalf("failed grant role with curl (%v)", err)
  202. }
  203. // enable auth
  204. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/enable"), value: string("{}"), expected: "revision"}); err != nil {
  205. t.Fatalf("failed enable auth with curl (%v)", err)
  206. }
  207. // put "bar" into "foo"
  208. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  209. testutil.AssertNil(t, err)
  210. // fail put no auth
  211. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), expected: "error"}); err != nil {
  212. t.Fatalf("failed no auth put with curl (%v)", err)
  213. }
  214. // auth request
  215. authreq, err := json.Marshal(&pb.AuthenticateRequest{Name: string("root"), Password: string("toor")})
  216. testutil.AssertNil(t, err)
  217. var (
  218. authHeader string
  219. cmdArgs []string
  220. lineFunc = func(txt string) bool { return true }
  221. )
  222. cmdArgs = cURLPrefixArgs(epc, "POST", cURLReq{endpoint: path.Join(pathPrefix, "/auth/authenticate"), value: string(authreq)})
  223. proc, err := spawnCmd(cmdArgs)
  224. testutil.AssertNil(t, err)
  225. cURLRes, err := proc.ExpectFunc(lineFunc)
  226. testutil.AssertNil(t, err)
  227. authRes := make(map[string]interface{})
  228. testutil.AssertNil(t, json.Unmarshal([]byte(cURLRes), &authRes))
  229. token, ok := authRes["token"].(string)
  230. if !ok {
  231. t.Fatalf("failed invalid token in authenticate response with curl")
  232. }
  233. authHeader = "Authorization : " + token
  234. // put with auth
  235. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), header: authHeader, expected: "revision"}); err != nil {
  236. t.Fatalf("failed auth put with curl (%v)", err)
  237. }
  238. }