Skip to content

Commit ac5d560

Browse files
committed
Code improvements, fix linting & existing tests
1 parent eccad1d commit ac5d560

File tree

9 files changed

+37
-30
lines changed

9 files changed

+37
-30
lines changed

handlers/auth.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func AuthMiddleware(roleAuth *AuthConfig) echo.MiddlewareFunc {
8989

9090
policiesFromClaims, err := getPoliciesFromClaims(policies)
9191
if err != nil {
92-
gaia.Cfg.Logger.Error(err.Error())
9392
return c.String(http.StatusForbidden, fmt.Sprintf("Permission denied for user %s. %s", username, err.Error()))
9493
}
9594

handlers/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"net/http"
55
"time"
66

7+
rice "github.com/GeertJohan/go.rice"
8+
"github.com/labstack/echo"
9+
"github.com/labstack/echo/middleware"
10+
711
"github.com/gaia-pipeline/gaia"
812
"github.com/gaia-pipeline/gaia/handlers/providers/pipelines"
913
"github.com/gaia-pipeline/gaia/handlers/providers/workers"
1014
"github.com/gaia-pipeline/gaia/helper/resourcehelper"
1115
"github.com/gaia-pipeline/gaia/helper/rolehelper"
1216
"github.com/gaia-pipeline/gaia/security/rbac"
1317
"github.com/gaia-pipeline/gaia/services"
14-
15-
rice "github.com/GeertJohan/go.rice"
16-
"github.com/labstack/echo"
17-
"github.com/labstack/echo/middleware"
1818
)
1919

2020
var (
@@ -55,7 +55,7 @@ func (s *GaiaHandler) InitHandlers(e *echo.Echo) error {
5555
rbacHandler := newRBACHandler(storeService, rbacSvc, resourcehelper.NewMarshaller())
5656
e.GET(p+"rbac/policy/:name", rbacHandler.RBACPolicyResourceGet)
5757
e.POST(p+"rbac/policy", rbacHandler.RBACPolicyResourcePut)
58-
e.PUT(p+"rbac/policy/:name/assign/:username", rbacHandler.AuthPolicyAssignmentPut)
58+
e.PUT(p+"rbac/policy/:name/bind/:username", rbacHandler.RBACPolicyBindingPut)
5959

6060
// Pipelines
6161
// Create pipeline provider

handlers/rbac.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package handlers
22

33
import (
4-
"github.com/gaia-pipeline/gaia/security/rbac"
54
"io/ioutil"
65
"net/http"
76

87
"github.com/labstack/echo"
98

109
"github.com/gaia-pipeline/gaia"
1110
"github.com/gaia-pipeline/gaia/helper/resourcehelper"
12-
gStore "github.com/gaia-pipeline/gaia/store"
11+
"github.com/gaia-pipeline/gaia/security/rbac"
12+
"github.com/gaia-pipeline/gaia/store"
1313
)
1414

1515
type rbacHandler struct {
16-
store gStore.GaiaStore
16+
store store.RBACStore
1717
svc rbac.Service
1818
rbacMarshaller resourcehelper.Marshaller
1919
}
2020

21-
func newRBACHandler(store gStore.GaiaStore, svc rbac.Service, rbacMarshaller resourcehelper.Marshaller) *rbacHandler {
21+
func newRBACHandler(store store.RBACStore, svc rbac.Service, rbacMarshaller resourcehelper.Marshaller) *rbacHandler {
2222
return &rbacHandler{store: store, svc: svc, rbacMarshaller: rbacMarshaller}
2323
}
2424

@@ -63,11 +63,11 @@ func (h rbacHandler) RBACPolicyResourceGet(c echo.Context) error {
6363
return c.String(http.StatusOK, string(bts))
6464
}
6565

66-
func (h rbacHandler) AuthPolicyAssignmentPut(c echo.Context) error {
66+
func (h rbacHandler) RBACPolicyBindingPut(c echo.Context) error {
6767
name := c.Param("name")
6868
username := c.Param("username")
6969

70-
if err := h.store.RBACPolicyBindingsPut(username, name); err != nil {
70+
if err := h.store.RBACPolicyBindingPut(username, name); err != nil {
7171
gaia.Cfg.Logger.Error("failed to put auth assignment: " + err.Error())
7272
return c.String(http.StatusBadRequest, "Error getting policy.")
7373
}

handlers/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func UserLogin(c echo.Context) error {
4646
return err
4747
}
4848

49-
userPolicies, err := storeService.RBACPolicyBindingsGet(u.Username)
49+
userPolicies, err := storeService.RBACPolicyBindingGetAll(u.Username)
5050
if err != nil {
5151
gaia.Cfg.Logger.Error("error getting policy bindings", "username", u.Username)
5252
return c.String(http.StatusInternalServerError, "an error has occurred.")

security/rbac/enforcer.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ var (
1515
errResourceDeny = errors.New("resource implicit deny")
1616
)
1717

18-
type User struct {
19-
Username string
20-
Policies map[string]interface{}
21-
}
22-
18+
// EnforcerConfig represents the config required for RBAC.
2319
type EnforcerConfig struct {
2420
User User
2521
Namespace gaia.RBACPolicyNamespace
2622
Action gaia.RBACPolicyAction
2723
Resource gaia.RBACPolicyResource
2824
}
2925

26+
// User represents the user to apply the enforcement to.
27+
type User struct {
28+
Username string
29+
Policies map[string]interface{}
30+
}
31+
3032
// PolicyEnforcer is for enforcing RBAC Policies.
3133
type PolicyEnforcer interface {
3234
Enforce(cfg EnforcerConfig) error
@@ -91,7 +93,7 @@ func (s *policyEnforcer) Evaluate(user User) (gaia.RBACEvaluatedPermissions, err
9193

9294
// Nothing in the cache, so start getting the policies for this user
9395
var stmts []gaia.RBACPolicyStatementV1
94-
for policyName, _ := range user.Policies {
96+
for policyName := range user.Policies {
9597
policyResource, _ := s.svc.GetPolicy(policyName)
9698
stmts = append(stmts, policyResource.Statement...)
9799
}
File renamed without changes.

security/rbac/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rbac
22

33
import (
44
"fmt"
5+
56
"github.com/gaia-pipeline/gaia"
67
"github.com/gaia-pipeline/gaia/store"
78
)
@@ -15,12 +16,12 @@ type Service interface {
1516
}
1617

1718
type service struct {
18-
store store.GaiaStore
19+
store store.RBACStore
1920
evaluatedPermsCache Cache
2021
}
2122

2223
// NewService creates a new RBAC Service.
23-
func NewService(store store.GaiaStore, cache Cache) Service {
24+
func NewService(store store.RBACStore, cache Cache) Service {
2425
return &service{store: store, evaluatedPermsCache: cache}
2526
}
2627

store/rbac.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import (
99
"github.com/gaia-pipeline/gaia"
1010
)
1111

12-
// RBACPolicyBindingsPut adds a new users policy assignments.
13-
func (s *BoltStore) RBACPolicyBindingsPut(username string, policy string) error {
14-
existing, err := s.RBACPolicyBindingsGet(username)
12+
// RBACStore represents the interface for all RBAC store actions.
13+
type RBACStore interface {
14+
RBACPolicyResourcePut(spec gaia.RBACPolicyResourceV1) error
15+
RBACPolicyResourceGet(name string) (gaia.RBACPolicyResourceV1, error)
16+
RBACPolicyBindingPut(username string, policy string) error
17+
RBACPolicyBindingGetAll(username string) (map[string]interface{}, error)
18+
}
19+
20+
// RBACPolicyBindingPut adds a new users policy assignments.
21+
func (s *BoltStore) RBACPolicyBindingPut(username string, policy string) error {
22+
existing, err := s.RBACPolicyBindingGetAll(username)
1523
if err != nil {
1624
return fmt.Errorf("failed to get bindings: %v", err.Error())
1725
}
@@ -29,8 +37,8 @@ func (s *BoltStore) RBACPolicyBindingsPut(username string, policy string) error
2937
})
3038
}
3139

32-
// RBACPolicyBindingsGet gets a users policy assignments.
33-
func (s *BoltStore) RBACPolicyBindingsGet(username string) (map[string]interface{}, error) {
40+
// RBACPolicyBindingGetAll gets a users policy assignments.
41+
func (s *BoltStore) RBACPolicyBindingGetAll(username string) (map[string]interface{}, error) {
3442
assignment := make(map[string]interface{})
3543

3644
err := s.db.View(func(tx *bolt.Tx) error {

store/store.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ type GaiaStore interface {
9797
WorkerGet(id string) (*gaia.Worker, error)
9898
UpsertSHAPair(pair gaia.SHAPair) error
9999
GetSHAPair(pipelineID int) (bool, gaia.SHAPair, error)
100-
RBACPolicyResourcePut(spec gaia.RBACPolicyResourceV1) error
101-
RBACPolicyResourceGet(name string) (gaia.RBACPolicyResourceV1, error)
102-
RBACPolicyBindingsPut(username string, policy string) error
103-
RBACPolicyBindingsGet(username string) (map[string]interface{}, error)
100+
RBACStore
104101
}
105102

106103
// Compile time interface compliance check for BoltStore. If BoltStore

0 commit comments

Comments
 (0)