|
| 1 | +# Copyright 2016-2025. Couchbase, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import uuid |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from couchbase.auth import CertificateAuthenticator, PasswordAuthenticator |
| 21 | +from couchbase.cluster import Cluster |
| 22 | +from couchbase.exceptions import InvalidArgumentException |
| 23 | +from couchbase.options import ClusterOptions |
| 24 | + |
| 25 | + |
| 26 | +class ClassicCredentialsTests: |
| 27 | + |
| 28 | + def test_update_credentials_reflected_in_connection_info(self, couchbase_config): |
| 29 | + conn_string = couchbase_config.get_connection_string() |
| 30 | + username, pw = couchbase_config.get_username_and_pw() |
| 31 | + |
| 32 | + cluster = Cluster.connect(conn_string, ClusterOptions(PasswordAuthenticator(username, pw))) |
| 33 | + |
| 34 | + # capture original |
| 35 | + info_before = cluster._get_client_connection_info() |
| 36 | + assert 'credentials' in info_before |
| 37 | + orig_creds = info_before['credentials'] |
| 38 | + |
| 39 | + # update to new (likely invalid) creds; we only assert that core origin is updated |
| 40 | + new_user = f"pycbc_{uuid.uuid4().hex[:8]}" |
| 41 | + new_pass = f"pw_{uuid.uuid4().hex[:8]}" |
| 42 | + cluster.update_credentials(PasswordAuthenticator(new_user, new_pass)) |
| 43 | + |
| 44 | + info_after = cluster._get_client_connection_info() |
| 45 | + assert info_after['credentials']['username'] == new_user |
| 46 | + assert info_after['credentials']['password'] == new_pass |
| 47 | + |
| 48 | + # restore original to avoid impacting other tests |
| 49 | + cluster.update_credentials(PasswordAuthenticator(orig_creds.get('username', username), |
| 50 | + orig_creds.get('password', pw))) |
| 51 | + |
| 52 | + cluster.close() |
| 53 | + |
| 54 | + def test_update_to_certificate_auth_without_tls_fails(self, couchbase_config): |
| 55 | + conn_string = couchbase_config.get_connection_string() |
| 56 | + username, pw = couchbase_config.get_username_and_pw() |
| 57 | + |
| 58 | + # Non-TLS connection (expected couchbase://) |
| 59 | + cluster = Cluster.connect(conn_string, ClusterOptions(PasswordAuthenticator(username, pw))) |
| 60 | + |
| 61 | + # Core should reject this at validation step, surfacing as InvalidArgumentException |
| 62 | + with pytest.raises(InvalidArgumentException): |
| 63 | + cluster.update_credentials(CertificateAuthenticator(cert_path='path/to/cert', |
| 64 | + key_path='path/to/key')) |
| 65 | + |
| 66 | + cluster.close() |
| 67 | + |
| 68 | + def test_update_credentials_failure_does_not_change_state(self, couchbase_config): |
| 69 | + conn_string = couchbase_config.get_connection_string() |
| 70 | + username, pw = couchbase_config.get_username_and_pw() |
| 71 | + |
| 72 | + cluster = Cluster.connect(conn_string, ClusterOptions(PasswordAuthenticator(username, pw))) |
| 73 | + |
| 74 | + # capture original |
| 75 | + info_before = cluster._get_client_connection_info() |
| 76 | + assert 'credentials' in info_before |
| 77 | + orig_creds = info_before['credentials'] |
| 78 | + |
| 79 | + # attempt to switch to certificate auth on non-TLS connection; expect failure |
| 80 | + with pytest.raises(InvalidArgumentException): |
| 81 | + cluster.update_credentials(CertificateAuthenticator(cert_path='path/to/cert', |
| 82 | + key_path='path/to/key')) |
| 83 | + |
| 84 | + # ensure credentials are unchanged after the failed update |
| 85 | + info_after = cluster._get_client_connection_info() |
| 86 | + assert info_after['credentials'] == orig_creds |
| 87 | + |
| 88 | + cluster.close() |
0 commit comments