authentication_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafka
  4. import (
  5. "testing"
  6. "github.com/IBM/sarama"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/config/configtls"
  10. )
  11. func TestAuthentication(t *testing.T) {
  12. saramaPlaintext := &sarama.Config{}
  13. saramaPlaintext.Net.SASL.Enable = true
  14. saramaPlaintext.Net.SASL.User = "jdoe"
  15. saramaPlaintext.Net.SASL.Password = "pass"
  16. saramaSASLSCRAM256Config := &sarama.Config{}
  17. saramaSASLSCRAM256Config.Net.SASL.Enable = true
  18. saramaSASLSCRAM256Config.Net.SASL.User = "jdoe"
  19. saramaSASLSCRAM256Config.Net.SASL.Password = "pass"
  20. saramaSASLSCRAM256Config.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA256
  21. saramaSASLSCRAM512Config := &sarama.Config{}
  22. saramaSASLSCRAM512Config.Net.SASL.Enable = true
  23. saramaSASLSCRAM512Config.Net.SASL.User = "jdoe"
  24. saramaSASLSCRAM512Config.Net.SASL.Password = "pass"
  25. saramaSASLSCRAM512Config.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
  26. saramaSASLHandshakeV1Config := &sarama.Config{}
  27. saramaSASLHandshakeV1Config.Net.SASL.Enable = true
  28. saramaSASLHandshakeV1Config.Net.SASL.User = "jdoe"
  29. saramaSASLHandshakeV1Config.Net.SASL.Password = "pass"
  30. saramaSASLHandshakeV1Config.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
  31. saramaSASLHandshakeV1Config.Net.SASL.Version = sarama.SASLHandshakeV1
  32. saramaSASLPLAINConfig := &sarama.Config{}
  33. saramaSASLPLAINConfig.Net.SASL.Enable = true
  34. saramaSASLPLAINConfig.Net.SASL.User = "jdoe"
  35. saramaSASLPLAINConfig.Net.SASL.Password = "pass"
  36. saramaSASLPLAINConfig.Net.SASL.Mechanism = sarama.SASLTypePlaintext
  37. saramaTLSCfg := &sarama.Config{}
  38. saramaTLSCfg.Net.TLS.Enable = true
  39. tlsClient := configtls.TLSClientSetting{}
  40. tlscfg, err := tlsClient.LoadTLSConfig()
  41. require.NoError(t, err)
  42. saramaTLSCfg.Net.TLS.Config = tlscfg
  43. saramaKerberosCfg := &sarama.Config{}
  44. saramaKerberosCfg.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI
  45. saramaKerberosCfg.Net.SASL.Enable = true
  46. saramaKerberosCfg.Net.SASL.GSSAPI.ServiceName = "foobar"
  47. saramaKerberosCfg.Net.SASL.GSSAPI.AuthType = sarama.KRB5_USER_AUTH
  48. saramaKerberosKeyTabCfg := &sarama.Config{}
  49. saramaKerberosKeyTabCfg.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI
  50. saramaKerberosKeyTabCfg.Net.SASL.Enable = true
  51. saramaKerberosKeyTabCfg.Net.SASL.GSSAPI.KeyTabPath = "/path"
  52. saramaKerberosKeyTabCfg.Net.SASL.GSSAPI.AuthType = sarama.KRB5_KEYTAB_AUTH
  53. tests := []struct {
  54. auth Authentication
  55. saramaConfig *sarama.Config
  56. err string
  57. }{
  58. {
  59. auth: Authentication{PlainText: &PlainTextConfig{Username: "jdoe", Password: "pass"}},
  60. saramaConfig: saramaPlaintext,
  61. },
  62. {
  63. auth: Authentication{TLS: &configtls.TLSClientSetting{}},
  64. saramaConfig: saramaTLSCfg,
  65. },
  66. {
  67. auth: Authentication{TLS: &configtls.TLSClientSetting{
  68. TLSSetting: configtls.TLSSetting{CAFile: "/doesnotexists"},
  69. }},
  70. saramaConfig: saramaTLSCfg,
  71. err: "failed to load TLS config",
  72. },
  73. {
  74. auth: Authentication{Kerberos: &KerberosConfig{ServiceName: "foobar"}},
  75. saramaConfig: saramaKerberosCfg,
  76. },
  77. {
  78. auth: Authentication{Kerberos: &KerberosConfig{UseKeyTab: true, KeyTabPath: "/path"}},
  79. saramaConfig: saramaKerberosKeyTabCfg,
  80. },
  81. {
  82. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "SCRAM-SHA-256"}},
  83. saramaConfig: saramaSASLSCRAM256Config,
  84. },
  85. {
  86. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "SCRAM-SHA-512"}},
  87. saramaConfig: saramaSASLSCRAM512Config,
  88. },
  89. {
  90. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "SCRAM-SHA-512", Version: 1}},
  91. saramaConfig: saramaSASLHandshakeV1Config,
  92. },
  93. {
  94. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "PLAIN"}},
  95. saramaConfig: saramaSASLPLAINConfig,
  96. },
  97. {
  98. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "SCRAM-SHA-222"}},
  99. saramaConfig: saramaSASLSCRAM512Config,
  100. err: "invalid SASL Mechanism",
  101. },
  102. {
  103. auth: Authentication{SASL: &SASLConfig{Username: "", Password: "pass", Mechanism: "SCRAM-SHA-512"}},
  104. saramaConfig: saramaSASLSCRAM512Config,
  105. err: "username have to be provided",
  106. },
  107. {
  108. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "", Mechanism: "SCRAM-SHA-512"}},
  109. saramaConfig: saramaSASLSCRAM512Config,
  110. err: "password have to be provided",
  111. },
  112. {
  113. auth: Authentication{SASL: &SASLConfig{Username: "jdoe", Password: "pass", Mechanism: "SCRAM-SHA-512", Version: 2}},
  114. saramaConfig: saramaSASLSCRAM512Config,
  115. err: "invalid SASL Protocol Version",
  116. },
  117. }
  118. for _, test := range tests {
  119. t.Run("", func(t *testing.T) {
  120. config := &sarama.Config{}
  121. err := ConfigureAuthentication(test.auth, config)
  122. if test.err != "" {
  123. require.Error(t, err)
  124. assert.Contains(t, err.Error(), test.err)
  125. } else {
  126. // equalizes SCRAMClientGeneratorFunc to do assertion with the same reference.
  127. config.Net.SASL.SCRAMClientGeneratorFunc = test.saramaConfig.Net.SASL.SCRAMClientGeneratorFunc
  128. assert.Equal(t, test.saramaConfig, config)
  129. }
  130. })
  131. }
  132. }