Skip to content

Commit 204a5f9

Browse files
authored
Allow compute/app engine default service account ids in regex (#1390)
1 parent 31a5c9d commit 204a5f9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

google/validation.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const (
2020

2121
RFC1035NameTemplate = "[a-z](?:[-a-z0-9]{%d,%d}[a-z0-9])"
2222
CloudIoTIdRegex = "^[a-zA-Z][-a-zA-Z0-9._+~%]{2,254}$"
23+
24+
// Format of default Compute service accounts created by Google
25+
// ${PROJECT_ID}[email protected] where PROJECT_ID is an int64 (max 20 digits)
26+
ComputeServiceAccountNameRegex = "[0-9]{1,20}[email protected]"
2327
)
2428

2529
var (
@@ -29,8 +33,20 @@ var (
2933
// 4 and 28 since the first and last character are excluded.
3034
ServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, 4, 28)
3135

32-
ProjectNameInDNSFormRegex = "[-a-z0-9\\.]{1,63}"
33-
ServiceAccountLinkRegex = "projects/" + ProjectRegex + "/serviceAccounts/" + ServiceAccountNameRegex + "@" + ProjectNameInDNSFormRegex + "\\.iam\\.gserviceaccount\\.com$"
36+
ServiceAccountLinkRegexPrefix = "projects/" + ProjectRegex + "/serviceAccounts/"
37+
PossibleServiceAccountNames = []string{
38+
AppEngineServiceAccountNameRegex,
39+
ComputeServiceAccountNameRegex,
40+
CreatedServiceAccountNameRegex,
41+
}
42+
ServiceAccountLinkRegex = ServiceAccountLinkRegexPrefix + "(" + strings.Join(PossibleServiceAccountNames, "|") + ")"
43+
44+
// Format of service accounts created through the API
45+
CreatedServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, 4, 28) + "@" + ProjectNameInDNSFormRegex + "\\.iam\\.gserviceaccount\\.com$"
46+
ProjectNameInDNSFormRegex = "[-a-z0-9\\.]{1,63}"
47+
48+
// Format of default App Engine service accounts created by Google
49+
AppEngineServiceAccountNameRegex = ProjectRegex + "@appspot.gserviceaccount.com"
3450
)
3551

3652
var rfc1918Networks = []string{

google/validation_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func TestValidateServiceAccountLink(t *testing.T) {
117117
{TestName: "valid with dash", Value: "projects/my-project/serviceAccounts/[email protected]"},
118118
{TestName: "valid with colon", Value: "projects/my:project/serviceAccounts/[email protected]"},
119119
{TestName: "valid with dot and colon", Value: "projects/my.thing:project/serviceAccounts/[email protected]"},
120+
{TestName: "valid with compute default service account", Value: "projects/my-project/serviceAccounts/[email protected]"},
121+
{TestName: "valid with app engine default service account", Value: "projects/my-project/serviceAccounts/[email protected]"},
120122

121123
// Errors
122124
{TestName: "multiple colons", Value: "projects/my:project:thing/serviceAccounts/[email protected]", ExpectError: true},

0 commit comments

Comments
 (0)