| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import time |
| 17 |
import concurrent |
| 18 |
import pytest |
| 19 |
|
| 20 |
from confluent_kafka.admin import UserScramCredentialsDescription, UserScramCredentialUpsertion, \ |
| 21 |
UserScramCredentialDeletion, ScramCredentialInfo, \ |
| 22 |
ScramMechanism |
| 23 |
from confluent_kafka.error import KafkaException, KafkaError |
| 24 |
|
| 25 |
from tests.common import TestUtils |
| 26 |
|
| 27 |
|
| 28 |
def test_user_scram_credentials(kafka_cluster): |
| 29 |
""" |
| 30 |
Tests for the alter and describe SASL/SCRAM credential operations. |
| 31 |
""" |
| 32 |
|
| 33 |
admin_client = kafka_cluster.admin() |
| 34 |
|
| 35 |
newuser = "non-existent" |
| 36 |
mechanism = ScramMechanism.SCRAM_SHA_256 |
| 37 |
iterations = 10000 |
| 38 |
password = b"password" |
| 39 |
salt = b"salt" |
| 40 |
|
| 41 |
futmap = admin_client.describe_user_scram_credentials([newuser]) |
| 42 |
assert isinstance(futmap, dict) |
| 43 |
assert len(futmap) == 1 |
| 44 |
assert newuser in futmap |
| 45 |
fut = futmap[newuser] |
| 46 |
with pytest.raises(KafkaException) as ex: |
| 47 |
result = fut.result() |
| 48 |
assert ex.value.args[0] == KafkaError.RESOURCE_NOT_FOUND |
| 49 |
|
| 50 |
|
| 51 |
futmap = admin_client.alter_user_scram_credentials([UserScramCredentialUpsertion(newuser, |
| 52 |
ScramCredentialInfo(mechanism, iterations), |
| 53 |
password, salt)]) |
| 54 |
fut = futmap[newuser] |
| 55 |
result = fut.result() |
| 56 |
assert result is None |
| 57 |
|
| 58 |
time.sleep(1) |
| 59 |
|
| 60 |
|
| 61 |
request = [UserScramCredentialUpsertion(newuser, |
| 62 |
ScramCredentialInfo( |
| 63 |
mechanism, iterations), |
| 64 |
password, salt), |
| 65 |
UserScramCredentialUpsertion(newuser, |
| 66 |
ScramCredentialInfo( |
| 67 |
ScramMechanism.SCRAM_SHA_512, 10000), |
| 68 |
password)] |
| 69 |
|
| 70 |
if TestUtils.use_kraft(): |
| 71 |
futmap = admin_client.alter_user_scram_credentials([request[0]]) |
| 72 |
result = futmap[newuser].result() |
| 73 |
assert result is None |
| 74 |
futmap = admin_client.alter_user_scram_credentials([request[1]]) |
| 75 |
else: |
| 76 |
futmap = admin_client.alter_user_scram_credentials(request) |
| 77 |
fut = futmap[newuser] |
| 78 |
result = fut.result() |
| 79 |
assert result is None |
| 80 |
|
| 81 |
time.sleep(1) |
| 82 |
|
| 83 |
|
| 84 |
futmap = admin_client.alter_user_scram_credentials([UserScramCredentialDeletion( |
| 85 |
newuser, |
| 86 |
ScramMechanism.SCRAM_SHA_512) |
| 87 |
]) |
| 88 |
fut = futmap[newuser] |
| 89 |
result = fut.result() |
| 90 |
assert result is None |
| 91 |
|
| 92 |
time.sleep(1) |
| 93 |
|
| 94 |
|
| 95 |
for args in [[], [None]]: |
| 96 |
f = admin_client.describe_user_scram_credentials(*args) |
| 97 |
assert isinstance(f, concurrent.futures.Future) |
| 98 |
results = f.result() |
| 99 |
assert newuser in results |
| 100 |
description = results[newuser] |
| 101 |
assert isinstance(description, UserScramCredentialsDescription) |
| 102 |
for scram_credential_info in description.scram_credential_infos: |
| 103 |
assert ((scram_credential_info.mechanism == mechanism) and |
| 104 |
(scram_credential_info.iterations == iterations)) |
| 105 |
|
| 106 |
|
| 107 |
futmap = admin_client.describe_user_scram_credentials([newuser]) |
| 108 |
assert isinstance(futmap, dict) |
| 109 |
assert len(futmap) == 1 |
| 110 |
assert newuser in futmap |
| 111 |
description = futmap[newuser].result() |
| 112 |
assert isinstance(description, UserScramCredentialsDescription) |
| 113 |
for scram_credential_info in description.scram_credential_infos: |
| 114 |
assert ((scram_credential_info.mechanism == mechanism) and |
| 115 |
(scram_credential_info.iterations == iterations)) |
| 116 |
|
| 117 |
|
| 118 |
futmap = admin_client.alter_user_scram_credentials([UserScramCredentialDeletion(newuser, mechanism)]) |
| 119 |
assert isinstance(futmap, dict) |
| 120 |
assert len(futmap) == 1 |
| 121 |
assert newuser in futmap |
| 122 |
fut = futmap[newuser] |
| 123 |
result = fut.result() |
| 124 |
assert result is None |
| 125 |
|
| 126 |
time.sleep(1) |
| 127 |
|
| 128 |
|
| 129 |
futmap = admin_client.describe_user_scram_credentials([newuser]) |
| 130 |
assert isinstance(futmap, dict) |
| 131 |
assert len(futmap) == 1 |
| 132 |
assert newuser in futmap |
| 133 |
fut = futmap[newuser] |
| 134 |
with pytest.raises(KafkaException) as ex: |
| 135 |
result = fut.result() |
| 136 |
assert ex.value.args[0] == KafkaError.RESOURCE_NOT_FOUND |