hdfs_kerberos.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package hdfs
  2. import (
  3. "fmt"
  4. "os"
  5. "os/user"
  6. "strings"
  7. krb "github.com/jcmturner/gokrb5/v8/client"
  8. "github.com/jcmturner/gokrb5/v8/config"
  9. "github.com/jcmturner/gokrb5/v8/credentials"
  10. )
  11. // copy-paste from https://github.com/colinmarc/hdfs/blob/master/cmd/hdfs/kerberos.go
  12. func getKerberosClient() (*krb.Client, error) {
  13. configPath := os.Getenv("KRB5_CONFIG")
  14. if configPath == "" {
  15. configPath = "/etc/krb5.conf"
  16. }
  17. cfg, err := config.Load(configPath)
  18. if err != nil {
  19. return nil, err
  20. }
  21. // Determine the ccache location from the environment, falling back to the
  22. // default location.
  23. ccachePath := os.Getenv("KRB5CCNAME")
  24. if strings.Contains(ccachePath, ":") {
  25. if strings.HasPrefix(ccachePath, "FILE:") {
  26. ccachePath = strings.SplitN(ccachePath, ":", 2)[1]
  27. } else {
  28. return nil, fmt.Errorf("unusable ccache: %s", ccachePath)
  29. }
  30. } else if ccachePath == "" {
  31. u, err := user.Current()
  32. if err != nil {
  33. return nil, err
  34. }
  35. ccachePath = fmt.Sprintf("/tmp/krb5cc_%s", u.Uid)
  36. }
  37. ccache, err := credentials.LoadCCache(ccachePath)
  38. if err != nil {
  39. return nil, err
  40. }
  41. client, err := krb.NewFromCCache(ccache, cfg)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return client, nil
  46. }