mondoohq / cnquery

open source, cloud-native, graph-based asset inventory
https://cnquery.io
Other
302 stars 20 forks source link

aws `instance-connect` is not using `--profile` for the connection #1846

Open czunker opened 1 year ago

czunker commented 1 year ago

Describe the bug When I run instance-connect with --profile I get this error:

cnquery run aws ec2 instance-connect ec2-user@i-08db02ceb047ea7fb --profile AdministratorAccess-1234567890 --region eu-central-1 -c 'asset{ name title ids platform }'
! CLI pre-processing encountered an issue error="unknown flag: --profile"
! using builtin provider for aws
→ loaded configuration from /etc/opt/mondoo/mondoo.yml using source default
→ no AWS region found, using us-east-1
FTL failed to run query error="operation error STS: GetCallerIdentity, exceeded maximum number of attempts, 3, failed to sign request: failed to retrieve credentials: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, request send failed, Get \"http://169.254.169.254/latest/meta-data/iam/security-credentials/\": dial tcp 169.254.169.254:80: i/o timeout"

Using the was CLI works:

aws ec2-instance-connect ssh --instance-id i-08db02ceb047ea7fb --region eu-central-1 --profile AdministratorAccess-1234567890                                              
The authenticity of host '3.71.8.75 (3.71.8.75)' can't be established.
ED25519 key fingerprint is SHA256:yBfOVxhFjUtRSH37CZ27x3TUWYKvNzsaK71mJF8AgBI.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '3.71.8.75' (ED25519) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
[ec2-user@ip-172-31-21-20 ~]$ 

Setting the env var, it works independent of the --profile parameter:

export AWS_PROFILE=AdministratorAccess-1234567890
cnquery run aws ec2 instance-connect ec2-user@i-08db02ceb047ea7fb --region eu-central-1 -c 'asset{ name title ids platform }'                                            
! CLI pre-processing encountered an issue error="unknown flag: --region"
! using builtin provider for aws
→ loaded configuration from /etc/opt/mondoo/mondoo.yml using source default
→ no AWS region found, using us-east-1
! using builtin provider for os
asset: {
  title: "Amazon Linux 2023"
  platform: "amazonlinux"
  ids: [
    0: "//platformid.api.mondoo.app/hostname/ip-172-31-21-20.eu-central-1.compute.internal"
  ]
  name: "ip-172-31-21-20.eu-central-1.compute.internal"
}

To Reproduce Steps to reproduce the behavior:

  1. Create new EC2 instance
  2. Set up an AWS profile
  3. Try instance-connect with this profile
  4. Note the error

Expected behavior The parameter should set the profile.

czunker commented 1 year ago

This also happens with aws ssm

czunker commented 1 year ago

I just read the help text for this flag. I don't have the file ~/.aws/credentials on my system.

I created the profile using aws sso configure

vjeffrey commented 1 year ago

ohh, it looks like those creds get put in ~/.aws/config...ill take a look at this, i would have expected the api to read it in both locations if it puts sso stuff in ~/.aws/config ...maybe there's an extra option we need to pass

arlimus commented 1 year ago

From the feedback I gather this is an issue in v8 as well. I assume it's not a release blocker and we want to get it fixed shortly after.