v3_curl_test.go 12 KB

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