Skip to content

Commit

Permalink
Remove user input, just print the warning
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola committed Sep 19, 2023
1 parent 545311d commit e4d0b52
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 34 deletions.
4 changes: 0 additions & 4 deletions pkg/cli/cmds/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ func NewTokenCommands(create, delete, generate, list, rotate func(ctx *cli.Conte
Name: "new-token",
Usage: "New token that replaces existing token",
Destination: &TokenConfig.NewToken,
},
&cli.BoolFlag{
Name: "f,force",
Usage: "Bypass user prompt warnings",
}),
SkipFlagParsing: false,
SkipArgReorder: true,
Expand Down
30 changes: 1 addition & 29 deletions pkg/cli/token/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package token

import (
"bufio"
"bytes"
"context"
"encoding/json"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -151,13 +149,7 @@ func Rotate(app *cli.Context) error {
if err := cmds.InitLogging(); err != nil {
return err
}
if !app.Bool("force") {
warning := fmt.Sprint("\033[33mWARNING\033[0m: This will replace the existing token with a new one.",
"Recommend keeping a record of the old token. If restoring from a snapshot, you must use the token associated with that snapshot.")
if !askContinue(warning) {
return nil
}
}
fmt.Println("\033[33mWARNING\033[0m: Recommended to keep a record of the old token. If restoring from a snapshot, you must use the token associated with that snapshot.")
info, err := serverAccess(&cmds.TokenConfig)
if err != nil {
return err
Expand All @@ -178,26 +170,6 @@ func Rotate(app *cli.Context) error {
return nil
}

func askContinue(prefix string) bool {
reader := bufio.NewReader(os.Stdin)

for i := 0; i < 3; i++ {
fmt.Printf("%s Continue: [y/n]: \n", prefix)

resp, err := reader.ReadString('\n')
if err != nil {
logrus.Fatal(err)
}
resp = strings.ToLower(strings.TrimSpace(resp))
if resp == "y" || resp == "yes" {
return true
} else if resp == "n" || resp == "no" {
return false
}
}
return false
}

func serverAccess(cfg *cmds.Token) (*clientaccess.Info, error) {
// hide process arguments from ps output, since they likely contain tokens.
gspt.SetProcTitle(os.Args[0] + " token")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ var _ = Describe("Use the token CLI to create and join agents", Ordered, func()
Context("Rotate server bootstrap token", func() {
serverToken := "1234"
It("Creates a new server token", func() {
Expect(e2e.RunCmdOnNode("k3s token rotate -f -t vagrant --new-token="+serverToken, serverNodeNames[0])).
Expect(e2e.RunCmdOnNode("k3s token rotate -t vagrant --new-token="+serverToken, serverNodeNames[0])).
To(ContainSubstring("Token rotated, restart k3s with new token"))
})
It("Restarts servers with the new token", func() {
Expand Down

0 comments on commit e4d0b52

Please sign in to comment.