Skip to content

Commit 1db144c

Browse files
committed
Extract authentication URL and rename internal
function and enums
1 parent 0093671 commit 1db144c

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

src/rdhttp.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,32 @@ const char *rd_http_req_get_content_type(rd_http_req_t *hreq) {
367367

368368
/**
369369
* @brief Perform a blocking HTTP(S) request to \p url.
370+
* Retries the request \p retries times with linear backoff.
371+
* Interval of \p retry_ms milliseconds is used between retries.
370372
*
371-
* Returns the response (even if there's a HTTP error code returned)
372-
* in \p *rbufp.
373+
* @param url The URL to perform the request to.
374+
* @param headers_array Array of HTTP(S) headers to set, each element
375+
* is a string in the form "key: value"
376+
* @param headers_array_cnt Number of elements in \p headers_array.
377+
* @param timeout_s Timeout in seconds for the request, 0 means default
378+
* `rd_http_req_init()` timeout.
379+
* @param retries Number of retries to perform on failure.
380+
* @param retry_ms Milliseconds to wait between retries.
381+
* @param rbufp (out) Pointer to a buffer that will be filled with the response.
382+
* @param content_type (out, optional) Pointer to a string that will be filled
383+
* with the content type of the response, if not NULL.
384+
* @param response_code (out, optional) Pointer to an integer that will be
385+
* filled with the HTTP response code, if not NULL.
373386
*
374-
* Returns NULL on success (HTTP response code < 400), or an error
375-
* object on transport or HTTP error - this error object must be destroyed
376-
* by calling rd_http_error_destroy(). In case of HTTP error the \p *rbufp
377-
* may be filled with the error response.
387+
* @return Returns NULL on success (HTTP response code < 400), or an error
388+
* object on transport or HTTP error.
389+
*
390+
* @remark Returned error object, when non-NULL, must be destroyed
391+
* by calling rd_http_error_destroy().
392+
*
393+
* @locality Any thread.
394+
* @locks None.
395+
* @locks_acquired None.
378396
*/
379397
rd_http_error_t *rd_http_get(rd_kafka_t *rk,
380398
const char *url,
@@ -409,6 +427,8 @@ rd_http_error_t *rd_http_get(rd_kafka_t *rk,
409427
headers = curl_slist_append(headers, header);
410428
}
411429
curl_easy_setopt(hreq.hreq_curl, CURLOPT_HTTPHEADER, headers);
430+
if (timeout_s > 0)
431+
curl_easy_setopt(hreq.hreq_curl, CURLOPT_TIMEOUT, timeout_s);
412432

413433
for (i = 0; i <= retries; i++) {
414434
if (rd_kafka_terminating(rk)) {

src/rdkafka.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,10 +2461,10 @@ rd_kafka_t *rd_kafka_new(rd_kafka_type_t type,
24612461
rk->rk_conf.sasl.oauthbearer.builtin_token_refresh_cb = rd_true;
24622462

24632463
if (rk->rk_conf.sasl.oauthbearer.metadata_authentication.type ==
2464-
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE) {
2464+
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE_IMDS) {
24652465
rd_kafka_conf_set_oauthbearer_token_refresh_cb(
24662466
&rk->rk_conf,
2467-
rd_kafka_oidc_token_metadata_azure_refresh_cb);
2467+
rd_kafka_oidc_token_metadata_azure_imds_refresh_cb);
24682468
} else if (
24692469
rk->rk_conf.sasl.oauthbearer.grant_type ==
24702470
RD_KAFKA_SASL_OAUTHBEARER_GRANT_TYPE_CLIENT_CREDENTIALS) {

src/rdkafka_conf.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,10 +1222,11 @@ static const struct rd_kafka_property rd_kafka_properties[] = {
12221222
"through `sasl.oauthbearer.config`.",
12231223
_UNSUPPORTED_OIDC,
12241224
.vdef = RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_NONE,
1225-
.s2i = {{RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_NONE,
1226-
"none"},
1227-
{RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE,
1228-
"azure_imds"}},
1225+
.s2i =
1226+
{{RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_NONE,
1227+
"none"},
1228+
{RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE_IMDS,
1229+
"azure_imds"}},
12291230
},
12301231

12311232
/* Plugins */
@@ -4066,10 +4067,10 @@ const char *rd_kafka_conf_finalize_oauthbearer_oidc(rd_kafka_conf_t *conf) {
40664067
"mutually exclusive";
40674068

40684069
if (conf->sasl.oauthbearer.metadata_authentication.type ==
4069-
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE &&
4070+
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE_IMDS &&
40704071
!conf->sasl.oauthbearer.token_endpoint_url) {
40714072
conf->sasl.oauthbearer.token_endpoint_url =
4072-
"http://169.254.169.254/metadata/identity/oauth2/token";
4073+
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_URL_AZURE_IMDS;
40734074
}
40744075

40754076
if (!conf->sasl.oauthbearer.token_endpoint_url) {

src/rdkafka_conf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ typedef enum {
166166

167167
typedef enum {
168168
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_NONE,
169-
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE,
169+
RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_TYPE_AZURE_IMDS,
170170
} rd_kafka_oauthbearer_metadata_authentication_type_t;
171171

172+
173+
#define RD_KAFKA_SASL_OAUTHBEARER_METADATA_AUTHENTICATION_URL_AZURE_IMDS \
174+
"http://169.254.169.254/metadata/identity/oauth2/token"
175+
176+
172177
typedef enum {
173178
RD_KAFKA_SSL_ENDPOINT_ID_NONE,
174179
RD_KAFKA_SSL_ENDPOINT_ID_HTTPS, /**< RFC2818 */

src/rdkafka_sasl_oauthbearer_oidc.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,12 +1000,13 @@ void rd_kafka_oidc_token_client_credentials_refresh_cb(
10001000
}
10011001

10021002
/**
1003-
* @brief Implementation of Oauth/OIDC token refresh callback function,
1004-
* will receive the JSON response after HTTP call to token provider,
1005-
* then extract the jwt from the JSON response, and forward it to
1006-
* the broker.
1003+
* @brief Implementation of Oauth/OIDC token refresh callback function
1004+
* for Azure IMDS,
1005+
* will receive the JSON response after HTTP(S) GET call to token
1006+
* provider, then extract the jwt from the JSON response, and forward it to the
1007+
* broker.
10071008
*/
1008-
void rd_kafka_oidc_token_metadata_azure_refresh_cb(
1009+
void rd_kafka_oidc_token_metadata_azure_imds_refresh_cb(
10091010
rd_kafka_t *rk,
10101011
const char *oauthbearer_config,
10111012
void *opaque) {

src/rdkafka_sasl_oauthbearer_oidc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void rd_kafka_oidc_token_client_credentials_refresh_cb(
3737
const char *oauthbearer_config,
3838
void *opaque);
3939

40-
void rd_kafka_oidc_token_metadata_azure_refresh_cb(
40+
void rd_kafka_oidc_token_metadata_azure_imds_refresh_cb(
4141
rd_kafka_t *rk,
4242
const char *oauthbearer_config,
4343
void *opaque);

0 commit comments

Comments
 (0)