Skip to content

Commit 48af43e

Browse files
authored
validate actions secret names (#714)
* validate actions secret names * remove duplicate case
1 parent 00a960a commit 48af43e

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

github/resource_github_actions_organization_secret.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ func resourceGithubActionsOrganizationSecret() *schema.Resource {
2020

2121
Schema: map[string]*schema.Schema{
2222
"secret_name": {
23-
Type: schema.TypeString,
24-
Required: true,
25-
ForceNew: true,
23+
Type: schema.TypeString,
24+
Required: true,
25+
ForceNew: true,
26+
ValidateFunc: validateSecretNameFunc,
2627
},
2728
"plaintext_value": {
2829
Type: schema.TypeString,

github/resource_github_actions_secret.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ func resourceGithubActionsSecret() *schema.Resource {
2525
Required: true,
2626
},
2727
"secret_name": {
28-
Type: schema.TypeString,
29-
Required: true,
30-
ForceNew: true,
28+
Type: schema.TypeString,
29+
Required: true,
30+
ForceNew: true,
31+
ValidateFunc: validateSecretNameFunc,
3132
},
3233
"plaintext_value": {
3334
Type: schema.TypeString,

github/util.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"regexp"
78
"strconv"
89
"strings"
910

@@ -143,3 +144,23 @@ func getTeamID(teamIDString string, meta interface{}) (int64, error) {
143144
return team.GetID(), nil
144145
}
145146
}
147+
148+
// https://docs.github.com/en/actions/reference/encrypted-secrets#naming-your-secrets
149+
var secretNameRegexp = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
150+
151+
func validateSecretNameFunc(v interface{}, keyName string) (we []string, errs []error) {
152+
name, ok := v.(string)
153+
if !ok {
154+
return nil, []error{fmt.Errorf("expected type of %s to be string", keyName)}
155+
}
156+
157+
if !secretNameRegexp.MatchString(name) {
158+
errs = append(errs, errors.New("Secret names can only contain alphanumeric characters or underscores and must not start with a number"))
159+
}
160+
161+
if strings.HasPrefix(strings.ToUpper(name), "GITHUB_") {
162+
errs = append(errs, errors.New("Secret names must not start with the GITHUB_ prefix"))
163+
}
164+
165+
return we, errs
166+
}

github/util_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,52 @@ func flipUsernameCase(username string) string {
106106
}
107107
return string(oc)
108108
}
109+
110+
func TestAccGithubUtilValidateSecretName(t *testing.T) {
111+
cases := []struct {
112+
Name string
113+
Error bool
114+
}{
115+
{
116+
Name: "valid",
117+
},
118+
{
119+
Name: "v",
120+
},
121+
{
122+
Name: "_valid_underscore_",
123+
},
124+
{
125+
Name: "valid_digit_1",
126+
},
127+
{
128+
Name: "invalid-dashed",
129+
Error: true,
130+
},
131+
{
132+
Name: "1_invalid_leading_digit",
133+
Error: true,
134+
},
135+
{
136+
Name: "GITHUB_PREFIX",
137+
Error: true,
138+
},
139+
{
140+
Name: "github_prefix",
141+
Error: true,
142+
},
143+
}
144+
145+
for _, tc := range cases {
146+
var name interface{} = tc.Name
147+
_, errors := validateSecretNameFunc(name, "")
148+
149+
if tc.Error != (len(errors) != 0) {
150+
if tc.Error {
151+
t.Fatalf("expected error, got none (%s)", tc.Name)
152+
} else {
153+
t.Fatalf("unexpected error(s): %s (%s)", errors, tc.Name)
154+
}
155+
}
156+
}
157+
}

0 commit comments

Comments
 (0)