Skip to content

Commit

Permalink
adding confirmation when unset cert issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Sep 2, 2024
1 parent 2944c5a commit 56576a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
15 changes: 12 additions & 3 deletions tsuru/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,26 @@ func (c *CertificateIssuerSet) Run(context *cmd.Context) error {

type CertificateIssuerUnset struct {
tsuruClientApp.AppNameMixIn
cname string
cmd.ConfirmationCommand
fs *gnuflag.FlagSet
cname string
}

func (c *CertificateIssuerUnset) Info() *cmd.Info {
return &cmd.Info{
Name: "certificate-issuer-unset",
Usage: "certificate issuer unset [-a/--app appname] [-c/--cname CNAME]",
Usage: "certificate issuer unset [-a/--app appname] [-c/--cname CNAME] [-y/--assume-yes]",
Desc: `Unset a certificate issuer from a specific app.`,
}
}

func (c *CertificateIssuerUnset) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = c.AppNameMixIn.Flags()
c.fs = mergeFlagSet(
c.AppNameMixIn.Flags(),
c.ConfirmationCommand.Flags(),
)

cname := "App CNAME"
c.fs.StringVar(&c.cname, "cname", "", cname)
c.fs.StringVar(&c.cname, "c", "", cname)
Expand All @@ -440,6 +445,10 @@ func (c *CertificateIssuerUnset) Run(context *cmd.Context) error {
return errors.New("You must set cname.")
}

if !c.Confirm(context, fmt.Sprintf(`Are you sure you want to remove certificate issuer for cname: "%s"?`, c.cname)) {
return nil
}

v := url.Values{}
v.Set("cname", c.cname)
u, err := config.GetURLVersion("1.0", fmt.Sprintf("/apps/%s/certissuer?%s", appName, v.Encode()))
Expand Down
31 changes: 30 additions & 1 deletion tsuru/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (s *S) TestCertificateIssuerUnsetRunSuccessfully(c *check.C) {
context := cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
Stdin: strings.NewReader("y\n"),
}
requestCount := 0
trans := &cmdtest.ConditionalTransport{
Expand All @@ -293,12 +294,40 @@ func (s *S) TestCertificateIssuerUnsetRunSuccessfully(c *check.C) {
return url && method && cname
},
}
expected := `Are you sure you want to remove certificate issuer for cname: "app.io"? (y/n) `
expected += "Certificate issuer removed.\n"
s.setupFakeTransport(trans)
command := CertificateIssuerUnset{}
command.Flags().Parse(true, []string{"-a", "secret", "-c", "app.io"})
c.Assert(command.cname, check.Equals, "app.io")
err := command.Run(&context)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, "Certificate issuer removed.\n")
c.Assert(stdout.String(), check.Equals, expected)
c.Assert(requestCount, check.Equals, 1)
}

func (s *S) TestCertificateIssuerUnsetRunWithoutAsking(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
}
trans := &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Status: http.StatusNoContent},
CondFunc: func(req *http.Request) bool {
url := strings.HasSuffix(req.URL.Path, "/apps/secret/certissuer")
method := req.Method == http.MethodDelete
cname := req.FormValue("cname") == "app.io"

return url && method && cname
},
}
expected := "Certificate issuer removed.\n"
s.setupFakeTransport(trans)
command := CertificateIssuerUnset{}
command.Flags().Parse(true, []string{"-a", "secret", "-c", "app.io", "-y"})
c.Assert(command.cname, check.Equals, "app.io")
err := command.Run(&context)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, expected)
}

0 comments on commit 56576a9

Please sign in to comment.