example_auth_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 clientv3_test
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. )
  21. func ExampleAuth() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. if _, err = cli.RoleAdd(context.TODO(), "root"); err != nil {
  31. log.Fatal(err)
  32. }
  33. if _, err = cli.UserAdd(context.TODO(), "root", "123"); err != nil {
  34. log.Fatal(err)
  35. }
  36. if _, err = cli.UserGrantRole(context.TODO(), "root", "root"); err != nil {
  37. log.Fatal(err)
  38. }
  39. if _, err = cli.RoleAdd(context.TODO(), "r"); err != nil {
  40. log.Fatal(err)
  41. }
  42. if _, err = cli.RoleGrantPermission(
  43. context.TODO(),
  44. "r", // role name
  45. "foo", // key
  46. "zoo", // range end
  47. clientv3.PermissionType(clientv3.PermReadWrite),
  48. ); err != nil {
  49. log.Fatal(err)
  50. }
  51. if _, err = cli.UserAdd(context.TODO(), "u", "123"); err != nil {
  52. log.Fatal(err)
  53. }
  54. if _, err = cli.UserGrantRole(context.TODO(), "u", "r"); err != nil {
  55. log.Fatal(err)
  56. }
  57. if _, err = cli.AuthEnable(context.TODO()); err != nil {
  58. log.Fatal(err)
  59. }
  60. cliAuth, err := clientv3.New(clientv3.Config{
  61. Endpoints: endpoints,
  62. DialTimeout: dialTimeout,
  63. Username: "u",
  64. Password: "123",
  65. })
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. defer cliAuth.Close()
  70. if _, err = cliAuth.Put(context.TODO(), "foo1", "bar"); err != nil {
  71. log.Fatal(err)
  72. }
  73. _, err = cliAuth.Txn(context.TODO()).
  74. If(clientv3.Compare(clientv3.Value("zoo1"), ">", "abc")).
  75. Then(clientv3.OpPut("zoo1", "XYZ")).
  76. Else(clientv3.OpPut("zoo1", "ABC")).
  77. Commit()
  78. fmt.Println(err)
  79. // now check the permission with the root account
  80. rootCli, err := clientv3.New(clientv3.Config{
  81. Endpoints: endpoints,
  82. DialTimeout: dialTimeout,
  83. Username: "root",
  84. Password: "123",
  85. })
  86. if err != nil {
  87. log.Fatal(err)
  88. }
  89. defer rootCli.Close()
  90. resp, err := rootCli.RoleGet(context.TODO(), "r")
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. fmt.Printf("user u permission: key %q, range end %q\n", resp.Perm[0].Key, resp.Perm[0].RangeEnd)
  95. if _, err = rootCli.AuthDisable(context.TODO()); err != nil {
  96. log.Fatal(err)
  97. }
  98. // Output: etcdserver: permission denied
  99. // user u permission: key "foo", range end "zoo"
  100. }