Skip to content

Commit

Permalink
Restrict length of volumes generated from secret names
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Dec 16, 2019
1 parent 0f20c35 commit 9b43d99
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/names/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ func (simpleNameGenerator) RestrictLengthWithRandomSuffix(base string) string {
return fmt.Sprintf("%s-%s", base, utilrand.String(randomLength))
}

var alphaNumericRE = regexp.MustCompile(`^[a-zA-Z0-9]+$`)

func (simpleNameGenerator) RestrictLength(base string) string {
if len(base) > maxNameLength {
base = base[:maxNameLength]
}
var isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString

for !isAlphaNumeric(base[len(base)-1:]) {
for !alphaNumericRE.MatchString(base[len(base)-1:]) {
base = base[:len(base)-1]
}
return base
Expand Down
68 changes: 68 additions & 0 deletions pkg/names/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package names

import (
"strings"
"testing"

"github.com/tektoncd/pipeline/test/names"
)

func TestRestrictLengthWithRandomSuffix(t *testing.T) {
for _, c := range []struct {
in, want string
}{{
in: "hello",
want: "hello-9l9zj",
}, {
in: strings.Repeat("a", 100),
want: strings.Repeat("a", 57) + "-9l9zj",
}} {
t.Run(c.in, func(t *testing.T) {
names.TestingSeed()
got := SimpleNameGenerator.RestrictLengthWithRandomSuffix(c.in)
if got != c.want {
t.Errorf("RestrictLengthWithRandomSuffix:\n got %q\nwant %q", got, c.want)
}
})
}
}

func TestRestrictLength(t *testing.T) {
for _, c := range []struct {
in, want string
}{{
in: "hello",
want: "hello",
}, {
in: strings.Repeat("a", 100),
want: strings.Repeat("a", maxNameLength),
}, {
// Values that don't end with an alphanumeric value are
// trimmed until they do.
in: "abcdefg !@#!$",
want: "abcdefg",
}} {
t.Run(c.in, func(t *testing.T) {
got := SimpleNameGenerator.RestrictLength(c.in)
if got != c.want {
t.Errorf("RestrictLength:\n got %q\nwant %q", got, c.want)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/pod/creds_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/tektoncd/pipeline/pkg/credentials"
"github.com/tektoncd/pipeline/pkg/credentials/dockercreds"
"github.com/tektoncd/pipeline/pkg/credentials/gitcreds"
"github.com/tektoncd/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -64,7 +65,7 @@ func credsInit(credsImage string, serviceAccountName, namespace string, kubeclie
}

if matched {
name := fmt.Sprintf("tekton-internal-secret-volume-%s", secret.Name)
name := names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("tekton-internal-secret-volume-%s", secret.Name))
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: name,
MountPath: credentials.VolumeName(secret.Name),
Expand Down

0 comments on commit 9b43d99

Please sign in to comment.