Skip to content

Commit

Permalink
Merge pull request #15547 from norseto/auth_cache_filename_fix
Browse files Browse the repository at this point in the history
Fix long auth helper cache file name
  • Loading branch information
k8s-ci-robot committed Jul 18, 2023
2 parents 0cd216b + 7bc1ac6 commit 55c64ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/commands/helpers/kubectl_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -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':
Expand All @@ -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)
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/commands/helpers/kubectl_auth_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 55c64ca

Please sign in to comment.