auth_commands.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/codegangsta/cli"
  20. "github.com/coreos/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) {
  43. authEnableDisable(c, true)
  44. }
  45. func actionAuthDisable(c *cli.Context) {
  46. authEnableDisable(c, false)
  47. }
  48. func mustNewAuthAPI(c *cli.Context) client.AuthAPI {
  49. hc := mustNewClient(c)
  50. if c.GlobalBool("debug") {
  51. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  52. }
  53. return client.NewAuthAPI(hc)
  54. }
  55. func authEnableDisable(c *cli.Context, enable bool) {
  56. if len(c.Args()) != 0 {
  57. fmt.Fprintln(os.Stderr, "No arguments accepted")
  58. os.Exit(1)
  59. }
  60. s := mustNewAuthAPI(c)
  61. ctx, cancel := contextWithTotalTimeout(c)
  62. var err error
  63. if enable {
  64. err = s.Enable(ctx)
  65. } else {
  66. err = s.Disable(ctx)
  67. }
  68. cancel()
  69. if err != nil {
  70. fmt.Fprintln(os.Stderr, err.Error())
  71. os.Exit(1)
  72. }
  73. if enable {
  74. fmt.Println("Authentication Enabled")
  75. } else {
  76. fmt.Println("Authentication Disabled")
  77. }
  78. }