APIM - set body return variable as secret

Pothiraj, Saranya-ADM 0 Reputation points
2025-01-20T18:10:35.95+00:00

Hi Team,

I have below policy

<outbound>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode == 200)">
                <set-status code="200" reason="ok" />
                <set-body template="liquid" >
                {
                    {{context.Variables["vault_response"]["data"]["data"]}}
                }
                </set-body>
            </when>
            <otherwise>
                <set-status code="@(context.Response.StatusCode)" reason="Bad Request" />
                <set-body>@(((IResponse)context.Variables["response"]).Body.As<JObject>(preserveContent: true).ToString())</set-body>
            </otherwise>
        </choose>

Where in set-body i return the secrets DB username and password to Azure Devops Pipeline

Here is the pipeline script:

 try {
      $params = @{
          ContentType = 'application/x-www-form-urlencoded'
          Method = 'GET'
          URI = 'https://apim.ddd.com/test-apim-login/v1/sys/health'
      }
      $token = Invoke-RestMethod @params
      Write-host $token
    }

Return value in pipeline:

{

"vault_response":""password": "msadfajfnbafb""username": "abcdef""

}

But i need the return value as hidden secret

{

"vault_response":""password": ******** "username":********

}

Please let me know how to achieve this

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,281 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shireesha Eeraboina 1,315 Reputation points Microsoft Vendor
    2025-01-28T08:00:50.82+00:00

    Hi @Pothiraj, Saranya-ADM,

    We sincerely apologize for the delay in response and appreciate your patience.

    I see. It looks like the masking is not working as expected and is replacing the actual values with asterisks instead of hiding them.

    In that case, you can try using a custom policy to mask the values in the response. Here's an example policy that you can use:

    <outbound>
    <base />
    <choose>
    <when condition="@(context.Response.StatusCode == 200)">
    <set-status code="200" reason="ok" />
    <set-body template="liquid" >
    
                {
    
                    "username": "{{context.Variables["vault_response"]["data"]["data"]["username"] | mask}}",
    
                    "password": "{{context.Variables["vault_response"]["data"]["data"]["password"] | mask}}"
    
                }
    </set-body>
    </when>
    <otherwise>
    <set-status code="@(context.Response.StatusCode)" reason="Bad Request" />
    <set-body>@(((IResponse)context.Variables["response"]).Body.As<JObject>(preserveContent: true).ToString())</set-body>
    </otherwise>
    </choose>
    </outbound> 
    
    
    
    

    This policy uses the Liquid template language to mask the values of the "username" and "password" fields in the response. The | mask filter will replace the actual values with asterisks.

    Please note that this policy will only mask the values in the response and not in the pipeline script. You will still need to use the variable group feature to store the secrets and reference them in your pipeline script as I mentioned earlier.

    I hope this helps! Let me know if you have any further questions or need additional assistance.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.