Skip to content

Commit acc6f7d

Browse files
committed
CCBC-1652: allow to force SASL when client certificate is being used
Change-Id: Ic6ea76f4c843aa29694e3f9be8984b190e89dde4 Reviewed-on: https://review.couchbase.org/c/libcouchbase/+/219911 Reviewed-by: Rishit Chaudhary <rishit.chaudhary@couchbase.com> Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Hiren <hiren.bavaskar@couchbase.com>
1 parent 42775a1 commit acc6f7d

9 files changed

Lines changed: 29 additions & 12 deletions

File tree

include/libcouchbase/cntl-private.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,12 @@ struct lcb_cntl_rdballocfactory {
298298
*/
299299
#define LCB_CNTL_QUERY_GRACE_PERIOD 0x64
300300

301+
/**
302+
* @brief Allow using SASL and BasicAuth with client certificate (assumes username/password provided and valid).
303+
*
304+
* @cntl_arg_both{lcb_U32*}
305+
* @volatile
306+
*/
307+
#define LCB_CNTL_USE_CREDENTIALS_WITH_CLIENT_CERTIFICATE 0x69
308+
301309
/**@}*/

include/libcouchbase/cntl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ typedef enum {
13461346
* This is not a command, but rather an indicator of the last item.
13471347
* @internal
13481348
*/
1349-
#define LCB_CNTL__MAX 0x69
1349+
#define LCB_CNTL__MAX 0x6a
13501350
/**@}*/
13511351

13521352
#ifdef __cplusplus

src/bucketconfig/bc_http.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ lcb_STATUS HttpProvider::setup_request_header(const lcb_host_t &host)
255255
}
256256

257257
request_buf.append(" HTTP/1.1\r\n");
258-
if (!settings().keypath) {
258+
if (!settings().keypath || settings().use_credentials_with_client_certificate) {
259259
// not using SSL client certificate to authenticate
260260
auto creds = settings().auth->credentials_for(LCBAUTH_SERVICE_MANAGEMENT, LCBAUTH_REASON_NEW_OPERATION,
261261
host.host, host.port, settings().bucket);

src/cntl.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,11 @@ HANDLER(unordered_execution_handler)
770770
RETURN_GET_SET(int, LCBT_SETTING(instance, enable_unordered_execution))
771771
}
772772

773+
HANDLER(use_credentials_with_client_certificate)
774+
{
775+
RETURN_GET_SET(int, LCBT_SETTING(instance, use_credentials_with_client_certificate))
776+
}
777+
773778
/* clang-format off */
774779
static ctl_handler handlers[] = {
775780
timeout_common, /* LCB_CNTL_OP_TIMEOUT */
@@ -877,6 +882,7 @@ static ctl_handler handlers[] = {
877882
timeout_common, /* LCB_CNTL_OP_METRICS_FLUSH_INTERVAL */
878883
enable_op_metrics_handler, /* LCB_CNTL_ENABLE_OP_METRICS */
879884
preferred_server_group_handler, /* LCB_CNTL_PREFERRED_SERVER_GROUP */
885+
use_credentials_with_client_certificate, /* LCB_CNTL_USE_CREDENTIALS_WITH_CLIENT_CERTIFICATE */
880886
nullptr
881887
};
882888
/* clang-format on */
@@ -1119,6 +1125,7 @@ static cntl_OPCODESTRS stropcode_map[] = {
11191125
{"operation_metrics_flush_interval", LCB_CNTL_OP_METRICS_FLUSH_INTERVAL, convert_timevalue},
11201126
{"enable_operation_metrics", LCB_CNTL_ENABLE_OP_METRICS, convert_intbool},
11211127
{"preferred_server_group", LCB_CNTL_PREFERRED_SERVER_GROUP, convert_passthru},
1128+
{"use_credentials_with_client_certificate", LCB_CNTL_USE_CREDENTIALS_WITH_CLIENT_CERTIFICATE, convert_intbool},
11221129
{nullptr, -1}};
11231130

11241131
#define CNTL_NUM_HANDLERS (sizeof(handlers) / sizeof(handlers[0]))

src/http/http.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ void Request::finish_or_retry(lcb_STATUS rc)
230230
finish(rc);
231231
return;
232232
}
233-
struct http_parser_url next_info {
234-
};
233+
struct http_parser_url next_info{};
235234
if (_lcb_http_parser_parse_url(nextnode, strlen(nextnode), 0, &next_info)) {
236235
lcb_log(LOGARGS(this, WARN), LOGFMT "Not retrying. Invalid API endpoint", LOGID(this));
237236
finish(LCB_ERR_INVALID_ARGUMENT);
@@ -646,9 +645,10 @@ lcb_STATUS Request::setup_inputs(const lcb_CMDHTTP *cmd)
646645
}
647646
}
648647

649-
if ((cmd->cmdflags & LCB_CMDHTTP_F_NOUPASS) || instance->settings->keypath) {
648+
if ((cmd->cmdflags & LCB_CMDHTTP_F_NOUPASS) ||
649+
(instance->settings->keypath && !instance->settings->use_credentials_with_client_certificate)) {
650650
// explicitly asked to skip Authorization header,
651-
// or using SSL client certificate to authenticate
651+
// or using SSL client certificate to authenticate with use_credentials_with_client_certificate=false
652652
username.clear();
653653
password.clear();
654654
} else if (username.empty() && password.empty()) {

src/instance.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const void *lcb_get_cookie(lcb_INSTANCE *instance)
129129
LIBCOUCHBASE_API
130130
void lcb_set_auth(lcb_INSTANCE *instance, lcb_AUTHENTICATOR *auth)
131131
{
132-
if (LCBT_SETTING(instance, keypath)) {
132+
if (LCBT_SETTING(instance, keypath) && !LCBT_SETTING(instance, use_credentials_with_client_certificate)) {
133133
lcb_log(LOGARGS(instance, WARN),
134134
"Custom authenticator ignored when SSL client certificate authentication in use");
135135
return;

src/mcserver/negotiate.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ SessionRequestImpl::MechStatus SessionRequestImpl::set_chosen_mech(std::string &
270270
cbsasl_error_t saslerr;
271271

272272
if (mechlist.empty()) {
273-
lcb_log(LOGARGS(this, WARN), LOGFMT "Server does not support SASL (no mechanisms supported)", LOGID(this));
273+
lcb_log(LOGARGS(this, WARN), LOGFMT "Server does not support SASL (no mechanisms supported, empty list)", LOGID(this));
274274
return MECH_NOT_NEEDED;
275275
}
276276

@@ -329,7 +329,7 @@ SessionRequestImpl::MechStatus SessionRequestImpl::set_chosen_mech(std::string &
329329
info->mech.assign(chosenmech);
330330
return MECH_OK;
331331
case SASL_NOMECH:
332-
lcb_log(LOGARGS(this, WARN), LOGFMT "Server does not support SASL (no mechanisms supported)", LOGID(this));
332+
lcb_log(LOGARGS(this, WARN), LOGFMT "Server does not support SASL (no mechanisms supported, SASL_NOMECH)", LOGID(this));
333333
return MECH_UNAVAILABLE;
334334
default:
335335
lcb_log(LOGARGS(this, ERROR), LOGFMT "cbsasl_client_start returned %d", LOGID(this), saslerr);
@@ -710,7 +710,7 @@ void SessionRequestImpl::handle_read(lcbio_CTX *ioctx)
710710
lcb_log(LOGARGS(this, TRACE), LOGFMT "GET_ERRORMAP unsupported/disabled", LOGID(this));
711711
}
712712

713-
if (settings->keypath) {
713+
if (settings->keypath && !settings->use_credentials_with_client_certificate) {
714714
completed = !expecting_error_map && !maybe_select_bucket();
715715
}
716716
break;
@@ -727,7 +727,7 @@ void SessionRequestImpl::handle_read(lcbio_CTX *ioctx)
727727
status);
728728
set_error(LCB_ERR_PROTOCOL_ERROR, "GET_ERRMAP response unexpected", &resp);
729729
}
730-
if (settings->keypath) {
730+
if (settings->keypath && !settings->use_credentials_with_client_certificate) {
731731
completed = !maybe_select_bucket();
732732
}
733733
// Note, there is no explicit state transition here. LIST_MECHS is
@@ -838,7 +838,7 @@ void SessionRequestImpl::start(lcbio_SOCKET *sock)
838838
} else {
839839
lcb_log(LOGARGS(this, TRACE), LOGFMT "GET_ERRORMAP disabled", LOGID(this));
840840
}
841-
if (!settings->keypath) {
841+
if (!settings->keypath || settings->use_credentials_with_client_certificate) {
842842
send_list_mechs();
843843
}
844844
LCBIO_CTX_RSCHEDULE(ctx, 24);

src/settings.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void lcb_default_settings(lcb_settings *settings)
8686
settings->op_metrics_flush_interval = LCB_DEFAULT_OP_METRICS_FLUSH_INTERVAL;
8787
settings->op_metrics_enabled = 1;
8888
settings->preferred_server_group = nullptr;
89+
settings->use_credentials_with_client_certificate = 0;
8990
}
9091

9192
LCB_INTERNAL_API

src/settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ typedef struct lcb_settings_st {
237237
char *network; /** network resolution, AKA "Multi Network Configurations" */
238238
lcb_U32 op_metrics_flush_interval;
239239
unsigned op_metrics_enabled : 1;
240+
unsigned use_credentials_with_client_certificate : 1;
240241
char *preferred_server_group;
241242
} lcb_settings;
242243

0 commit comments

Comments
 (0)