From 6ad4f25aa0b0cc372cf83b89daf545e035544ae9 Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Fri, 3 Jun 2022 01:26:39 -0500 Subject: [PATCH] Fix securityProtocol sasl config in consummer --- src/Consumers/ConsumerBuilder.php | 14 +++++++++++++- tests/Consumers/ConsumerBuilderTest.php | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Consumers/ConsumerBuilder.php b/src/Consumers/ConsumerBuilder.php index e280db11..f407b25e 100644 --- a/src/Consumers/ConsumerBuilder.php +++ b/src/Consumers/ConsumerBuilder.php @@ -364,7 +364,7 @@ public function build(): Consumer $config = new Config( broker: $this->brokers, topics: $this->topics, - securityProtocol: $this->securityProtocol, + securityProtocol: $this->getSecurityProtocol(), commit: $this->commit, groupId: $this->groupId, consumer: new CallableConsumer($this->handler, $this->middlewares), @@ -395,6 +395,18 @@ private function validateTopic(mixed $topic) } } + /** + * Get security protocol depending if sasl is been set. + * + * @return string + */ + private function getSecurityProtocol(): string + { + return $this->saslConfig !== null + ? $this->saslConfig->getSecurityProtocol() + : $this->securityProtocol; + } + /** * Returns batch config if batching is enabled * if batching is disabled then null config returned diff --git a/tests/Consumers/ConsumerBuilderTest.php b/tests/Consumers/ConsumerBuilderTest.php index 94aaabdc..140aeb10 100644 --- a/tests/Consumers/ConsumerBuilderTest.php +++ b/tests/Consumers/ConsumerBuilderTest.php @@ -219,6 +219,27 @@ public function testItCanSetSecurityProtocol() $this->assertEquals('security', $securityProtocol); } + public function testItCanSetSecurityProtocolViaSaslConfig() + { + $consumer = ConsumerBuilder::create('broker', ['foo'], 'group') + ->withSasl( + $sasl = new Sasl( + 'username', + 'password', + 'mechanisms', + 'protocol' + ) + ); + + $consummerBuilt = $consumer->build(); + $this->assertInstanceOf(Consumer::class, $consummerBuilt); + + $consumerConfig = $this->getPropertyWithReflection('config', $consummerBuilt); + $securityProtocol = $this->getPropertyWithReflection('securityProtocol', $consumerConfig); + + $this->assertEquals('protocol', $securityProtocol); + } + public function testItCanSetAutoCommit() { $consumer = ConsumerBuilder::create('broker')->withAutoCommit();