Skip to content

Commit 94d2405

Browse files
📝 Add docstrings to fix/aws-msk-iam-optimization
Docstrings generation was requested by @kalavt. * #11211 (comment) The following files were modified: * `src/aws/flb_aws_credentials_ec2.c` * `src/aws/flb_aws_credentials_http.c` * `src/aws/flb_aws_credentials_profile.c` * `src/aws/flb_aws_credentials_sts.c` * `src/aws/flb_aws_msk_iam.c`
1 parent ae14454 commit 94d2405

File tree

5 files changed

+245
-81
lines changed

5 files changed

+245
-81
lines changed

src/aws/flb_aws_credentials_ec2.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,26 @@ struct flb_aws_credentials *get_credentials_fn_ec2(struct flb_aws_provider
125125
return creds;
126126
}
127127

128+
/**
129+
* Force an immediate refresh of EC2 IMDS credentials for the given provider.
130+
*
131+
* Attempts to acquire the provider lock and, if successful, triggers an immediate
132+
* credentials refresh from the EC2 Instance Metadata Service. If the lock cannot
133+
* be acquired the function does not perform a refresh.
134+
*
135+
* @param provider The AWS provider whose EC2 IMDS credentials should be refreshed.
136+
* @returns `0` on successful credential refresh, `-1` if the refresh failed or did not occur.
137+
*/
128138
int refresh_fn_ec2(struct flb_aws_provider *provider) {
129139
struct flb_aws_provider_ec2 *implementation = provider->implementation;
130140
int ret = -1;
131141

132142
flb_debug("[aws_credentials] Refresh called on the EC2 IMDS provider");
143+
133144
if (try_lock_provider(provider)) {
145+
/* Set to 1 (epoch start) to trigger immediate refresh via time check */
146+
implementation->next_refresh = 1;
147+
134148
ret = get_creds_ec2(implementation);
135149
unlock_provider(provider);
136150
}
@@ -379,4 +393,4 @@ static int ec2_credentials_request(struct flb_aws_provider_ec2
379393

380394
flb_sds_destroy(credentials_response);
381395
return 0;
382-
}
396+
}

src/aws/flb_aws_credentials_http.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,26 @@ struct flb_aws_credentials *get_credentials_fn_http(struct flb_aws_provider
152152
return NULL;
153153
}
154154

155+
/**
156+
* Trigger an immediate credentials refresh for an HTTP provider.
157+
*
158+
* If the provider can be locked, forces an immediate refresh and performs a
159+
* credential fetch using the provider's HTTP implementation; the lock is
160+
* released after the fetch completes. If the provider lock cannot be
161+
* acquired, no refresh is attempted.
162+
*
163+
* @param provider AWS provider that contains the HTTP implementation to refresh.
164+
* @returns `0` on successful credential retrieval and update, `-1` on failure or if the provider lock could not be acquired.
165+
*/
155166
int refresh_fn_http(struct flb_aws_provider *provider) {
156167
struct flb_aws_provider_http *implementation = provider->implementation;
157168
int ret = -1;
158169
flb_debug("[aws_credentials] Refresh called on the http provider");
159170

160171
if (try_lock_provider(provider)) {
172+
/* Set to 1 (epoch start) to trigger immediate refresh via time check */
173+
implementation->next_refresh = 1;
174+
161175
ret = http_credentials_request(implementation);
162176
unlock_provider(provider);
163177
}
@@ -690,4 +704,4 @@ struct flb_aws_credentials *flb_parse_json_credentials(char *response,
690704
flb_aws_credentials_destroy(creds);
691705
flb_free(tokens);
692706
return NULL;
693-
}
707+
}

src/aws/flb_aws_credentials_profile.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,19 @@ static int get_shared_config_credentials(char* config_path,
643643
return result;
644644
}
645645

646+
/**
647+
* Load AWS credentials for the given profile from the shared credentials file.
648+
*
649+
* Allocates and fills a flb_aws_credentials structure pointed to by `*creds` when a matching
650+
* profile is found in the file at `credentials_path`. On failure the function frees any
651+
* allocated resources and sets `*creds` to NULL.
652+
*
653+
* @param credentials_path Path to the shared credentials file.
654+
* @param profile Name of the profile to load.
655+
* @param creds Output pointer that will receive an allocated credentials structure on success.
656+
* @param debug_only If non-zero, suppresses warning-level messages in favor of debug-level logging.
657+
* @return `0` on success (credentials populated in `*creds`), `-1` on failure (`*creds` is set to NULL).
658+
*/
646659
static int get_shared_credentials(char* credentials_path,
647660
char* profile,
648661
struct flb_aws_credentials** creds,
@@ -663,8 +676,7 @@ static int get_shared_credentials(char* credentials_path,
663676

664677
if (flb_read_file(credentials_path, &buf, &size) < 0) {
665678
if (errno == ENOENT) {
666-
AWS_CREDS_ERROR_OR_DEBUG(debug_only, "Shared credentials file %s does not exist",
667-
credentials_path);
679+
AWS_CREDS_DEBUG("Shared credentials file %s does not exist", credentials_path);
668680
} else {
669681
flb_errno();
670682
AWS_CREDS_ERROR_OR_DEBUG(debug_only, "Could not read shared credentials file %s",
@@ -750,4 +762,4 @@ static int refresh_credentials(struct flb_aws_provider_profile *implementation,
750762
error:
751763
flb_aws_credentials_destroy(creds);
752764
return -1;
753-
}
765+
}

src/aws/flb_aws_credentials_sts.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,25 @@ struct flb_aws_credentials *get_credentials_fn_sts(struct flb_aws_provider
170170
return NULL;
171171
}
172172

173+
/**
174+
* Trigger an immediate refresh of STS credentials for the given provider.
175+
*
176+
* Sets the provider's next_refresh to epoch start to force an immediate AssumeRole
177+
* request and attempts to perform the STS AssumeRole call to update cached credentials.
178+
*
179+
* @param provider The AWS provider instance whose STS implementation will be refreshed.
180+
* @returns `0` if the credentials were successfully refreshed; `-1` on failure or if the provider lock could not be acquired.
181+
*/
173182
int refresh_fn_sts(struct flb_aws_provider *provider) {
174183
int ret = -1;
175184
struct flb_aws_provider_sts *implementation = provider->implementation;
176185

177186
flb_debug("[aws_credentials] Refresh called on the STS provider");
178-
187+
179188
if (try_lock_provider(provider)) {
189+
/* Set to 1 (epoch start) to trigger immediate refresh via time check */
190+
implementation->next_refresh = 1;
191+
180192
ret = sts_assume_role_request(implementation->sts_client,
181193
&implementation->creds, implementation->uri,
182194
&implementation->next_refresh);
@@ -475,12 +487,24 @@ struct flb_aws_credentials *get_credentials_fn_eks(struct flb_aws_provider
475487
return NULL;
476488
}
477489

490+
/**
491+
* Trigger a credentials refresh for the EKS provider.
492+
*
493+
* Attempts to acquire the provider lock, forces an immediate refresh window, and requests new credentials using the web-identity flow.
494+
*
495+
* @param provider EKS provider instance.
496+
* @returns 0 on success, -1 on failure or if the provider lock could not be acquired.
497+
*/
478498
int refresh_fn_eks(struct flb_aws_provider *provider) {
479499
int ret = -1;
480500
struct flb_aws_provider_eks *implementation = provider->implementation;
481501

482502
flb_debug("[aws_credentials] Refresh called on the EKS provider");
503+
483504
if (try_lock_provider(provider)) {
505+
/* Set to 1 (epoch start) to trigger immediate refresh via time check */
506+
implementation->next_refresh = 1;
507+
484508
ret = assume_with_web_identity(implementation);
485509
unlock_provider(provider);
486510
}
@@ -955,4 +979,4 @@ static flb_sds_t get_node(char *cred_node, char* node_name, int node_name_len, c
955979
}
956980

957981
return val;
958-
}
982+
}

0 commit comments

Comments
 (0)