Skip to content

Commit

Permalink
Merge pull request #133 from TencentBlueKing/develop
Browse files Browse the repository at this point in the history
1.11.7
  • Loading branch information
wklken authored Jun 16, 2022
2 parents 558efe5 + a72fbc5 commit bdce7fc
Show file tree
Hide file tree
Showing 26 changed files with 578 additions and 76 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.6
1.11.7
8 changes: 8 additions & 0 deletions build/support-files/sql/0021_iam_20220425-1050_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS `bkiam`.`subject_black_list` (
`pk` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`subject_pk` INT UNSIGNED NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`pk`),
UNIQUE KEY `idx_uk_subject_pk` (`subject_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2 changes: 1 addition & 1 deletion cmd/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Start() {
initSuperAppCode()
initSuperUser()
initSupportShieldFeatures()
initShareAppCode()
initSecurityAuditAppCode()
initComponents()
initQuota()
initSwitch()
Expand Down
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func initSupportShieldFeatures() {
config.InitSupportShieldFeatures(globalConfig.SupportShieldFeatures)
}

func initShareAppCode() {
config.InitShareAppCode(globalConfig.ShareAppCode)
func initSecurityAuditAppCode() {
config.InitSecurityAuditAppCode(globalConfig.SecurityAuditAppCode)
}

func initComponents() {
Expand Down
37 changes: 32 additions & 5 deletions pkg/api/policy/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package handler

import (
"errors"
"fmt"
"time"

"github.com/TencentBlueKing/gopkg/errorx"
Expand Down Expand Up @@ -57,21 +58,30 @@ func Auth(c *gin.Context) {
return
}

// check blacklist
if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
util.SystemErrorJSONResponse(c, err)
return
}

if hasSuperPerm {
util.SuccessJSONResponse(c, "ok", authResponse{
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", authResponse{
Allowed: true,
})
return
}

// 隔离结构体
var req = request.NewRequest()
req := request.NewRequest()
copyRequestFromAuthBody(req, &body)

// 鉴权
Expand Down Expand Up @@ -134,6 +144,15 @@ func BatchAuthByActions(c *gin.Context) {

result := make(authByActionsResponse, len(body.Actions))

// check blacklist
if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

// super admin and system admin
hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
Expand All @@ -145,7 +164,7 @@ func BatchAuthByActions(c *gin.Context) {
for _, action := range body.Actions {
result[action.ID] = true
}
util.SuccessJSONResponse(c, "ok", result)
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", result)
return
}

Expand Down Expand Up @@ -224,6 +243,14 @@ func BatchAuthByResources(c *gin.Context) {

data := make(authByResourcesResponse, len(body.ResourcesList))

if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

// super admin and system admin
hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
Expand All @@ -236,12 +263,12 @@ func BatchAuthByResources(c *gin.Context) {
data[buildResourceID(r)] = true
}

util.SuccessJSONResponse(c, "ok", data)
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", data)
return
}

// 隔离结构体
var req = request.NewRequest()
req := request.NewRequest()
copyRequestFromAuthByResourcesBody(req, &body)

// 鉴权
Expand Down
36 changes: 31 additions & 5 deletions pkg/api/policy/handler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ package handler

import (
"errors"
"fmt"

"github.com/TencentBlueKing/gopkg/errorx"
"github.com/gin-gonic/gin"

"iam/pkg/abac/pdp"
"iam/pkg/abac/types"
"iam/pkg/abac/types/request"
"iam/pkg/cacheimpls"
"iam/pkg/logging/debug"
"iam/pkg/util"
)
Expand Down Expand Up @@ -55,19 +57,27 @@ func Query(c *gin.Context) {
return
}

if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
util.SystemErrorJSONResponse(c, err)
return
}

if hasSuperPerm {
util.SuccessJSONResponse(c, "ok", AnyExpression)
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", AnyExpression)
return
}

// 隔离结构体
var req = request.NewRequest()
req := request.NewRequest()
copyRequestFromQueryBody(req, &body)

var entry *debug.Entry
Expand Down Expand Up @@ -133,6 +143,14 @@ func BatchQueryByActions(c *gin.Context) {

policies := make([]actionPoliciesResponse, 0, len(body.Actions))

if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
util.SystemErrorJSONResponse(c, err)
Expand All @@ -146,7 +164,7 @@ func BatchQueryByActions(c *gin.Context) {
Condition: AnyExpression,
})
}
util.SuccessJSONResponse(c, "ok", policies)
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", policies)
return
}

Expand Down Expand Up @@ -226,6 +244,14 @@ func QueryByExtResources(c *gin.Context) {
return
}

if cacheimpls.IsSubjectInBlackList(body.Subject.Type, body.Subject.ID) {
util.ForbiddenJSONResponse(
c,
fmt.Sprintf("subject(type=%s,id=%s) has been frozen", body.Subject.Type, body.Subject.ID),
)
return
}

hasSuperPerm, err := hasSystemSuperPermission(systemID, body.Subject.Type, body.Subject.ID)
if err != nil {
util.SystemErrorJSONResponse(c, err)
Expand All @@ -250,15 +276,15 @@ func QueryByExtResources(c *gin.Context) {
extResourcesWithAttr = append(extResourcesWithAttr, extResourceWithAttr)
}

util.SuccessJSONResponse(c, "ok", map[string]interface{}{
util.SuccessJSONResponse(c, "ok, as super_manager or system_manager", map[string]interface{}{
"expression": AnyExpression,
"ext_resources": extResourcesWithAttr,
})
return
}

// 隔离结构体
var req = request.NewRequest()
req := request.NewRequest()
copyRequestFromQueryBody(req, &body.queryRequest)

var entry *debug.Entry
Expand Down
9 changes: 8 additions & 1 deletion pkg/api/policy/handler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func buildResourceID(rs []resource) string {
return strings.Join(nodes, "/")
}

// ValidateSystemMatchClient ...
// ValidateSystemMatchClient will check if the client can call the system's policy/[query/auth]
// note that, the audit app_code can access all system's policy/[query/auth]
// so, this function should be only called in this module: policy/handler
func ValidateSystemMatchClient(systemID, clientID string) error {
if systemID == "" || clientID == "" {
return fmt.Errorf("system_id or client_id do not allow empty")
Expand All @@ -164,6 +166,11 @@ func ValidateSystemMatchClient(systemID, clientID string) error {
return fmt.Errorf("get system(%s) valid clients fail, err=%w", systemID, err)
}

// security audit app can be the valid client of all systems
if config.SecurityAuditAppCode.Has(clientID) {
return nil
}

for _, c := range validClients {
if clientID == c {
return nil
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/policy/handler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/TencentBlueKing/gopkg/collection/set"
"github.com/agiledragon/gomonkey/v2"
. "github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -217,7 +218,6 @@ func TestAnyExpression(t *testing.T) {
}

var _ = Describe("util", func() {

Describe("validateSystemSuperUser", func() {
var patches *gomonkey.Patches
BeforeEach(func() {
Expand Down Expand Up @@ -279,7 +279,6 @@ var _ = Describe("util", func() {
})

Describe("buildResourceID", func() {

It("empty", func() {
uid := buildResourceID([]resource{})
assert.Equal(GinkgoT(), uid, "")
Expand Down Expand Up @@ -315,16 +314,16 @@ var _ = Describe("util", func() {
})

func Test_validateSystemMatchClient(t *testing.T) {
var (
expiration = 5 * time.Minute
)
expiration := 5 * time.Minute
retrieveFunc := func(key cache.Key) (interface{}, error) {
return []string{"test"}, nil
}
mockCache := memory.NewCache(
"mockCache", false, retrieveFunc, expiration, nil)
cacheimpls.LocalSystemClientsCache = mockCache

config.SecurityAuditAppCode = set.NewStringSetWithValues([]string{"audit_app"})

type args struct {
systemID string
clientID string
Expand All @@ -342,6 +341,14 @@ func Test_validateSystemMatchClient(t *testing.T) {
},
want: true,
},
{
name: "right, secure audit app",
args: args{
systemID: "test",
clientID: "audit_app",
},
want: true,
},
{
name: "empty_system",
args: args{
Expand Down
Loading

0 comments on commit bdce7fc

Please sign in to comment.