| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import time |
| 17 |
import pytest |
| 18 |
|
| 19 |
from confluent_kafka.admin import (AclBinding, AclBindingFilter, ResourceType, |
| 20 |
ResourcePatternType, AclOperation, AclPermissionType) |
| 21 |
from confluent_kafka.error import ConsumeError |
| 22 |
from confluent_kafka import ConsumerGroupState, TopicCollection |
| 23 |
|
| 24 |
from tests.common import TestUtils |
| 25 |
|
| 26 |
topic_prefix = "test-topic" |
| 27 |
|
| 28 |
|
| 29 |
def verify_commit_result(err, _): |
| 30 |
assert err is not None |
| 31 |
|
| 32 |
|
| 33 |
def consume_messages(sasl_cluster, group_id, topic, num_messages=None): |
| 34 |
conf = {'group.id': group_id, |
| 35 |
'session.timeout.ms': 6000, |
| 36 |
'enable.auto.commit': False, |
| 37 |
'on_commit': verify_commit_result, |
| 38 |
'auto.offset.reset': 'earliest'} |
| 39 |
consumer = sasl_cluster.consumer(conf) |
| 40 |
consumer.subscribe([topic]) |
| 41 |
read_messages = 0 |
| 42 |
msg = None |
| 43 |
while True: |
| 44 |
try: |
| 45 |
msg = consumer.poll() |
| 46 |
if msg is None: |
| 47 |
raise Exception('Got timeout from poll() without a timeout set: %s' % msg) |
| 48 |
|
| 49 |
consumer.commit(msg, asynchronous=False) |
| 50 |
read_messages += 1 |
| 51 |
if num_messages is not None and read_messages == num_messages: |
| 52 |
print('Read all the required messages: exiting') |
| 53 |
break |
| 54 |
except ConsumeError as e: |
| 55 |
print('Consumer error: %s: ignoring' % str(e)) |
| 56 |
break |
| 57 |
except Exception: |
| 58 |
raise |
| 59 |
consumer.close() |
| 60 |
|
| 61 |
|
| 62 |
def get_future_key_list(arg_as_list): |
| 63 |
return arg_as_list[0] |
| 64 |
|
| 65 |
|
| 66 |
def get_future_key_TopicCollection(arg_as_TopicCollection): |
| 67 |
return get_future_key_list(arg_as_TopicCollection.topic_names) |
| 68 |
|
| 69 |
|
| 70 |
def get_future_key(*arg): |
| 71 |
if len(arg) > 0: |
| 72 |
arg_type = type(arg[0]) |
| 73 |
if arg_type is list: |
| 74 |
return get_future_key_list(arg[0]) |
| 75 |
elif arg_type is TopicCollection: |
| 76 |
return get_future_key_TopicCollection(arg[0]) |
| 77 |
return None |
| 78 |
|
| 79 |
|
| 80 |
def perform_admin_operation_sync(operation, *arg, **kwargs): |
| 81 |
future_key = get_future_key(*arg) |
| 82 |
fs = operation(*arg, **kwargs) |
| 83 |
fs = fs[future_key] if future_key else fs |
| 84 |
return fs.result() |
| 85 |
|
| 86 |
|
| 87 |
def create_acls(admin_client, acl_bindings): |
| 88 |
perform_admin_operation_sync(admin_client.create_acls, acl_bindings) |
| 89 |
time.sleep(1) |
| 90 |
|
| 91 |
|
| 92 |
def delete_acls(admin_client, acl_binding_filters): |
| 93 |
perform_admin_operation_sync(admin_client.delete_acls, acl_binding_filters) |
| 94 |
time.sleep(1) |
| 95 |
|
| 96 |
|
| 97 |
def verify_provided_describe_for_authorized_operations( |
| 98 |
admin_client, |
| 99 |
describe_fn, |
| 100 |
operation_to_allow, |
| 101 |
operation_to_check, |
| 102 |
restype, |
| 103 |
resname, |
| 104 |
*arg): |
| 105 |
kwargs = {} |
| 106 |
kwargs['request_timeout'] = 10 |
| 107 |
|
| 108 |
|
| 109 |
kwargs['include_authorized_operations'] = False |
| 110 |
desc = perform_admin_operation_sync(describe_fn, *arg, **kwargs) |
| 111 |
assert desc.authorized_operations is None |
| 112 |
|
| 113 |
|
| 114 |
kwargs['include_authorized_operations'] = True |
| 115 |
desc = perform_admin_operation_sync(describe_fn, *arg, **kwargs) |
| 116 |
assert len(desc.authorized_operations) > 0 |
| 117 |
assert operation_to_allow in desc.authorized_operations |
| 118 |
assert operation_to_check in desc.authorized_operations |
| 119 |
|
| 120 |
|
| 121 |
acl_binding = AclBinding(restype, resname, ResourcePatternType.LITERAL, |
| 122 |
"User:sasl_user", "*", operation_to_allow, AclPermissionType.ALLOW) |
| 123 |
create_acls(admin_client, [acl_binding]) |
| 124 |
time.sleep(1) |
| 125 |
|
| 126 |
|
| 127 |
desc = perform_admin_operation_sync(describe_fn, *arg, **kwargs) |
| 128 |
assert len(desc.authorized_operations) > 0 |
| 129 |
assert operation_to_allow in desc.authorized_operations |
| 130 |
assert operation_to_check not in desc.authorized_operations |
| 131 |
|
| 132 |
|
| 133 |
acl_binding_filter = AclBindingFilter(restype, resname, ResourcePatternType.ANY, |
| 134 |
None, None, AclOperation.ANY, AclPermissionType.ANY) |
| 135 |
delete_acls(admin_client, [acl_binding_filter]) |
| 136 |
time.sleep(1) |
| 137 |
return desc |
| 138 |
|
| 139 |
|
| 140 |
def verify_describe_topics(admin_client, topic_name): |
| 141 |
desc = verify_provided_describe_for_authorized_operations(admin_client, |
| 142 |
admin_client.describe_topics, |
| 143 |
AclOperation.READ, |
| 144 |
AclOperation.DELETE, |
| 145 |
ResourceType.TOPIC, |
| 146 |
topic_name, |
| 147 |
TopicCollection([topic_name])) |
| 148 |
assert desc.name == topic_name |
| 149 |
assert len(desc.partitions) == 1 |
| 150 |
assert not desc.is_internal |
| 151 |
assert desc.partitions[0].id == 0 |
| 152 |
assert desc.partitions[0].leader is not None |
| 153 |
assert len(desc.partitions[0].replicas) == 1 |
| 154 |
assert len(desc.partitions[0].isr) == 1 |
| 155 |
|
| 156 |
|
| 157 |
def verify_describe_groups(cluster, admin_client, topic): |
| 158 |
|
| 159 |
|
| 160 |
p = cluster.producer() |
| 161 |
p.produce(topic, 'Hello Python!') |
| 162 |
p.produce(topic, key='Just a key') |
| 163 |
p.flush() |
| 164 |
|
| 165 |
|
| 166 |
group = 'test-group' |
| 167 |
consume_messages(cluster, group, topic, 2) |
| 168 |
|
| 169 |
|
| 170 |
desc = verify_provided_describe_for_authorized_operations(admin_client, |
| 171 |
admin_client.describe_consumer_groups, |
| 172 |
AclOperation.READ, |
| 173 |
AclOperation.DELETE, |
| 174 |
ResourceType.GROUP, |
| 175 |
group, |
| 176 |
[group]) |
| 177 |
assert group == desc.group_id |
| 178 |
assert desc.is_simple_consumer_group is False |
| 179 |
assert desc.state == ConsumerGroupState.EMPTY |
| 180 |
|
| 181 |
|
| 182 |
perform_admin_operation_sync(admin_client.delete_consumer_groups, [group], request_timeout=10) |
| 183 |
|
| 184 |
|
| 185 |
def verify_describe_cluster(admin_client): |
| 186 |
desc = verify_provided_describe_for_authorized_operations(admin_client, |
| 187 |
admin_client.describe_cluster, |
| 188 |
AclOperation.ALTER, |
| 189 |
AclOperation.ALTER_CONFIGS, |
| 190 |
ResourceType.BROKER, |
| 191 |
"kafka-cluster") |
| 192 |
assert isinstance(desc.cluster_id, str) |
| 193 |
assert len(desc.nodes) > 0 |
| 194 |
assert desc.controller is not None |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.parametrize('sasl_cluster', [{'broker_cnt': 1}], indirect=True) |
| 198 |
def test_describe_operations(sasl_cluster): |
| 199 |
|
| 200 |
admin_client = sasl_cluster.admin() |
| 201 |
|
| 202 |
|
| 203 |
verify_describe_cluster(admin_client) |
| 204 |
|
| 205 |
|
| 206 |
topic_config = {"compression.type": "gzip"} |
| 207 |
our_topic = sasl_cluster.create_topic_and_wait_propogation(topic_prefix, |
| 208 |
{ |
| 209 |
"num_partitions": 1, |
| 210 |
"config": topic_config, |
| 211 |
"replication_factor": 1, |
| 212 |
}, |
| 213 |
validate_only=False |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
verify_describe_topics(admin_client, our_topic) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if not TestUtils.use_group_protocol_consumer(): |
| 224 |
verify_describe_groups(sasl_cluster, admin_client, our_topic) |
| 225 |
|
| 226 |
|
| 227 |
perform_admin_operation_sync(admin_client.delete_topics, [our_topic], operation_timeout=0, request_timeout=10) |