Skip to content

Commit 1112a0f

Browse files
authored
use fips-compliant hash for K8sNameHash func (#35)
* add unit-test for K8sNameHash On-behalf-of: SAP <simon.bein@sap.com> Signed-off-by: Simon Bein <simontheleg@gmail.com> * use fips-compliant sha256 hash On-behalf-of: SAP <simon.bein@sap.com> Signed-off-by: Simon Bein <simontheleg@gmail.com> --------- Signed-off-by: Simon Bein <simontheleg@gmail.com>
1 parent d1159ba commit 1112a0f

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

pkg/controller/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controller
22

33
import (
4-
"crypto/sha1"
4+
"crypto/sha256"
55
"encoding/base32"
66
"reflect"
77
"strings"
@@ -10,15 +10,16 @@ import (
1010
)
1111

1212
const (
13-
maxLength int = 63
14-
Base32EncodeStdLowerCase = "abcdefghijklmnopqrstuvwxyz234567"
13+
Base32EncodeStdLowerCase = "abcdefghijklmnopqrstuvwxyz234567"
1514
)
1615

17-
// K8sNameHash takes any number of string arguments and computes a hash out of it, which is then base32-encoded to be a valid k8s resource name.
16+
// K8sNameHash takes any number of string arguments and computes a hash out of it, which is then base32-encoded to be a valid DNS1123Subdomain k8s resource name
1817
// The arguments are joined with '/' before being hashed.
1918
func K8sNameHash(ids ...string) string {
2019
name := strings.Join(ids, "/")
21-
h := sha1.New()
20+
// since we are not worried about length-extension attacks (in fact we are not even using hashing for
21+
// any security purposes), use sha2 for better performance compared to sha3
22+
h := sha256.New()
2223
_, _ = h.Write([]byte(name))
2324
// we need base32 encoding as some base64 (even url safe base64) characters are not supported by k8s
2425
// see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/

pkg/controller/utils_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"k8s.io/apimachinery/pkg/util/validation"
8+
)
9+
10+
func TestK8sNameHash(t *testing.T) {
11+
tt := []struct {
12+
input []string
13+
expHash string
14+
}{
15+
{
16+
[]string{"test1"},
17+
"dnhq5gcrs4mzrzzsa6cujsllg3b5ahhn67fkgmrvtvxr3a2woaka",
18+
},
19+
{
20+
// check that the same string produces the same hash
21+
[]string{"test1"},
22+
"dnhq5gcrs4mzrzzsa6cujsllg3b5ahhn67fkgmrvtvxr3a2woaka",
23+
},
24+
{
25+
[]string{"bla"},
26+
"jxz4h5upzsb3e7u5ileqimnhesm7c6dvzanftg2wnsmitoljm4bq",
27+
},
28+
{
29+
[]string{"some other test", "this is a very, very long string"},
30+
"rjphpfjbmwn6qqydv6xhtmj3kxrlzepn2tpwy4okw2ypoc3nlffq",
31+
},
32+
}
33+
34+
for _, tc := range tt {
35+
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
36+
res := K8sNameHash(tc.input...)
37+
38+
if res != tc.expHash {
39+
t.Errorf("exp hash %q, got %q", tc.expHash, res)
40+
}
41+
42+
// ensure the result is a valid DNS1123Subdomain
43+
if errs := validation.IsDNS1123Subdomain(res); errs != nil {
44+
t.Errorf("value %q is invalid: %v", res, errs)
45+
}
46+
47+
})
48+
}
49+
50+
}

0 commit comments

Comments
 (0)