Skip to content

Commit

Permalink
add: certificate and permission to client
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosRoGuerra committed Aug 13, 2021
1 parent 24d08e5 commit 698f07f
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 137 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/sethvargo/go-password v0.1.1
github.com/tsuru/config v0.0.0-20201023175036-375aaee8b560
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa
github.com/tsuru/go-tsuruclient v0.0.0-20210726185852-a34d558923df
github.com/tsuru/go-tsuruclient v0.0.0-20210813191311-aa50779317a0
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6
github.com/tsuru/tsuru v0.0.0-20210706143918-b89a484dc93f
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ github.com/tsuru/go-tsuruclient v0.0.0-20210706193529-586e01e63f2c h1:+DMQy5Egzm
github.com/tsuru/go-tsuruclient v0.0.0-20210706193529-586e01e63f2c/go.mod h1:P2AL93lzpzJlgOvu37SOhee4pnLWod1GMJ8YYQq2b7o=
github.com/tsuru/go-tsuruclient v0.0.0-20210726185852-a34d558923df h1:EcC/XZqcMWU8yWz2TRsXUsbBJ4PKFZGmrKPT3vqgXiM=
github.com/tsuru/go-tsuruclient v0.0.0-20210726185852-a34d558923df/go.mod h1:P2AL93lzpzJlgOvu37SOhee4pnLWod1GMJ8YYQq2b7o=
github.com/tsuru/go-tsuruclient v0.0.0-20210813191311-aa50779317a0 h1:RQ5VuJs7sHVCjGiibwkbml4mlxCmJxowTcldXbg/eQo=
github.com/tsuru/go-tsuruclient v0.0.0-20210813191311-aa50779317a0/go.mod h1:P2AL93lzpzJlgOvu37SOhee4pnLWod1GMJ8YYQq2b7o=
github.com/tsuru/monsterqueue v0.0.0-20160909010522-70e946ec66c3 h1:+aJngj5cQjYyx/qSjffQceop25t97hLS0+t8bvx9hg4=
github.com/tsuru/monsterqueue v0.0.0-20160909010522-70e946ec66c3/go.mod h1:3KR1vkjfm5b7Lhu5OXuO0NMIyZNG0d0d9xh6ufWYVxg=
github.com/tsuru/tablecli v0.0.0-20180215113938-82de88f75181/go.mod h1:ztYpOhW+u1k21FEqp7nZNgpWbr0dUKok5lgGCZi+1AQ=
Expand Down
52 changes: 27 additions & 25 deletions tsuru/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package client

import (
"context"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
Expand All @@ -18,7 +19,10 @@ import (
"strings"

"github.com/tsuru/gnuflag"
tsuruClient "github.com/tsuru/go-tsuruclient/pkg/client"
"github.com/tsuru/go-tsuruclient/pkg/tsuru"
"github.com/tsuru/tablecli"

"github.com/tsuru/tsuru-client/tsuru/formatter"
"github.com/tsuru/tsuru/cmd"
)
Expand Down Expand Up @@ -47,42 +51,43 @@ func (c *CertificateSet) Flags() *gnuflag.FlagSet {
}
return c.fs
}

func (c *CertificateSet) Run(context *cmd.Context, client *cmd.Client) error {
func (c *CertificateSet) CertificateAdd(cert []byte, key []byte) tsuru.CertificateSetData {
certificate := tsuru.CertificateSetData{
Cname: c.cname,
Certificate: cert,
Key: key,
}
return certificate
}
func (c *CertificateSet) Run(ctx *cmd.Context, client *cmd.Client) error {
appName, err := c.AppName()
if err != nil {
return err
}
if c.cname == "" {
return errors.New("You must set cname.")
}
cert, err := ioutil.ReadFile(context.Args[0])
cert, err := ioutil.ReadFile(ctx.Args[0])
if err != nil {
return err
}
key, err := ioutil.ReadFile(context.Args[1])
key, err := ioutil.ReadFile(ctx.Args[1])
if err != nil {
return err
}
v := url.Values{}
v.Set("cname", c.cname)
v.Set("certificate", string(cert))
v.Set("key", string(key))
u, err := cmd.GetURLVersion("1.2", fmt.Sprintf("/apps/%s/certificate", appName))
certificate := c.CertificateAdd(cert, key)
apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}
request, err := http.NewRequest(http.MethodPut, u, strings.NewReader(v.Encode()))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
response, err := apiClient.AppApi.CertificateSet(context.TODO(), appName, certificate)
if err != nil {
return err
}
defer response.Body.Close()
fmt.Fprintln(context.Stdout, "Successfully created the certificated.")
fmt.Fprintln(ctx.Stdout, "Successfully created the certificated.")
return nil
}

Expand Down Expand Up @@ -110,7 +115,7 @@ func (c *CertificateUnset) Flags() *gnuflag.FlagSet {
return c.fs
}

func (c *CertificateUnset) Run(context *cmd.Context, client *cmd.Client) error {
func (c *CertificateUnset) Run(ctx *cmd.Context, client *cmd.Client) error {
appName, err := c.AppName()
if err != nil {
return err
Expand All @@ -120,21 +125,18 @@ func (c *CertificateUnset) Run(context *cmd.Context, client *cmd.Client) error {
}
v := url.Values{}
v.Set("cname", c.cname)
u, err := cmd.GetURLVersion("1.2", fmt.Sprintf("/apps/%s/certificate?%s", appName, v.Encode()))
apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}
request, err := http.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
response, err := apiClient.AppApi.CertificatUnset(context.TODO(), appName)
if err != nil {
return err
}
defer response.Body.Close()
fmt.Fprintln(context.Stdout, "Certificate removed.")
fmt.Fprintln(ctx.Stdout, "Certificate removed.")
return nil
}

Expand Down
34 changes: 28 additions & 6 deletions tsuru/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ import (
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/tsuru/go-tsuruclient/pkg/tsuru"
"github.com/tsuru/tsuru-client/tsuru/formatter"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/cmd/cmdtest"
check "gopkg.in/check.v1"
)

func convert(b []byte) string {
s := make([]string, len(b))
for i := range b {
s[i] = strconv.Itoa(int(b[i]))
}
return strings.Join(s, ",")
}
func (s *S) TestCertificateSetRunSuccessfully(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Expand All @@ -34,10 +43,23 @@ func (s *S) TestCertificateSetRunSuccessfully(c *check.C) {
CondFunc: func(req *http.Request) bool {
url := strings.HasSuffix(req.URL.Path, "/apps/secret/certificate")
method := req.Method == http.MethodPut
cname := req.FormValue("cname") == "app.io"
certificate := req.FormValue("certificate") == s.mustReadFileString(c, "./testdata/cert/server.crt")
key := req.FormValue("key") == s.mustReadFileString(c, "./testdata/cert/server.key")
return url && method && cname && certificate && key
var cert tsuru.CertificateSetData
err := json.NewDecoder(req.Body).Decode(&cert)
c.Assert(err, check.IsNil)
certifica, err := ioutil.ReadFile("./testdata/cert/server.crt")
if err != nil {
return false
}
key, err := ioutil.ReadFile("./testdata/cert/server.key")
if err != nil {
return false
}
c.Assert(cert, check.DeepEquals, tsuru.CertificateSetData{
Cname: "app.io",
Certificate: certifica,
Key: key,
})
return url && method
},
}
client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager)
Expand Down Expand Up @@ -82,9 +104,9 @@ func (s *S) TestCertificateUnsetRunSuccessfully(c *check.C) {
requestCount++
url := strings.HasSuffix(req.URL.Path, "/apps/secret/certificate")
method := req.Method == http.MethodDelete
cname := req.FormValue("cname") == "app.io"
//cname := req.FormValue("cname") == "app.io"

return url && method && cname
return url && method
},
}
client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager)
Expand Down
Loading

0 comments on commit 698f07f

Please sign in to comment.