-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KeyVault access policy for Service Principal doesn't work as expected and doesn't provide access to secrets #1569
Comments
@gvilarino I have encountered this problem as well, the ICON displays the created account as a User (image 2) instead of an Application Icon (image 1) . I believe that is somehow related to the problem, possibly the wrong API function being used. |
Precisely. If one adds the access policy manually via the Azure Portal UI or with the azure-cli command I mention, it does appear correctly with the Application account/icon. @whytoe did you find any workaround other than the one I mention above? |
@gvilarino I have not invested too much time into it, just experienced the exact same issue, I believe. Possibly if you add the Object ID and the AppID it might work?
|
I've only run into this with an incorrect You can get the correct id via |
@whytoe i have added application_id, tenant_id and object_id still not able to access through SPN. i see same behavior with keyvault like @gvilarino... If it's Group SPN, there is no issue by adding to keyvault through terraform module, only issue if it's application SPN. |
Ok, some progress, I have been able to successfully set a working access policy: when you
These both will have the same I was using the In any case, I have been able to create a vault with the proper SP configuration. I still think the docs are not on-point, because they do not tell you how to get the proper Object ID for the terraform resource. So the confusion comes from Azure not having a clear visual representation of the Service Princial figure, rather having two "application"-related entities: the Enterprise Application and the Application Registration. However, I still can't complete my terraform project since even tho I'm able to get the resource created properly, I'm unable to access the SP as a And even if I do set the SP as an subscription contributor, and set said permissions to the App registration, I still cant access the data source and get:
Any ideas? |
@gvilarino apologies if I missed it, but did you give the SP you're using with terraform the proper Azure AD permissions (see the note at the top of https://www.terraform.io/docs/providers/azurerm/d/azuread_service_principal.html)? IIRC, adding those permissions requires you to be an admin, but I don't recall if it's of the subscription, the Azure AD instance, or both. |
Hi @phekmat. Yes I did set those on the App (not the spn as I didn't find a way of doing that). I also added the spn as subscription Contributor, and I am Subscription owner and Global Admin. Following the docs I can't seem to find any more directions as to what I'm missing |
@gvilarino this is way more complicated than it needs to be, I hit into this yesterday and spent way too long on it as well The permissions @phekmat mentioned is to the Service Principal that you are accessing Azure API via the Terraform azurerm provider as mentioned on the Data Source: azurerm_azuread_service_principal:-
when you have given those permissions you also need to Grant permissions, looks like the following via the portal:- Word of warning as I am now hitting an issue with the data source as it cannot always find the Service Principal ID for an Application Registration even though you can see it with Issue raised here #1844 |
@steve-hawkins wow thanks for the heads-up. This whole Azure IAM is so... obscure... It really makes me feel like I know nothing about software engineering :( |
Hi, I got the same issue with Terraform v0.11.8 and azure provider 1.14.0. I created my Keyvault & secret using Terraform and a SPN. When I want, later, to get a secret using datasource and the same SPN, I got a 403 error. Problem is a bad creation of access policy with a SPN |
@steve-hawkins Long time no see! Two policies will apear (with the same name), one for the SP and one for the App Registration. Once set all my 403s magically went away and I could access/create secrets |
I'll leave my 2 cents here for anyone that might care to be in the same position as me. I have 2 different TF templates; one to build the keyvault with perms, another to deploy a resource and add a secret to the keyvault at the same time. I had pre-setup a service principal to access/deploy to the subscription which had been given contributor rights. I used that same principal whilst deploying my TF templates set in the environment variables of bash. I added the object_id of the service principal to the TF templates by grabbing it from App Registrations in Active Directory (taking the object ID, not the app ID). No matter what I did, I received the same 403 error as everyone has described here. I found the issue to be using the wrong object ID of the service principal. The working ID I grabbed from I also noticed that using the correct object ID shows the correct BLUE application icon for the spn in Access Policies (under the keyvault), not the GREY human icon (as mentioned above). |
@paulmackinnon-adv365 i think by providing object id from (Enterprise Applications -> All Applications -> my-spn -> Properties -> Object ID), it works fine. (Application ID not needed) initially i was taking object id from (Azure AD ---> App Registrations --> my-spn --> Object ID & Application ID), it was not working. SPN is same but Object id is different when we get from Enterprise Applications. @gvilarino i would suggest you to follow same procedure hopefully it fixes your issue as well. |
@vijayakrishnarg1 This works, 100% for me
This creates an application instead of creating a user which the "Application" Object ID creates |
I ran into this problem too, and was able to resolve it by using a data source to grab the right ID.
And then in the keyvault:
|
I had this same issue. Using the data source approach as mentioned by @bpoland worked, but only using the |
Check this one out. It works for me. |
…olicy for it to work For details, please see hashicorp/terraform-provider-azurerm#1569
👋 Taking a look through here since this appears to have been fixed by updating the Terraform Configuration from using the Application ID to using the Object ID - as such I'm going to close this issue for the moment, but if your still seeing this please let us know and we'll take another look. Thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
Terraform Version
Affected Resource(s)
Terraform Configuration Files
Debug Output
https://gist.github.com/gvilarino/2888944a0c62a41791f8ff3dfbacfc17
Expected Behavior
There should be a keyvault named as in the example, with an access policy for the given Service Principal, that allows me to set and get secrets, and that looks like this in the portal:
This is how the commands should work, and what is expected by following the Service Principal creation guide in the official Terraform docs:
Actual Behavior
The KeyVault is created, but the access policy looks like this:
It does have the
Get
andSet
secret permissions, but is still unable to access any secrets in the KeyVault:Steps to Reproduce
Important Factoids
Factoid 1
If you add the
application_id
parameter to theaccess_policy
block in theazurerm_key_vault
resource, the access policy looks different in the portal:But it still doesn't work.
Factoid 2
If I use an
azurerm_key_vault_access_policy
resource instead of theaccess_policy
block inside theazurerm_key_vault
resource, the result is exactly the same.Factoid 3 - Workaround
I managed to work around this through trial and error by removing all access policy definitions and instead using a
local-exec
provisioner. I added the following to myazurerm_key_vault
resource":HOWEVER, for this to work, the azure-cli must be logged in (I tried as the Service Principal, should work for personal account as well) as the
ARM_*
env vars or specifying parameters to theazurearm
Terraform provider (in the file) makes theaz keyvault seet-policy
fail for invalid auth.The debug output for this can be seen in this gist: https://gist.github.com/gvilarino/aa6d3a1b2bd217c1acfa606c3365dc71
References
I did not find an issue that describes exactly this problem, this way.
The text was updated successfully, but these errors were encountered: