ctl_v3_auth_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "fmt"
  17. "testing"
  18. )
  19. func TestCtlV3AuthEnable(t *testing.T) { testCtl(t, authEnableTest) }
  20. func TestCtlV3AuthDisable(t *testing.T) { testCtl(t, authDisableTest) }
  21. func TestCtlV3AuthWriteKey(t *testing.T) { testCtl(t, authCredWriteKeyTest) }
  22. func authEnableTest(cx ctlCtx) {
  23. if err := authEnable(cx); err != nil {
  24. cx.t.Fatal(err)
  25. }
  26. }
  27. func authEnable(cx ctlCtx) error {
  28. // create root user with root role
  29. if err := ctlV3User(cx, []string{"add", "root", "--interactive=false"}, "User root created", []string{"root"}); err != nil {
  30. return fmt.Errorf("failed to create root user %v", err)
  31. }
  32. if err := ctlV3User(cx, []string{"grant-role", "root", "root"}, "Role root is granted to user root", nil); err != nil {
  33. return fmt.Errorf("failed to grant root user root role %v", err)
  34. }
  35. if err := ctlV3AuthEnable(cx); err != nil {
  36. return fmt.Errorf("authEnableTest ctlV3AuthEnable error (%v)", err)
  37. }
  38. return nil
  39. }
  40. func ctlV3AuthEnable(cx ctlCtx) error {
  41. cmdArgs := append(cx.PrefixArgs(), "auth", "enable")
  42. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  43. }
  44. func authDisableTest(cx ctlCtx) {
  45. if err := ctlV3AuthDisable(cx); err != nil {
  46. cx.t.Fatalf("authDisableTest ctlV3AuthDisable error (%v)", err)
  47. }
  48. }
  49. func ctlV3AuthDisable(cx ctlCtx) error {
  50. cmdArgs := append(cx.PrefixArgs(), "auth", "disable")
  51. return spawnWithExpect(cmdArgs, "Authentication Disabled")
  52. }
  53. func authCredWriteKeyTest(cx ctlCtx) {
  54. // baseline key to check for failed puts
  55. if err := ctlV3Put(cx, "foo", "a", ""); err != nil {
  56. cx.t.Fatal(err)
  57. }
  58. if err := authEnable(cx); err != nil {
  59. cx.t.Fatal(err)
  60. }
  61. cx.user, cx.pass = "root", "root"
  62. authSetupTestUser(cx)
  63. // confirm root role doesn't grant access to all keys
  64. if err := ctlV3PutFailPerm(cx, "foo", "bar"); err != nil {
  65. cx.t.Fatal(err)
  66. }
  67. if err := ctlV3GetFailPerm(cx, "foo"); err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. // try invalid user
  71. cx.user, cx.pass = "a", "b"
  72. if err := ctlV3PutFailAuth(cx, "foo", "bar"); err != nil {
  73. cx.t.Fatal(err)
  74. }
  75. // confirm put failed
  76. cx.user, cx.pass = "test-user", "pass"
  77. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "a"}}...); err != nil {
  78. cx.t.Fatal(err)
  79. }
  80. // try good user
  81. cx.user, cx.pass = "test-user", "pass"
  82. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  83. cx.t.Fatal(err)
  84. }
  85. // confirm put succeeded
  86. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  87. cx.t.Fatal(err)
  88. }
  89. // try bad password
  90. cx.user, cx.pass = "test-user", "badpass"
  91. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  92. cx.t.Fatal(err)
  93. }
  94. // confirm put failed
  95. cx.user, cx.pass = "test-user", "pass"
  96. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  97. cx.t.Fatal(err)
  98. }
  99. }
  100. func ctlV3PutFailAuth(cx ctlCtx, key, val string) error {
  101. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "authentication failed")
  102. }
  103. func ctlV3PutFailPerm(cx ctlCtx, key, val string) error {
  104. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "permission denied")
  105. }
  106. func ctlV3GetFailPerm(cx ctlCtx, key string) error {
  107. return spawnWithExpect(append(cx.PrefixArgs(), "get", key), "permission denied")
  108. }
  109. func authSetupTestUser(cx ctlCtx) {
  110. if err := ctlV3User(cx, []string{"add", "test-user", "--interactive=false"}, "User test-user created", []string{"pass"}); err != nil {
  111. cx.t.Fatal(err)
  112. }
  113. if err := spawnWithExpect(append(cx.PrefixArgs(), "role", "add", "test-role"), "Role test-role created"); err != nil {
  114. cx.t.Fatal(err)
  115. }
  116. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role"}, "Role test-role is granted to user test-user", nil); err != nil {
  117. cx.t.Fatal(err)
  118. }
  119. cmd := append(cx.PrefixArgs(), "role", "grant-permission", "test-role", "readwrite", "foo")
  120. if err := spawnWithExpect(cmd, "Role test-role updated"); err != nil {
  121. cx.t.Fatal(err)
  122. }
  123. }