github.com/Jeffail/benthos/v3@v3.65.0/lib/util/kafka/sasl/sasl_test.go (about)

     1  package sasl
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Jeffail/benthos/v3/lib/types"
     7  	"github.com/Shopify/sarama"
     8  )
     9  
    10  //------------------------------------------------------------------------------
    11  
    12  type mockMgr struct {
    13  	cache mockCache
    14  	types.DudMgr
    15  }
    16  
    17  type mockCache struct {
    18  	entries map[string]string
    19  	types.Cache
    20  }
    21  
    22  func (m mockMgr) GetCache(name string) (types.Cache, error) {
    23  	return m.cache, nil
    24  }
    25  
    26  func (c mockCache) Get(key string) ([]byte, error) {
    27  	v, ok := c.entries[key]
    28  	if !ok {
    29  		return nil, types.ErrKeyNotFound
    30  	}
    31  
    32  	return []byte(v), nil
    33  }
    34  
    35  //------------------------------------------------------------------------------
    36  
    37  func TestApplyPlaintext(t *testing.T) {
    38  	conf := &sarama.Config{}
    39  
    40  	saslConf := Config{
    41  		Mechanism: string(sarama.SASLTypePlaintext),
    42  		User:      "foo",
    43  		Password:  "bar",
    44  	}
    45  
    46  	err := saslConf.Apply(types.NoopMgr(), conf)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if !conf.Net.SASL.Enable {
    52  		t.Errorf("SASL not enabled")
    53  	}
    54  
    55  	if conf.Net.SASL.Mechanism != sarama.SASLTypePlaintext {
    56  		t.Errorf("Wrong SASL mechanism: %v != %v", conf.Net.SASL.Mechanism, sarama.SASLTypePlaintext)
    57  	}
    58  
    59  	if conf.Net.SASL.User != "foo" {
    60  		t.Errorf("Wrong SASL user: %v != %v", conf.Net.SASL.User, "foo")
    61  	}
    62  
    63  	if conf.Net.SASL.Password != "bar" {
    64  		t.Errorf("Wrong SASL password: %v != %v", conf.Net.SASL.Password, "bar")
    65  	}
    66  }
    67  
    68  func TestApplyPlaintextDeprecated(t *testing.T) {
    69  	conf := &sarama.Config{}
    70  
    71  	saslConf := Config{
    72  		Enabled:   true,
    73  		Mechanism: "",
    74  		User:      "foo",
    75  		Password:  "bar",
    76  	}
    77  
    78  	err := saslConf.Apply(types.NoopMgr(), conf)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	if !conf.Net.SASL.Enable {
    84  		t.Errorf("SASL not enabled")
    85  	}
    86  
    87  	if conf.Net.SASL.Mechanism != sarama.SASLTypePlaintext {
    88  		t.Errorf("Wrong SASL mechanism: %v != %v", conf.Net.SASL.Mechanism, sarama.SASLTypePlaintext)
    89  	}
    90  
    91  	if conf.Net.SASL.User != "foo" {
    92  		t.Errorf("Wrong SASL user: %v != %v", conf.Net.SASL.User, "foo")
    93  	}
    94  
    95  	if conf.Net.SASL.Password != "bar" {
    96  		t.Errorf("Wrong SASL password: %v != %v", conf.Net.SASL.Password, "bar")
    97  	}
    98  }
    99  
   100  func TestApplyOAuthBearerStaticProvider(t *testing.T) {
   101  	conf := &sarama.Config{}
   102  
   103  	saslConf := Config{
   104  		Mechanism:   string(sarama.SASLTypeOAuth),
   105  		AccessToken: "foo",
   106  	}
   107  
   108  	err := saslConf.Apply(types.NoopMgr(), conf)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	if !conf.Net.SASL.Enable {
   114  		t.Errorf("SASL not enabled")
   115  	}
   116  
   117  	if conf.Net.SASL.Mechanism != sarama.SASLTypeOAuth {
   118  		t.Errorf("Wrong SASL mechanism: %v != %v", conf.Net.SASL.Mechanism, sarama.SASLTypeOAuth)
   119  	}
   120  
   121  	token, err := conf.Net.SASL.TokenProvider.Token()
   122  	if err != nil {
   123  		t.Errorf("Failed to get token")
   124  	}
   125  
   126  	if act := token.Token; act != "foo" {
   127  		t.Errorf("Wrong SASL token: %v != %v", act, "foo")
   128  	}
   129  }
   130  
   131  func TestApplyOAuthBearerCacheProvider(t *testing.T) {
   132  	conf := &sarama.Config{}
   133  
   134  	saslConf := Config{
   135  		Mechanism:  string(sarama.SASLTypeOAuth),
   136  		TokenCache: "token_provider",
   137  		TokenKey:   "jwt",
   138  	}
   139  
   140  	cache := mockCache{
   141  		entries: map[string]string{"jwt": "foo"},
   142  	}
   143  	err := saslConf.Apply(mockMgr{cache: cache}, conf)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if !conf.Net.SASL.Enable {
   149  		t.Errorf("SASL not enabled")
   150  	}
   151  
   152  	if conf.Net.SASL.Mechanism != sarama.SASLTypeOAuth {
   153  		t.Errorf("Wrong SASL mechanism: %v != %v", conf.Net.SASL.Mechanism, sarama.SASLTypeOAuth)
   154  	}
   155  
   156  	token, err := conf.Net.SASL.TokenProvider.Token()
   157  	if err != nil {
   158  		t.Errorf("Failed to get token")
   159  	}
   160  
   161  	if act := token.Token; act != "foo" {
   162  		t.Errorf("Wrong SASL token: %v != %v", act, "foo")
   163  	}
   164  
   165  	// Test with missing key
   166  	saslConf.TokenKey = "bar"
   167  	err = saslConf.Apply(mockMgr{cache: cache}, conf)
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  
   172  	if _, err := conf.Net.SASL.TokenProvider.Token(); err == nil {
   173  		t.Errorf("Expected failure to get token")
   174  	}
   175  }
   176  
   177  func TestApplyUnknownMechanism(t *testing.T) {
   178  	conf := &sarama.Config{}
   179  
   180  	saslConf := Config{
   181  		Mechanism: "foo",
   182  	}
   183  
   184  	err := saslConf.Apply(types.NoopMgr(), conf)
   185  	if err != ErrUnsupportedSASLMechanism {
   186  		t.Errorf("Err %v != %v", err, ErrUnsupportedSASLMechanism)
   187  	}
   188  }
   189  
   190  //------------------------------------------------------------------------------