#!/bin/sh
# aws wrapper for the ambient AWS path (aws-broker.ts).
#
# Defaults AWS_PROFILE to `self` for THIS CHILD ONLY and execs the real CLI.
# The profile lives in the generated /opt/sky/aws/config, where its
# `credential_process` fetches short-lived credentials for an assumed
# READ-ONLY role from the loopback broker. `aws --profile <label>` reaches
# any other account the instance has been given a role in.
#
# WHY A WRAPPER, and not `ENV AWS_PROFILE=self` in the Dockerfile: the agent
# process shares its environment with every subprocess, and its OWN AWS SDK
# clients (DynamoDB, S3, Secrets Manager, SQS) resolve credentials from that
# same environment. A global AWS_PROFILE would route those WRITES through a
# read-only role — and route the broker's own AssumeRole call back into the
# broker. AWS_CONFIG_FILE is exported globally instead, which is safe because
# the generated file declares no `[default]` profile: the SDK's ini step
# finds nothing there and falls through to the execution role, as before.
#
# An explicit AWS_PROFILE in the environment wins (never overridden), so
# `AWS_PROFILE=client-acme aws s3 ls` works too.
#
# This instance has no read-only role at all: the `aws-readonly` capability is off,
# or no role is configured. Say so once, plainly, instead of letting every
# command fail as an obscure credential error.
if [ "${FOUNDATION_AWS_DISABLED:-}" = "1" ]; then
  echo "AWS is not connected on this instance." >&2
  exit 1
fi

# Overridable ONLY so the wrapper can be tested against a stub CLI;
# production always uses the image's real binary.
AWS_REAL="${FOUNDATION_AWS_REAL:-/opt/sky/bin/aws-real}"

AWS_PROFILE="${AWS_PROFILE:-self}" \
  AWS_CONFIG_FILE="${AWS_CONFIG_FILE:-/opt/sky/aws/config}" \
  exec "$AWS_REAL" "$@"
