2

We are using Azure API Management and Powershell to import the configuration on the portal with a script, however whenever the API is imported the "Subscription required" check from the portal is enabled (The API required Subscription Key). We are not using the subscription feature by now on the API so we need to disable this when imported. We are using Import-AzApiManagementApi and Set-AzApiManagementApi with the following code:

Set-AzApiManagementApi -ApiId $apiId -Context $context -Protocols @('https') -ServiceUrl $serviceBase$path -Name $api.Name
Set-AzApiManagementPolicy -Context $context -ApiId $apiId -PolicyFilePath "$pwd/src/private/security_policy.xml"

We haven't found in the documentation the way to import the API without this check. Is there any script to disable this feature via powershell?

jaxkodex
  • 484
  • 2
  • 6
  • 17
  • 1
    Potential duplicate of this Q&A --- https://stackoverflow.com/questions/51376248/azure-api-management-is-it-possible-to-disable-subscription-key – postanote May 11 '19 at 00:55

3 Answers3

1

Quick addition to this. The following Powershell command will set an API's Subscription Required property to false. A word of warning though: this currently does not work with Azure DevOps pipelines. The pipeline will claim the task ran successfully, and if you run an echo of $api in the pipeline it will show that the api has SubscriptionRequired : False. However, if you go to check the api in Azure, it will still be set to True (the default behavior).

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -Name "Name of Api"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
  • 1
    Almost there but to persist that false value you need to use Set-AzApiManagementApi as per https://github.com/Azure/azure-powershell/issues/9350#issuecomment-502833214 (and also as per Andrew Tregonning's solution here) – Martin D Feb 23 '22 at 01:51
1

I was unable to change SubscriptionRequired to false on an imported API using Set-AzApiManagementApi as suggested by https://stackoverflow.com/a/56095090/240586. As SubscriptionRequired is a switch parameter I couldn't specify it as false.

A variation of https://stackoverflow.com/a/66675277/240586 worked, however. @RomericRobo's answer is missing the final Set-AzApiManagementApi command to save the change (sorry, I'm unable to edit or comment on his answer). My complete working solution is:

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -ApiId "api-id"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
Set-AzApiManagementApi -InputObject $api
0

You can either use the Arm module command Set-AzureRmApiManagementProduct or the Az module command Set-AzApiManagementProduct to disable the required subscription option

Check the below documents

https://github.com/Azure/azure-powershell/blob/master/src/ApiManagement/ApiManagement/help/Set-AzApiManagementProduct.md

https://learn.microsoft.com/en-us/powershell/module/azurerm.apimanagement/Set-AzureRmApiManagementProduct?view=azurermps-6.13.0

Update

I can see that the set-azapimanagementapi supports the SubscriptionRequired parameter now

https://learn.microsoft.com/en-us/powershell/module/az.apimanagement/set-azapimanagementapi?view=azps-2.4.0#description

Set-AzApiManagementApi
   -Context <PsApiManagementContext>
   -ApiId <String>
   [-Name <String>]
   [-Description <String>]
   [-ServiceUrl <String>]
   [-Path <String>]
   [-Protocols <PsApiManagementSchema[]>]
   [-AuthorizationServerId <String>]
   [-AuthorizationScope <String>]
   [-OpenIdProviderId <String>]
   [-BearerTokenSendingMethod <String[]>]
   [-SubscriptionKeyHeaderName <String>]
   [-SubscriptionKeyQueryParamName <String>]
   [-SubscriptionRequired]
   [-PassThru]
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]
Mo Haidar
  • 3,748
  • 6
  • 37
  • 76
  • I am not currently using a subscription, you mean I should create a suscription with no key requirement and asign it to the api's i am importing? – jaxkodex May 12 '19 at 01:22
  • I did test it, removed the configuration from the product and later imported an api with this product, and the check still was on true by default – jaxkodex May 12 '19 at 01:31
  • You need to set the subscription requires property to false explicitly using the command mentioned in the answer. – Mo Haidar May 12 '19 at 01:43
  • You are right, ended up doing this, however feels like a hack since the check on the API is still true. The command to add the product is documented [here](https://learn.microsoft.com/en-us/powershell/module/az.apimanagement/add-azapimanagementapitoproduct?view=azps-2.0.0) – jaxkodex May 12 '19 at 18:34
  • I was thinking you already have it attached to a product, sorry for the confusion. You are right, actually I do not find a way to create api with disabled required subscription option, hopefully they will add it in the future. Below is a full lit of AzApiManagement commands https://learn.microsoft.com/en-us/powershell/module/az.apimanagement/?view=azps-2.0.0 – Mo Haidar May 12 '19 at 23:20