auth_commands.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 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 command
  15. import (
  16. "fmt"
  17. "os"
  18. "strings"
  19. "github.com/urfave/cli"
  20. "go.etcd.io/etcd/client"
  21. )
  22. func NewAuthCommands() cli.Command {
  23. return cli.Command{
  24. Name: "auth",
  25. Usage: "overall auth controls",
  26. Subcommands: []cli.Command{
  27. {
  28. Name: "enable",
  29. Usage: "enable auth access controls",
  30. ArgsUsage: " ",
  31. Action: actionAuthEnable,
  32. },
  33. {
  34. Name: "disable",
  35. Usage: "disable auth access controls",
  36. ArgsUsage: " ",
  37. Action: actionAuthDisable,
  38. },
  39. },
  40. }
  41. }
  42. func actionAuthEnable(c *cli.Context) error {
  43. authEnableDisable(c, true)
  44. return nil
  45. }
  46. func actionAuthDisable(c *cli.Context) error {
  47. authEnableDisable(c, false)
  48. return nil
  49. }
  50. func mustNewAuthAPI(c *cli.Context) client.AuthAPI {
  51. hc := mustNewClient(c)
  52. if c.GlobalBool("debug") {
  53. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  54. }
  55. return client.NewAuthAPI(hc)
  56. }
  57. func authEnableDisable(c *cli.Context, enable bool) {
  58. if len(c.Args()) != 0 {
  59. fmt.Fprintln(os.Stderr, "No arguments accepted")
  60. os.Exit(1)
  61. }
  62. s := mustNewAuthAPI(c)
  63. ctx, cancel := contextWithTotalTimeout(c)
  64. var err error
  65. if enable {
  66. err = s.Enable(ctx)
  67. } else {
  68. err = s.Disable(ctx)
  69. }
  70. cancel()
  71. if err != nil {
  72. fmt.Fprintln(os.Stderr, err.Error())
  73. os.Exit(1)
  74. }
  75. if enable {
  76. fmt.Println("Authentication Enabled")
  77. } else {
  78. fmt.Println("Authentication Disabled")
  79. }
  80. }