-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
初始化tencentcloud-vault开源代码,以后腾讯云国际站维护
- Loading branch information
Showing
45 changed files
with
2,614 additions
and
2,510 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,114 @@ | ||
package vault_plugin_auth_tencentcloud | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
arnRoleType arnType = iota // roleName | ||
arnAssumedRoleType // assumed-role | ||
) | ||
|
||
const ( | ||
roleName = "roleName" | ||
assumedRole = "assumed-role" | ||
) | ||
|
||
type arnType int | ||
|
||
// toString | ||
func (t arnType) String() string { | ||
switch t { | ||
case arnRoleType: | ||
return roleName | ||
case arnAssumedRoleType: | ||
return assumedRole | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
type arn struct { | ||
Uin string | ||
RoleName string | ||
RoleId string | ||
Full string | ||
Type arnType | ||
} | ||
|
||
// check member | ||
func (a *arn) IsMemberOf(possibleParent *arn) bool { | ||
if possibleParent.Type != arnRoleType && possibleParent.Type != arnAssumedRoleType { | ||
return false | ||
} | ||
if possibleParent.Uin != a.Uin { | ||
return false | ||
} | ||
if possibleParent.RoleName != a.RoleName { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func parseARN(a string) (*arn, error) { | ||
// camArn should look like one of the following: | ||
// 1. qcs::cam::uin/<uin>:roleName/<RoleName> | ||
// 2. qcs::sts:<uin>:assumed-role/<RoleId> | ||
// if we get something like 2, then we want to transform that back to what | ||
// most people would expect, which is qcs::cam::uin/<uin>:roleName/<RoleName> | ||
if a == "" { | ||
return nil, fmt.Errorf("no arn provided") | ||
} | ||
parsed := &arn{ | ||
Full: a, | ||
} | ||
outerFields := strings.Split(a, ":") | ||
if len(outerFields) != 6 && len(outerFields) != 5 { | ||
return nil, fmt.Errorf("unrecognized arn: contains %d colon-separated fields, expected 6 or 5", len(outerFields)) | ||
} | ||
if outerFields[0] != "qcs" { | ||
return nil, errors.New(`unrecognized arn: does not begin with "qcs:"`) | ||
} | ||
if outerFields[2] != "cam" && outerFields[2] != "sts" { | ||
return nil, fmt.Errorf("unrecognized service: %v, not cam or sts", outerFields[2]) | ||
} | ||
if outerFields[2] == "cam" { | ||
uinFields := strings.Split(outerFields[4], "/") | ||
if len(uinFields) < 2 { | ||
return nil, fmt.Errorf("unrecognized arn: %q contains fewer than 2 slash-separated uinFields", outerFields[4]) | ||
} | ||
parsed.Uin = uinFields[1] | ||
roleFiles := strings.Split(outerFields[5], "/") | ||
if len(roleFiles) == 2 { | ||
parsed.Type = arnRoleType | ||
if roleFiles[0] == roleName { | ||
parsed.RoleName = roleFiles[1] | ||
} else { | ||
return nil, errors.New("the caller's arn does not match the role's arn") | ||
} | ||
} else { | ||
return nil, fmt.Errorf("unrecognized arn: %q contains fewer than 2 slash-separated roleFiles", outerFields[4]) | ||
} | ||
} else if outerFields[2] == "sts" { | ||
parsed.Uin = outerFields[3] | ||
roleFiles := strings.Split(outerFields[4], "/") | ||
if len(roleFiles) == 2 { | ||
parsed.Type = arnAssumedRoleType | ||
if roleFiles[0] == assumedRole { | ||
parsed.RoleId = roleFiles[1] | ||
} else { | ||
return nil, errors.New("the caller's arn does not match the role's arn") | ||
} | ||
} else { | ||
return nil, fmt.Errorf("unrecognized arn: %q contains fewer than 2 slash-separated roleFiles", outerFields[4]) | ||
} | ||
} | ||
return parsed, nil | ||
} | ||
|
||
// toString | ||
func (a *arn) String() string { | ||
return a.Full | ||
} |
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,55 @@ | ||
package vault_plugin_auth_tencentcloud | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseRoleArn(t *testing.T) { | ||
// qcs::cam::uin/100021543***:roleName/**** | ||
arn := "qcs::cam::uin/1000215438890:roleName/elk" | ||
result, err := parseARN(arn) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if result.Uin != "1000215438890" { | ||
t.Fatalf("got %s but expected %s", result.Uin, "1000215438890") | ||
} | ||
if result.Type != arnRoleType { | ||
t.Fatalf("got %d but expected %d", result.Type, arnRoleType) | ||
} | ||
if result.RoleName != "elk" { | ||
t.Fatalf("got %s but wanted %s", result.RoleName, "elk") | ||
} | ||
if result.RoleId != "" { | ||
t.Fatalf("got %s but wanted %s", result.RoleId, "") | ||
} | ||
} | ||
|
||
func TestParseAssumedRoleArn(t *testing.T) { | ||
// qcs::sts:1000262***:assumed-role/461168601842741*** | ||
arn := "qcs::sts:1000215438890:assumed-role/4611686018427418890" | ||
result, err := parseARN(arn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if result.Uin != "1000215438890" { | ||
t.Fatalf("got %s but expected %s", result.Uin, "1000215438890") | ||
} | ||
if result.Type != arnAssumedRoleType { | ||
t.Fatalf("got %d but expected %d", result.Type, arnAssumedRoleType) | ||
} | ||
if result.RoleName != "" { | ||
t.Fatalf("got %s but wanted %s", result.RoleName, "") | ||
} | ||
if result.RoleId != "4611686018427418890" { | ||
t.Fatalf("got %s but wanted %s", result.RoleId, "4611686018427418890") | ||
} | ||
} | ||
|
||
func TestParseEmpty(t *testing.T) { | ||
arn := "" | ||
_, err := parseARN(arn) | ||
if err == nil { | ||
t.Fatal("expected an err") | ||
} | ||
} |
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
Oops, something went wrong.