DEV Community

Cover image for Day 99: One Link Short and a Header I Did Not Send
Nnamdi Felix Ibe
Nnamdi Felix Ibe

Posted on AI-assisted

Day 99: One Link Short and a Header I Did Not Send

Day 99 of DevOps, Day 49 of Azure. Both tasks gave me a reading that is correct and means less than it looks like it means.

AttachmentCount: 1 is true and does not mean an EC2 instance can use the role. A 409 is true and does not mean the service answers unauthenticated requests with 409.

One Terraform task, one Azure task. A DynamoDB table with a scoped IAM policy, then a VM serving a file it pulls from private blob storage. The tasks come from the KodeKloud Engineer platform.

Task 1: Attaching an IAM policy for DynamoDB access

A table, a role assumable by EC2, a read-only policy scoped to that table, and the policy attached to the role.

Step 1: The configuration

variable "KKE_TABLE_NAME"  { type = string }
variable "KKE_ROLE_NAME"   { type = string }
variable "KKE_POLICY_NAME" { type = string }

resource "aws_iam_policy" "readonly_policy" {
  name = var.KKE_POLICY_NAME
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = ["dynamodb:GetItem", "dynamodb:Scan", "dynamodb:Query"]
      Resource = aws_dynamodb_table.devops_table.arn
    }]
  })
}

resource "aws_iam_role_policy_attachment" "policy_attach" {
  role       = aws_iam_role.devops_role.name
  policy_arn = aws_iam_policy.readonly_policy.arn
}
Enter fullscreen mode Exit fullscreen mode

Two deliberate things in there. The variables have no defaults, so the plan fails until somebody supplies a value rather than quietly using one nobody chose. And the policy's Resource is the table's ARN attribute rather than a hand-written string, which is both correct and what creates the dependency edge that makes Terraform build the table first.

Step 2: Apply, and the field I was watching for

aws_iam_role.devops_role: Creation complete after 0s
aws_dynamodb_table.devops_table: Creation complete after 3s
aws_iam_policy.readonly_policy: Creation complete after 0s
aws_iam_role_policy_attachment.policy_attach: Creation complete after 0s
Enter fullscreen mode Exit fullscreen mode
"PolicyName": "devops-readonly-policy",
"AttachmentCount": 1,
Enter fullscreen mode Exit fullscreen mode

Two days ago I wrote a policy with AttachmentCount: 0 and noted that an unattached policy grants nobody anything. This is the same field saying the opposite, which felt like the loop closing.

It is not, quite.

Step 3: Working out what still cannot happen

The role's trust policy names ec2.amazonaws.com, so an EC2 instance is the intended principal. There is no aws_iam_instance_profile anywhere in the configuration.

AWS documents the relationship: "Amazon EC2 uses an instance profile as a container for an IAM role." On how the profile comes to exist: "When you create an IAM role using the IAM console, the console creates an instance profile automatically and gives it the same name as the role." And then: "If you use the AWS CLI, API, or an AWS SDK to create a role, you create the role and instance profile as separate actions, with potentially different names."

Terraform is the API path. aws_iam_role creates a role and nothing else. So no instance can be launched with this role attached, and the fix is two lines:

resource "aws_iam_instance_profile" "devops_profile" {
  name = "devops-profile"
  role = aws_iam_role.devops_role.name
}
Enter fullscreen mode Exit fullscreen mode

One caveat on the documentation: that page never uses the word "required". It says EC2 uses the profile as the container and that the console and API paths differ. The practical result is the same either way.

So this is the third day running where the thing I built is one link short of doing anything. The chain is policy, role, instance profile, instance. Two of the four exist. AttachmentCount: 1 is a correct reading of the first link and says nothing at all about the other two.

What the docs recommend

One thing worth putting beside Day 97, because the two days produced opposite-looking policies from the same person.

Day 97's policy had Resource = "*", which looked careless and was mandatory, because EC2's Describe actions do not support resource-level permissions. This one names a single table ARN, because DynamoDB's do. The difference is entirely which service was being granted.

Which means the question to ask of any Resource = "*" is not whether somebody was lazy. It is whether that action supports anything narrower, and the Service Authorization Reference answers it per action.

Two smaller things about the table. On-demand billing reports ReadCapacityUnits: 0 and WriteCapacityUnits: 0, which is not a misconfiguration: on-demand tables do not use provisioned throughput, so a check asserting capacity above zero would fail a healthy table. And the attribute block declaring only id is not a schema. DynamoDB needs attribute definitions only for attributes used in keys and indexes; everything else arrives with the item.


Task 2: A VM serving a file from private blob storage

A VM in a VNet, nginx, and an index.html fetched from a container that refuses anonymous access.

Step 1: The finding, which revises a table I have revised twice already

Two anonymous GETs of the same private blob, differing only by one header:

unversioned:              code=409   <Code>PublicAccessNotPermitted</Code>
x-ms-version: 2021-08-06  code=401   <Code>NoAuthenticationInformation</Code>
Enter fullscreen mode Exit fullscreen mode

Same account, same blob, same absence of credentials. 409 without a version header, 401 with one.

An anonymous request that omits x-ms-version is served by the earliest available API version, which for this account is from 2009. What I had not realised is that the old version does not only lack newer features. It expresses access denial in older semantics. allowBlobPublicAccess postdates 2009 by well over a decade, so the 2009 contract reports it as a 409 Conflict, and a modern version reports the same condition as a 401 with a www-authenticate header.

401 is the better answer. 409 was always a strange status for an unauthenticated request.

So the table I started building on Day 38 has now been revised three times:

  • Day 38: the status code tells you which layer blocked the request
  • Day 46: two unrelated causes share 409, so the error code identifies the layer
  • Day 49: the status itself depends on the API version, so neither is stable without pinning it

The durable version is narrower than any of the three. Read the error code, and send an explicit x-ms-version if you intend to compare status codes at all. A test whose result changes based on a header you did not send is measuring the default, not the service.

Step 2: The part where my test was wrong

This nearly went into the notes as a finding, and it was nonsense.

curl -s -o /dev/null -w "anon blob read: %{http_code}\n" "$URL"
curl -s -H "x-ms-version: 2021-08-06" "$URL" | head -c 300
Enter fullscreen mode Exit fullscreen mode

Two separate requests. The status came from the first and the body from the second, so my output read as "409 NoAuthenticationInformation", a pairing that never existed.

What caught it was incoherence, not diligence. "Server failed to authenticate the request, refer to the www-authenticate header" is the text of a 401. Seeing it attached to a 409 did not make sense, and that mismatch is what made me re-run both requests with the body written to a file so each status sat with its own body.

Two habits out of that. Capture the status and the body from the same request. And treat a status and a message that disagree as a bug in the measurement, not a curiosity about the service, because the service is internally consistent far more often than a hand-written test is.

The near-miss is the real point. I have produced two 409 findings before this one and written both into notes. This is the first time the measurement itself was wrong, and the trigger for checking was that the result was interesting. That is probably the rule: a finding that is interesting is exactly when to re-check how it was obtained.

What the docs recommend

Two things that cost nothing and caught something each.

The baseline. I listed the web root before the download, so nginx's own page was on record first. A single passing curl at the end would be satisfied by nginx serving anything, including its default page if the download had silently failed, and both files end up in the same directory.

And reading the Nginx directives instead of assuming an edit was needed. The task says "ensure Nginx is configured to serve the file from /var/www/html/index.html", which sounds like a config change. Ubuntu's default site already has root /var/www/html; and lists index.html ahead of index.nginx-debian.html, so dropping the file in place was the whole configuration step. Had the order been reversed, the file would have been in the right place and invisible, which is exactly the false pass the baseline was guarding against.

The last one is about the key. The task's own example puts --account-key xxxxx in the command line, where it is visible in ps to every user on the box, and the same task says to follow security best practices. Those pull in opposite directions.

export AZURE_STORAGE_ACCOUNT=$SA
export AZURE_STORAGE_KEY=$(echo "$SAKEY_B64" | base64 -d)
az storage blob download --container-name "$CONTAINER" --name index.html --file /tmp/index.html
sudo install -o root -g root -m 0644 /tmp/index.html /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

az storage reads both variables natively, so the credential never reaches an argument list. Splitting the download from the placement also avoids sudo -E, because sudo discards the environment under Ubuntu's default env_reset, and install sets ownership and mode in one step where mv would carry my ownership across.

What I learned

AttachmentCount: 1 says the policy reaches a role. It says nothing about whether anything reaches the role.

EC2 uses an instance profile as the container for a role, and the API path that Terraform uses creates the role and the profile separately.

Whether Resource = "*" is lazy or mandatory depends on the service, not the author.

On-demand DynamoDB reports zero provisioned capacity, and a check asserting otherwise fails a healthy table.

An anonymous Azure Storage request with no x-ms-version is answered by a 2009 API that changes the status code of a denial, not just the features available.

Capture the status and the body from the same request, and treat a disagreement between them as a bug in your test.

Establish the baseline before the change.

So here is the Day 99 question. When something you verified turns out to be interesting, do you re-check how you measured it? I did, once, by accident, because the answer did not make sense. The two times before that, I just wrote it down.

Day 99 down. One to go.

Top comments (3)

Collapse
 
aisovereign profile image
Ananya Soni •

Yes. I always check like that

Collapse
 
ndcodes profile image
Nnamdi Felix Ibe •

Great to hear.

Honestly, it's like dopamine for me. Every time something strange shows up, I get overly excited to dig in.

Which is the problem, too. The result I'm excited about is the one I want to be true, so that's exactly when I should be checking how I measured it.

Collapse
 
aisovereign profile image
Ananya Soni •

I think it is universal for any developer. In my case it is post training of lumina it is taking too much time then expected. And I am just like why. On the completely different note. It is very wild to think about that most of the founders and CEO after becoming that. They stop writing code by themselves. But whenever I get time I still right code sometimes I am not saying every but sometimes. Like no one said before starting a company that you have to do another work not just a i and coding