-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redpanda_oauth_test: Improve test example
- Create a user under the demo realm, with realm-admin priv - Update client's service account to include an email address - This will appear in the access token as the grant includes the 'email' scope. - Adjust usage to account for keycloak-python based impl
- Loading branch information
Showing
2 changed files
with
51 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
import time | ||
import functools | ||
import json | ||
|
||
from rptest.clients.oauth_producer import OAuthProducer | ||
from rptest.clients.python_librdkafka import PythonLibrdkafka | ||
|
@@ -24,14 +25,15 @@ | |
|
||
CLIENT_ID = 'myapp' | ||
|
||
|
||
class RedpandaOIDCTestBase(Test): | ||
""" | ||
Base class for tests that use the Redpanda service with OIDC | ||
""" | ||
def __init__(self, | ||
test_context, | ||
num_nodes=5, | ||
sasl_mechanisms=[ 'SCRAM', 'OAUTHBEARER'], | ||
sasl_mechanisms=['SCRAM', 'OAUTHBEARER'], | ||
**kwargs): | ||
super(RedpandaOIDCTestBase, self).__init__(test_context, **kwargs) | ||
self.produce_messages = [] | ||
|
@@ -64,10 +66,13 @@ def delivery_report(self, err, msg): | |
""" | ||
if err is not None: | ||
self.produce_errors.append('Delivery failed for User record {}: {}'.format(msg.key(), err)) | ||
self.produce_errors.append( | ||
'Delivery failed for User record {}: {}'.format( | ||
msg.key(), err)) | ||
return | ||
self.produce_messages.append('User record {} successfully produced to {} [{}] at offset {}'.format( | ||
msg.key(), msg.topic(), msg.partition(), msg.offset())) | ||
self.produce_messages.append( | ||
'User record {} successfully produced to {} [{}] at offset {}'. | ||
format(msg.key(), msg.topic(), msg.partition(), msg.offset())) | ||
|
||
def setUp(self): | ||
self.produce_messages.clear() | ||
|
@@ -77,7 +82,6 @@ def setUp(self): | |
|
||
|
||
class RedpandaOIDCTest(RedpandaOIDCTestBase): | ||
|
||
@cluster(num_nodes=5) | ||
def test_init(self): | ||
kc_node = self.keycloak.nodes[0] | ||
|
@@ -90,31 +94,42 @@ def test_init(self): | |
|
||
self.rpk.create_topic('foo') | ||
|
||
# TODO: Improve understanding of client config space and hopefully bake some | ||
# of this into KeycloakService. | ||
self.keycloak.create_client(kc_node, { | ||
'clientId': CLIENT_ID, | ||
'enabled': True, | ||
'serviceAccountsEnabled': True, | ||
'standardFlowEnabled': True, | ||
'directAccessGrantsEnabled': True, | ||
'implicitFlowEnabled': True, | ||
}) | ||
|
||
cfg = self.keycloak.get_oauth_config(kc_node, CLIENT_ID) | ||
self.keycloak.admin.create_user('norma', | ||
'desmond', | ||
realm_admin=True, | ||
email='[email protected]') | ||
self.keycloak.login_admin_user(kc_node, 'norma', 'desmond') | ||
self.keycloak.admin.create_client(CLIENT_ID) | ||
|
||
# add an email address to myapp client's service user. this should | ||
# appear alongside the access token. | ||
self.keycloak.admin.update_user(f'service-account-{CLIENT_ID}', | ||
email='[email protected]') | ||
|
||
cfg = self.keycloak.generate_oauth_config(kc_node, CLIENT_ID) | ||
assert cfg.client_secret is not None | ||
assert cfg.token_endpoint is not None | ||
p_client = OAuthProducer(self.redpanda, oauth_config=cfg) | ||
producer = p_client.get_producer() | ||
|
||
producer.produce(topic='foo', key='bar', value='23', on_delivery=self.delivery_report) | ||
producer.produce(topic='foo', key='baz', value='23', on_delivery=self.delivery_report) | ||
producer.produce(topic='foo', key='qux', value='23', on_delivery=self.delivery_report) | ||
producer.produce(topic='foo', | ||
key='bar', | ||
value='23', | ||
on_delivery=self.delivery_report) | ||
producer.produce(topic='foo', | ||
key='baz', | ||
value='23', | ||
on_delivery=self.delivery_report) | ||
producer.produce(topic='foo', | ||
key='qux', | ||
value='23', | ||
on_delivery=self.delivery_report) | ||
|
||
self.logger.info('Flushing {} records...'.format(len(producer))) | ||
|
||
# Without the OIDC PoC, Producer.flush raises an AttributeError for some | ||
# reason. With OIDC support in place, this works as expected. | ||
# reason (rather than just failing). With OIDC support in place, this works | ||
# as expected. | ||
# TODO: Remove Me | ||
with expect_exception(AttributeError, lambda _: True): | ||
producer.flush() | ||
|