2424#include "azure_blob.h"
2525#include "azure_blob_conf.h"
2626#include "azure_blob_db.h"
27+ #include "azure_blob_msiauth.h"
2728
2829#include <sys/types.h>
2930#include <sys/stat.h>
@@ -599,6 +600,29 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
599600 else if (strcasecmp (tmp , "sas" ) == 0 ) {
600601 ctx -> atype = AZURE_BLOB_AUTH_SAS ;
601602 }
603+ else if (strcasecmp (tmp , "managed_identity" ) == 0 ) {
604+ if (!ctx -> client_id ) {
605+ flb_plg_error (ctx -> ins ,
606+ "managed_identity auth requires 'client_id' "
607+ "(set to 'system' for system-assigned)" );
608+ return NULL ;
609+ }
610+ if (strcasecmp (ctx -> client_id , "system" ) == 0 ) {
611+ ctx -> atype = AZURE_BLOB_AUTH_MI_SYSTEM ;
612+ }
613+ else {
614+ ctx -> atype = AZURE_BLOB_AUTH_MI_USER ;
615+ }
616+ }
617+ else if (strcasecmp (tmp , "workload_identity" ) == 0 ) {
618+ ctx -> atype = AZURE_BLOB_AUTH_WI ;
619+ if (!ctx -> tenant_id || !ctx -> client_id ) {
620+ flb_plg_error (ctx -> ins ,
621+ "workload_identity auth requires "
622+ "'tenant_id' and 'client_id'" );
623+ return NULL ;
624+ }
625+ }
602626 else {
603627 flb_plg_error (ctx -> ins , "invalid auth_type value '%s'" , tmp );
604628 return NULL ;
@@ -755,6 +779,59 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
755779 flb_sds_printf (& ctx -> shared_key_prefix , "SharedKey %s:" , ctx -> account_name );
756780 }
757781
782+ /* Create OAuth2 context for managed identity / workload identity */
783+ if (ctx -> atype == AZURE_BLOB_AUTH_MI_SYSTEM ||
784+ ctx -> atype == AZURE_BLOB_AUTH_MI_USER ) {
785+ /* Construct IMDS URL */
786+ if (ctx -> atype == AZURE_BLOB_AUTH_MI_SYSTEM ) {
787+ ctx -> oauth_url = flb_sds_create_size (
788+ sizeof (FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE ) + 1 );
789+ if (!ctx -> oauth_url ) {
790+ return NULL ;
791+ }
792+ flb_sds_snprintf (& ctx -> oauth_url , flb_sds_alloc (ctx -> oauth_url ),
793+ FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE , "" , "" );
794+ }
795+ else {
796+ ctx -> oauth_url = flb_sds_create_size (
797+ sizeof (FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE ) +
798+ sizeof ("&client_id=" ) + flb_sds_len (ctx -> client_id ));
799+ if (!ctx -> oauth_url ) {
800+ return NULL ;
801+ }
802+ flb_sds_snprintf (& ctx -> oauth_url , flb_sds_alloc (ctx -> oauth_url ),
803+ FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE ,
804+ "&client_id=" , ctx -> client_id );
805+ }
806+
807+ ctx -> o = flb_oauth2_create (config , ctx -> oauth_url , 3000 );
808+ if (!ctx -> o ) {
809+ flb_plg_error (ctx -> ins , "cannot create OAuth2 context for IMDS" );
810+ return NULL ;
811+ }
812+ flb_stream_disable_async_mode (& ctx -> o -> u -> base );
813+ pthread_mutex_init (& ctx -> token_mutex , NULL );
814+ }
815+ else if (ctx -> atype == AZURE_BLOB_AUTH_WI ) {
816+ /* Construct Azure AD token endpoint URL */
817+ ctx -> oauth_url = flb_sds_create_size (
818+ sizeof (FLB_AZURE_BLOB_MSAL_AUTH_URL_TEMPLATE ) +
819+ flb_sds_len (ctx -> tenant_id ));
820+ if (!ctx -> oauth_url ) {
821+ return NULL ;
822+ }
823+ flb_sds_snprintf (& ctx -> oauth_url , flb_sds_alloc (ctx -> oauth_url ),
824+ FLB_AZURE_BLOB_MSAL_AUTH_URL_TEMPLATE , ctx -> tenant_id );
825+
826+ ctx -> o = flb_oauth2_create (config , ctx -> oauth_url , 3000 );
827+ if (!ctx -> o ) {
828+ flb_plg_error (ctx -> ins , "cannot create OAuth2 context for workload identity" );
829+ return NULL ;
830+ }
831+ flb_stream_disable_async_mode (& ctx -> o -> u -> base );
832+ pthread_mutex_init (& ctx -> token_mutex , NULL );
833+ }
834+
758835 /* Sanitize path: remove any ending slash */
759836 if (ctx -> path ) {
760837 if (ctx -> path [flb_sds_len (ctx -> path ) - 1 ] == '/' ) {
@@ -778,7 +855,11 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
778855 ctx -> btype == AZURE_BLOB_APPENDBLOB ? "appendblob" : "blockblob" ,
779856 ctx -> emulator_mode ? "yes" : "no" ,
780857 ctx -> real_endpoint ? ctx -> real_endpoint : "no" ,
781- ctx -> atype == AZURE_BLOB_AUTH_KEY ? "key" : "sas" );
858+ ctx -> atype == AZURE_BLOB_AUTH_KEY ? "key" :
859+ ctx -> atype == AZURE_BLOB_AUTH_SAS ? "sas" :
860+ ctx -> atype == AZURE_BLOB_AUTH_MI_SYSTEM ? "managed_identity (system)" :
861+ ctx -> atype == AZURE_BLOB_AUTH_MI_USER ? "managed_identity (user)" :
862+ "workload_identity" );
782863 return ctx ;
783864}
784865
@@ -822,6 +903,20 @@ void flb_azure_blob_conf_destroy(struct flb_azure_blob *ctx)
822903 flb_sds_destroy (ctx -> shared_key_prefix );
823904 }
824905
906+ if (ctx -> oauth_url ) {
907+ flb_sds_destroy (ctx -> oauth_url );
908+ }
909+
910+ if (ctx -> o ) {
911+ flb_oauth2_destroy (ctx -> o );
912+ }
913+
914+ if (ctx -> atype == AZURE_BLOB_AUTH_MI_SYSTEM ||
915+ ctx -> atype == AZURE_BLOB_AUTH_MI_USER ||
916+ ctx -> atype == AZURE_BLOB_AUTH_WI ) {
917+ pthread_mutex_destroy (& ctx -> token_mutex );
918+ }
919+
825920 if (ctx -> u ) {
826921 flb_upstream_destroy (ctx -> u );
827922 }
0 commit comments