Build Log / Entry 006 / Platform
Third-party access with zero open ports.
A third party needed to query a partner’s production database. We gave it an IAM role that opens one kind of session to one host, with no inbound ports.
The short version
- Give a third party an IAM role for SSM port forwarding instead of an IP allowlist or a VPN.
- Require an external ID and MFA on the third party's role, and limit it to one instance.
- Run the bastion with no inbound rules and no SSH, and check its live security group on every deploy.
- CloudTrail records who connected, not what they queried, so pair it with a read-only database account.
In this entry · 5 sections
A third-party company working with one of our partners needed to query the partner’s production Postgres directly, outside the application. The common ways to grant that are an allowlist entry for the third party’s IP addresses on the database, or a VPN into the environment. Both give an outside party network access, and both leave something listening that has to be maintained.
We wanted the third party to have exactly one capability: open a tunnel to one database, from its own AWS account, with every connection recorded. The partner is in the middle of a SOC 2 audit, so each part also had to leave evidence an auditor can check.
A role that can ask for one thing
The third party’s AWS account assumes a role in the partner’s. The trust policy requires an external ID, shared out of band, and multi-factor authentication on the third party’s side. The external ID is AWS’s standard guard against the confused-deputy problem, where a trusted account is tricked into using its access on someone else’s behalf.
The role’s own policy is narrow: it can start a port-forwarding session against one named instance, and it can end or resume only the sessions it started.
resource "aws_iam_role" "third_party_prod_bastion_access" {
name = "third-party-prod-bastion-access"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { AWS = "arn:aws:iam::${var.third_party_aws_account_id}:root" }
Action = "sts:AssumeRole"
Condition = {
StringEquals = { "sts:ExternalId" = var.third_party_external_id }
Bool = { "aws:MultiFactorAuthPresent" = "true" }
}
}]
})
}
resource "aws_iam_role_policy" "third_party_prod_bastion_session" {
name = "third-party-prod-bastion-session"
role = aws_iam_role.third_party_prod_bastion_access.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = "ssm:StartSession"
Resource = [
"arn:aws:ec2:${var.aws_region}:${data.aws_caller_identity.current.account_id}:instance/${module.bastion.instance_id}",
"arn:aws:ssm:${var.aws_region}::document/AWS-StartPortForwardingSessionToRemoteHost",
]
},
{
Effect = "Allow"
Action = ["ssm:TerminateSession", "ssm:ResumeSession"]
Resource = ["arn:aws:ssm:*:*:session/${aws:userid}-*"]
},
]
})
}aws ssm start-session --profile prod-bastion \
--target <bastion-instance-id> \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters host="<prod-db-endpoint>",portNumber="5432",localPortNumber="5432"
# In a second terminal, with the read-only database account:
psql -h localhost -p 5432 -U <readonly-user> -d <prod-database>No AWS credential of ours changes hands. The third party’s CLI profile assumes the role, prompts for its MFA code, and opens the tunnel; its database client then connects to localhost. AWS access only gets them to the port. What they can read once connected is set by a separate read-only Postgres account, provisioned and rotated on its own schedule.
Nothing to reach
The instance on the other end of the session is as small as the job allows: one t4g.nano whose only task is forwarding TCP. Its security group has no inbound rules at all. Outbound, it has exactly two: 5432 to the database’s security group, and 443 so the SSM Agent can register with Session Manager and stay reachable. We learned the second one was needed when the bastions never showed up in Session Manager without it. SSH is disabled on the instance as well, since nothing uses it.
Checking it stays that way
A Terraform apply proves the rules were right when it ran. It doesn’t catch a rule someone adds later in the AWS console. So the Ansible role that configures the bastion reads its security group straight from the AWS API on every deploy, rather than from Terraform state, and fails the deploy if the rules differ from what Terraform defines: the count, the ports, and where each rule points.
- name: Assert the bastion SG has zero ingress rules
ansible.builtin.assert:
that:
- bastion_sg.security_groups[0].ip_permissions | length == 0
- name: Assert the bastion SG has exactly the two expected egress rules
vars:
egress: "{{ bastion_sg.security_groups[0].ip_permissions_egress }}"
db_rule: "{{ egress | selectattr('from_port', 'equalto', 5432) | list }}"
https_rule: "{{ egress | selectattr('from_port', 'equalto', 443) | list }}"
ansible.builtin.assert:
that:
- egress | length == 2
- db_rule | length == 1
- (db_rule[0].user_id_group_pairs | map(attribute='group_id') | list) == [db_sg_id]
- https_rule | length == 1
- https_rule[0].to_port == 443
- (https_rule[0].ip_ranges | map(attribute='cidr_ip') | list) == ["0.0.0.0/0"]Checking where the 5432 rule points matters as much as counting rules. A rule that still allows 5432 but to a wider range would pass a count check and quietly widen what the bastion can reach.
What gets recorded
Session Manager can stream session activity to CloudWatch, and the partner has that turned on account-wide. It doesn’t apply here: AWS doesn’t log the contents of port-forwarding sessions, only shell sessions.
The record for this path is CloudTrail. Every StartSession call lands in the partner’s multi-region trail, with the assumed role session, the target instance, the session document and the time; the AssumeRole call just before it records the third-party account it came from. Log file validation is on, so the trail can show it hasn’t been altered. CloudTrail shows who connected and when; it doesn’t show the queries they ran, which is part of why the read-only database account matters.
Where it stands
The third party connects with one CLI profile and one command. The bastion has no inbound rules, every deploy checks that it still has none, and every session start is in CloudTrail. The SOC 2 audit is still in progress, so we can’t say yet how the auditor will treat this control, only that each claim above can be checked against a log or an API call.