auth_commands.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 CoreOS, Inc.
  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/coreos/etcd/Godeps/_workspace/src/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. Action: actionAuthEnable,
  31. },
  32. {
  33. Name: "disable",
  34. Usage: "disable auth access controls",
  35. Action: actionAuthDisable,
  36. },
  37. },
  38. }
  39. }
  40. func actionAuthEnable(c *cli.Context) {
  41. authEnableDisable(c, true)
  42. }
  43. func actionAuthDisable(c *cli.Context) {
  44. authEnableDisable(c, false)
  45. }
  46. func mustNewAuthAPI(c *cli.Context) client.AuthAPI {
  47. hc := mustNewClient(c)
  48. if c.GlobalBool("debug") {
  49. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  50. }
  51. return client.NewAuthAPI(hc)
  52. }
  53. func authEnableDisable(c *cli.Context, enable bool) {
  54. if len(c.Args()) != 0 {
  55. fmt.Fprintln(os.Stderr, "No arguments accepted")
  56. os.Exit(1)
  57. }
  58. s := mustNewAuthAPI(c)
  59. ctx, cancel := contextWithTotalTimeout(c)
  60. var err error
  61. if enable {
  62. err = s.Enable(ctx)
  63. } else {
  64. err = s.Disable(ctx)
  65. }
  66. cancel()
  67. if err != nil {
  68. fmt.Fprintln(os.Stderr, err.Error())
  69. os.Exit(1)
  70. }
  71. if enable {
  72. fmt.Println("Authentication Enabled")
  73. } else {
  74. fmt.Println("Authentication Disabled")
  75. }
  76. }