Skip to content

Commit 69b2503

Browse files
committed
small refactoring
1 parent aadd3a7 commit 69b2503

8 files changed

Lines changed: 234 additions & 305 deletions

File tree

internal/pkg/heimdall/heimdall.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,40 @@ func (h *Heimdall) Init() error {
8282
}
8383
h.agentName = fmt.Sprintf("%s-%d", strings.ToLower(hostname), time.Now().UnixMicro())
8484

85-
// // let's load all the plugins
86-
// plugins, err := h.loadPlugins()
87-
// if err != nil {
88-
// return err
89-
// }
85+
// let's load all the plugins
86+
plugins, err := h.loadPlugins()
87+
if err != nil {
88+
return err
89+
}
9090

91-
// h.commandHandlers = make(map[string]plugin.Handler)
91+
h.commandHandlers = make(map[string]plugin.Handler)
9292

9393
// process commands / add default values if missing, write commands to db
94-
// for _, c := range h.Commands {
95-
96-
// // set defaults for missing properties
97-
// if err := c.Init(); err != nil {
98-
// return err
99-
// }
100-
101-
// // // set command handlers
102-
// // pluginNew, found := plugins[c.Plugin]
103-
// // if !found {
104-
// // return fmt.Errorf(formatErrUnknownPlugin, c.Plugin)
105-
// // }
106-
107-
// // handler, err := pluginNew(c.Context)
108-
// // if err != nil {
109-
// // return err
110-
// // }
111-
// // h.commandHandlers[c.ID] = handler
112-
113-
// // let's record command in the database
114-
// if err := h.commandUpsert(c); err != nil {
115-
// return err
116-
// }
117-
118-
// }
94+
for _, c := range h.Commands {
95+
96+
// set defaults for missing properties
97+
if err := c.Init(); err != nil {
98+
return err
99+
}
100+
101+
// set command handlers
102+
pluginNew, found := plugins[c.Plugin]
103+
if !found {
104+
return fmt.Errorf(formatErrUnknownPlugin, c.Plugin)
105+
}
106+
107+
handler, err := pluginNew(c.Context)
108+
if err != nil {
109+
return err
110+
}
111+
h.commandHandlers[c.ID] = handler
112+
113+
// let's record command in the database
114+
if err := h.commandUpsert(c); err != nil {
115+
return err
116+
}
117+
118+
}
119119

120120
rbacsByName := map[string]rbac.RBAC{}
121121
for rbacName, r := range h.RBACs {

pkg/rbac/main/main.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

pkg/rbac/ranger/client.go

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,12 @@ const (
1919
getGroupsEndpoint = `/service/xusers/groups`
2020
)
2121

22-
type User struct {
23-
ID int64 `json:"id,omitempty"`
24-
Name string `json:"name,omitempty"`
25-
FirstName string `json:"firstName,omitempty"`
26-
LastName string `json:"lastName,omitempty"`
27-
EmailAddress string `json:"emailAddress,omitempty"`
28-
UserRoleList []string `json:"userRoleList,omitempty"`
29-
Password string `json:"password,omitempty"`
30-
SyncSource string `json:"syncSource,omitempty"`
31-
GroupIdList []int64 `json:"groupIdList,omitempty"`
32-
}
33-
34-
type Group struct {
35-
ID int64 `json:"id,omitempty"`
36-
Name string `json:"name,omitempty"`
37-
Description string `json:"description,omitempty"`
38-
SyncSource string `json:"syncSource,omitempty"`
39-
}
40-
41-
type getResponse struct {
42-
PageSize int `json:"pageSize"`
43-
StartIndex int `json:"startIndex"`
44-
ResultSize int `json:"resultSize"`
45-
VXUsers []*User `json:"vXUsers,omitempty"`
46-
VXGroups []*Group `json:"vXGroups,omitempty"`
47-
}
48-
4922
//go:generate go run github.com/vektra/mockery/v2@v2.53.4 --name=Client --output=./mocks --outpkg=mocks
23+
type Client interface {
24+
GetUsers() (map[string]*User, error)
25+
GetGroups() (map[string]*Group, error)
26+
GetPolicies(serviceName string) ([]*Policy, error)
27+
}
5028

5129
type ClientWrapper struct {
5230
Client Client
@@ -74,16 +52,37 @@ func (cw *ClientWrapper) GetPolicies(serviceName string) ([]*Policy, error) {
7452
return cw.Client.GetPolicies(serviceName)
7553
}
7654

77-
type Client interface {
78-
GetUsers() (map[string]*User, error)
79-
GetGroups() (map[string]*Group, error)
80-
GetPolicies(serviceName string) ([]*Policy, error)
55+
type User struct {
56+
ID int64 `json:"id,omitempty"`
57+
Name string `json:"name,omitempty"`
58+
FirstName string `json:"firstName,omitempty"`
59+
LastName string `json:"lastName,omitempty"`
60+
EmailAddress string `json:"emailAddress,omitempty"`
61+
UserRoleList []string `json:"userRoleList,omitempty"`
62+
Password string `json:"password,omitempty"`
63+
SyncSource string `json:"syncSource,omitempty"`
64+
GroupIdList []int64 `json:"groupIdList,omitempty"`
65+
}
66+
67+
type Group struct {
68+
ID int64 `json:"id,omitempty"`
69+
Name string `json:"name,omitempty"`
70+
Description string `json:"description,omitempty"`
71+
SyncSource string `json:"syncSource,omitempty"`
72+
}
73+
74+
type getResponse struct {
75+
PageSize int `json:"pageSize"`
76+
StartIndex int `json:"startIndex"`
77+
ResultSize int `json:"resultSize"`
78+
VXUsers []*User `json:"vXUsers,omitempty"`
79+
VXGroups []*Group `json:"vXGroups,omitempty"`
8180
}
8281

8382
type client struct {
84-
URL string `yaml:"url" json:"url" omitempty"`
85-
Username string `yaml:"username" json:"username" omitempty"`
86-
Password string `yaml:"password" json:"password" omitempty"`
83+
URL string `yaml:"url,omitempty" json:"url,omitempty"`
84+
Username string `yaml:"username,omitempty" json:"username,omitempty"`
85+
Password string `yaml:"password,omitempty" json:"password,omitempty"`
8786
client *http.Client
8887
}
8988

@@ -95,6 +94,7 @@ func NewClient(url, username, password string) Client {
9594
client: &http.Client{},
9695
}
9796
}
97+
9898
func (c *client) GetUsers() (map[string]*User, error) {
9999

100100
responses, err := c.executeBatchRequest(http.MethodGet, getUsersEndpoint)
@@ -119,7 +119,6 @@ func (c *client) GetUsers() (map[string]*User, error) {
119119
}
120120

121121
func (c *client) GetGroups() (map[string]*Group, error) {
122-
123122
responses, err := c.executeBatchRequest(http.MethodGet, getGroupsEndpoint)
124123
if err != nil {
125124
return nil, err
@@ -131,12 +130,10 @@ func (c *client) GetGroups() (map[string]*Group, error) {
131130
for _, group := range resp.VXGroups {
132131
groupsMap[group.Name] = group
133132
}
134-
135133
}
136134

137135
log.Printf("Number of Ranger Groups pulled: %d\n", len(groupsMap))
138136
return groupsMap, nil
139-
140137
}
141138

142139
func (c *client) GetPolicies(serviceName string) ([]*Policy, error) {
@@ -146,12 +143,6 @@ func (c *client) GetPolicies(serviceName string) ([]*Policy, error) {
146143
}
147144

148145
func (c *client) createRequest(method, endpoint string, reqBody interface{}) (*http.Request, error) {
149-
150-
// Ensure client exists
151-
if c.client == nil {
152-
c.client = &http.Client{}
153-
}
154-
155146
var jsonBody []byte
156147
var err error
157148

@@ -177,14 +168,13 @@ func (c *client) createRequest(method, endpoint string, reqBody interface{}) (*h
177168

178169
}
179170

180-
func (r *client) executeRequest(method string, endpoint string, v interface{}, reqBody interface{}) error {
181-
182-
req, err := r.createRequest(method, endpoint, reqBody)
171+
func (c *client) executeRequest(method string, endpoint string, v interface{}, reqBody interface{}) error {
172+
req, err := c.createRequest(method, endpoint, reqBody)
183173
if err != nil {
184174
return err
185175
}
186176

187-
resp, err := r.client.Do(req)
177+
resp, err := c.client.Do(req)
188178
if err != nil {
189179
return err
190180
}
@@ -206,12 +196,10 @@ func (r *client) executeRequest(method string, endpoint string, v interface{}, r
206196
}
207197

208198
return nil
209-
210199
}
211200

212201
// executeBatchRequest performs paginated API requests and returns all aggregated results
213-
func (r *client) executeBatchRequest(method string, endpoint string) ([]getResponse, error) {
214-
202+
func (c *client) executeBatchRequest(method string, endpoint string) ([]getResponse, error) {
215203
results := make([]getResponse, 500)
216204
pageSize := 200
217205
startIndex := 0
@@ -222,7 +210,7 @@ func (r *client) executeBatchRequest(method string, endpoint string) ([]getRespo
222210

223211
// Marshall into generic get
224212
getResponse := &getResponse{}
225-
if err := r.executeRequest(method, batchEndpoint, getResponse, nil); err != nil {
213+
if err := c.executeRequest(method, batchEndpoint, getResponse, nil); err != nil {
226214
return nil, err
227215
}
228216

@@ -236,9 +224,7 @@ func (r *client) executeBatchRequest(method string, endpoint string) ([]getRespo
236224
}
237225

238226
startIndex += pageSize
239-
240227
}
241228

242229
return results, nil
243-
244230
}

0 commit comments

Comments
 (0)