-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13cc665
commit 8c5d36c
Showing
8 changed files
with
183 additions
and
73 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
const ( | ||
ADMIN string = "ckman" | ||
GUEST string = "guest" | ||
ORDINARY string = "ordinary" | ||
|
||
DefaultAdminName = "ckman" | ||
InternalOrdinaryName = "ordinary" | ||
) | ||
|
||
type UserInfo struct { | ||
Policy string | ||
Password string | ||
UserFile string | ||
} | ||
|
||
var UserMap map[string]UserInfo | ||
var lock sync.Mutex | ||
|
||
func LoadUsers(configPath string) { | ||
//usermap | ||
lock.Lock() | ||
defer lock.Unlock() | ||
UserMap = make(map[string]UserInfo) | ||
userPath := path.Join(configPath, "users") | ||
entries, err := os.ReadDir(userPath) | ||
if err == nil { | ||
for _, entry := range entries { | ||
if entry.IsDir() { | ||
continue | ||
} | ||
|
||
userfile := path.Join(userPath, entry.Name()) | ||
userinfos := strings.Split(entry.Name(), ".") | ||
if len(userinfos) != 2 { | ||
continue | ||
} | ||
username := userinfos[0] | ||
policy := userinfos[1] | ||
if policy != GUEST && policy != ORDINARY { | ||
continue | ||
} | ||
password, err := os.ReadFile(userfile) | ||
if err == nil { | ||
UserMap[username] = UserInfo{ | ||
Policy: policy, | ||
Password: string(password), | ||
UserFile: userfile, | ||
} | ||
} | ||
} | ||
} | ||
|
||
passwordFile := path.Join(configPath, "password") | ||
password, err := os.ReadFile(passwordFile) | ||
if err != nil { | ||
return | ||
} | ||
|
||
UserMap[DefaultAdminName] = UserInfo{ | ||
Policy: ADMIN, | ||
Password: string(password), | ||
UserFile: passwordFile, | ||
} | ||
|
||
UserMap[InternalOrdinaryName] = UserInfo{ | ||
Policy: ORDINARY, | ||
//Password: "change me", // InternalOrdinaryName 专门给userToken方式的用户使用,无需密码 | ||
} | ||
} | ||
|
||
func GetUserInfo(name string) (UserInfo, error) { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
if userinfo, ok := UserMap[name]; ok { | ||
return userinfo, nil | ||
} | ||
return UserInfo{}, fmt.Errorf("user %s doesn't exist", name) | ||
} |
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,35 @@ | ||
package enforce | ||
|
||
func GuestPolicies() []Policy { | ||
return []Policy{ | ||
{"/ck/cluster", GET}, | ||
{"/ck/cluster/*", GET}, | ||
{"/ck/table/*", GET}, | ||
{"/ck/table/group_uniq_array/*", GET}, | ||
{"/ck/query/*", GET}, | ||
{"/ck/query_explain/*", GET}, | ||
{"/ck/query_history/*", GET}, | ||
{"/ck/table_lists/*", GET}, | ||
{"/ck/table_schema/*", GET}, | ||
{"/ck/get/*", GET}, | ||
{"/ck/partition/*", GET}, | ||
{"/ck/table_metric/*", GET}, | ||
{"/ck/table_merges/*", GET}, | ||
{"/ck/open_sessions/*", GET}, | ||
{"/ck/slow_sessions/*", GET}, | ||
{"/ck/ddl_queue/*", GET}, | ||
{"/ck/node/log/*", POST}, | ||
{"/ck/ping/*", POST}, | ||
{"/ck/config/*", GET}, | ||
{"/zk/status/*", GET}, | ||
{"/zk/replicated_table/*", GET}, | ||
{"/package", GET}, | ||
{"/metric/query/*", GET}, | ||
{"/metric/query_range/*", GET}, | ||
{"/version", GET}, | ||
{"/ui/schema", GET}, | ||
{"/task/*", GET}, | ||
{"/task/lists", GET}, | ||
{"/task/running", GET}, | ||
} | ||
} |
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,24 @@ | ||
package enforce | ||
|
||
func OrdinaryPolicies() []Policy { | ||
var OrdinaryPolicies = []Policy{ | ||
{"/ck/table/*", POST}, | ||
{"/ck/dist_logic_table/*", POST}, | ||
{"/ck/dist_logic_table/*", DELETE}, | ||
{"/ck/table/*", PUT}, | ||
{"/ck/truncate_table/*", DELETE}, | ||
{"/ck/table/ttl/*", PUT}, | ||
{"/ck/table/readonly/*", PUT}, | ||
{"/ck/table/view/*", PUT}, | ||
{"/ck/table/orderby/*", PUT}, | ||
{"/ck/table/group_uniq_array/*", POST}, | ||
{"/ck/table/group_uniq_array/*", DELETE}, | ||
{"/ck/table/*", DELETE}, | ||
{"/ck/table_all/*", DELETE}, | ||
{"/ck/query_history/*", DELETE}, | ||
{"/ck/open_sessions/*", PUT}, | ||
{"/ck/purge_tables/*", POST}, | ||
{"/ck/archive/*", POST}, | ||
} | ||
return append(OrdinaryPolicies, GuestPolicies()...) | ||
} |
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