mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-09 06:21:57 +00:00
TRA-4202 role management (#688)
* WIP * wip * Update keto.yml, socket_routes.go, and 12 more files... * fixes and docs * Update api.js * Update auth.go and api.js * Update user_role_provider.go * Update config_routes.go and api.js * Update consts.go
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mizuserver/pkg/middlewares"
|
||||
"mizuserver/pkg/models"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -49,7 +48,7 @@ func init() {
|
||||
func WebSocketRoutes(app *gin.Engine, eventHandlers EventHandlers, startTime int64) {
|
||||
app.GET("/ws", func(c *gin.Context) {
|
||||
websocketHandler(c.Writer, c.Request, eventHandlers, false, startTime)
|
||||
}, middlewares.RequiresAuth())
|
||||
})
|
||||
|
||||
app.GET("/wsTapper", func(c *gin.Context) { // TODO: add m2m authentication to this route
|
||||
websocketHandler(c.Writer, c.Request, eventHandlers, true, startTime)
|
||||
|
@@ -16,3 +16,8 @@ func IsSetupNecessary(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, IsInstallNeeded)
|
||||
}
|
||||
}
|
||||
|
||||
func SetupAdminUser(c *gin.Context) {
|
||||
token, err, formErrorMessages := providers.CreateAdminUser(c.PostForm("password"), c.Request.Context())
|
||||
handleRegistration(token, err, formErrorMessages, c)
|
||||
}
|
||||
|
@@ -5,6 +5,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/up9inc/mizu/shared/logger"
|
||||
|
||||
ory "github.com/ory/kratos-client-go"
|
||||
)
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
@@ -25,7 +27,12 @@ func Logout(c *gin.Context) {
|
||||
}
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
if token, _, err, formErrorMessages := providers.RegisterUser(c.PostForm("username"), c.PostForm("password"), c.Request.Context()); err != nil {
|
||||
token, _, err, formErrorMessages := providers.RegisterUser(c.PostForm("username"), c.PostForm("password"), c.Request.Context())
|
||||
handleRegistration(token, err, formErrorMessages, c)
|
||||
}
|
||||
|
||||
func handleRegistration(token *string, err error, formErrorMessages map[string][]ory.UiText, c *gin.Context) {
|
||||
if err != nil {
|
||||
if formErrorMessages != nil {
|
||||
logger.Log.Infof("user attempted to register but had form errors %v %v", formErrorMessages, err)
|
||||
c.AbortWithStatusJSON(400, formErrorMessages)
|
||||
@@ -34,6 +41,6 @@ func Register(c *gin.Context) {
|
||||
c.AbortWithStatusJSON(500, gin.H{"error": "internal error occured while registering"})
|
||||
}
|
||||
} else {
|
||||
c.JSON(200, gin.H{"token": token})
|
||||
c.JSON(201, gin.H{"token": token})
|
||||
}
|
||||
}
|
||||
|
73
agent/pkg/middlewares/auth.go
Normal file
73
agent/pkg/middlewares/auth.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"mizuserver/pkg/config"
|
||||
"mizuserver/pkg/providers"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
ory "github.com/ory/kratos-client-go"
|
||||
"github.com/up9inc/mizu/shared/logger"
|
||||
)
|
||||
|
||||
func RequiresAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// auth is irrelevant for ephermeral mizu
|
||||
if !config.Config.StandaloneMode {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
verifyKratosSessionForRequest(c)
|
||||
if !c.IsAborted() {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RequiresAdmin() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// auth is irrelevant for ephermeral mizu
|
||||
if !config.Config.StandaloneMode {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
session := verifyKratosSessionForRequest(c)
|
||||
if c.IsAborted() {
|
||||
return
|
||||
}
|
||||
|
||||
traits := session.Identity.Traits.(map[string]interface{})
|
||||
username := traits["username"].(string)
|
||||
|
||||
isAdmin, err := providers.CheckIfUserHasSystemRole(username, providers.AdminRole)
|
||||
if err != nil {
|
||||
logger.Log.Errorf("error checking user role %v", err)
|
||||
c.AbortWithStatusJSON(403, gin.H{"error": "unknown auth error occured"})
|
||||
} else if !isAdmin {
|
||||
logger.Log.Warningf("user %s attempted to call an admin only endpoint with insufficient privileges", username)
|
||||
c.AbortWithStatusJSON(403, gin.H{"error": "unauthorized"})
|
||||
} else {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func verifyKratosSessionForRequest(c *gin.Context) *ory.Session {
|
||||
token := c.GetHeader("x-session-token")
|
||||
if token == "" {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "token header is empty"})
|
||||
return nil
|
||||
}
|
||||
|
||||
if session, err := providers.VerifyToken(token, c.Request.Context()); err != nil {
|
||||
logger.Log.Errorf("error verifying token %v", err)
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "unknown auth error occured"})
|
||||
return nil
|
||||
} else if session == nil {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "invalid token"})
|
||||
return nil
|
||||
} else {
|
||||
return session
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"mizuserver/pkg/config"
|
||||
"mizuserver/pkg/providers"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/up9inc/mizu/shared/logger"
|
||||
)
|
||||
|
||||
const cachedValidTokensRetainmentTime = time.Minute * 1
|
||||
|
||||
var cachedValidTokens = cache.New(cachedValidTokensRetainmentTime, cachedValidTokensRetainmentTime)
|
||||
|
||||
func RequiresAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// auth is irrelevant for ephermeral mizu
|
||||
if !config.Config.StandaloneMode {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
token := c.GetHeader("x-session-token")
|
||||
if token == "" {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "token header is empty"})
|
||||
return
|
||||
}
|
||||
|
||||
if _, isTokenCached := cachedValidTokens.Get(token); isTokenCached {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
if isTokenValid, err := providers.VerifyToken(token, c.Request.Context()); err != nil {
|
||||
logger.Log.Errorf("error verifying token %s", err)
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "unknown auth error occured"})
|
||||
return
|
||||
} else if !isTokenValid {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "invalid token"})
|
||||
return
|
||||
}
|
||||
|
||||
cachedValidTokens.Set(token, true, cachedValidTokensRetainmentTime)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
@@ -2,9 +2,14 @@ package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"mizuserver/pkg/config"
|
||||
|
||||
ory "github.com/ory/kratos-client-go"
|
||||
)
|
||||
|
||||
const AdminUsername = "admin"
|
||||
|
||||
func IsInstallNeeded() (bool, error) {
|
||||
if !config.Config.StandaloneMode { // install not needed in ephermeral mizu
|
||||
return false, nil
|
||||
@@ -16,3 +21,27 @@ func IsInstallNeeded() (bool, error) {
|
||||
return !anyUserExists, nil
|
||||
}
|
||||
}
|
||||
|
||||
func CreateAdminUser(password string, ctx context.Context) (token *string, err error, formErrorMessages map[string][]ory.UiText) {
|
||||
if isInstallNeeded, err := IsInstallNeeded(); err != nil {
|
||||
return nil, err, nil
|
||||
} else if !isInstallNeeded {
|
||||
return nil, errors.New("The admin user has already been created"), nil
|
||||
}
|
||||
|
||||
token, identityId, err, formErrors := RegisterUser(AdminUsername, password, ctx)
|
||||
if err != nil {
|
||||
return nil, err, formErrors
|
||||
}
|
||||
|
||||
err = SetUserSystemRole(AdminUsername, AdminRole)
|
||||
|
||||
if err != nil {
|
||||
//Delete the user to prevent a half-setup situation where admin user is created without admin privileges
|
||||
DeleteUser(identityId, ctx)
|
||||
|
||||
return nil, err, nil
|
||||
}
|
||||
|
||||
return token, nil, nil
|
||||
}
|
||||
|
@@ -66,17 +66,17 @@ func PerformLogin(username string, password string, ctx context.Context) (*strin
|
||||
return result.SessionToken, nil
|
||||
}
|
||||
|
||||
func VerifyToken(token string, ctx context.Context) (bool, error) {
|
||||
func VerifyToken(token string, ctx context.Context) (*ory.Session, error) {
|
||||
flow, _, err := client.V0alpha2Api.ToSession(ctx).XSessionToken(token).Execute()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if flow == nil {
|
||||
return false, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return flow, nil
|
||||
}
|
||||
|
||||
func DeleteUser(identityId string, ctx context.Context) error {
|
||||
|
180
agent/pkg/providers/user_role_provider.go
Normal file
180
agent/pkg/providers/user_role_provider.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package providers
|
||||
|
||||
/*
|
||||
This provider abstracts keto role management down to what we need for mizu
|
||||
|
||||
Keto, in the configuration we use it, is basically a tuple database. Each tuple consists of 4 strings (namespace, object, relation, subjectID) - for example ("workspaces", "sock-shop-workspace", "viewer", "ramiberman")
|
||||
|
||||
namespace - used to organize tuples into groups - we currently use "system" for defining admins and "workspaces" for defining workspace permissions
|
||||
objects - represents something one can have permissions to (files, mizu workspaces etc)
|
||||
relation - represents the permission (viewer, editor, owner etc) - we currently use only viewer and admin
|
||||
subject - represents the user or group that has the permission - we currently use usernames
|
||||
|
||||
more on keto here: https://www.ory.sh/keto/docs/
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mizuserver/pkg/utils"
|
||||
|
||||
ketoClient "github.com/ory/keto-client-go/client"
|
||||
ketoRead "github.com/ory/keto-client-go/client/read"
|
||||
ketoWrite "github.com/ory/keto-client-go/client/write"
|
||||
ketoModels "github.com/ory/keto-client-go/models"
|
||||
)
|
||||
|
||||
const (
|
||||
ketoHost = "localhost"
|
||||
ketoReadPort = 4466
|
||||
ketoWritePort = 4467
|
||||
)
|
||||
|
||||
var (
|
||||
readClient = getKetoClient(fmt.Sprintf("%s:%d", ketoHost, ketoReadPort))
|
||||
writeClient = getKetoClient(fmt.Sprintf("%s:%d", ketoHost, ketoWritePort))
|
||||
systemRoleNamespace = "system"
|
||||
workspacesRoleNamespace = "workspaces"
|
||||
|
||||
systemObject = "system"
|
||||
|
||||
AdminRole = "admin"
|
||||
ViewerRole = "viewer"
|
||||
)
|
||||
|
||||
func GetUserSystemRoles(username string) ([]string, error) {
|
||||
return getObjectRelationsForSubjectID(systemRoleNamespace, systemObject, username)
|
||||
}
|
||||
|
||||
func CheckIfUserHasSystemRole(username string, role string) (bool, error) {
|
||||
systemRoles, err := GetUserSystemRoles(username)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, systemRole := range systemRoles {
|
||||
if systemRole == role {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func GetUserWorkspaceRole(username string, workspace string) ([]string, error) {
|
||||
return getObjectRelationsForSubjectID(workspacesRoleNamespace, workspace, username)
|
||||
}
|
||||
|
||||
func SetUserWorkspaceRole(username string, workspace string, role string) error {
|
||||
return createObjectRelationForSubjectID(workspacesRoleNamespace, workspace, username, role)
|
||||
}
|
||||
|
||||
func SetUserSystemRole(username string, role string) error {
|
||||
return createObjectRelationForSubjectID(systemRoleNamespace, systemObject, username, role)
|
||||
}
|
||||
|
||||
func DeleteAllUserWorkspaceRoles(username string) error {
|
||||
return deleteAllNamespacedRelationsForSubjectID(workspacesRoleNamespace, username)
|
||||
}
|
||||
|
||||
func createObjectRelationForSubjectID(namespace string, object string, subjectID string, relation string) error {
|
||||
tuple := ketoModels.RelationQuery{
|
||||
Namespace: &namespace,
|
||||
Object: object,
|
||||
Relation: relation,
|
||||
SubjectID: subjectID,
|
||||
}
|
||||
|
||||
_, err := writeClient.Write.CreateRelationTuple(ketoWrite.
|
||||
NewCreateRelationTupleParams().
|
||||
WithPayload(&tuple))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getObjectRelationsForSubjectID(namespace string, object string, subjectID string) ([]string, error) {
|
||||
relationTuples, err := queryRelationTuples(&namespace, &object, &subjectID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
relations := make([]string, 0)
|
||||
|
||||
for _, clientRelation := range relationTuples {
|
||||
relations = append(relations, *clientRelation.Relation)
|
||||
}
|
||||
|
||||
return utils.UniqueStringSlice(relations), nil
|
||||
}
|
||||
|
||||
func deleteAllNamespacedRelationsForSubjectID(namespace string, subjectID string) error {
|
||||
relationTuples, err := queryRelationTuples(&namespace, nil, &subjectID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, clientRelation := range relationTuples {
|
||||
_, err := writeClient.Write.DeleteRelationTuple(ketoWrite.
|
||||
NewDeleteRelationTupleParams().
|
||||
WithNamespace(*clientRelation.Namespace).
|
||||
WithObject(*clientRelation.Object).
|
||||
WithRelation(*clientRelation.Relation).
|
||||
WithSubjectID(&clientRelation.SubjectID))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func queryRelationTuples(namespace *string, object *string, subjectID *string, role *string) ([]*ketoModels.InternalRelationTuple, error) {
|
||||
relationTuplesQuery := ketoRead.NewGetRelationTuplesParams()
|
||||
if namespace != nil {
|
||||
relationTuplesQuery = relationTuplesQuery.WithNamespace(*namespace)
|
||||
}
|
||||
if object != nil {
|
||||
relationTuplesQuery = relationTuplesQuery.WithObject(object)
|
||||
}
|
||||
if subjectID != nil {
|
||||
relationTuplesQuery = relationTuplesQuery.WithSubjectID(subjectID)
|
||||
}
|
||||
if role != nil {
|
||||
relationTuplesQuery = relationTuplesQuery.WithRelation(role)
|
||||
}
|
||||
|
||||
return recursiveKetoPagingTraverse(relationTuplesQuery, make([]*ketoModels.InternalRelationTuple, 0), "")
|
||||
}
|
||||
|
||||
func recursiveKetoPagingTraverse(queryParams *ketoRead.GetRelationTuplesParams, tuples []*ketoModels.InternalRelationTuple, pagingToken string) ([]*ketoModels.InternalRelationTuple, error) {
|
||||
params := queryParams
|
||||
if pagingToken != "" {
|
||||
params = queryParams.WithPageToken(&pagingToken)
|
||||
}
|
||||
|
||||
clientRelationsResponse, err := readClient.Read.GetRelationTuples(params)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tuples = append(tuples, clientRelationsResponse.Payload.RelationTuples...)
|
||||
|
||||
if clientRelationsResponse.Payload.NextPageToken != "" {
|
||||
return recursiveKetoPagingTraverse(queryParams, tuples, clientRelationsResponse.Payload.NextPageToken)
|
||||
}
|
||||
|
||||
return tuples, nil
|
||||
}
|
||||
|
||||
func getKetoClient(url string) *ketoClient.OryKeto {
|
||||
return ketoClient.NewHTTPClientWithConfig(nil,
|
||||
ketoClient.
|
||||
DefaultTransportConfig().
|
||||
WithSchemes([]string{"http"}).
|
||||
WithHost(url))
|
||||
}
|
@@ -1,15 +1,16 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"mizuserver/pkg/controllers"
|
||||
"mizuserver/pkg/middlewares"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ConfigRoutes(ginApp *gin.Engine) {
|
||||
routeGroup := ginApp.Group("/config")
|
||||
routeGroup.Use(middlewares.RequiresAuth())
|
||||
|
||||
routeGroup.POST("/tapConfig", controllers.PostTapConfig)
|
||||
routeGroup.GET("/tapConfig", controllers.GetTapConfig)
|
||||
routeGroup.POST("/tap", middlewares.RequiresAdmin(), controllers.PostTapConfig)
|
||||
routeGroup.GET("/tap", controllers.GetTapConfig)
|
||||
}
|
||||
|
@@ -10,4 +10,5 @@ func InstallRoutes(ginApp *gin.Engine) {
|
||||
routeGroup := ginApp.Group("/install")
|
||||
|
||||
routeGroup.GET("/isNeeded", controllers.IsSetupNecessary)
|
||||
routeGroup.POST("/admin", controllers.SetupAdminUser)
|
||||
}
|
||||
|
@@ -85,3 +85,18 @@ func SaveJsonFile(filePath string, value interface{}) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UniqueStringSlice(s []string) []string {
|
||||
uniqueSlice := make([]string, 0)
|
||||
uniqueMap := map[string]bool{}
|
||||
|
||||
for _, val := range s {
|
||||
if uniqueMap[val] == true {
|
||||
continue
|
||||
}
|
||||
uniqueMap[val] = true
|
||||
uniqueSlice = append(uniqueSlice, val)
|
||||
}
|
||||
|
||||
return uniqueSlice
|
||||
}
|
||||
|
Reference in New Issue
Block a user