kerberos_client.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package sarama
  2. import (
  3. krb5client "github.com/jcmturner/gokrb5/v8/client"
  4. krb5config "github.com/jcmturner/gokrb5/v8/config"
  5. "github.com/jcmturner/gokrb5/v8/credentials"
  6. "github.com/jcmturner/gokrb5/v8/keytab"
  7. "github.com/jcmturner/gokrb5/v8/types"
  8. )
  9. type KerberosGoKrb5Client struct {
  10. krb5client.Client
  11. }
  12. func (c *KerberosGoKrb5Client) Domain() string {
  13. return c.Credentials.Domain()
  14. }
  15. func (c *KerberosGoKrb5Client) CName() types.PrincipalName {
  16. return c.Credentials.CName()
  17. }
  18. // NewKerberosClient creates kerberos client used to obtain TGT and TGS tokens.
  19. // It uses pure go Kerberos 5 solution (RFC-4121 and RFC-4120).
  20. // uses gokrb5 library underlying which is a pure go kerberos client with some GSS-API capabilities.
  21. func NewKerberosClient(config *GSSAPIConfig) (KerberosClient, error) {
  22. cfg, err := krb5config.Load(config.KerberosConfigPath)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return createClient(config, cfg)
  27. }
  28. func createClient(config *GSSAPIConfig, cfg *krb5config.Config) (KerberosClient, error) {
  29. var client *krb5client.Client
  30. switch config.AuthType {
  31. case KRB5_KEYTAB_AUTH:
  32. kt, err := keytab.Load(config.KeyTabPath)
  33. if err != nil {
  34. return nil, err
  35. }
  36. client = krb5client.NewWithKeytab(config.Username, config.Realm, kt, cfg, krb5client.DisablePAFXFAST(config.DisablePAFXFAST))
  37. case KRB5_CCACHE_AUTH:
  38. cc, err := credentials.LoadCCache(config.CCachePath)
  39. if err != nil {
  40. return nil, err
  41. }
  42. client, err = krb5client.NewFromCCache(cc, cfg, krb5client.DisablePAFXFAST(config.DisablePAFXFAST))
  43. if err != nil {
  44. return nil, err
  45. }
  46. default:
  47. client = krb5client.NewWithPassword(config.Username,
  48. config.Realm, config.Password, cfg, krb5client.DisablePAFXFAST(config.DisablePAFXFAST))
  49. }
  50. return &KerberosGoKrb5Client{*client}, nil
  51. }