Skip to content

Commit 699be88

Browse files
authored
refactor hdfs kerberos keytab load (#3874)
1 parent e2fa798 commit 699be88

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

Diff for: pkg/object/hdfs_kerberos.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ package object
88

99
import (
1010
"encoding/base64"
11-
"encoding/binary"
1211
"fmt"
1312
"github.com/jcmturner/gokrb5/v8/keytab"
1413
"os"
1514
"os/user"
16-
"path/filepath"
1715
"strings"
1816

1917
krb "github.com/jcmturner/gokrb5/v8/client"
@@ -35,30 +33,26 @@ func getKerberosClient() (*krb.Client, error) {
3533
// Try to authenticate with keytab file first.
3634
keytabPath := os.Getenv("KRB5KEYTAB")
3735
keytabBase64 := os.Getenv("KRB5KEYTAB_BASE64")
36+
principal := os.Getenv("KRB5PRINCIPAL")
37+
38+
var kt *keytab.Keytab
3839
if keytabBase64 != "" {
3940
decodedKeytab, err := base64.StdEncoding.DecodeString(keytabBase64)
4041
if err != nil {
4142
return nil, fmt.Errorf("error decoding Base64 encoded data %s", err)
4243
}
43-
decodedKeytabPath := filepath.Join(os.TempDir(), "decodedKeytab")
44-
decodedKeytabFile, err := os.Create(decodedKeytabPath)
45-
if err != nil {
46-
return nil, fmt.Errorf("failed to create %s", decodedKeytabPath)
47-
}
48-
defer decodedKeytabFile.Close()
49-
// keytab file format uses network byte order
50-
err = binary.Write(decodedKeytabFile, binary.BigEndian, decodedKeytab)
44+
kt = new(keytab.Keytab)
45+
err = kt.Unmarshal(decodedKeytab)
5146
if err != nil {
52-
return nil, fmt.Errorf("failed to write %s", decodedKeytabPath)
47+
return nil, err
5348
}
54-
keytabPath = decodedKeytabPath
55-
}
56-
principal := os.Getenv("KRB5PRINCIPAL")
57-
if keytabPath != "" && principal != "" {
58-
kt, err := keytab.Load(keytabPath)
49+
} else if keytabPath != "" {
50+
kt, err = keytab.Load(keytabPath)
5951
if err != nil {
6052
return nil, err
6153
}
54+
}
55+
if kt != nil {
6256
// e.g. KRB5PRINCIPAL="primary/instance@realm"
6357
sp := strings.Split(principal, "@")
6458
if len(sp) != 2 {

0 commit comments

Comments
 (0)