-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: support user-defined builtin roles
add new configuration: builtinRoles user can define roles in config file: milvus.yaml there is an example: 1. db_ro, only have read privileges, include load 2. db_rw, read and write privileges, include create/drop/rename collection 3. db_admin, not only read and write privileges, but also user administration Signed-off-by: PowderLi <[email protected]>
- Loading branch information
Showing
12 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package paramtable | ||
|
||
import ( | ||
"github.com/milvus-io/milvus/pkg/config" | ||
"github.com/milvus-io/milvus/pkg/util/funcutil" | ||
) | ||
|
||
type roleConfig struct { | ||
Enabled ParamItem `refreshable:"false"` | ||
Roles ParamItem `refreshable:"false"` | ||
} | ||
|
||
func (p *roleConfig) init(base *BaseTable) { | ||
p.Enabled = ParamItem{ | ||
Key: "builtinRoles.enable", | ||
DefaultValue: "false", | ||
Version: "2.3.4", | ||
Doc: "Whether to init builtin roles", | ||
Export: true, | ||
} | ||
p.Enabled.Init(base.mgr) | ||
|
||
p.Roles = ParamItem{ | ||
Key: "builtinRoles.roles", | ||
DefaultValue: `{}`, | ||
Version: "2.3.4", | ||
Doc: "what builtin roles should be init", | ||
Export: true, | ||
} | ||
p.Roles.Init(base.mgr) | ||
|
||
p.panicIfNotValid(base.mgr) | ||
} | ||
|
||
func (p *roleConfig) panicIfNotValid(mgr *config.Manager) { | ||
if p.Enabled.GetAsBool() { | ||
m := p.Roles.GetAsRoleDetails() | ||
if m == nil { | ||
panic("builtinRoles.roles not invalid, should be json format") | ||
} | ||
|
||
j := funcutil.RoleDetailsToJSON(m) | ||
mgr.SetConfig("builtinRoles.roles", string(j)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package paramtable | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/milvus-io/milvus/pkg/config" | ||
) | ||
|
||
func TestRoleConfig_Init(t *testing.T) { | ||
params := ComponentParam{} | ||
params.Init(NewBaseTable(SkipRemote(true))) | ||
cfg := ¶ms.RoleCfg | ||
assert.Equal(t, cfg.Enabled.GetAsBool(), false) | ||
assert.Equal(t, cfg.Roles.GetValue(), "{}") | ||
assert.Equal(t, len(cfg.Roles.GetAsJSONMap()), 0) | ||
} | ||
|
||
func TestRoleConfig_Invalid(t *testing.T) { | ||
t.Run("valid roles", func(t *testing.T) { | ||
mgr := config.NewManager() | ||
mgr.SetConfig("builtinRoles.enable", "true") | ||
mgr.SetConfig("builtinRoles.roles", `{"db_admin": {"privileges": [{"object_type": "Global", "object_name": "*", "privilege": "CreateCollection", "db_name": "*"}]}}`) | ||
p := &roleConfig{ | ||
Enabled: ParamItem{ | ||
Key: "builtinRoles.enable", | ||
}, | ||
Roles: ParamItem{ | ||
Key: "builtinRoles.roles", | ||
}, | ||
} | ||
p.Enabled.Init(mgr) | ||
p.Roles.Init(mgr) | ||
assert.NotPanics(t, func() { | ||
p.panicIfNotValid(mgr) | ||
}) | ||
}) | ||
t.Run("invalid roles", func(t *testing.T) { | ||
mgr := config.NewManager() | ||
mgr.SetConfig("builtinRoles.enable", "true") | ||
mgr.SetConfig("builtinRoles.roles", `{"db_admin": {"privileges": {"object_type": "Global", "object_name": "*", "privilege": "CreateCollection", "db_name": "*"}}}`) | ||
p := &roleConfig{ | ||
Enabled: ParamItem{ | ||
Key: "builtinRoles.enable", | ||
}, | ||
Roles: ParamItem{ | ||
Key: "builtinRoles.roles", | ||
}, | ||
} | ||
p.Enabled.Init(mgr) | ||
p.Roles.Init(mgr) | ||
assert.Panics(t, func() { | ||
p.panicIfNotValid(mgr) | ||
}) | ||
}) | ||
} |