Skip to content

Commit

Permalink
Fixed default securityProtocol config (#215)
Browse files Browse the repository at this point in the history
* Fixed default securityProtocol

* Added plaintext fallback

* Improve readability

---------

Co-authored-by: Mateus Junges <[email protected]>
  • Loading branch information
SergkeiM and mateusjunges authored Oct 24, 2023
1 parent 445aa85 commit e97955c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions config/kafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
*/
'brokers' => env('KAFKA_BROKERS', 'localhost:9092'),

/*
| Default security protocol
*/
'securityProtocol' => env('KAFKA_SECURITY_PROTOCOL', 'PLAINTEXT'),

/*
| Default sasl configuration
*/
'sasl' => [
'mechanisms' => env('KAFKA_MECHANISMS', 'PLAINTEXT'),
'username' => env('KAFKA_USERNAME', null),
'password' => env('KAFKA_PASSWORD', null)
],

/*
| Kafka consumers belonging to the same consumer group share a group id.
| The consumers in a group then divides the topic partitions as fairly amongst themselves as possible by
Expand Down
10 changes: 7 additions & 3 deletions src/Console/Commands/KafkaConsumer/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Options
private ?int $commit = 1;
private ?string $dlq = null;
private int $maxMessages = -1;
private ?string $securityProtocol = 'plaintext';
private ?string $securityProtocol = null;
private ?string $saslUsername;
private ?string $saslPassword;
private ?string $saslMechanisms;
Expand Down Expand Up @@ -81,13 +81,17 @@ public function getSasl(): ?Sasl
username: $this->saslUsername,
password: $this->saslPassword,
mechanisms: $this->saslMechanisms,
securityProtocol: $this->securityProtocol
securityProtocol: $this->getSecurityProtocol()
);
}

public function getSecurityProtocol(): ?string
{
return $this->securityProtocol;
$securityProtocol = strlen($this->securityProtocol) > 1
? $this->securityProtocol
: $this->config['securityProtocol'];

return $securityProtocol ?? 'plaintext';
}

public function getBroker()
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Commands/KafkaConsumerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function handle()

return;
}
$options = new Options($this->options(), $this->config);

$parsedOptions = array_map(fn ($value) => $value === '?' ? null : $value, $this->options());

$options = new Options($parsedOptions, $this->config);

$consumer = $options->getConsumer();
$deserializer = $options->getDeserializer();
Expand Down

0 comments on commit e97955c

Please sign in to comment.