You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

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