v3_curl_test.go 13 KB

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