azure-sdk-for-java icon indicating copy to clipboard operation
azure-sdk-for-java copied to clipboard

[BUG] Not able to update tag with tag contributors role

Open johnakash opened this issue 6 years ago • 29 comments

Describe the bug We are trying to update / add tag to resource group using a service principal. While using PowerShell it is working as expected. But when we are trying to update tag via JAVA sdk we are getting following error

com.microsoft.azure.CloudException: Status code 403, {"error":{"code":"AuthorizationFailed","message":"The client '4c6edf90-19ac-6543-9aa5-0bb9994565bb' with object id '4c6edf90-19ac-6543-9aa5-0bb9994565bb' does not have authorization to perform action 'Microsoft.Resources/subscriptions/resourcegroups/write' over scope '/subscriptions/20dbcfe8-abcd-4c2d-C002-0927a4b7fffd/resourcegroups/cloud-shell-storage-southcentralus' or the scope is invalid. If access was recently granted, please refresh your credentials."}}: The client '4c6edf90-19ac-6543-9aa5-0bb9994565bb' with object id '4c6edf90-19ac-6543-9aa5-0bb9994565bb' does not have authorization to perform action 'Microsoft.Resources/subscriptions/resourcegroups/write' over scope '/subscriptions/20dbcfe8-abcd-4c2d-C002-0927a4b7fffd/resourcegroups/cloud-shell' or the scope is invalid. If access was recently granted, please refresh your credentials.

To Reproduce

  • Create a service principal
  • Attach tag contributor
  • Run java code to update resource group

Code Snippet image

Setup (please complete the following information):

  • OS: RHEL
  • IDE : Eclipse

The same setup will allow us to update the tag if we are using PowerShell but it won't work with Azure CLI / Java SDK. could you please help us to fix it?

We cannot grant a service principal with permission 'Microsoft.Resources/subscriptions/resourcegroups/write' as it will gain the capability to update any RGs without any restrtction.

johnakash avatar Jun 18 '20 22:06 johnakash

Hi @johnakash

Could you try if this would work?

            azure.genericResources().manager().inner().resourceGroups()
                    .update(resourceGroupName, new ResourceGroupPatchable().withTags(Collections.singletonMap(tagKey, tagValue)));

Please note it is an overwrite of existing tags.

weidongxu-microsoft avatar Jun 19 '20 02:06 weidongxu-microsoft

@weidongxu-microsoft I work with Akash. The above API will update existing tags??

santhoshigorle avatar Jun 19 '20 05:06 santhoshigorle

Lets assume Resource Group has environment tag and if I try to set application tag using code you mentioned . will it replace existing application tag??

santhoshigorle avatar Jun 19 '20 05:06 santhoshigorle

Hi @santhoshigorle

I am not aware there is a distinguish between environment tag and application tag.

If you would like to add new tag to existing tags, you would need to first get the existing tags, then add new one, finally do the update.

            ResourceGroup resourceGroup = azure.resourceGroups().getByName(RG_NAME);
            Map<String, String> tags = new HashMap<>(resourceGroup.tags());
            tags.put(newTagKey, newTagValue);

            azure.genericResources().manager().inner().resourceGroups()
                    .update(RG_NAME, new ResourceGroupPatchable().withTags(tags));

weidongxu-microsoft avatar Jun 19 '20 06:06 weidongxu-microsoft

My guess on why rg.update().withTag().apply() fails on write access is that it calls a PUT call.

Above code azure.genericResources().manager().inner().resourceGroups().update(...) would use a PATCH call.

weidongxu-microsoft avatar Jun 19 '20 06:06 weidongxu-microsoft

@weidongxu-microsoft - We will give a try and get back to you.

johnakash avatar Jun 19 '20 13:06 johnakash

@weidongxu-microsoft - Hi, I am Akash's team member. We tried with the code snippet u gave. But still getting same error.

com.microsoft.azure.CloudException: Status code 403, {"error":{"code":"AuthorizationFailed","message":"The client '4c6edf90-kojh-4f25-9aa5-0997ll565bb' with object id '4b6eae90-19ab-4f25-9aa5-0bb7043565bd' does not have authorization to perform action 'Microsoft.Resources/subscriptions/resourcegroups/write' over scope or the scope is invalid. If access was recently granted, please refresh your credentials."}}

Could you please check this.

NShenoy5 avatar Jun 24 '20 15:06 NShenoy5

@NShenoy5

Is the PowerShell command Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge?

weidongxu-microsoft avatar Jun 24 '20 15:06 weidongxu-microsoft

@weidongxu-microsoft - We are using java sdk for doing this operation.

johnakash avatar Jun 24 '20 15:06 johnakash

@johnakash

I am trying to mirror the API that is used in PowerShell command that you mentioned working.

If this is the command, PowerShell is using this API, which was added at api-version 2019-10-01 https://docs.microsoft.com/en-us/rest/api/resources/tags/updateatscope

However current Java SDK api-version is 2019-08-01, so it cannot provide this API without feature upgrade.

All above assuming that Update-AzTag is the PowerShell API.

weidongxu-microsoft avatar Jun 24 '20 15:06 weidongxu-microsoft

Thanks @weidongxu-microsoft , both the New-AzTag and the Update-AzTag command-lets are working as expected through an account that only has the "Tag Contributor" role assigned to it.

c4rlosmarin avatar Jun 24 '20 17:06 c4rlosmarin

@c4rlosmarin

Thanks for the info on role and the confirmation on PowerShell.

https://docs.microsoft.com/en-us/rest/api/resources/tags/updateatscope would be a new API to support in Java SDK. We will schedule for it. But current SDK is not able to do that.

Let me know how urgent you need this feature, and we could plan accordingly.

weidongxu-microsoft avatar Jun 25 '20 00:06 weidongxu-microsoft

@weidongxu-microsoft

Tagging in Cloud helps in many ways like, managing cost distribution and to keep Cloud Compliant. Tagging plays very crucial role in my organization. could you please prioritize this??

santhoshigorle avatar Jun 28 '20 05:06 santhoshigorle

Thanks @santhoshigorle

@weidongxu-microsoft , how soon can we expect this to be added to the Java SDK?

c4rlosmarin avatar Jun 30 '20 16:06 c4rlosmarin

@johnakash @santhoshigorle @c4rlosmarin Hi guys, our development focus has shifted to the next version of Java SDK management libraries The guide about this new version of SDK can be found here:

https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/management

It has been released and is in public preview right now.

Would it be okay for you guys to add this feature to the newer version of SDK?

nickzhums avatar Jul 01 '20 06:07 nickzhums

@nickzhums You want us try this SDK version?? com.azure.resourcemanager azure-resourcemanager 2.0.0-beta.2

santhoshigorle avatar Jul 01 '20 23:07 santhoshigorle

@santhoshigorle yes that would be the next generation of Azure Java Management SDK

nickzhums avatar Jul 02 '20 02:07 nickzhums

2.0.0-beta.2 does not yet had the tag feature.

In long run, major feature upgrade would be focused on this line of releases.

weidongxu-microsoft avatar Jul 02 '20 02:07 weidongxu-microsoft

Thanks @weidongxu-microsoft , is there an ETA you could share for when the feature would be publicly available?

c4rlosmarin avatar Jul 02 '20 18:07 c4rlosmarin

@c4rlosmarin would you be okay with using a preview version in your environment? it will contain the new features but might also introduce some breaking changes or minor issues. If you are okay with this, we can happy to put this request to our backlog and give you an ETA

nickzhums avatar Jul 07 '20 02:07 nickzhums