example_auth_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/clientv3"
  19. "golang.org/x/net/context"
  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. authapi := clientv3.NewAuth(cli)
  31. if _, err = authapi.RoleAdd(context.TODO(), "root"); err != nil {
  32. log.Fatal(err)
  33. }
  34. if _, err = authapi.RoleGrantPermission(
  35. context.TODO(),
  36. "root", // role name
  37. "foo", // key
  38. "zoo", // range end
  39. clientv3.PermissionType(clientv3.PermReadWrite),
  40. ); err != nil {
  41. log.Fatal(err)
  42. }
  43. if _, err = authapi.UserAdd(context.TODO(), "root", "123"); err != nil {
  44. log.Fatal(err)
  45. }
  46. if _, err = authapi.UserGrantRole(context.TODO(), "root", "root"); err != nil {
  47. log.Fatal(err)
  48. }
  49. if _, err = authapi.AuthEnable(context.TODO()); err != nil {
  50. log.Fatal(err)
  51. }
  52. cliAuth, err := clientv3.New(clientv3.Config{
  53. Endpoints: endpoints,
  54. DialTimeout: dialTimeout,
  55. Username: "root",
  56. Password: "123",
  57. })
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. defer cliAuth.Close()
  62. kv := clientv3.NewKV(cliAuth)
  63. if _, err = kv.Put(context.TODO(), "foo1", "bar"); err != nil {
  64. log.Fatal(err)
  65. }
  66. _, err = kv.Txn(context.TODO()).
  67. If(clientv3.Compare(clientv3.Value("zoo1"), ">", "abc")).
  68. Then(clientv3.OpPut("zoo1", "XYZ")).
  69. Else(clientv3.OpPut("zoo1", "ABC")).
  70. Commit()
  71. fmt.Println(err)
  72. // now check the permission
  73. authapi2 := clientv3.NewAuth(cliAuth)
  74. resp, err := authapi2.RoleGet(context.TODO(), "root")
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. fmt.Printf("root user permission: key %q, range end %q\n", resp.Perm[0].Key, resp.Perm[0].RangeEnd)
  79. if _, err = authapi2.AuthDisable(context.TODO()); err != nil {
  80. log.Fatal(err)
  81. }
  82. // Output: etcdserver: permission denied
  83. // root user permission: key "foo", range end "zoo"
  84. }