/
permission.go
104 lines (82 loc) · 2.33 KB
/
permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package rbac
import (
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"strings"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/ekristen/dockit/pkg/apiserver/response"
"github.com/ekristen/dockit/pkg/commands/global"
"github.com/ekristen/dockit/pkg/common"
)
type permissionCommand struct{}
func (s *permissionCommand) Execute(c *cli.Context) (err error) {
if c.Args().Len() != 2 {
return fmt.Errorf("incorrect usage")
}
username, password, err := getCredentials(c)
if err != nil {
return err
}
url1 := strings.Join(c.Args().Slice(), "|")
url2 := strings.ReplaceAll(url1, "/", "_")
url3 := strings.ReplaceAll(url2, "|", "/")
url := fmt.Sprintf("%s/admin/%s", c.String("base-url"), url3)
logrus.WithField("url", url).Debug("request url")
method := "PUT"
if c.Command.Name == "revoke" {
method = "DELETE"
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
basicCreds := fmt.Sprintf("%s:%s", username, password)
authHeader := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(basicCreds)))
req.Header.Set("Authorization", authHeader)
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: c.Bool("insecure"),
},
}}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
logrus.WithField("headers", resp.Header).Debug("response headers")
logrus.WithField("status", resp.StatusCode).Debug("response Status Code")
res, err := response.ReadAllDecode(resp.Body)
if err != nil {
return err
}
if res.Status {
fmt.Printf("%s successful\n", c.Command.Name)
} else {
fmt.Printf("%s failed\n", c.Command.Name)
}
return nil
}
func init() {
cmd := permissionCommand{}
// flags := []cli.Flag{}
// grant user repository name action
grantCmd := &cli.Command{
Name: "grant",
Usage: "grant (user|group):<name> (repository|namespace):<name>:(pull|push)",
Action: cmd.Execute,
Flags: append(rbacFlags, global.Flags()...),
Before: global.Before,
}
revokeCmd := &cli.Command{
Name: "revoke",
Usage: "revoke (user|group):<name> (repository|namespace):<name>:(pull|push)",
Action: cmd.Execute,
Flags: append(rbacFlags, global.Flags()...),
Before: global.Before,
}
common.RegisterSubcommand("rbac", grantCmd)
common.RegisterSubcommand("rbac", revokeCmd)
}