Some improvements for ABAC

This commit is contained in:
Max 2024-11-16 12:51:17 +03:00
parent fac1b30bf4
commit 4c129e776c
18 changed files with 299 additions and 72 deletions

View File

@ -0,0 +1 @@
package blogspolicies

View File

@ -0,0 +1,22 @@
package blogrules
import (
globalrules "enshi/ABAC/globalRules"
"enshi/ABAC/rules"
"github.com/gin-gonic/gin"
)
func BlogCreateRule(c *gin.Context) (bool, []error) {
rulesToCheck := []rules.RuleFunction{
globalrules.AuthorizedRule,
}
isAllowed, errors := rules.CheckRules(
c,
rulesToCheck,
rules.ALL_RULES_MUST_BE_COMPLETED,
)
return isAllowed, errors
}

View File

@ -9,10 +9,10 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func AuthorizedRule(c *gin.Context) (bool, error) { func AuthorizedRule(c *gin.Context) (bool, []error) {
cookies := c.Request.CookiesNamed("auth_cookie") cookies := c.Request.CookiesNamed("auth_cookie")
if len(cookies) == 0 { if len(cookies) == 0 {
return false, fmt.Errorf("no cookies provided") return false, []error{fmt.Errorf("no cookies provided")}
} }
tokenFromCookies := cookies[0].Value tokenFromCookies := cookies[0].Value
@ -20,7 +20,7 @@ func AuthorizedRule(c *gin.Context) (bool, error) {
if err != nil { if err != nil {
c.IndentedJSON(http.StatusUnauthorized, gin.H{"error auth": err.Error()}) c.IndentedJSON(http.StatusUnauthorized, gin.H{"error auth": err.Error()})
c.Abort() c.Abort()
return false, err return false, []error{err}
} else { } else {
c.Set(global.ContextUserId, cookieClimes["id"]) c.Set(global.ContextUserId, cookieClimes["id"])
c.Set(global.ContextTokenData, cookieClimes) c.Set(global.ContextTokenData, cookieClimes)

View File

@ -0,0 +1,33 @@
package globalrules
import (
"context"
db_repo "enshi/db/go_queries"
"enshi/db_connection"
"enshi/middleware/getters"
"fmt"
"github.com/gin-gonic/gin"
)
func IsAdminRule(c *gin.Context) (bool, []error) {
contextUserId, err := getters.GetUserIdFromContext(c)
if err != nil {
return false, []error{err}
}
user, err :=
db_repo.New(db_connection.Dbx).
GetUserById(context.Background(), contextUserId)
if err != nil || user.UserID == 0 {
return false, []error{err}
}
if !user.IsAdmin {
return false, []error{fmt.Errorf("not admin")}
}
return true, nil
}

View File

@ -0,0 +1,39 @@
package globalrules
import (
"context"
db_repo "enshi/db/go_queries"
"enshi/db_connection"
"enshi/middleware/getters"
"fmt"
"github.com/gin-gonic/gin"
)
func IsOwnerOfTheBlogRule(c *gin.Context) (bool, []error) {
blogId, err := getters.GetInt64Param(c, "blog-id")
if err != nil {
return false, []error{err}
}
contextUserId, err := getters.GetUserIdFromContext(c)
if err != nil {
return false, []error{err}
}
blog, err :=
db_repo.New(db_connection.Dbx).
GetBlogByBlogId(context.Background(), blogId)
if err != nil {
return false, []error{err}
}
if blog.UserID != contextUserId {
return false, []error{fmt.Errorf("now owner of the blog")}
}
return true, nil
}

View File

@ -0,0 +1,39 @@
package globalrules
import (
"context"
db_repo "enshi/db/go_queries"
"enshi/db_connection"
"enshi/middleware/getters"
"fmt"
"github.com/gin-gonic/gin"
)
func IsOwnerOfThePostRule(c *gin.Context) (bool, []error) {
postId, err := getters.GetInt64Param(c, "post-id")
if err != nil {
return false, []error{err}
}
contextUserId, err := getters.GetUserIdFromContext(c)
if err != nil {
return false, []error{err}
}
post, err :=
db_repo.New(db_connection.Dbx).
GetPostsByPostId(context.Background(), postId)
if err != nil {
return false, []error{err}
}
if post.UserID != contextUserId {
return false, []error{fmt.Errorf("now owner of the post")}
}
return true, nil
}

View File

@ -2,6 +2,7 @@ package postspolicies
import ( import (
postRules "enshi/ABAC/postsPolicies/postRules" postRules "enshi/ABAC/postsPolicies/postRules"
"enshi/ABAC/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -13,36 +14,25 @@ const (
GET_POST = "get_post" GET_POST = "get_post"
) )
func checkRule( func PostsPolicies(c *gin.Context) (bool, []error) {
c *gin.Context,
ruleChecker func(*gin.Context) (bool, error),
) (bool, error) {
IsAllowed, err := ruleChecker(c)
if err != nil {
return false, err
}
return IsAllowed, nil
}
func PostsPolicies(c *gin.Context) (bool, error) {
target, exists := c.Get("target") target, exists := c.Get("target")
if !exists { if !exists {
return false, nil return false, nil
} }
// Permit if one permit
switch target { switch target {
case DELETE_POST: case DELETE_POST:
return checkRule(c, postRules.DeleteRule) return rules.CheckRule(c, postRules.DeleteRule)
case UPDATE_POST: case UPDATE_POST:
return checkRule(c, postRules.PostUpdateRule) return rules.CheckRule(c, postRules.PostUpdateRule)
case GET_POST: case GET_POST:
return checkRule(c, postRules.PostReadRule) return rules.CheckRule(c, postRules.PostReadRule)
case CREATE_POST: case CREATE_POST:
return checkRule(c, postRules.PostCreateRule) return rules.CheckRule(c, postRules.PostCreateRule)
} }

View File

@ -1,10 +1,23 @@
package postRules package postRules
import ( import (
globalrules "enshi/ABAC/globalRules"
"enshi/ABAC/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Only owner of the post can change it // Only owner of the post can change it
func PostCreateRule(c *gin.Context) (bool, error) { func PostCreateRule(c *gin.Context) (bool, []error) {
return true, nil rulesToCheck := []rules.RuleFunction{
globalrules.AuthorizedRule,
}
isAllowed, errors := rules.CheckRules(
c,
rulesToCheck,
rules.ALL_RULES_MUST_BE_COMPLETED,
)
return isAllowed, errors
} }

View File

@ -2,30 +2,26 @@ package postRules
import ( import (
globalrules "enshi/ABAC/globalRules" globalrules "enshi/ABAC/globalRules"
"enshi/middleware/checkRole" "enshi/ABAC/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
const RULES_NUMBER_TO_COMPLETE = 2
// Only owner or admin can delete post // Only owner or admin can delete post
func DeleteRule(c *gin.Context) (bool, error) { func DeleteRule(c *gin.Context) (bool, []error) {
// Sender should be authorized rulesToCheck := []rules.RuleFunction{
isAuthorized, err := globalrules.AuthorizedRule(c) globalrules.AuthorizedRule,
if err != nil { globalrules.IsOwnerOfThePostRule,
return false, err globalrules.IsAdminRule,
} else if !isAuthorized {
return false, nil
} }
isOwner, err := checkRole.IsOwnerOfThePost(c) isAllowed, errors := rules.CheckRules(
if err != nil { c,
return false, err rulesToCheck,
} RULES_NUMBER_TO_COMPLETE,
)
isAdmin, err := checkRole.IsAdmin(c) return isAllowed, errors
if err != nil {
return false, err
}
return isAdmin || isOwner, err
} }

View File

@ -5,6 +5,6 @@ import (
) )
// Only owner of the post can change it // Only owner of the post can change it
func PostReadRule(c *gin.Context) (bool, error) { func PostReadRule(c *gin.Context) (bool, []error) {
return true, nil return true, nil
} }

View File

@ -2,25 +2,23 @@ package postRules
import ( import (
globalrules "enshi/ABAC/globalRules" globalrules "enshi/ABAC/globalRules"
"enshi/middleware/checkRole" "enshi/ABAC/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Only owner of the post can change it // Only owner of the post can change it
func PostUpdateRule(c *gin.Context) (bool, error) { func PostUpdateRule(c *gin.Context) (bool, []error) {
// Sender should be authorized rulesToCheck := []rules.RuleFunction{
isAuthorized, err := globalrules.AuthorizedRule(c) globalrules.AuthorizedRule,
if err != nil { globalrules.IsOwnerOfThePostRule,
return false, err
} else if !isAuthorized {
return false, nil
} }
isOwner, err := checkRole.IsOwnerOfThePost(c) isAllowed, errors := rules.CheckRules(
if err != nil { c,
return false, err rulesToCheck,
} rules.ALL_RULES_MUST_BE_COMPLETED,
)
return isOwner, nil return isAllowed, errors
} }

View File

@ -0,0 +1,72 @@
package rules
import (
"fmt"
"strconv"
"github.com/gin-gonic/gin"
)
type RuleFunction func(*gin.Context) (bool, []error)
const (
ALL_RULES_MUST_BE_COMPLETED = iota
)
func CheckRule(
c *gin.Context,
ruleChecker RuleFunction,
) (bool, []error) {
IsAllowed, err := ruleChecker(c)
if err != nil {
return false, err
}
return IsAllowed, nil
}
func CheckRules(
c *gin.Context,
rules []RuleFunction,
completedRulesCount int,
) (bool, []error) {
var allowancesIndexes []int
var errors []error
if len(rules) < completedRulesCount {
return false, []error{fmt.Errorf("there is less rules, that should be completed")}
}
for i, rule := range rules {
if isAllowed, err := CheckRule(c, rule); err != nil {
errors = append(
errors,
err...,
)
} else if !isAllowed {
errors = append(
errors,
fmt.Errorf("rule "+
strconv.Itoa(i)+
" was rejected"),
)
} else {
allowancesIndexes = append(allowancesIndexes, i)
}
}
switch completedRulesCount {
case ALL_RULES_MUST_BE_COMPLETED:
if len(allowancesIndexes) == len(rules) {
return true, nil
} else {
return false, errors
}
default:
if len(allowancesIndexes) >= completedRulesCount {
return true, nil
} else {
return false, errors
}
}
}

View File

@ -56,6 +56,26 @@ func (q *Queries) DeleteBlogByBlogId(ctx context.Context, blogID int64) error {
return err return err
} }
const getBlogByBlogId = `-- name: GetBlogByBlogId :one
SELECT blog_id, user_id, title, description, category_id, created_at
FROM public.blogs
WHERE blog_id = $1
`
func (q *Queries) GetBlogByBlogId(ctx context.Context, blogID int64) (Blog, error) {
row := q.db.QueryRow(ctx, getBlogByBlogId, blogID)
var i Blog
err := row.Scan(
&i.BlogID,
&i.UserID,
&i.Title,
&i.Description,
&i.CategoryID,
&i.CreatedAt,
)
return i, err
}
const getBlogsByUserId = `-- name: GetBlogsByUserId :many const getBlogsByUserId = `-- name: GetBlogsByUserId :many
SELECT blog_id, user_id, title, description, category_id, created_at SELECT blog_id, user_id, title, description, category_id, created_at
FROM public.blogs FROM public.blogs

View File

@ -15,6 +15,11 @@ SELECT *
FROM public.blogs FROM public.blogs
WHERE user_id = $1; WHERE user_id = $1;
-- name: GetBlogByBlogId :one
SELECT *
FROM public.blogs
WHERE blog_id = $1;
-- name: DeleteBlogByBlogId :exec -- name: DeleteBlogByBlogId :exec
DELETE FROM public.blogs DELETE FROM public.blogs
WHERE blog_id=$1; WHERE blog_id=$1;

View File

@ -6,7 +6,7 @@ import (
"enshi/db_connection" "enshi/db_connection"
"enshi/env" "enshi/env"
"enshi/global" "enshi/global"
utils "enshi/utils" "enshi/routes"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -27,7 +27,7 @@ func main() {
defer db_connection.Dbx_connection.Close(context.Background()) defer db_connection.Dbx_connection.Close(context.Background())
router := gin.Default() router := gin.Default()
if err := utils.SetupRotes(router); err != nil { if err := routes.SetupRotes(router); err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return return
} }

View File

@ -4,6 +4,7 @@ import (
postspolicies "enshi/ABAC/postsPolicies" postspolicies "enshi/ABAC/postsPolicies"
rest_api_stuff "enshi/REST_API_stuff" rest_api_stuff "enshi/REST_API_stuff"
"fmt" "fmt"
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -22,10 +23,15 @@ func PostsMiddleware() gin.HandlerFunc {
c.Set("target", postspolicies.GET_POST) c.Set("target", postspolicies.GET_POST)
} }
isAllowed, err := postspolicies.PostsPolicies(c) isAllowed, errors := postspolicies.PostsPolicies(c)
if err != nil { var errorsMap = map[int]string{}
rest_api_stuff.InternalErrorAnswer(c, err) for i, error := range errors {
errorsMap[i] = error.Error()
}
if errors != nil {
c.IndentedJSON(http.StatusUnauthorized, errorsMap)
c.Abort() c.Abort()
return return
} }

View File

@ -18,20 +18,14 @@ func UpdatePost(c *gin.Context) {
return return
} }
_, err := getters.GetUserIdFromContext(c) postId, err := getters.GetInt64Param(c, "post-id")
if err != nil { if err != nil {
rest_api_stuff.InternalErrorAnswer(c, err) rest_api_stuff.InternalErrorAnswer(c, err)
return return
} }
// if isOwner, _ := checkRole.IsOwnerOfThePost( UpdatedPostParams.PostID = postId
// userId,
// UpdatedPostParams.PostID,
// ); !isOwner {
// rest_api_stuff.UnauthorizedAnswer(c, fmt.Errorf("you are now allowed to change this"))
// return
// }
_, err = db_repo.New( _, err = db_repo.New(
db_connection.Dbx, db_connection.Dbx,

View File

@ -1,4 +1,4 @@
package utils package routes
import ( import (
"enshi/middleware" "enshi/middleware"
@ -37,10 +37,6 @@ func SetupRotes(g *gin.Engine) error {
authRoutes.RegisterUser, authRoutes.RegisterUser,
) )
// Auth group routes
authGroup := g.Group("/")
authGroup.Use(middleware.AuthMiddleware())
postsGroup := g.Group("/") postsGroup := g.Group("/")
postsGroup.Use(middleware.PostsMiddleware()) postsGroup.Use(middleware.PostsMiddleware())
@ -61,6 +57,9 @@ func SetupRotes(g *gin.Engine) error {
postsRoutes.DeletePost, postsRoutes.DeletePost,
) )
// Auth group routes
authGroup := g.Group("/")
authGroup.Use(middleware.AuthMiddleware())
authGroup.PUT( authGroup.PUT(
"user-profiles", "user-profiles",
userProfileRoutes.UpdateUserProfile, userProfileRoutes.UpdateUserProfile,