Azure DevOps Build Agent not able to access Azure Storage Account during deployment
az feedbackauto-generates most of the information requested below, as of CLI version 2.0.62
Describe the bug I have a static website that I wish to have CD pipeline for it, the pipeline has one task which is Azure CLI to copy files to storage account, I'm using a service connection that has a service principal that has access to the resource. I keep getting the following error You do not have the required permissions needed to perform this operation. Depending on your operation, you may need to be assigned one of the following roles: "Storage Blob Data Contributor" "Storage Blob Data Reader" "Storage Queue Data Contributor" "Storage Queue Data Reader" In the script I'm adding the IP of the build agent to the list of allowed IP on the firewall for the storage account so it would have access then I can issue azure cli commands to list or upload content. This behavior used to work up to yesterday and out of sudden it stopped working, I tried to reproduce on Azure Shell itself and it is working fine and also I tried locally on my work machine and the same steps work fine. To Reproduce Steps to reproduce the behavior.
Expected behavior A successful upload to Azure Storage account container.
Environment summary Azure CLI Version: 2.0.74 Azure CLI Task version: 1.0
Additional context Add any other context about the problem here.
Thanks for your feedback. I will have a look at it and try to fix it in next sprint. So you only have problems in Azure DevOps and it's fine for local machine and Azure Cloud Shell, right?
@Juliehzl that is correct, it works fine on both local machine and Azure Cloud Shell.
@Juliehzl any news on this? I've also added the service principal as a Contributor on the subscription but still the same error happens.
Hi @micheleissa , through your log information, I found that you have set network rules for your storage account and default action is "Deny" as follows:
2019-10-29T18:36:44.3801455Z "networkRuleSet": {
2019-10-29T18:36:44.3801570Z "bypass": "AzureServices",
2019-10-29T18:36:44.3801639Z "defaultAction": "Deny",
2019-10-29T18:36:44.3801753Z "ipRules": [
2019-10-29T18:36:44.3801860Z {
2019-10-29T18:36:44.3801926Z "action": "Allow",
2019-10-29T18:36:44.3802044Z "ipAddressOrRange": "47.187.19.110"
2019-10-29T18:36:44.3802145Z },
2019-10-29T18:36:44.3802254Z {
2019-10-29T18:36:44.3802317Z "action": "Allow",
2019-10-29T18:36:44.3802616Z "ipAddressOrRange": "76.228.200.171"
2019-10-29T18:36:44.3802734Z },
2019-10-29T18:36:44.3802798Z {
2019-10-29T18:36:44.3802890Z "action": "Allow",
2019-10-29T18:36:44.3803071Z "ipAddressOrRange": "139.180.25.115"
2019-10-29T18:36:44.3803159Z },
2019-10-29T18:36:44.3803218Z {
2019-10-29T18:36:44.3803444Z "action": "Allow",
2019-10-29T18:36:44.3803514Z "ipAddressOrRange": "104.41.132.253"
2019-10-29T18:36:44.3803622Z },
2019-10-29T18:36:44.3803679Z {
2019-10-29T18:36:44.3803896Z "action": "Allow",
2019-10-29T18:36:44.3803966Z "ipAddressOrRange": "63.96.91.120"
2019-10-29T18:36:44.3804082Z },
2019-10-29T18:36:44.3804141Z {
2019-10-29T18:36:44.3804883Z "action": "Allow",
2019-10-29T18:36:44.3804972Z "ipAddressOrRange": "13.66.0.100"
2019-10-29T18:36:44.3805091Z },
2019-10-29T18:36:44.3805199Z {
2019-10-29T18:36:44.3805260Z "action": "Allow",
2019-10-29T18:36:44.3805391Z "ipAddressOrRange": "40.71.169.68"
2019-10-29T18:36:44.3805470Z }
When you access blob in ADO, your ip may be not in the rule set. Can you confirm if you have allowed ADO ip is allowed for your storage account?
As I stated above, I manually add the IP of the build agent on the fly during the deployment and list all the allowed IPs and the agent is one of them.
According to your error code, it should be caused by Firewall. You can check it by setting default action of your storage account to Allow, and it should works well. But why not set default action to Allow? Do you have any concern? If you want to config network rules, what you need to do is to config IP address correctly. Could you please confirm ip address is right and satisfy the rules? By the way, how do you find build agent ip in ADO?
The IP address is the public IP given to the build agent machine and I obtain it from an api call to this so it is right and satisfy the rules. The weird part is before the problem started to happen, the original approach of adding IP rule to firewall was working fine. I managed to get it to work by using default action allow then deny again after uploading the static content of the website. To find the ip in ADO is like that:
# obtain build agent IP to be used later
Build_Agent_IP=$(curl -k https://ipinfo.io/ip)
echo 'Build Agent IP is: ' $Build_Agent_IP
@micheleissa Thanks for you information. At least we know it is caused by network rules, not CLI issue. In ADO, typically build agent is assigned randomly. But I think what you need to do is to find the correct IP address for you build agent and add it to allow for you storage account. You can find ADO guys for help. I will close it now because it is not a CLI issue and if you still have problems when using CLI, feel free to create a new one.😊
@Juliehzl How is that not a ADO CLI issue?? And I did find the correct IP Address so I don't know what are you talking about.
@micheleissa were you able to solve the issue ? I am facing the same issue.
@nidhyn also experiencing the same issue - the only thing that allows it to work is setting 'allow public networks' which is the same as setting default rule of allow all - not something we wish to allow.
I am also having the same issue, it's intermittent and seems to be an issue with adding the agent ip. @c-mccutcheon @nidhyn by any chance did either of you manage to get round this issue?
@c-mccutcheon @cjmcewan no luck so far.
@c-mccutcheon @cjmcewan If you are still like to hear what the issue is, What I got from Microsoft is that the system is whitelisting the private IP of the Azure Vm instead of the public IP. And it is not always that case, 2/10 attempts gets successful. But that's a shame. We resolved the issue with a custom agent in the same vnet.
The IP address is the public IP given to the build agent machine and I obtain it from an api call to this so it is right and satisfy the rules. The weird part is before the problem started to happen, the original approach of adding IP rule to firewall was working fine. I managed to get it to work by using default action allow then deny again after uploading the static content of the website. To find the ip in ADO is like that:
# obtain build agent IP to be used later Build_Agent_IP=$(curl -k https://ipinfo.io/ip) echo 'Build Agent IP is: ' $Build_Agent_IP
I am doing similar commands in my Azure Pipeline, however there seem to be a delay from whitelisting the IP of the Microsoft hosted agent to it being allowed to write/read from the Storage account. I have, for a temporary solution, added a sleep command in there for 4-5 minutes but is seems to be random if the network rules has been applied. Does anyone know if it is possible to return an exit code 2 instead of the standard 'the request may be blocked by network rules of storage account', which is only a warning meaning it will not fail in the Azure Pipeline.
For now I have tried the following (assumes logged into subscription):
export CURRENT_IP=$(curl -s http://ipinfo.io/json | jq -r '.ip')
echo "Whitelisting $CURRENT_IP"
az storage account network-rule add \
--resource-group [resource-group-name] \
--account-name [storage-account-name] \
--ip-address $CURRENT_IP
az storage account show -n [storage-account-name] --query networkRuleSet --resource-group [resource-group-name] > network_rules.json
IP_MATCH=$(cat network_rules.json | jq --arg CURRENT_IP "$CURRENT_IP" '.ipRules[] | select(.ipAddressOrRange | contains($CURRENT_IP))')
if [ -z "$IP_MATCH" ]
then
echo "IP $CURRENT_IP was not whitelisted. Try again."
exit 2
else
# Upload file or something
I am doing something similar and it's still quite problematic, especially with Terraform, that works a bit differently than the CLI. There are API delays and e.g. the firewall rules can take up to 2 minutes to even appear in the target resource...
Set to public - no problems, set with firewall exceptions? Some magical stuff happens from time to time. It's unreliable. The same routine can work 4 times in a row, to throw API error on the next attempt.
Also, "According to your error code, it should be caused by Firewall. You can check it by setting default action of your storage account to Allow, and it should works well. But why not set default action to Allow? Do you have any concern?"
Why would they even try allowing an IP if they wished for their default rule to be on "allow"?
Is this really not possible to include DevOps agents as Microsoft Service bypass in the Resource settings? It's only quite rational to categorize Microsoft Agents as Microsoft Services, is it not? The workaround is not very complicated, but I can see some use-cases, where this potentially becomes an obstacle.
PS: Your documentation on assigning the agent's IP states that the public IPs belong under the (the ServiceTags file), AzureCloud, which can contain even 300 different IPs. Assigning those IPs manually is not a solution at all.
I can't see why exactly it's not supported to even retrieve the IP of the agent through some kind of a pre-defined variable? There seems to be no other way than to write custom scripts for this purpose, and they are a workaround using the API, which brings additional delay to the pipeline and even makes them waste time needlessly.
PS2: In case anybody is wondering why exactly a Storage Account with explicitly allowed public IP might WORK FINE 4 times in a row, then throw an API error, here is a random answer from a random place
- Services deployed in the same region as the storage account use private Azure IP addresses for communication.
- IP network rules have no effect on requests originating from the same Azure region as the storage account
I would be happy to find this in the documentation instead of wasting 4-5 hours manually testing why exactly it's broken. Please, tell me, what is the point of putting that information in: https://learn.microsoft.com/en-us/azure/storage/common/storage-network-security?tabs=azure-portal
When at the same time it isn't present in: https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml
So basically there is a guide on how to find the IPs of managed agents (which is by the way, pretty much of no use, given the restrictions of the firewall settings), but it doesn't mention that in fact - even if bypassed, it will not work. Great.
I am doing something similar and it's still quite problematic, especially with Terraform, that works a bit differently than the CLI. There are API delays and e.g. the firewall rules can take up to 2 minutes to even appear in the target resource...
Set to public - no problems, set with firewall exceptions? Some magical stuff happens from time to time. It's unreliable. The same routine can work 4 times in a row, to throw API error on the next attempt.
Also, "According to your error code, it should be caused by Firewall. You can check it by setting default action of your storage account to Allow, and it should works well. But why not set default action to Allow? Do you have any concern?"
Why would they even try allowing an IP if they wished for their default rule to be on "allow"?
Is this really not possible to include DevOps agents as Microsoft Service bypass in the Resource settings? It's only quite rational to categorize Microsoft Agents as Microsoft Services, is it not? The workaround is not very complicated, but I can see some use-cases, where this potentially becomes an obstacle.
PS: Your documentation on assigning the agent's IP states that the public IPs belong under the (the ServiceTags file), AzureCloud, which can contain even 300 different IPs. Assigning those IPs manually is not a solution at all.
I can't see why exactly it's not supported to even retrieve the IP of the agent through some kind of a pre-defined variable? There seems to be no other way than to write custom scripts for this purpose, and they are a workaround using the API, which brings additional delay to the pipeline and even makes them waste time needlessly.
PS2: In case anybody is wondering why exactly a Storage Account with explicitly allowed public IP might WORK FINE 4 times in a row, then throw an API error, here is a random answer from a random place
- Services deployed in the same region as the storage account use private Azure IP addresses for communication. - IP network rules have no effect on requests originating from the same Azure region as the storage accountI would be happy to find this in the documentation instead of wasting 4-5 hours manually testing why exactly it's broken. Please, tell me, what is the point of putting that information in: https://learn.microsoft.com/en-us/azure/storage/common/storage-network-security?tabs=azure-portal
When at the same time it isn't present in: https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml
So basically there is a guide on how to find the IPs of managed agents (which is by the way, pretty much of no use, given the restrictions of the firewall settings), but it doesn't mention that in fact - even if bypassed, it will not work. Great.
I share the same frustration when it comes to the build agent and whitelist rules, specifically in the context of QA and end-to-end automation.