Skip to content

Commit 92813e3

Browse files
feat: dont register instance when metadata service give us bad results (#188)
1 parent 7a3af3b commit 92813e3

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.spacelift/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: 2
2-
module_version: 5.4.1
2+
module_version: 5.4.2
33

44
tests:
55
- name: AMD64-based workerpool

user_data/saas.tftpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,26 @@ spacelift () {(
6565
echo "Making the Spacelift launcher executable" >> /var/log/spacelift/info.log
6666
chmod 755 /usr/bin/spacelift-launcher 2>>/var/log/spacelift/error.log
6767

68+
validate_metadata() {
69+
local value="$1"
70+
local name="$2"
71+
if [[ -z "$value" ]] || [[ "$value" == "null" ]] || [[ "$value" == "None" ]]; then
72+
echo "ERROR: $name is null or empty. EC2 metadata service returned invalid data." >> /var/log/spacelift/error.log
73+
return 1
74+
fi
75+
return 0
76+
}
77+
6878
echo "Retrieving EC2 instance id and AMI id" >> /var/log/spacelift/info.log
6979
export SPACELIFT_METADATA_instance_id=$(ec2-metadata --instance-id | cut -d ' ' -f2)
80+
validate_metadata "$SPACELIFT_METADATA_instance_id" "instance_id" || return 1
81+
7082
export SPACELIFT_METADATA_ami_id=$(ec2-metadata --ami-id | cut -d ' ' -f2)
83+
validate_metadata "$SPACELIFT_METADATA_ami_id" "ami_id" || return 1
7184

7285
echo "Retrieving EC2 ASG ID" >> /var/log/spacelift/info.log
7386
export SPACELIFT_METADATA_asg_id=$(aws autoscaling --region=${region} describe-auto-scaling-instances --instance-ids $SPACELIFT_METADATA_instance_id | jq -r '.AutoScalingInstances[0].AutoScalingGroupName')
87+
validate_metadata "$SPACELIFT_METADATA_asg_id" "asg_id" || return 1
7488

7589
echo "Starting the Spacelift binary" >> /var/log/spacelift/info.log
7690
/usr/bin/spacelift-launcher 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log

user_data/selfhosted.tftpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,25 @@ create_spacelift_launcher_script () {(
7777
#!/bin/bash
7878
join_strings () { local d="$1"; echo -n "$2"; shift 2 && printf '%s' "$${!@/#/$d}"; }
7979
80+
validate_metadata() {
81+
local value="\$1"
82+
local name="\$2"
83+
if [[ -z "\$value" ]] || [[ "\$value" == "null" ]] || [[ "\$value" == "None" ]]; then
84+
echo "ERROR: \$name is null or empty. EC2 metadata service returned invalid data." >> /var/log/spacelift/error.log
85+
exit 1
86+
fi
87+
}
88+
8089
echo "Retrieving EC2 instance ID and ami ID" >> /var/log/spacelift/info.log
8190
export SPACELIFT_METADATA_instance_id=\$(ec2-metadata --instance-id | cut -d ' ' -f2)
91+
validate_metadata "\$SPACELIFT_METADATA_instance_id" "instance_id"
92+
8293
export SPACELIFT_METADATA_ami_id=\$(ec2-metadata --ami-id | cut -d ' ' -f2)
94+
validate_metadata "\$SPACELIFT_METADATA_ami_id" "ami_id"
95+
8396
echo "Retrieving EC2 ASG ID" >> /var/log/spacelift/info.log
8497
export SPACELIFT_METADATA_asg_id=\$(aws autoscaling --region=${region} describe-auto-scaling-instances --instance-ids \$SPACELIFT_METADATA_instance_id | jq -r '.AutoScalingInstances[0].AutoScalingGroupName')
98+
validate_metadata "\$SPACELIFT_METADATA_asg_id" "asg_id"
8599
if [[ "${http_proxy_config}" != "" || "${https_proxy_config}" != "" || "${no_proxy_config}" != "" ]]; then
86100
whitelisted_vars=()
87101
echo "Configuring HTTP proxy information" >> /var/log/spacelift/info.log

0 commit comments

Comments
 (0)