feat: (rbac) validate index and cluster unit test
This commit is contained in:
parent
e865ee5f67
commit
7006c0b256
|
@ -27,7 +27,8 @@ func init() {
|
||||||
BuildRoles["admin"] = map[string]interface{}{
|
BuildRoles["admin"] = map[string]interface{}{
|
||||||
"id": "admin",
|
"id": "admin",
|
||||||
"name": "管理员",
|
"name": "管理员",
|
||||||
"platform": []string{"system.role:all", "system.user:all"},
|
"type": "console",
|
||||||
|
"platform": AdminPrivilege,
|
||||||
"builtin": true,
|
"builtin": true,
|
||||||
"description": "is admin",
|
"description": "is admin",
|
||||||
"created": time.Now(),
|
"created": time.Now(),
|
||||||
|
|
|
@ -135,3 +135,27 @@ func CombineUserRoles(roleNames []string) RolePermission {
|
||||||
newRole.IndexPrivilege = m
|
newRole.IndexPrivilege = m
|
||||||
return newRole
|
return newRole
|
||||||
}
|
}
|
||||||
|
func FilterCluster(roles []string, cluster []string) []string {
|
||||||
|
newRole := CombineUserRoles(roles)
|
||||||
|
userClusterMap := make(map[string]struct{}, 0)
|
||||||
|
for _, v := range newRole.Cluster {
|
||||||
|
userClusterMap[v] = struct{}{}
|
||||||
|
}
|
||||||
|
realCluster := make([]string, 0)
|
||||||
|
for _, v := range cluster {
|
||||||
|
if _, ok := userClusterMap[v]; ok {
|
||||||
|
realCluster = append(realCluster, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return realCluster
|
||||||
|
}
|
||||||
|
func FilterIndex(roles []string, index []string) []string {
|
||||||
|
realIndex := make([]string, 0)
|
||||||
|
newRole := CombineUserRoles(roles)
|
||||||
|
for _, v := range index {
|
||||||
|
if _, ok := newRole.IndexPrivilege[v]; ok {
|
||||||
|
realIndex = append(realIndex, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return realIndex
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package biz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -99,6 +100,18 @@ func Test_validateCluster(t *testing.T) {
|
||||||
},
|
},
|
||||||
}, "no cluster permission",
|
}, "no cluster permission",
|
||||||
},
|
},
|
||||||
|
{"no cluster",
|
||||||
|
args{
|
||||||
|
req: ClusterRequest{
|
||||||
|
Cluster: []string{"cluster1"},
|
||||||
|
Privilege: []string{"indices.get_mapping"},
|
||||||
|
},
|
||||||
|
userRole: RolePermission{
|
||||||
|
Cluster: []string{},
|
||||||
|
ClusterPrivilege: []string{},
|
||||||
|
},
|
||||||
|
}, "no cluster permission",
|
||||||
|
},
|
||||||
{"no cluster api",
|
{"no cluster api",
|
||||||
args{
|
args{
|
||||||
req: ClusterRequest{
|
req: ClusterRequest{
|
||||||
|
@ -122,10 +135,162 @@ func Test_validateCluster(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
got := ValidateCluster(tt.args.req, tt.args.userRole)
|
got := ValidateCluster(tt.args.req, tt.args.userRole)
|
||||||
|
|
||||||
assert.EqualError(t, got, tt.want)
|
assert.EqualError(t, got, tt.want)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func TestStringInArray(t *testing.T) {
|
||||||
|
array := []string{"a", "b", "c", "d", "e"}
|
||||||
|
assert.Equal(t, true, util.StringInArray(array, "c"))
|
||||||
|
assert.Equal(t, false, util.StringInArray(array, "h"))
|
||||||
|
}
|
||||||
|
func TestFilterCluster(t *testing.T) {
|
||||||
|
RoleMap["test"] = Role{
|
||||||
|
Cluster: []struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Id: "c97rd2les10hml00pgh0",
|
||||||
|
Name: "docker-cluster",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ClusterPrivilege: []string{"cat.*"},
|
||||||
|
Index: []struct {
|
||||||
|
Name []string `json:"name"`
|
||||||
|
Privilege []string `json:"privilege"`
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: []string{".infini_rbac-role"},
|
||||||
|
Privilege: []string{"indices.get_mapping"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: []string{".infini_rbac-user", ".infini_rbac-role"},
|
||||||
|
Privilege: []string{"cat.*"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
roles []string
|
||||||
|
cluster []string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
cluster: []string{
|
||||||
|
"cluser1", "cluster2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "one",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
cluster: []string{
|
||||||
|
"cluser1", "cluster2", "c97rd2les10hml00pgh0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{"c97rd2les10hml00pgh0"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
cluster: []string{
|
||||||
|
"c97rd2les10hml00pgh0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{"c97rd2les10hml00pgh0"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := FilterCluster(tt.args.roles, tt.args.cluster)
|
||||||
|
assert.Equal(t, got, tt.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestFilterIndex(t *testing.T) {
|
||||||
|
RoleMap["test"] = Role{
|
||||||
|
Cluster: []struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Id: "c97rd2les10hml00pgh0",
|
||||||
|
Name: "docker-cluster",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ClusterPrivilege: []string{"cat.*"},
|
||||||
|
Index: []struct {
|
||||||
|
Name []string `json:"name"`
|
||||||
|
Privilege []string `json:"privilege"`
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: []string{".infini_rbac-role"},
|
||||||
|
Privilege: []string{"indices.get_mapping"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: []string{".infini_rbac-user", ".infini_rbac-role"},
|
||||||
|
Privilege: []string{"cat.*"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
roles []string
|
||||||
|
index []string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
index: []string{
|
||||||
|
"index1", "index2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "one",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
index: []string{
|
||||||
|
"index1", "index2", ".infini_rbac-user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{".infini_rbac-user"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only",
|
||||||
|
args: args{
|
||||||
|
roles: []string{"test"},
|
||||||
|
index: []string{
|
||||||
|
".infini_rbac-user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{".infini_rbac-user"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := FilterIndex(tt.args.roles, tt.args.index)
|
||||||
|
assert.Equal(t, got, tt.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@ func PermissionRequired(h httprouter.Handle, permissions ...string) httprouter.H
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
w = handleError(w, http.StatusUnauthorized, err)
|
w = handleError(w, http.StatusUnauthorized, err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = biz.ValidatePermission(claims, permissions)
|
err = biz.ValidatePermission(claims, permissions)
|
||||||
|
|
2
main.go
2
main.go
|
@ -138,8 +138,8 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("init alerting task error: %v", err)
|
log.Errorf("init alerting task error: %v", err)
|
||||||
}
|
}
|
||||||
rbacApi.Init()
|
|
||||||
}()
|
}()
|
||||||
|
go rbacApi.Init()
|
||||||
|
|
||||||
}, nil) {
|
}, nil) {
|
||||||
app.Run()
|
app.Run()
|
||||||
|
|
|
@ -27,7 +27,7 @@ func Init(cfg *config.AppConfig) {
|
||||||
|
|
||||||
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "doc/:index/_search"), m.IndexRequired(handler.HandleSearchDocumentAction, "doc.search"))
|
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "doc/:index/_search"), m.IndexRequired(handler.HandleSearchDocumentAction, "doc.search"))
|
||||||
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "doc/:index"), m.IndexRequired(handler.HandleAddDocumentAction, "doc.create"))
|
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "doc/:index"), m.IndexRequired(handler.HandleAddDocumentAction, "doc.create"))
|
||||||
api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "doc/:index/:docId"), m.IndexRequired(handler.HandleUpdateDocumentAction, "doc.create"))
|
api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "doc/:index/:docId"), m.IndexRequired(handler.HandleUpdateDocumentAction, "doc.update"))
|
||||||
api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "doc/:index/:docId"), m.ClusterRequired(handler.HandleDeleteDocumentAction, "doc.delete"))
|
api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "doc/:index/:docId"), m.ClusterRequired(handler.HandleDeleteDocumentAction, "doc.delete"))
|
||||||
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "doc/_validate"), handler.ValidateDocIDAction)
|
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "doc/_validate"), handler.ValidateDocIDAction)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package rbac
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
"infini.sh/console/internal/biz"
|
"infini.sh/console/internal/biz"
|
||||||
"infini.sh/console/internal/biz/enum"
|
"infini.sh/console/internal/biz/enum"
|
||||||
m "infini.sh/console/internal/middleware"
|
m "infini.sh/console/internal/middleware"
|
||||||
|
@ -11,7 +12,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
log "src/github.com/cihub/seelog"
|
log "src/github.com/cihub/seelog"
|
||||||
"src/github.com/mitchellh/mapstructure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Rbac struct {
|
type Rbac struct {
|
||||||
|
@ -73,30 +73,8 @@ func loadRolePermission() {
|
||||||
|
|
||||||
biz.RoleMap["admin"] = biz.Role{
|
biz.RoleMap["admin"] = biz.Role{
|
||||||
Platform: enum.AdminPrivilege,
|
Platform: enum.AdminPrivilege,
|
||||||
Cluster: []struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
Id: "c97rd2les10hml00pgh0",
|
|
||||||
Name: "docker-cluster",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ClusterPrivilege: []string{"cat.*"},
|
|
||||||
Index: []struct {
|
|
||||||
Name []string `json:"name"`
|
|
||||||
Privilege []string `json:"privilege"`
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
Name: []string{".infini_rbac-role"},
|
|
||||||
Privilege: []string{"indices.get_mapping"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: []string{".infini_rbac-user", ".infini_rbac-role"},
|
|
||||||
Privilege: []string{"cat.*"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := biz.SearchRole("", 0, 1000)
|
res, err := biz.SearchRole("", 0, 1000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
|
|
@ -56,17 +56,17 @@ func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
res, err := biz.SearchRole(keyword, from, size)
|
res, err := biz.SearchRole(keyword, from, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
|
||||||
h.ErrorInternalServer(w, err.Error())
|
h.ErrorInternalServer(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
response := elastic.SearchResponse{}
|
response := elastic.SearchResponse{}
|
||||||
util.FromJSONBytes(res.Raw, &response)
|
util.FromJSONBytes(res.Raw, &response)
|
||||||
|
|
||||||
list := response.Hits.Hits
|
hits := response.Hits.Hits
|
||||||
|
list := make([]elastic.IndexDocument, 0)
|
||||||
total := response.GetTotal()
|
total := response.GetTotal()
|
||||||
var index string
|
var index string
|
||||||
for _, v := range list {
|
for _, v := range hits {
|
||||||
index = v.Index
|
index = v.Index
|
||||||
}
|
}
|
||||||
for k, v := range enum.BuildRoles {
|
for k, v := range enum.BuildRoles {
|
||||||
|
@ -78,7 +78,7 @@ func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
})
|
})
|
||||||
total++
|
total++
|
||||||
}
|
}
|
||||||
|
list = append(list, hits...)
|
||||||
response.Hits.Hits = list
|
response.Hits.Hits = list
|
||||||
response.Hits.Total = total
|
response.Hits.Total = total
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue