Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate old APIs in RBAC package #533

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
66 changes: 2 additions & 64 deletions pkg/agent/authorization/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package authorization
import (
"github.com/pkg/errors"
"fmt"
"strings"
"net/http"

"github.com/spiffe/tornjak/pkg/agent/authentication/user"
Expand All @@ -16,27 +15,7 @@ type RBACAuthorizer struct {
apiV1Mapping map[string]map[string][]string
}

// TODO put this in a common constants file
var staticAPIList = map[string]struct{}{
"/": {},
"/api/healthcheck": {},
"/api/debugserver": {},
"/api/agent/list": {},
"/api/entry/list": {},
"/api/tornjak/serverinfo": {},
"/api/tornjak/selectors/list": {},
"/api/tornjak/agents/list": {},
"/api/tornjak/clusters/list": {},
"/api/agent/ban": {},
"/api/agent/delete": {},
"/api/agent/createjointoken": {},
"/api/entry/create": {},
"/api/entry/delete": {},
"/api/tornjak/selectors/register": {},
"/api/tornjak/clusters/create": {},
"/api/tornjak/clusters/edit": {},
"/api/tornjak/clusters/delete": {},
}
// TODO put this in a common constants filecd
var staticAPIV1List = map[string]map[string]struct{}{
"/api/v1/spire/serverinfo" :{"GET": {}},
"/api/v1/spire/healthcheck" :{"GET": {}},
Expand All @@ -58,9 +37,6 @@ func validateInitParameters(roleList map[string]string, apiMapping map[string][]
}
for api, allowList := range apiMapping {
// check that API exists
if _, ok := staticAPIList[api]; !ok {
return errors.Errorf("API %s does not exist", api)
}

// check that each role exists in roleList
for _, allowedRole := range allowList {
Expand Down Expand Up @@ -101,31 +77,6 @@ func NewRBACAuthorizer(policyName string, roleList map[string]string, apiMapping
}, nil
}

func (a *RBACAuthorizer) authorizeAPIRequest(r *http.Request, u *user.UserInfo) error {
userRoles := u.Roles
apiPath := r.URL.Path

allowedRoles := a.apiMapping[apiPath]

// if no role listed for api, reject
if len(allowedRoles) == 0 {
return errors.New("Unauthorized request")
}

// check each allowed role
for _, allowedRole := range allowedRoles {
if allowedRole == "" { // all authenticated allowed
return nil
}
for _, role := range userRoles {
// user has role
if role == allowedRole {
return nil
}
}
}
return errors.New("Unauthorized Request")
}

func (a *RBACAuthorizer) authorizeAPIV1Request(r *http.Request, u *user.UserInfo) error {
userRoles := u.Roles
Expand Down Expand Up @@ -160,23 +111,10 @@ func (a *RBACAuthorizer) AuthorizeRequest(r *http.Request, u *user.UserInfo) err
return errors.Errorf("Authentication error: %v", u.AuthenticationError)
}

// based on path
apiPath := r.URL.Path
isV1 := strings.HasPrefix(apiPath, "/api/v1")

if !isV1 {
// check old API Request
err := a.authorizeAPIRequest(r, u)
if err != nil {
return errors.Errorf("Tornjak API Authorization error: %v", err)
}
} else {
// check API V1 Request
// if not authorized fail and return error
err := a.authorizeAPIV1Request(r, u)
if err != nil {
return errors.Errorf("Tornjak API V1 Authorization error: %v", err)
}
}

return nil
}