cloudyr / aws.s3

Amazon Simple Storage Service (S3) API Client
https://cloud.r-project.org/package=aws.s3
381 stars 147 forks source link

Unable to access bucket (HTTP 403) when using IAM Roles attached to Service Accounts on EKS #430

Open NicholasFiorentini opened 1 year ago

NicholasFiorentini commented 1 year ago

Please specify whether your issue is about:

I'm running my code from rshiny server. The session token is provided using https://docs.aws.amazon.com/eks/latest/userguide/pod-configuration.html.

## Install package
install.packages(c(
    "aws.ec2metadata",
    "aws.signature", 
    "aws.s3",
  ),
  repos=c(cloudyr = "http://cloudyr.github.io/drat", getOption("repos"))
)

# tried this:
# install.packages("aws.s3", repos="https://rforge.net")
# but the packages is not loaded

## load package
library(aws.ec2metadata)
library(aws.signature)
library(aws.s3)

## Minimal example
credentials <- aws.signature::locate_credentials()

bucket_exist <- aws.s3::bucket_exists("name_of_my_bucket")
cat(file=stderr(), paste0("AWS_DEFAULT_REGION: ", Sys.getenv("AWS_DEFAULT_REGION"), "\n"))
cat(file=stderr(), paste0("Bucket exists: ", bucket_exist, "\n"))

The bucket_exists call fails with the error:

Client error: (403) Forbidden
 Warning: Error in : Bucket name_of_my_bucket does not exist.
] Error : Bucket name_of_my_bucket does not exist.

Further details

The output of credentials shows the correct key id, secret, toke, and region.

The attached token is valid: running awscli from the same pod I can access the bucket without error.

Output of sessionInfo()

platform = "x86_64-pc-linux-gnu"
arch = "x86_64"
os = "linux-gnu"
system = "x86_64, linux-gnu"
status = ""
major = "4"
minor = "2.3"
`svn rev` = "83980"
language = "R"
version.string = "R version 4.2.3 (2023-03-15)"
nickname = "Shortstop Beagle"
LC_CTYPE=en_US.UTF-8;
LC_NUMERIC=C;
LC_TIME=en_US.UTF-8;
LC_COLLATE=en_US.UTF-8;
LC_MONETARY=en_US.UTF-8;
LC_MESSAGES=en_US.UTF-8;
LC_PAPER=en_US.UTF-8;
LC_NAME=C;
LC_ADDRESS=C;
LC_TELEPHONE=C;
LC_MEASUREMENT=en_US.UTF-8;
LC_IDENTIFICATION=C
Package = "aws.s3", Type = "Package", Title = "'AWS S3' Client Package", Version = "0.3.22"
Package = "aws.signature", Type = "Package", Title = "Amazon Web Services Request Signatures", Version = "0.6.0",
Package = "aws.ec2metadata", Type = "Package", Title = "Get EC2 Instance Metadata", Version = "0.2.0"
NicholasFiorentini commented 1 year ago

Just to double-check, I tried this:

cat(file=stderr(), paste0("Key: ", aws_credentials$key, "\n"))
cat(file=stderr(), paste0("Secret: ", aws_credentials$secret, "\n"))
cat(file=stderr(), paste0("Token: ", aws_credentials$session_token, "\n"))
cat(file=stderr(), paste0("Region: ", aws_credentials$region, "\n"))

bucket_access <- aws.s3::bucket_exists(
        "my_bucket",
        key=aws_credentials$key,
        secret=aws_credentials$secret,
        session_token=aws_credentials$session_token,
        region=aws_credentials$region)

Output:

Key: ***
Secret: ***
Token: ***
Region: us-west-2

Client error: (403) Forbidden

Does aws.s3 support STS tokens?

andrewhharmon commented 1 year ago

I'm facing the same issue. Any workarounds?

NicholasFiorentini commented 1 year ago

Ultimately, I moved to a different library to overcome this limitation.

The workaround would be using an AWS API key, but I would avoid it.