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