chore: check link local address before querying cloud instance metadata (#12973)
check link local address before querying cloud providers data
Ilya Mashchenko committed
May 20, 2022 at 16:19 UTC
c6265545aea2b88f4c084758955cc82446c20db0
1 file changed
+33
-27
daemon/system-info.sh
+33
-27
@@ -434,37 +434,43 @@ CLOUD_INSTANCE_TYPE="unknown"
434
CLOUD_INSTANCE_REGION="unknown"
435
436
if [ "${VIRTUALIZATION}" != "none" ] && command -v curl > /dev/null 2>&1; then
437
- # Try AWS IMDSv2
438
- if [ "${CLOUD_TYPE}" = "unknown" ]; then
439
- AWS_IMDS_TOKEN="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")"
440
- if [ -n "${AWS_IMDS_TOKEN}" ]; then
441
- CLOUD_TYPE="AWS"
442
- CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/instance-type" 2> /dev/null)"
443
- CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/placement/region" 2> /dev/null)"
437
+ # Returned HTTP status codes: GCP is 200, AWS is 200, DO is 404.
438
+ curl --fail -s -m 1 --noproxy "*" http://169.254.169.254 >/dev/null 2>&1
439
+ ret=$?
440
+ # anything but operation timeout.
441
+ if [ "$ret" != 28 ]; then
442
+ # Try AWS IMDSv2
443
+ if [ "${CLOUD_TYPE}" = "unknown" ]; then
444
+ AWS_IMDS_TOKEN="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")"
445
+ if [ -n "${AWS_IMDS_TOKEN}" ]; then
446
+ CLOUD_TYPE="AWS"
447
+ CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/instance-type" 2>/dev/null)"
448
+ CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/placement/region" 2>/dev/null)"
449
+ fi
450
fi
445
- fi
451
447
- # Try GCE computeMetadata v1
448
- if [ "${CLOUD_TYPE}" = "unknown" ]; then
449
- if [ -n "$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
450
- CLOUD_TYPE="GCP"
451
- CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type")"
452
- [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
453
- CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
454
- [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
452
+ # Try GCE computeMetadata v1
453
+ if [ "${CLOUD_TYPE}" = "unknown" ]; then
454
+ if [ -n "$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
455
+ CLOUD_TYPE="GCP"
456
+ CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type")"
457
+ [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
458
+ CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
459
+ [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
460
+ fi
461
fi
456
- fi
462
458
- # TODO: needs to be tested in Microsoft Azure
459
- # Try Azure IMDS
460
- # if [ "${CLOUD_TYPE}" = "unknown" ]; then
461
- # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
462
- # if [ -n "${AZURE_IMDS_DATA}" ]; then
463
- # CLOUD_TYPE="Azure"
464
- # CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/vmSize?version=2021-10-01&format=text")"
465
- # CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?version=2021-10-01&format=text")"
466
- # fi
467
- # fi
463
+ # TODO: needs to be tested in Microsoft Azure
464
+ # Try Azure IMDS
465
+ # if [ "${CLOUD_TYPE}" = "unknown" ]; then
466
+ # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
467
+ # if [ -n "${AZURE_IMDS_DATA}" ]; then
468
+ # CLOUD_TYPE="Azure"
469
+ # CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/vmSize?version=2021-10-01&format=text")"
470
+ # CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?version=2021-10-01&format=text")"
471
+ # fi
472
+ # fi
473
+ fi
474
fi
475
476
echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}"