Forum Discussion

rayphoon's avatar
rayphoon
New Member
6 months ago
Solved

API call to modify Workspace Diagnostics does not work when run under a Service Principal

We are trying to use REST API calls to Fabric to configure diagnostic settings for our Fabric workspaces using a service principal as described in 

https://learn.microsoft.com/en-us/rest/api/fabric/core/onelake-settings/modify-diagnostics?tabs=HTTP

 

The API call returns a 500 error with message "InternalServerError". The same API call works when run using an access token from a user.

 

Tested that the "Get Settings" API call for the onelake diagnostic settings works fine using the service principal.

https://learn.microsoft.com/en-us/rest/api/fabric/core/onelake-settings/get-settings?tabs=HTTP

 

Checked that the user and service principal has the same roles (Admin) on the workspace as well as the target diagnostic workspace where the lakehouse resides.

 

Has anybody come across this issue?

 

Thanks.

 

  • hi rayphoon , I have good news (hopefully 🀞) I was able to reproduce your issue, that is, using a SPN calling the interface as documented in the https://learn.microsoft.com/en-us/rest/api/fabric/core/onelake-settings/modify-diagnostics?tabs=HTTP and got the same the same 500 error, but I knew my SPN has all the neccesary Fabric tenant permissions because I use it to create Fabric items ALL THE TIME, also, the same way you do, have a security group with the SPN as member as admin of the workspace, so basically I knew it wasn't a permission issue. 

     

    But then, I remember other instances where, in order for the SPN to execute some operations, IT NEEDS TO BE THE OWNER OF THE ITEMS, else get a 500 error, so, I decided to test this; as shown below, the SPN has admin permission on the workspace via an Entra ID sec. group, and I created a Lakehouse whitout schema support, as shown in the image, the SPN is the owner! 

     

     

    And after I did this, I invoked the interface using the SPN (as usual) and IT WORKED! 

     

     

     I'm pretty confident it will work for you the same way it did for me ... give a try and let us know 😁🀞  ! 

     

    v-karpurapud  can you help us understand why would it be requiered for the SPN to be owner of the lakehouse in addition of workspace admin permissions?  Perhaps also strzala  can you confirm the ownership of the lakehouse for your script to work? I think this confirmation will help figure this out! 

     

    Don't forget a thumbs up if you find this information useful and accept as solution if appropiate. 

     

8 Replies

  • hi rayphoon , I have good news (hopefully 🀞) I was able to reproduce your issue, that is, using a SPN calling the interface as documented in the https://learn.microsoft.com/en-us/rest/api/fabric/core/onelake-settings/modify-diagnostics?tabs=HTTP and got the same the same 500 error, but I knew my SPN has all the neccesary Fabric tenant permissions because I use it to create Fabric items ALL THE TIME, also, the same way you do, have a security group with the SPN as member as admin of the workspace, so basically I knew it wasn't a permission issue. 

     

    But then, I remember other instances where, in order for the SPN to execute some operations, IT NEEDS TO BE THE OWNER OF THE ITEMS, else get a 500 error, so, I decided to test this; as shown below, the SPN has admin permission on the workspace via an Entra ID sec. group, and I created a Lakehouse whitout schema support, as shown in the image, the SPN is the owner! 

     

     

    And after I did this, I invoked the interface using the SPN (as usual) and IT WORKED! 

     

     

     I'm pretty confident it will work for you the same way it did for me ... give a try and let us know 😁🀞  ! 

     

    v-karpurapud  can you help us understand why would it be requiered for the SPN to be owner of the lakehouse in addition of workspace admin permissions?  Perhaps also strzala  can you confirm the ownership of the lakehouse for your script to work? I think this confirmation will help figure this out! 

     

    Don't forget a thumbs up if you find this information useful and accept as solution if appropiate. 

     

    • strzala's avatar
      strzala
      Frequent Visitor

      In my case, I am the owner of the lakehouse not SP.

       


      Did I answer your question?
      Please mark as Solution to help others find the answer.
      πŸ‘ Kudos are always appreciated!

      Andrzej Strzala | Data Guideline
      LinkedIn | Blog | YouTube

       

    • v-karpurapud's avatar
      v-karpurapud
      Community Support

      Hi svenchio

      Just to clarify, I did not suggest that the service principal must be the Owner of the lakehouse. In this scenario, Workspace Admin permissions should be sufficient to modify OneLake diagnostic settings. My earlier comment only emphasized the importance of verifying the appropriate workspace-level roles on both the source and destination workspaces, rather than any item-level ownership requirements.

      Regards,

      Microsoft Fabric Community Support Team.

  • strzala's avatar
    strzala
    Frequent Visitor

    Have you tried granting admin permissions to the Service Principal at the workspace level instead of to the entire security group?

    You can also test the code below in PowerShell ISE and verify the response you receive. Make sure to provide the Service Principal details, as well as the Workspace ID and Lakehouse ID where diagnostic is enabled.
    Request body is just a sample from documentation: https://learn.microsoft.com/en-us/rest/api/fabric/core/onelake-settings/modify-diagnostics

    $TenantId       = "YOUR_TENANT_ID"
    $ClientId       = "YOUR_CLIENT_ID"
    $ClientSecret   = "YOUR_CLIENT_SECRET"
    
    $WorkspaceId    = "YOUR_WORKSAPCE_ID"
    $LakehouseItemId = "YOUR_LAKEHOUSE_ID"
    
    $tokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
    
    $tokenBody = @{
        grant_type    = "client_credentials"
        client_id     = $ClientId
        client_secret = $ClientSecret
        scope         = "https://api.fabric.microsoft.com/.default"
    }
    
    try {
        $tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $tokenBody
        $accessToken = $tokenResponse.access_token
        Write-Host "Access token acquired successfully." -ForegroundColor Green
    }
    catch {
        Write-Error "Failed to acquire access token: $_"
        exit 1
    }
    
    $apiUrl = "https://api.fabric.microsoft.com/v1/workspaces/$WorkspaceId/onelake/settings/modifyDiagnostics"
    
    $body = @{
        status      = "Enabled"
        destination = @{
            type      = "Lakehouse"
            lakehouse = @{
                referenceType = "ById"
                itemId        = $LakehouseItemId
                workspaceId   = $WorkspaceId
            }
        }
    } | ConvertTo-Json -Depth 5
    
    $headers = @{
        Authorization  = "Bearer $accessToken"
        "Content-Type" = "application/json"
    }
    
    try {
        $response = Invoke-WebRequest -Method Post -Uri $apiUrl -Headers $headers -Body $body -UseBasicParsing
        Write-Host "Response Code: $($response.StatusCode)" -ForegroundColor Green
        Write-Host "Diagnostics setting modified successfully." -ForegroundColor Green
        if ($response.Content) {
            $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 5
        }
    }
    catch {
        if ($_.Exception.Response) {
            $statusCode = [int]$_.Exception.Response.StatusCode
            Write-Error "API call failed with status code: $statusCode"
            $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
            $errorBody = $reader.ReadToEnd()
            Write-Error "Response body: $errorBody"
        }
        else {
            Write-Error "API call failed: $($_.Exception.Message)"
        }
    }

     
    You should receive following status:

     

     

    • rayphoon's avatar
      rayphoon
      New Member

      Thanks for your reply.

       

      Yes, I had tried adding the service principal directly as admin on the workspace as well as the group it is a member of. I also tried giving Fabric Admin role to the Service Principal.

       

      Running the code provided gives the following error:

      PS C:\Users\rayp> .\test.ps1
      Access token acquired successfully.
      C:\Users\rayp\test.ps1 : API call failed with status code: 500
      At line:1 char:1
      + .\test.ps1
      + ~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

      C:\Users\rayp\test.ps1 : Response body: {"requestId":"889be9a9-b834-429d-ae49-f9e3d61b5e6c","errorCode":"InternalServer
      Error","moreDetails":[{"errorCode":"ServerError","message":"","relatedResource":{"resourceId":"913c514f-8d8a-48f4-a365-
      71e1d2574a77","resourceType":"Workspace"}}],"message":"An error
      occured","relatedResource":{"resourceId":"913c514f-8d8a-48f4-a365-71e1d2574a77","resourceType":"Workspace"}}
      At line:1 char:1
      + .\test.ps1
      + ~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

       

      • strzala's avatar
        strzala
        Frequent Visitor

        rayphoon I've managed to recrete the issue. You are right, when lakehouse is in another Workspace I get the same error code. I think Microsoft should investigate this.

        } : API call failed with status code: 500
            + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
            + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
        
        Response body: {"requestId":"d177ee27-301c-49b3-a111-ca375d4a12ba","errorCode":"InternalServerError","moreDetails":[{"errorCode":"ServerError","message":"","related
        Resource":{"resourceId":"135911f1-13ef-4317-83ce-f822697e4ff1","resourceType":"Workspace"}}],"message":"An error occured","relatedResource":{"resourceId":"135922f1-13ef
        -4317-83ce-f822697e4ff1","resourceType":"Workspace"}}
            + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
            + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
        

         

         


        Did I answer your question?
        Please mark as Solution to help others find the answer.
        πŸ‘ Kudos are always appreciated!

        Andrzej Strzala | Data Guideline
        LinkedIn | Blog | YouTube
  • v-karpurapud's avatar
    v-karpurapud
    Community Support

    Hi rayphoon 
    Thank you for reaching out to the Microsoft Fabric community forum.
     

    The behavior you’re observing indicates that authentication and initial authorization are working, as the GET endpoint is successful and no 401 or 403 errors are returned. However, the Modify OneLake Diagnostics operation fails during backend processing. While the API documentation states that service principals are supported, write operations follow a different execution path than read operations, including additional internal validation and handling for long-running operations.
     

    Before assuming a product issue, please verify that tenant settings allow service principals to access Fabric APIs, that the service principal has the required roles on both the source and destination workspaces, that both workspaces are on the same capacity, and that the 202/long-running operation pattern is being handled correctly. If all these conditions are met and the request still returns a 500 error only when using a service principal but works with a user token, this likely indicates a backend issue.

     

    For more details, please refer to the Microsoft official documentation on
    Developer admin settings - Microsoft Fabric | Microsoft Learn
    Tenant settings index - Microsoft Fabric | Microsoft Learn

     

    If you have any further questions, feel free to reach out and we'll be glad to assist.

     

    Regards,

    Microsoft Fabric Community Support Team.
     

  • Hi, I should have mentioned that testing was done using Postman so I could see the response codes returned.

    Both workspaces are on the same capacity and the Service Principal is a member of a security group which has been granted API access for basically all settings in the Fabric Admin portal that has "service principal" in the setting name.

     

    We have also tried adding the following API permissions to the App Registration service principal:

    Azure Service Management - user_impersonation - Delegated

    Azure Storage - user_impersonation - Delegated

    Microsoft Graph - User.Read - Delegated

    Power BI Service - App.Read.All, OneLake.ReadWrite.All - Delegated

     

    The error is 500 - Internal server error with the following response:

    {
        "requestId": "93ffd950-f22e-47aa-a5e7-f54235584ba6",
        "errorCode": "InternalServerError",
        "moreDetails": [
            {
                "errorCode": "ServerError",
                "message": "",
                "relatedResource": {
                    "resourceId": "73fe3d71-d140-4377-a3c0-73916a1272a0",
                    "resourceType": "Workspace"
                }
            }
        ],
        "message": "An error occured",
        "relatedResource": {
            "resourceId": "73fe3d71-d140-4377-a3c0-73916a1272a0",
            "resourceType": "Workspace"
        }
    }