v3_curl_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/base64"
  17. "encoding/json"
  18. "path"
  19. "strconv"
  20. "testing"
  21. epb "github.com/coreos/etcd/etcdserver/api/v3election/v3electionpb"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "github.com/grpc-ecosystem/grpc-gateway/runtime"
  25. )
  26. // TODO: remove /v3alpha tests in 3.4 release
  27. func TestV3CurlPutGetNoTLSAlpha(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS, "/v3alpha") }
  28. func TestV3CurlPutGetNoTLSBeta(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS, "/v3beta") }
  29. func TestV3CurlPutGetAutoTLSAlpha(t *testing.T) {
  30. testCurlPutGetGRPCGateway(t, &configAutoTLS, "/v3alpha")
  31. }
  32. func TestV3CurlPutGetAutoTLSBeta(t *testing.T) {
  33. testCurlPutGetGRPCGateway(t, &configAutoTLS, "/v3beta")
  34. }
  35. func TestV3CurlPutGetAllTLSAlpha(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS, "/v3alpha") }
  36. func TestV3CurlPutGetAllTLSBeta(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS, "/v3beta") }
  37. func TestV3CurlPutGetPeerTLSAlpha(t *testing.T) {
  38. testCurlPutGetGRPCGateway(t, &configPeerTLS, "/v3alpha")
  39. }
  40. func TestV3CurlPutGetPeerTLSBeta(t *testing.T) {
  41. testCurlPutGetGRPCGateway(t, &configPeerTLS, "/v3beta")
  42. }
  43. func TestV3CurlPutGetClientTLSAlpha(t *testing.T) {
  44. testCurlPutGetGRPCGateway(t, &configClientTLS, "/v3alpha")
  45. }
  46. func TestV3CurlPutGetClientTLSBeta(t *testing.T) {
  47. testCurlPutGetGRPCGateway(t, &configClientTLS, "/v3beta")
  48. }
  49. func testCurlPutGetGRPCGateway(t *testing.T, cfg *etcdProcessClusterConfig, pathPrefix string) {
  50. defer testutil.AfterTest(t)
  51. epc, err := newEtcdProcessCluster(cfg)
  52. if err != nil {
  53. t.Fatalf("could not start etcd process cluster (%v)", err)
  54. }
  55. defer func() {
  56. if cerr := epc.Close(); err != nil {
  57. t.Fatalf("error closing etcd processes (%v)", cerr)
  58. }
  59. }()
  60. var (
  61. key = []byte("foo")
  62. value = []byte("bar") // this will be automatically base64-encoded by Go
  63. expectPut = `"revision":"`
  64. expectGet = `"value":"`
  65. )
  66. putData, err := json.Marshal(&pb.PutRequest{
  67. Key: key,
  68. Value: value,
  69. })
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. rangeData, err := json.Marshal(&pb.RangeRequest{
  74. Key: key,
  75. })
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putData), expected: expectPut}); err != nil {
  80. t.Fatalf("failed put with curl (%v)", err)
  81. }
  82. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/range"), value: string(rangeData), expected: expectGet}); err != nil {
  83. t.Fatalf("failed get with curl (%v)", err)
  84. }
  85. if cfg.clientTLS == clientTLSAndNonTLS {
  86. if err := cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/range"), value: string(rangeData), expected: expectGet, isTLS: true}); err != nil {
  87. t.Fatalf("failed get with curl (%v)", err)
  88. }
  89. }
  90. }
  91. func TestV3CurlWatchAlpha(t *testing.T) { testV3CurlWatch(t, "/v3alpha") }
  92. func TestV3CurlWatchBeta(t *testing.T) { testV3CurlWatch(t, "/v3beta") }
  93. func testV3CurlWatch(t *testing.T, pathPrefix string) {
  94. defer testutil.AfterTest(t)
  95. epc, err := newEtcdProcessCluster(&configNoTLS)
  96. if err != nil {
  97. t.Fatalf("could not start etcd process cluster (%v)", err)
  98. }
  99. defer func() {
  100. if cerr := epc.Close(); err != nil {
  101. t.Fatalf("error closing etcd processes (%v)", cerr)
  102. }
  103. }()
  104. // store "bar" into "foo"
  105. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), expected: "revision"}); err != nil {
  110. t.Fatalf("failed put with curl (%v)", err)
  111. }
  112. // watch for first update to "foo"
  113. wcr := &pb.WatchCreateRequest{Key: []byte("foo"), StartRevision: 1}
  114. wreq, err := json.Marshal(wcr)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. // marshaling the grpc to json gives:
  119. // "{"RequestUnion":{"CreateRequest":{"key":"Zm9v","start_revision":1}}}"
  120. // but the gprc-gateway expects a different format..
  121. wstr := `{"create_request" : ` + string(wreq) + "}"
  122. // expects "bar", timeout after 2 seconds since stream waits forever
  123. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/watch"), value: wstr, expected: `"YmFy"`, timeout: 2}); err != nil {
  124. t.Fatal(err)
  125. }
  126. }
  127. func TestV3CurlTxnAlpha(t *testing.T) { testV3CurlTxn(t, "/v3alpha") }
  128. func TestV3CurlTxnBeta(t *testing.T) { testV3CurlTxn(t, "/v3beta") }
  129. func testV3CurlTxn(t *testing.T, pathPrefix string) {
  130. defer testutil.AfterTest(t)
  131. epc, err := newEtcdProcessCluster(&configNoTLS)
  132. if err != nil {
  133. t.Fatalf("could not start etcd process cluster (%v)", err)
  134. }
  135. defer func() {
  136. if cerr := epc.Close(); err != nil {
  137. t.Fatalf("error closing etcd processes (%v)", cerr)
  138. }
  139. }()
  140. txn := &pb.TxnRequest{
  141. Compare: []*pb.Compare{
  142. {
  143. Key: []byte("foo"),
  144. Result: pb.Compare_EQUAL,
  145. Target: pb.Compare_CREATE,
  146. TargetUnion: &pb.Compare_CreateRevision{0},
  147. },
  148. },
  149. Success: []*pb.RequestOp{
  150. {
  151. Request: &pb.RequestOp_RequestPut{
  152. RequestPut: &pb.PutRequest{
  153. Key: []byte("foo"),
  154. Value: []byte("bar"),
  155. },
  156. },
  157. },
  158. },
  159. }
  160. m := &runtime.JSONPb{}
  161. jsonDat, jerr := m.Marshal(txn)
  162. if jerr != nil {
  163. t.Fatal(jerr)
  164. }
  165. expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]`
  166. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/txn"), value: string(jsonDat), expected: expected}); err != nil {
  167. t.Fatalf("failed txn with curl (%v)", err)
  168. }
  169. // was crashing etcd server
  170. malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}`
  171. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/txn"), value: malformed, expected: "error"}); err != nil {
  172. t.Fatalf("failed put with curl (%v)", err)
  173. }
  174. }
  175. func TestV3CurlAuthAlpha(t *testing.T) { testV3CurlAuth(t, "/v3alpha") }
  176. func TestV3CurlAuthBeta(t *testing.T) { testV3CurlAuth(t, "/v3beta") }
  177. func testV3CurlAuth(t *testing.T, pathPrefix string) {
  178. defer testutil.AfterTest(t)
  179. epc, err := newEtcdProcessCluster(&configNoTLS)
  180. if err != nil {
  181. t.Fatalf("could not start etcd process cluster (%v)", err)
  182. }
  183. defer func() {
  184. if cerr := epc.Close(); err != nil {
  185. t.Fatalf("error closing etcd processes (%v)", cerr)
  186. }
  187. }()
  188. // create root user
  189. userreq, err := json.Marshal(&pb.AuthUserAddRequest{Name: string("root"), Password: string("toor")})
  190. testutil.AssertNil(t, err)
  191. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/add"), value: string(userreq), expected: "revision"}); err != nil {
  192. t.Fatalf("failed add user with curl (%v)", err)
  193. }
  194. // create root role
  195. rolereq, err := json.Marshal(&pb.AuthRoleAddRequest{Name: string("root")})
  196. testutil.AssertNil(t, err)
  197. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/role/add"), value: string(rolereq), expected: "revision"}); err != nil {
  198. t.Fatalf("failed create role with curl (%v)", err)
  199. }
  200. // grant root role
  201. grantrolereq, err := json.Marshal(&pb.AuthUserGrantRoleRequest{User: string("root"), Role: string("root")})
  202. testutil.AssertNil(t, err)
  203. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/grant"), value: string(grantrolereq), expected: "revision"}); err != nil {
  204. t.Fatalf("failed grant role with curl (%v)", err)
  205. }
  206. // enable auth
  207. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/enable"), value: string("{}"), expected: "revision"}); err != nil {
  208. t.Fatalf("failed enable auth with curl (%v)", err)
  209. }
  210. // put "bar" into "foo"
  211. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  212. testutil.AssertNil(t, err)
  213. // fail put no auth
  214. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), expected: "error"}); err != nil {
  215. t.Fatalf("failed no auth put with curl (%v)", err)
  216. }
  217. // auth request
  218. authreq, err := json.Marshal(&pb.AuthenticateRequest{Name: string("root"), Password: string("toor")})
  219. testutil.AssertNil(t, err)
  220. var (
  221. authHeader string
  222. cmdArgs []string
  223. lineFunc = func(txt string) bool { return true }
  224. )
  225. cmdArgs = cURLPrefixArgs(epc, "POST", cURLReq{endpoint: path.Join(pathPrefix, "/auth/authenticate"), value: string(authreq)})
  226. proc, err := spawnCmd(cmdArgs)
  227. testutil.AssertNil(t, err)
  228. cURLRes, err := proc.ExpectFunc(lineFunc)
  229. testutil.AssertNil(t, err)
  230. authRes := make(map[string]interface{})
  231. testutil.AssertNil(t, json.Unmarshal([]byte(cURLRes), &authRes))
  232. token, ok := authRes["token"].(string)
  233. if !ok {
  234. t.Fatalf("failed invalid token in authenticate response with curl")
  235. }
  236. authHeader = "Authorization : " + token
  237. // put with auth
  238. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), header: authHeader, expected: "revision"}); err != nil {
  239. t.Fatalf("failed auth put with curl (%v)", err)
  240. }
  241. }
  242. func TestV3CurlCampaignAlpha(t *testing.T) { testV3CurlCampaign(t, "/v3alpha") }
  243. func TestV3CurlCampaignBeta(t *testing.T) { testV3CurlCampaign(t, "/v3beta") }
  244. func testV3CurlCampaign(t *testing.T, pathPrefix string) {
  245. defer testutil.AfterTest(t)
  246. epc, err := newEtcdProcessCluster(&configNoTLS)
  247. if err != nil {
  248. t.Fatalf("could not start etcd process cluster (%v)", err)
  249. }
  250. defer func() {
  251. if cerr := epc.Close(); err != nil {
  252. t.Fatalf("error closing etcd processes (%v)", cerr)
  253. }
  254. }()
  255. cdata, err := json.Marshal(&epb.CampaignRequest{
  256. Name: []byte("/election-prefix"),
  257. Value: []byte("v1"),
  258. })
  259. if err != nil {
  260. t.Fatal(err)
  261. }
  262. cargs := cURLPrefixArgs(epc, "POST", cURLReq{
  263. endpoint: path.Join(pathPrefix, "/election/campaign"),
  264. value: string(cdata),
  265. })
  266. lines, err := spawnWithExpectLines(cargs, `"leader":{"name":"`)
  267. if err != nil {
  268. t.Fatalf("failed post campaign request (%s) (%v)", pathPrefix, err)
  269. }
  270. if len(lines) != 1 {
  271. t.Fatalf("len(lines) expected 1, got %+v", lines)
  272. }
  273. var cresp campaignResponse
  274. if err = json.Unmarshal([]byte(lines[0]), &cresp); err != nil {
  275. t.Fatalf("failed to unmarshal campaign response %v", err)
  276. }
  277. ndata, err := base64.StdEncoding.DecodeString(cresp.Leader.Name)
  278. if err != nil {
  279. t.Fatalf("failed to decode leader key %v", err)
  280. }
  281. kdata, err := base64.StdEncoding.DecodeString(cresp.Leader.Key)
  282. if err != nil {
  283. t.Fatalf("failed to decode leader key %v", err)
  284. }
  285. rev, _ := strconv.ParseInt(cresp.Leader.Rev, 10, 64)
  286. lease, _ := strconv.ParseInt(cresp.Leader.Lease, 10, 64)
  287. pdata, err := json.Marshal(&epb.ProclaimRequest{
  288. Leader: &epb.LeaderKey{
  289. Name: ndata,
  290. Key: kdata,
  291. Rev: rev,
  292. Lease: lease,
  293. },
  294. Value: []byte("v2"),
  295. })
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. if err = cURLPost(epc, cURLReq{
  300. endpoint: path.Join(pathPrefix, "/election/proclaim"),
  301. value: string(pdata),
  302. expected: `"revision":`,
  303. }); err != nil {
  304. t.Fatalf("failed post proclaim request (%s) (%v)", pathPrefix, err)
  305. }
  306. }
  307. func TestV3CurlProclaimMissiongLeaderKeyNoTLS(t *testing.T) {
  308. testCtl(t, testV3CurlProclaimMissiongLeaderKey, withCfg(configNoTLS))
  309. }
  310. func testV3CurlProclaimMissiongLeaderKey(cx ctlCtx) {
  311. pdata, err := json.Marshal(&epb.ProclaimRequest{Value: []byte("v2")})
  312. if err != nil {
  313. cx.t.Fatal(err)
  314. }
  315. if err != nil {
  316. cx.t.Fatal(err)
  317. }
  318. if err = cURLPost(cx.epc, cURLReq{
  319. endpoint: path.Join("/v3beta", "/election/proclaim"),
  320. value: string(pdata),
  321. expected: `{"error":"\"leader\" field must be provided","code":2}`,
  322. }); err != nil {
  323. cx.t.Fatalf("failed post proclaim request (%s) (%v)", "/v3beta", err)
  324. }
  325. }
  326. func TestV3CurlResignMissiongLeaderKeyNoTLS(t *testing.T) {
  327. testCtl(t, testV3CurlResignMissiongLeaderKey, withCfg(configNoTLS))
  328. }
  329. func testV3CurlResignMissiongLeaderKey(cx ctlCtx) {
  330. if err := cURLPost(cx.epc, cURLReq{
  331. endpoint: path.Join("/v3beta", "/election/resign"),
  332. value: `{}`,
  333. expected: `{"error":"\"leader\" field must be provided","code":2}`,
  334. }); err != nil {
  335. cx.t.Fatalf("failed post resign request (%s) (%v)", "/v3beta", err)
  336. }
  337. }
  338. // to manually decode; JSON marshals integer fields with
  339. // string types, so can't unmarshal with epb.CampaignResponse
  340. type campaignResponse struct {
  341. Leader struct {
  342. Name string `json:"name,omitempty"`
  343. Key string `json:"key,omitempty"`
  344. Rev string `json:"rev,omitempty"`
  345. Lease string `json:"lease,omitempty"`
  346. } `json:"leader,omitempty"`
  347. }