v3_curl_test.go 12 KB

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