Skip to content

Commit 5a7f650

Browse files
committed
azure_auth: add common Azure authentication module
Add centralized Azure authentication code for Managed Identity (MSI), Workload Identity, and Service Principal authentication. This provides shared authentication functionality for Azure-related output plugins. Features: - Support for system-assigned and user-assigned managed identities - Workload identity authentication with federated tokens - Service principal authentication with client credentials - Dynamic OAuth URL builder for different auth types and resources - Configurable resource scopes for different Azure services Signed-off-by: zshuang0316 <zshuang0316@163.com>
1 parent 15299f0 commit 5a7f650

3 files changed

Lines changed: 441 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2026 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef FLB_AZURE_AUTH_H
21+
#define FLB_AZURE_AUTH_H
22+
23+
#include <fluent-bit/flb_info.h>
24+
#include <fluent-bit/flb_oauth2.h>
25+
#include <fluent-bit/flb_sds.h>
26+
27+
/* Authentication types for Azure services */
28+
typedef enum {
29+
FLB_AZURE_AUTH_KEY = 0, /* Shared Access Key */
30+
FLB_AZURE_AUTH_SAS, /* Shared Access Signature */
31+
FLB_AZURE_AUTH_SERVICE_PRINCIPAL, /* Service Principal (Client ID + Secret) */
32+
FLB_AZURE_AUTH_MANAGED_IDENTITY_SYSTEM, /* System-assigned Managed Identity */
33+
FLB_AZURE_AUTH_MANAGED_IDENTITY_USER, /* User-assigned Managed Identity */
34+
FLB_AZURE_AUTH_WORKLOAD_IDENTITY /* Workload Identity (Federated Token) */
35+
} flb_azure_auth_type;
36+
37+
/* Azure Instance Metadata Service (IMDS) endpoint for Managed Identity */
38+
#define FLB_AZURE_IMDS_HOST "169.254.169.254"
39+
#define FLB_AZURE_IMDS_PORT "80"
40+
41+
/* Managed Identity authentication URL template */
42+
#define FLB_AZURE_MSI_AUTH_URL_TEMPLATE \
43+
"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2021-02-01%s%s&resource=%s"
44+
45+
/* Microsoft Authentication Library (MSAL) authorization URL template */
46+
#define FLB_AZURE_MSAL_AUTH_URL_TEMPLATE \
47+
"https://login.microsoftonline.com/%s/oauth2/v2.0/token"
48+
49+
/* Azure Blob Storage default resource scope (with .default suffix for OAuth) */
50+
#define FLB_AZURE_BLOB_RESOURCE_SCOPE "https://storage.azure.com/.default"
51+
52+
/* Azure Kusto default resource scope (with .default suffix for OAuth) */
53+
#define FLB_AZURE_KUSTO_RESOURCE_SCOPE "https://help.kusto.windows.net/.default"
54+
55+
/* Default Workload Identity token file path */
56+
#define FLB_AZURE_WORKLOAD_IDENTITY_TOKEN_FILE \
57+
"/var/run/secrets/azure/tokens/azure-identity-token"
58+
59+
/**
60+
* Get an OAuth2 access token using Azure Managed Identity (MSI)
61+
*
62+
* This function retrieves an access token from the Azure Instance Metadata Service (IMDS)
63+
* for use with Azure services. Supports both system-assigned and user-assigned managed identities.
64+
*
65+
* @param ctx OAuth2 context containing connection and token information
66+
* @return Access token string on success, NULL on failure
67+
*/
68+
char *flb_azure_msi_token_get(struct flb_oauth2 *ctx);
69+
70+
/**
71+
* Get an OAuth2 access token using Azure Workload Identity
72+
*
73+
* This function exchanges a federated token (JWT) for an Azure AD access token
74+
* using the OAuth2 client credentials flow with client assertion.
75+
*
76+
* @param ctx OAuth2 context for token management
77+
* @param token_file Path to the file containing the federated token
78+
* @param client_id Client ID of the Azure AD application
79+
* @param tenant_id Tenant ID of the Azure AD directory
80+
* @param resource Resource scope for the token (e.g., "https://storage.azure.com/")
81+
* @return 0 on success, -1 on failure
82+
*/
83+
int flb_azure_workload_identity_token_get(struct flb_oauth2 *ctx,
84+
const char *token_file,
85+
const char *client_id,
86+
const char *tenant_id,
87+
const char *resource);
88+
89+
/**
90+
* Build OAuth URL for Azure authentication
91+
*
92+
* Creates the appropriate OAuth2 endpoint URL based on the authentication type.
93+
* For Managed Identity, uses IMDS endpoint. For Service Principal and Workload Identity,
94+
* uses Azure AD OAuth2 endpoint.
95+
*
96+
* @param auth_type Type of authentication to use
97+
* @param tenant_id Azure AD tenant ID (required for Service Principal and Workload Identity)
98+
* @param client_id Client ID (optional, used for user-assigned managed identity)
99+
* @param resource Resource scope for the token (e.g., storage.azure.com)
100+
* @return Allocated SDS string with OAuth URL, or NULL on failure
101+
*/
102+
flb_sds_t flb_azure_auth_build_oauth_url(flb_azure_auth_type auth_type,
103+
const char *tenant_id,
104+
const char *client_id,
105+
const char *resource);
106+
107+
#endif /* FLB_AZURE_AUTH_H */

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ if(FLB_TLS)
164164
"tls/flb_tls.c"
165165
"flb_oauth2.c"
166166
"flb_oauth2_jwt.c"
167+
"flb_azure_auth.c"
167168
)
168169

169170
# Make sure our output targets links to the TLS library

0 commit comments

Comments
 (0)