-
Notifications
You must be signed in to change notification settings - Fork 8
/
keys.go
153 lines (128 loc) · 3.84 KB
/
keys.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package salt
import (
"context"
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
)
type ListKeysReturn struct {
Local []string `json:"local"`
Minions []string `json:"minions"`
MinionsPre []string `json:"minions_pre"`
MinionsRejected []string `json:"minions_rejected"`
MinionsDenied []string `json:"minions_denied"`
}
type listKeysResponse struct {
Return ListKeysReturn `json:"return"`
}
func (c *Client) ListKeys(ctx context.Context) (*ListKeysReturn, error) {
data, err := c.get(ctx, "keys")
if err != nil {
return nil, err
}
var resp listKeysResponse
if err := json.Unmarshal(data, &resp); err != nil {
return nil, err
}
return &resp.Return, nil
}
func (c *Client) GetKeyString(ctx context.Context, match string) (map[string]string, error) {
req := commandRequest{
Client: WheelClient,
Function: "key.print",
Match: match,
}
data, err := c.post(ctx, "", req)
if err != nil {
return nil, err
}
if v := gjson.GetBytes(data, "return.0.data.success"); !v.Bool() {
return nil, fmt.Errorf("failed to get key for %s, error: %s", match, gjson.GetBytes(data, "return.0.data.return").String())
}
value := gjson.GetBytes(data, "return.0.data.return.minions")
minions := make(map[string]string)
for k, v := range value.Map() {
minions[k] = v.String()
}
return minions, nil
}
func (c *Client) GetKeyFinger(ctx context.Context, match string) (map[string]string, error) {
req := commandRequest{
Client: WheelClient,
Function: "key.finger",
Match: match,
}
data, err := c.post(ctx, "", req)
if err != nil {
return nil, err
}
if v := gjson.GetBytes(data, "return.0.data.success"); !v.Bool() {
return nil, fmt.Errorf("failed to get key finger for %s, error: %s", match, gjson.GetBytes(data, "return.0.data.return").String())
}
value := gjson.GetBytes(data, "return.0.data.return.minions."+match)
minions := make(map[string]string)
for k, v := range value.Map() {
minions[k] = v.String()
}
return minions, nil
}
func (c *Client) AcceptKey(ctx context.Context, mid string) ([]string, error) {
req := commandRequest{
Client: WheelClient,
Function: "key.accept",
Match: mid,
}
data, err := c.post(ctx, "", req)
if err != nil {
return nil, err
}
if v := gjson.GetBytes(data, "return.0.data.success"); !v.Bool() {
return nil, fmt.Errorf("failed to accept key for %s, error: %s", mid, gjson.GetBytes(data, "return.0.data.return").String())
}
value := gjson.GetBytes(data, "return.0.data.return.minions")
var minions []string
for _, v := range value.Array() {
minions = append(minions, v.String())
}
return minions, nil
}
func (c *Client) RejectedKey(ctx context.Context, mid string) ([]string, error) {
req := commandRequest{
Client: WheelClient,
Function: "key.reject",
Match: mid,
}
data, err := c.post(ctx, "", req)
if err != nil {
return nil, err
}
if v := gjson.GetBytes(data, "return.0.data.success"); !v.Bool() {
return nil, fmt.Errorf("failed to reject key for %s, error: %s", mid, gjson.GetBytes(data, "return.0.data.return").String())
}
value := gjson.GetBytes(data, "return.0.data.return.minions")
var minions []string
for _, v := range value.Array() {
minions = append(minions, v.String())
}
return minions, nil
}
func (c *Client) DeleteKey(ctx context.Context, match string) ([]string, error) {
req := commandRequest{
Client: WheelClient,
Function: "key.delete",
Match: match,
}
data, err := c.post(ctx, "", req)
if err != nil {
return nil, err
}
if v := gjson.GetBytes(data, "return.0.data.success"); !v.Bool() {
return nil, fmt.Errorf("failed to delete key for %s, error: %s", match, gjson.GetBytes(data, "return.0.data.return").String())
}
value := gjson.GetBytes(data, "return.0.data.return.minions")
var minions []string
for _, v := range value.Array() {
minions = append(minions, v.String())
}
return minions, nil
}