kerberos_client_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package sarama
  2. import (
  3. "errors"
  4. "testing"
  5. krbcfg "gopkg.in/jcmturner/gokrb5.v7/config"
  6. "gopkg.in/jcmturner/gokrb5.v7/test/testdata"
  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. }
  80. func TestCreateWithDisablePAFXFAST(t *testing.T) {
  81. kerberosConfig, err := krbcfg.NewConfigFromString(testdata.TEST_KRB5CONF)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. // Expect to try to create a client with keytab and fails with "o such file or directory" error
  86. expectedErr := errors.New("open nonexist.keytab: no such file or directory")
  87. clientConfig := NewConfig()
  88. clientConfig.Net.SASL.Mechanism = SASLTypeGSSAPI
  89. clientConfig.Net.SASL.Enable = true
  90. clientConfig.Net.SASL.GSSAPI.ServiceName = "kafka"
  91. clientConfig.Net.SASL.GSSAPI.Realm = "EXAMPLE.COM"
  92. clientConfig.Net.SASL.GSSAPI.Username = "client"
  93. clientConfig.Net.SASL.GSSAPI.AuthType = KRB5_KEYTAB_AUTH
  94. clientConfig.Net.SASL.GSSAPI.KeyTabPath = "nonexist.keytab"
  95. clientConfig.Net.SASL.GSSAPI.KerberosConfigPath = "/etc/krb5.conf"
  96. clientConfig.Net.SASL.GSSAPI.DisablePAFXFAST = true
  97. _, err = createClient(&clientConfig.Net.SASL.GSSAPI, kerberosConfig)
  98. if err.Error() != expectedErr.Error() {
  99. t.Errorf("Expected error:%s, got:%s.", err, expectedErr)
  100. }
  101. }