auth_command.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 command
  15. import (
  16. "fmt"
  17. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  18. "github.com/spf13/cobra"
  19. )
  20. // NewAuthCommand returns the cobra command for "auth".
  21. func NewAuthCommand() *cobra.Command {
  22. ac := &cobra.Command{
  23. Use: "auth <enable or disable>",
  24. Short: "Enable or disable authentication",
  25. }
  26. ac.AddCommand(newAuthEnableCommand())
  27. ac.AddCommand(newAuthDisableCommand())
  28. return ac
  29. }
  30. func newAuthEnableCommand() *cobra.Command {
  31. return &cobra.Command{
  32. Use: "enable",
  33. Short: "Enables authentication",
  34. Run: authEnableCommandFunc,
  35. }
  36. }
  37. // authEnableCommandFunc executes the "auth enable" command.
  38. func authEnableCommandFunc(cmd *cobra.Command, args []string) {
  39. if len(args) != 0 {
  40. ExitWithError(ExitBadArgs, fmt.Errorf("auth enable command does not accept any arguments."))
  41. }
  42. ctx, cancel := commandCtx(cmd)
  43. cli := mustClientFromCmd(cmd)
  44. var err error
  45. for err == nil {
  46. if _, err = cli.AuthEnable(ctx); err == nil {
  47. break
  48. }
  49. if err == rpctypes.ErrRootRoleNotExist {
  50. if _, err = cli.RoleAdd(ctx, "root"); err != nil {
  51. break
  52. }
  53. if _, err = cli.UserGrantRole(ctx, "root", "root"); err != nil {
  54. break
  55. }
  56. }
  57. }
  58. cancel()
  59. if err != nil {
  60. ExitWithError(ExitError, err)
  61. }
  62. fmt.Println("Authentication Enabled")
  63. }
  64. func newAuthDisableCommand() *cobra.Command {
  65. return &cobra.Command{
  66. Use: "disable",
  67. Short: "Disables authentication",
  68. Run: authDisableCommandFunc,
  69. }
  70. }
  71. // authDisableCommandFunc executes the "auth disable" command.
  72. func authDisableCommandFunc(cmd *cobra.Command, args []string) {
  73. if len(args) != 0 {
  74. ExitWithError(ExitBadArgs, fmt.Errorf("auth disable command does not accept any arguments."))
  75. }
  76. ctx, cancel := commandCtx(cmd)
  77. _, err := mustClientFromCmd(cmd).Auth.AuthDisable(ctx)
  78. cancel()
  79. if err != nil {
  80. ExitWithError(ExitError, err)
  81. }
  82. fmt.Println("Authentication Disabled")
  83. }