auth_commands.go 2.1 KB

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