Merge pull request #15547 from norseto/auth_cache_filename_fix

Fix long auth helper cache file name
This commit is contained in:
Kubernetes Prow Robot 2023-07-18 09:31:09 -07:00 committed by GitHub
commit 55c64ca970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"os/user"
"path/filepath"
@ -174,7 +175,11 @@ func cacheFilePath(kopsStateStore string, clusterName string) string {
b.WriteString(clusterName)
b.WriteByte(0)
hash := fmt.Sprintf("%x", sha256.New().Sum(b.Bytes()))
var i big.Int
hb := sha256.Sum224(b.Bytes())
i.SetBytes(hb[:])
hash := i.Text(62)
sanitizedName := strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z':
@ -187,6 +192,9 @@ func cacheFilePath(kopsStateStore string, clusterName string) string {
return '_'
}
}, clusterName)
if len(sanitizedName) > 32 {
sanitizedName = sanitizedName[:32]
}
return filepath.Join(homedir.HomeDir(), ".kube", "cache", "kops-authentication", sanitizedName+"_"+hash)
}

View File

@ -0,0 +1,44 @@
/*
Copyright 2023 The Kubernetes 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 helpers
import (
"path"
"testing"
)
func Test_cacheFilePath(t *testing.T) {
inputs := []struct {
kopsStateStore string
clusterName string
}{
{
kopsStateStore: "s3://abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk",
clusterName: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde." +
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk." +
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk." +
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.com",
},
}
output1 := cacheFilePath(inputs[0].kopsStateStore, inputs[0].clusterName)
_, file := path.Split(output1)
if len(file) > 71 {
t.Errorf("cacheFilePath() got %v, too long(%v)", output1, len(file))
}
}