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

Develop #306

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.14
1.12.15
68 changes: 68 additions & 0 deletions pkg/abac/pap/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type GroupController interface {
_type, id, systemID string, beforeExpiredAt, limit, offset int64,
) ([]SubjectGroup, error)
ListGroupSubjectBeforeExpiredAtBySubjects(subjects []Subject, expiredAt int64) ([]GroupSubject, error)
ListSubjectGroupDetails(_type, id string, groupIDs []string) ([]SubjectGroup, error)
CheckSubjectEffectGroups(_type, id string, groupIDs []string) (map[string]map[string]interface{}, error)

GetGroupMemberCount(_type, id string) (int64, error)
Expand Down Expand Up @@ -169,6 +170,73 @@ func (c *groupController) ListGroupSubjectBeforeExpiredAtBySubjects(
return relations, nil
}

func (c *groupController) ListSubjectGroupDetails(_type, id string, groupIDs []string) ([]SubjectGroup, error) {
errorWrapf := errorx.NewLayerFunctionErrorWrapf(GroupCTL, "ListSubjectGroupDetails")

// subject Type+ID to PK
subjectPK, err := cacheimpls.GetLocalSubjectPK(_type, id)
if err != nil {
return nil, errorWrapf(err, "cacheimpls.GetLocalSubjectPK _type=`%s`, id=`%s` fail", _type, id)
}

groupPKToID := make(map[int64]string, len(groupIDs))
groupPKs := make([]int64, 0, len(groupIDs))
for _, groupID := range groupIDs {
// if groupID is empty, skip
if groupID == "" {
continue
}

// get the groupPK via groupID
groupPK, err := cacheimpls.GetLocalSubjectPK(types.GroupType, groupID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.WithError(err).Debugf("cacheimpls.GetSubjectPK type=`group`, id=`%s` fail", groupID)
continue
}

return nil, errorWrapf(
err,
"cacheimpls.GetSubjectPK _type=`%s`, id=`%s` fail",
types.GroupType,
groupID,
)
}

groupPKs = append(groupPKs, groupPK)
groupPKToID[groupPK] = groupID
}

// NOTE: if the performance is a problem, change this to a local cache, key: subjectPK, value int64Set
svcSubjectGroups, err := c.service.ListSubjectGroupsBySubjectPKGroupPKs(subjectPK, groupPKs)
if err != nil {
return nil, errorWrapf(
err,
"service.ListSubjectGroupsBySubjectPKGroupPKs subjectPKs=`%d`, groupPKs=`%+v` fail",
subjectPK,
groupPKs,
)
}

groups := make([]SubjectGroup, 0, len(svcSubjectGroups))
for _, m := range svcSubjectGroups {
groupID, ok := groupPKToID[m.GroupPK]
if !ok {
continue
}

groups = append(groups, SubjectGroup{
PK: m.PK,
Type: types.GroupType,
ID: groupID,
ExpiredAt: m.ExpiredAt,
CreatedAt: m.CreatedAt,
})
}

return groups, nil
}

func (c *groupController) CheckSubjectEffectGroups(
_type, id string,
groupIDs []string,
Expand Down
89 changes: 89 additions & 0 deletions pkg/abac/pap/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,95 @@ var _ = Describe("GroupController", func() {
})
})

Describe("ListSubjectGroupDetails", func() {
var ctl *gomock.Controller
var patches *gomonkey.Patches
BeforeEach(func() {
ctl = gomock.NewController(GinkgoT())

patches = gomonkey.ApplyFunc(cacheimpls.GetLocalSubjectPK, func(_type, id string) (pk int64, err error) {
if _type == "user" && id == "1" {
return int64(1), nil
}
if _type == "user" && id == "2" {
return int64(2), nil
}
if _type == "group" && id == "10" {
return int64(10), nil
}

if _type == "group" && id == "20" {
return int64(20), nil
}

return 0, sql.ErrNoRows
})

patches.ApplyFunc(cacheimpls.GetSubjectDepartmentPKs, func(subjectPK int64) ([]int64, error) {
return []int64{10, 20, 30}, nil
})
})
AfterEach(func() {
ctl.Finish()
patches.Reset()
})

It("get user subject PK fail", func() {
c := &groupController{
service: mock.NewMockGroupService(ctl),
}

_, err := c.ListSubjectGroupDetails("user", "notexist", []string{"10", "20"})
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "cacheimpls.GetLocalSubjectPK")
})
It("get subject all group pks fail", func() {
mockGroupService := mock.NewMockGroupService(ctl)
mockGroupService.EXPECT().ListSubjectGroupsBySubjectPKGroupPKs(gomock.Any(), gomock.Any()).Return(
nil, errors.New("error"),
).AnyTimes()

c := &groupController{
service: mockGroupService,
}

_, err := c.ListSubjectGroupDetails("user", "1", []string{"10", "20"})

assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "ListSubjectGroupsBySubjectPKGroupPKs")
})

It("ok, all groupID valid", func() {
mockGroupService := mock.NewMockGroupService(ctl)
mockGroupService.EXPECT().ListSubjectGroupsBySubjectPKGroupPKs(gomock.Any(), gomock.Any()).Return(
[]types.SubjectGroup{{
PK: 1,
GroupPK: 10,
ExpiredAt: 1,
CreatedAt: time.Time{},
}, {
PK: 2,
GroupPK: 20,
ExpiredAt: 1,
CreatedAt: time.Time{},
}}, nil,
).AnyTimes()

c := &groupController{
service: mockGroupService,
}

groups, err := c.ListSubjectGroupDetails("user", "1", []string{"10", "20"})
assert.NoError(GinkgoT(), err)
assert.Len(GinkgoT(), groups, 2)
assert.Equal(GinkgoT(), groups[0].PK, int64(1))
assert.Equal(GinkgoT(), groups[0].ID, "10")
assert.Equal(GinkgoT(), groups[1].PK, int64(2))
assert.Equal(GinkgoT(), groups[1].ID, "20")

})
})

Describe("CheckSubjectExistGroups", func() {
var ctl *gomock.Controller
var patches *gomonkey.Patches
Expand Down
15 changes: 15 additions & 0 deletions pkg/abac/pap/mock/group.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/api/model/handler/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/actions [post]
// @Router /api/v1/model/systems/{system_id}/actions [post]
func BatchCreateActions(c *gin.Context) {
var body []actionSerializer
if err := c.ShouldBindJSON(&body); err != nil {
Expand Down Expand Up @@ -123,7 +123,7 @@ func BatchCreateActions(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/actions/{action_id} [put]
// @Router /api/v1/model/systems/{system_id}/actions/{action_id} [put]
func UpdateAction(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down Expand Up @@ -256,7 +256,7 @@ func UpdateAction(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/actions/{action_id} [delete]
// @Router /api/v1/model/systems/{system_id}/actions/{action_id} [delete]
func DeleteAction(c *gin.Context) {
systemID := c.Param("system_id")
actionID := c.Param("action_id")
Expand All @@ -280,7 +280,7 @@ func DeleteAction(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/actions [delete]
// @Router /api/v1/model/systems/{system_id}/actions [delete]
func BatchDeleteActions(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down
6 changes: 3 additions & 3 deletions pkg/api/model/handler/instance_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/instance-selections [post]
// @Router /api/v1/model/systems/{system_id}/instance-selections [post]
func BatchCreateInstanceSelections(c *gin.Context) {
var body []instanceSelectionSerializer
if err := c.ShouldBindJSON(&body); err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func BatchCreateInstanceSelections(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/instance-selections/{instance_selection_id} [put]
// @Router /api/v1/model/systems/{system_id}/instance-selections/{instance_selection_id} [put]
func UpdateInstanceSelection(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down Expand Up @@ -197,7 +197,7 @@ func UpdateInstanceSelection(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/instance-selections/{instance_selection_id} [delete]
// @Router /api/v1/model/systems/{system_id}/instance-selections/{instance_selection_id} [delete]
func DeleteInstanceSelection(c *gin.Context) {
systemID := c.Param("system_id")
instanceSelectionID := c.Param("instance_selection_id")
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/model/handler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ const (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/query [get]
//
// @Router /api/v1/model/systems/{system_id}/query [get]
//nolint:gocognit
func SystemInfoQuery(c *gin.Context) {
var query querySerializer
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/model/handler/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/resource-types [post]
// @Router /api/v1/model/systems/{system_id}/resource-types [post]
func BatchCreateResourceTypes(c *gin.Context) {
var body []resourceTypeSerializer
if err := c.ShouldBindJSON(&body); err != nil {
Expand Down Expand Up @@ -117,7 +117,7 @@ func BatchCreateResourceTypes(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/resource-types/{resource_type_id} [put]
// @Router /api/v1/model/systems/{system_id}/resource-types/{resource_type_id} [put]
func UpdateResourceType(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down Expand Up @@ -219,7 +219,7 @@ func UpdateResourceType(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/resource-types/{resource_type_id} [delete]
// @Router /api/v1/model/systems/{system_id}/resource-types/{resource_type_id} [delete]
func DeleteResourceType(c *gin.Context) {
systemID := c.Param("system_id")
resourceTypeID := c.Param("resource_type_id")
Expand All @@ -242,7 +242,7 @@ func DeleteResourceType(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/resource-types [delete]
// @Router /api/v1/model/systems/{system_id}/resource-types [delete]
func BatchDeleteResourceTypes(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down
8 changes: 4 additions & 4 deletions pkg/api/model/handler/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func defaultValidClients(c *gin.Context, originClients string) string {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems [post]
// @Router /api/v1/model/systems [post]
func CreateSystem(c *gin.Context) {
// validate the body
var body systemSerializer
Expand Down Expand Up @@ -122,7 +122,7 @@ func CreateSystem(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id} [put]
// @Router /api/v1/model/systems/{system_id} [put]
func UpdateSystem(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down Expand Up @@ -210,7 +210,7 @@ func UpdateSystem(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id} [get]
// @Router /api/v1/model/systems/{system_id} [get]
func GetSystem(c *gin.Context) {
// validate the body
systemID := c.Param("system_id")
Expand Down Expand Up @@ -253,7 +253,7 @@ func GetSystem(c *gin.Context) {
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/clients [get]
// @Router /api/v1/model/systems/{system_id}/clients [get]
func GetSystemClients(c *gin.Context) {
// validate the body
systemID := c.Param("system_id")
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/model/handler/system_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/configs/{name} [POST]
// @Router /api/v1/model/systems/{system_id}/configs/{name} [POST]
func CreateOrUpdateConfigDispatch(c *gin.Context) {
systemID := c.Param("system_id")

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/model/handler/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
// @Header 200 {string} X-Request-Id "the request id"
// @Security AppCode
// @Security AppSecret
// @Router /api/v1/systems/{system_id}/token [get]
// @Router /api/v1/model/systems/{system_id}/token [get]
func GetToken(c *gin.Context) {
// validate the body
systemID := c.Param("system_id")
Expand Down
Loading
Loading