@@ -590,27 +590,70 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
590590 /* Set Auth type */
591591 tmp = (char * ) flb_output_get_property ("auth_type" , ins );
592592 if (!tmp ) {
593- ctx -> atype = AZURE_BLOB_AUTH_KEY ;
593+ ctx -> atype = FLB_AZURE_AUTH_KEY ; /* Default to legacy key auth */
594594 }
595595 else {
596596 if (strcasecmp (tmp , "key" ) == 0 ) {
597- ctx -> atype = AZURE_BLOB_AUTH_KEY ;
597+ ctx -> atype = FLB_AZURE_AUTH_KEY ;
598598 }
599599 else if (strcasecmp (tmp , "sas" ) == 0 ) {
600- ctx -> atype = AZURE_BLOB_AUTH_SAS ;
600+ ctx -> atype = FLB_AZURE_AUTH_SAS ;
601+ }
602+ else if (strcasecmp (tmp , "service_principal" ) == 0 ) {
603+ ctx -> atype = FLB_AZURE_AUTH_SERVICE_PRINCIPAL ;
604+
605+ /* Verify required parameters for Service Principal auth */
606+ if (!ctx -> tenant_id || !ctx -> client_id || !ctx -> client_secret ) {
607+ flb_plg_error (ins , "When using service_principal auth, tenant_id, client_id, and client_secret are required" );
608+ return NULL ;
609+ }
610+ }
611+ else if (strcasecmp (tmp , "managed_identity" ) == 0 ) {
612+ /* Check if client_id indicates system-assigned or user-assigned managed identity */
613+ if (!ctx -> client_id ) {
614+ flb_plg_error (ins , "When using managed_identity auth, client_id must be set to 'system' for system-assigned or the managed identity client ID" );
615+ return NULL ;
616+ }
617+
618+ if (strcasecmp (ctx -> client_id , "system" ) == 0 ) {
619+ ctx -> atype = FLB_AZURE_AUTH_MANAGED_IDENTITY_SYSTEM ;
620+ } else {
621+ ctx -> atype = FLB_AZURE_AUTH_MANAGED_IDENTITY_USER ;
622+ }
623+ }
624+ else if (strcasecmp (tmp , "workload_identity" ) == 0 ) {
625+ ctx -> atype = FLB_AZURE_AUTH_WORKLOAD_IDENTITY ;
626+
627+ /* Verify required parameters for Workload Identity auth */
628+ if (!ctx -> tenant_id || !ctx -> client_id ) {
629+ flb_plg_error (ins , "When using workload_identity auth, tenant_id and client_id are required" );
630+ return NULL ;
631+ }
632+
633+ /* Set default token file path if not specified */
634+ if (!ctx -> workload_identity_token_file ) {
635+ ctx -> workload_identity_token_file = flb_sds_create (FLB_AZURE_WORKLOAD_IDENTITY_TOKEN_FILE );
636+ if (!ctx -> workload_identity_token_file ) {
637+ flb_errno ();
638+ flb_plg_error (ins , "Could not allocate default workload identity token path" );
639+ return NULL ;
640+ }
641+ }
601642 }
602643 else {
603- flb_plg_error (ctx -> ins , "invalid auth_type value '%s'" , tmp );
644+ flb_plg_error (ctx -> ins , "invalid auth_type value '%s'. Valid options are: 'key', 'sas', 'service_principal', 'managed_identity', or 'workload_identity' " , tmp );
604645 return NULL ;
605646 }
606647 }
607- if (ctx -> atype == AZURE_BLOB_AUTH_KEY &&
648+
649+ /* Validate auth-specific requirements */
650+ if (ctx -> atype == FLB_AZURE_AUTH_KEY &&
608651 ctx -> shared_key == NULL ) {
609652 flb_plg_error (ctx -> ins , "'shared_key' has not been set" );
610653 return NULL ;
611654 }
612655
613- if (ctx -> atype == AZURE_BLOB_AUTH_SAS ) {
656+ if (ctx -> atype == FLB_AZURE_AUTH_SAS ) {
614657 if (ctx -> sas_token == NULL ) {
615658 flb_plg_error (ctx -> ins , "'sas_token' has not been set" );
616659 return NULL ;
@@ -730,6 +773,35 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
730773 }
731774 flb_output_upstream_set (ctx -> u , ins );
732775
776+ /* Initialize OAuth2 context for OAuth-based authentication methods */
777+ if (ctx -> atype == FLB_AZURE_AUTH_SERVICE_PRINCIPAL ||
778+ ctx -> atype == FLB_AZURE_AUTH_MANAGED_IDENTITY_SYSTEM ||
779+ ctx -> atype == FLB_AZURE_AUTH_MANAGED_IDENTITY_USER ||
780+ ctx -> atype == FLB_AZURE_AUTH_WORKLOAD_IDENTITY ) {
781+
782+ /* Build OAuth URL based on auth type */
783+ ctx -> oauth_url = flb_azure_auth_build_oauth_url (ctx -> atype ,
784+ ctx -> tenant_id ,
785+ ctx -> client_id ,
786+ FLB_AZURE_BLOB_RESOURCE_SCOPE );
787+ if (!ctx -> oauth_url ) {
788+ flb_plg_error (ctx -> ins , "failed to create OAuth URL" );
789+ return NULL ;
790+ }
791+
792+ /* Create OAuth2 context */
793+ ctx -> o = flb_oauth2_create (ctx -> config , ctx -> oauth_url , 3000 );
794+ if (!ctx -> o ) {
795+ flb_plg_error (ctx -> ins , "cannot create oauth2 context" );
796+ return NULL ;
797+ }
798+
799+ /* Initialize token mutex */
800+ pthread_mutex_init (& ctx -> token_mutex , NULL );
801+
802+ flb_plg_info (ctx -> ins , "oauth2 context initialized for auth type" );
803+ }
804+
733805 /* Compose base uri */
734806 ctx -> base_uri = flb_sds_create_size (256 );
735807 if (!ctx -> base_uri ) {
@@ -772,13 +844,40 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
772844
773845 pthread_mutex_init (& ctx -> file_upload_commit_file_parts , NULL );
774846
775- flb_plg_info (ctx -> ins ,
776- "account_name=%s, container_name=%s, blob_type=%s, emulator_mode=%s, endpoint=%s, auth_type=%s" ,
777- ctx -> account_name , ctx -> container_name ,
778- ctx -> btype == AZURE_BLOB_APPENDBLOB ? "appendblob" : "blockblob" ,
779- ctx -> emulator_mode ? "yes" : "no" ,
780- ctx -> real_endpoint ? ctx -> real_endpoint : "no" ,
781- ctx -> atype == AZURE_BLOB_AUTH_KEY ? "key" : "sas" );
847+ /* Log configuration summary */
848+ {
849+ const char * auth_type_str ;
850+ switch (ctx -> atype ) {
851+ case FLB_AZURE_AUTH_KEY :
852+ auth_type_str = "key" ;
853+ break ;
854+ case FLB_AZURE_AUTH_SAS :
855+ auth_type_str = "sas" ;
856+ break ;
857+ case FLB_AZURE_AUTH_SERVICE_PRINCIPAL :
858+ auth_type_str = "service_principal" ;
859+ break ;
860+ case FLB_AZURE_AUTH_MANAGED_IDENTITY_SYSTEM :
861+ auth_type_str = "managed_identity (system)" ;
862+ break ;
863+ case FLB_AZURE_AUTH_MANAGED_IDENTITY_USER :
864+ auth_type_str = "managed_identity (user)" ;
865+ break ;
866+ case FLB_AZURE_AUTH_WORKLOAD_IDENTITY :
867+ auth_type_str = "workload_identity" ;
868+ break ;
869+ default :
870+ auth_type_str = "unknown" ;
871+ }
872+
873+ flb_plg_info (ctx -> ins ,
874+ "account_name=%s, container_name=%s, blob_type=%s, emulator_mode=%s, endpoint=%s, auth_type=%s" ,
875+ ctx -> account_name , ctx -> container_name ,
876+ ctx -> btype == AZURE_BLOB_APPENDBLOB ? "appendblob" : "blockblob" ,
877+ ctx -> emulator_mode ? "yes" : "no" ,
878+ ctx -> real_endpoint ? ctx -> real_endpoint : "no" ,
879+ auth_type_str );
880+ }
782881 return ctx ;
783882}
784883
@@ -826,6 +925,21 @@ void flb_azure_blob_conf_destroy(struct flb_azure_blob *ctx)
826925 flb_upstream_destroy (ctx -> u );
827926 }
828927
928+ /* Cleanup OAuth2 resources */
929+ if (ctx -> oauth_url ) {
930+ flb_sds_destroy (ctx -> oauth_url );
931+ }
932+
933+ if (ctx -> o ) {
934+ flb_oauth2_destroy (ctx -> o );
935+ }
936+
937+ if (ctx -> atype == FLB_AZURE_AUTH_SERVICE_PRINCIPAL ||
938+ ctx -> atype == FLB_AZURE_AUTH_MANAGED_IDENTITY_SYSTEM ||
939+ ctx -> atype == FLB_AZURE_AUTH_MANAGED_IDENTITY_USER ||
940+ ctx -> atype == FLB_AZURE_AUTH_WORKLOAD_IDENTITY ) {
941+ pthread_mutex_destroy (& ctx -> token_mutex );
942+ }
829943
830944 azb_db_close (ctx );
831945 flb_free (ctx );
0 commit comments