Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion internal/pkg/rbac/ranger/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
"net/http"
"strings"
"time"

"github.com/hladush/go-telemetry/pkg/telemetry"
)

const (
getUsersEndpoint = `/service/xusers/users`
getServicePoliciesEndpoint = `/service/public/v2/api/service/%s/policy`
)

var (
executeRequestMethod = telemetry.NewMethod("ranger_execute_request", "heimdall")
)

//go:generate go run github.com/vektra/mockery/v2@v2.53.4 --name=Client --output=./mocks --outpkg=mocks
type Client interface {
GetUsers() (map[string]*User, error)
Expand Down Expand Up @@ -128,13 +134,18 @@ func (c *client) createRequest(method, endpoint string, reqBody interface{}) (*h
}

func (c *client) executeRequest(method string, endpoint string, v interface{}, reqBody interface{}) error {
executeRequestMethod.CountRequest()
defer executeRequestMethod.RecordLatency(time.Now())

req, err := c.createRequest(method, endpoint, reqBody)
if err != nil {
executeRequestMethod.CountError()
return err
}

resp, err := c.client.Do(req)
if err != nil {
executeRequestMethod.CountError()
return err
}
defer resp.Body.Close()
Expand All @@ -147,9 +158,12 @@ func (c *client) executeRequest(method string, endpoint string, v interface{}, r
return nil
}

executeRequestMethod.CountError()
return fmt.Errorf("request to %s failed with status %s\n%s", req.URL.String(), resp.Status, bodyString)
}

executeRequestMethod.CountSuccess()

vals, _ := io.ReadAll(resp.Body)
resp.Body = io.NopCloser(bytes.NewReader(vals))
if v != nil {
Expand All @@ -161,6 +175,9 @@ func (c *client) executeRequest(method string, endpoint string, v interface{}, r

// executeBatchRequest performs paginated API requests and returns all aggregated results
func (c *client) executeBatchRequest(method string, endpoint string) ([]getResponse, error) {
executeRequestMethod.CountRequest("batch")
defer executeRequestMethod.RecordLatency(time.Now(), "batch")

results := make([]getResponse, 500)
pageSize := 500
startIndex := 0
Expand Down Expand Up @@ -188,4 +205,4 @@ func (c *client) executeBatchRequest(method string, endpoint string) ([]getRespo
}

return results, nil
}
}
1 change: 0 additions & 1 deletion internal/pkg/rbac/ranger/ranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (r *Ranger) HasAccess(user string, query string) (bool, error) {
return true, nil
}


func (r *Ranger) SyncState() error {
// receive all policies from ranger
policies, err := r.Client.GetPolicies(r.ServiceName)
Expand Down
26 changes: 13 additions & 13 deletions internal/pkg/rbac/ranger/tests/group_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ func getDefaultGroupAllowPolicy(accessType []string) *ranger.Policy {
PolicyPriority: 1,
Resources: &ranger.Resource{
Catalog: &ranger.ResourceField{
RawValues: []string{"default_catalog"},
RawValues: []string{"default_catalog"},
IsExcludes: false,
},
Schema: &ranger.ResourceField{
RawValues: []string{"public"},
RawValues: []string{"public"},
IsExcludes: false,
},
Table: &ranger.ResourceField{
RawValues: []string{"table1"},
RawValues: []string{"table1"},
IsExcludes: false,
},
},
Expand Down Expand Up @@ -243,15 +243,15 @@ func getDefaultAllActionsGroupPolicyWithExcludeForDefaultGroup(excludeAccess []s
PolicyPriority: 1,
Resources: &ranger.Resource{
Catalog: &ranger.ResourceField{
RawValues: []string{"default_catalog"},
RawValues: []string{"default_catalog"},
IsExcludes: false,
},
Schema: &ranger.ResourceField{
RawValues: []string{"public"},
RawValues: []string{"public"},
IsExcludes: false,
},
Table: &ranger.ResourceField{
RawValues: []string{"table1"},
RawValues: []string{"table1"},
IsExcludes: false,
},
},
Expand Down Expand Up @@ -289,15 +289,15 @@ func getAllowAllPolicyWithDenyForGroup(denyAccess []string) []*ranger.Policy {
PolicyPriority: 1,
Resources: &ranger.Resource{
Catalog: &ranger.ResourceField{
RawValues: []string{"default_catalog"},
RawValues: []string{"default_catalog"},
IsExcludes: false,
},
Schema: &ranger.ResourceField{
RawValues: []string{"public"},
RawValues: []string{"public"},
IsExcludes: false,
},
Table: &ranger.ResourceField{
RawValues: []string{"table1"},
RawValues: []string{"table1"},
IsExcludes: false,
},
},
Expand Down Expand Up @@ -336,15 +336,15 @@ func getAllowAllPolicyWithDenyAndExceptionForGroup(denyAccess, exceptionAccess [
PolicyPriority: 1,
Resources: &ranger.Resource{
Catalog: &ranger.ResourceField{
RawValues: []string{"default_catalog"},
RawValues: []string{"default_catalog"},
IsExcludes: false,
},
Schema: &ranger.ResourceField{
RawValues: []string{"public"},
RawValues: []string{"public"},
IsExcludes: false,
},
Table: &ranger.ResourceField{
RawValues: []string{"table1"},
RawValues: []string{"table1"},
IsExcludes: false,
},
},
Expand Down Expand Up @@ -382,4 +382,4 @@ func getAllowAllPolicyWithDenyAndExceptionForGroup(denyAccess, exceptionAccess [
},
},
}
}
}
Loading
Loading