Skip to content

Commit

Permalink
Enable to get EC2 instance ID by IMDSv2. (#1520)
Browse files Browse the repository at this point in the history
Co-authored-by: kido3160 <s.kido.fy@future.co.jp>
  • Loading branch information
kidokidofire and kido3160 authored Aug 24, 2022
1 parent 1e45732 commit 595e26d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions scanner/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ func (l *base) detectRunningOnAws() (ok bool, instanceID string, err error) {
return true, id, nil
}

cmd = "curl -X PUT --max-time 1 --noproxy 169.254.169.254 -H \"X-aws-ec2-metadata-token-ttl-seconds: 300\" http://169.254.169.254/latest/api/token"
r = l.exec(cmd, noSudo)
if r.isSuccess() {
token := strings.TrimSpace(r.Stdout)
cmd = fmt.Sprintf("curl -H \"X-aws-ec2-metadata-token: %s\" --max-time 1 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id", token)
r = l.exec(cmd, noSudo)
if r.isSuccess() {
id := strings.TrimSpace(r.Stdout)
if !l.isAwsInstanceID(id) {
return false, "", nil
}
return true, id, nil
}
}

switch r.ExitStatus {
case 28, 7:
// Not running on AWS
Expand Down

0 comments on commit 595e26d

Please sign in to comment.