kerberos_client_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package sarama
  2. import (
  3. "errors"
  4. krbcfg "gopkg.in/jcmturner/gokrb5.v7/config"
  5. "gopkg.in/jcmturner/gokrb5.v7/test/testdata"
  6. "testing"
  7. )
  8. /*
  9. * Minimum requirement for client creation
  10. * we are not testing the client itself, we only test that the client is created
  11. * properly.
  12. *
  13. */
  14. func TestFaildToCreateKerberosConfig(t *testing.T) {
  15. expectedErr := errors.New("configuration file could not be opened: krb5.conf open krb5.conf: no such file or directory")
  16. clientConfig := NewConfig()
  17. clientConfig.Net.SASL.Mechanism = SASLTypeGSSAPI
  18. clientConfig.Net.SASL.Enable = true
  19. clientConfig.Net.SASL.GSSAPI.ServiceName = "kafka"
  20. clientConfig.Net.SASL.GSSAPI.Realm = "EXAMPLE.COM"
  21. clientConfig.Net.SASL.GSSAPI.Username = "client"
  22. clientConfig.Net.SASL.GSSAPI.AuthType = KRB5_USER_AUTH
  23. clientConfig.Net.SASL.GSSAPI.Password = "qwerty"
  24. clientConfig.Net.SASL.GSSAPI.KerberosConfigPath = "krb5.conf"
  25. _, err := NewKerberosClient(&clientConfig.Net.SASL.GSSAPI)
  26. // Expect to create client with password
  27. if err.Error() != expectedErr.Error() {
  28. t.Errorf("Expected error:%s, got:%s.", err, expectedErr)
  29. }
  30. }
  31. func TestCreateWithPassword(t *testing.T) {
  32. kerberosConfig, err := krbcfg.NewConfigFromString(testdata.TEST_KRB5CONF)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. expectedDoman := "EXAMPLE.COM"
  37. expectedCName := "client"
  38. clientConfig := NewConfig()
  39. clientConfig.Net.SASL.Mechanism = SASLTypeGSSAPI
  40. clientConfig.Net.SASL.Enable = true
  41. clientConfig.Net.SASL.GSSAPI.ServiceName = "kafka"
  42. clientConfig.Net.SASL.GSSAPI.Realm = "EXAMPLE.COM"
  43. clientConfig.Net.SASL.GSSAPI.Username = "client"
  44. clientConfig.Net.SASL.GSSAPI.AuthType = KRB5_USER_AUTH
  45. clientConfig.Net.SASL.GSSAPI.Password = "qwerty"
  46. clientConfig.Net.SASL.GSSAPI.KerberosConfigPath = "/etc/krb5.conf"
  47. client, _ := createClient(&clientConfig.Net.SASL.GSSAPI, kerberosConfig)
  48. // Expect to create client with password
  49. if client == nil {
  50. t.Errorf("Expected client not nil")
  51. }
  52. if client.Domain() != expectedDoman {
  53. t.Errorf("Client domain: %s, got: %s", expectedDoman, client.Domain())
  54. }
  55. if client.CName().NameString[0] != expectedCName {
  56. t.Errorf("Client domain:%s, got: %s", expectedCName, client.CName().NameString[0])
  57. }
  58. }
  59. func TestCreateWithKeyTab(t *testing.T) {
  60. kerberosConfig, err := krbcfg.NewConfigFromString(testdata.TEST_KRB5CONF)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. // Expect to try to create a client with keytab and fails with "o such file or directory" error
  65. expectedErr := errors.New("open nonexist.keytab: no such file or directory")
  66. clientConfig := NewConfig()
  67. clientConfig.Net.SASL.Mechanism = SASLTypeGSSAPI
  68. clientConfig.Net.SASL.Enable = true
  69. clientConfig.Net.SASL.GSSAPI.ServiceName = "kafka"
  70. clientConfig.Net.SASL.GSSAPI.Realm = "EXAMPLE.COM"
  71. clientConfig.Net.SASL.GSSAPI.Username = "client"
  72. clientConfig.Net.SASL.GSSAPI.AuthType = KRB5_KEYTAB_AUTH
  73. clientConfig.Net.SASL.GSSAPI.KeyTabPath = "nonexist.keytab"
  74. clientConfig.Net.SASL.GSSAPI.KerberosConfigPath = "/etc/krb5.conf"
  75. _, err = createClient(&clientConfig.Net.SASL.GSSAPI, kerberosConfig)
  76. if err.Error() != expectedErr.Error() {
  77. t.Errorf("Expected error:%s, got:%s.", err, expectedErr)
  78. }
  79. }