2323 export DEFAULT_REGION=" us-west-2"
2424
2525 # Extract variables and save them to a file
26- export REPO_SUFFIX=$( grep " Repo Suffix:" deployment_output.txt | awk ' {print $3}' )
26+ export REPO_SUFFIX=$( grep " Repo Suffix:" deployment_output.txt 2> /dev/null | awk ' {print $3}' || echo " " )
2727 export AWS_ACCOUNT_ID=$( aws sts get-caller-identity --query " Account" --output text)
28- export CLUSTER_NAME=$( grep " EKS Cluster Name:" deployment_output.txt | awk ' {print $4}' )
29- export REPO_NAME=$( grep " ECR Repository Name:" deployment_output.txt | awk ' {print $4}' )
30- export ROLE_NAME=$( grep " EC2 Role Name:" deployment_output.txt | awk ' {print $4}' )
31- export EKS_ROLE_NAME=$( grep " EKS Node Role Name:" deployment_output.txt | awk ' {print $5}' )
32- export BUCKET_NAME=$( grep " S3 Bucket Name:" deployment_output.txt | awk ' {print $4}' )
28+ export CLUSTER_NAME=$( grep " EKS Cluster Name:" deployment_output.txt 2> /dev/null | awk ' {print $4}' || echo " " )
29+ export REPO_NAME=$( grep " ECR Repository Name:" deployment_output.txt 2> /dev/null | awk ' {print $4}' || echo " " )
30+ export ROLE_NAME=$( grep " EC2 Role Name:" deployment_output.txt 2> /dev/null | awk ' {print $4}' || echo " " )
31+ export EKS_ROLE_NAME=$( grep " EKS Node Role Name:" deployment_output.txt 2> /dev/null | awk ' {print $5}' || echo " " )
32+ export BUCKET_NAME=$( grep " S3 Bucket Name:" deployment_output.txt 2> /dev/null | awk ' {print $4}' || echo " " )
33+ export INSTANCE_PROFILE_NAME=" peachycloudsecurity-ip-${REPO_SUFFIX} "
34+ export S3_POLICY_NAME=" peachycloudsecurity-listSpecificS3Buckets-${REPO_SUFFIX} "
35+ export CLOUDFORMATION_STACK_NAME=" eksctl-${CLUSTER_NAME} -cluster"
3336
3437 # Save the variables to a file for future use
3538 cat << EOL > "$VARIABLES_FILE "
@@ -41,6 +44,9 @@ export REPO_NAME="$REPO_NAME"
4144export ROLE_NAME="$ROLE_NAME "
4245export EKS_ROLE_NAME="$EKS_ROLE_NAME "
4346export BUCKET_NAME="$BUCKET_NAME "
47+ export INSTANCE_PROFILE_NAME="$INSTANCE_PROFILE_NAME "
48+ export S3_POLICY_NAME="$S3_POLICY_NAME "
49+ export CLOUDFORMATION_STACK_NAME="$CLOUDFORMATION_STACK_NAME "
4450EOL
4551fi
4652
@@ -74,30 +80,49 @@ while true; do
7480
7581 " CHECKPOINT_1" )
7682 echo " Deleting EKS cluster with name ${CLUSTER_NAME} in region ${REGION} ..."
77- eksctl delete cluster --name ${CLUSTER_NAME} --region ${REGION}
83+ # Check if cluster exists before trying to delete
84+ if eksctl get cluster --name ${CLUSTER_NAME} --region ${REGION} & > /dev/null; then
85+ eksctl delete cluster --name ${CLUSTER_NAME} --region ${REGION}
86+ else
87+ echo " Cluster ${CLUSTER_NAME} does not exist, skipping cluster deletion."
88+ # If cluster doesn't exist, try to delete CloudFormation stack if it exists
89+ if aws cloudformation describe-stacks --stack-name ${CLOUDFORMATION_STACK_NAME} --region ${REGION} & > /dev/null; then
90+ echo " Deleting CloudFormation stack ${CLOUDFORMATION_STACK_NAME} ..."
91+ aws cloudformation delete-stack --stack-name ${CLOUDFORMATION_STACK_NAME} --region ${REGION}
92+ echo " Waiting for stack deletion to complete..."
93+ aws cloudformation wait stack-delete-complete --stack-name ${CLOUDFORMATION_STACK_NAME} --region ${REGION} || true
94+ fi
95+ fi
7896 echo " CHECKPOINT_2" > " $CHECKPOINT_FILE "
7997 ;;
8098
8199 " CHECKPOINT_2" )
82- # Get the image digest for the latest tag
83- export IMAGE_DIGEST=$( aws ecr list-images \
84- --repository-name ${REPO_NAME} \
85- --filter " tagStatus=TAGGED" \
86- --query ' imageIds[?imageTag==`latest`].imageDigest' \
87- --output text \
88- --region ${REGION} )
89-
90- # Delete the image using the image digest
91- aws ecr batch-delete-image \
92- --repository-name ${REPO_NAME} \
93- --image-ids imageDigest=${IMAGE_DIGEST} \
94- --region ${REGION}
95-
96- # Delete the ECR repository
97- aws ecr delete-repository \
98- --repository-name ${REPO_NAME} \
99- --force \
100- --region ${REGION}
100+ # Delete ECR repository if it exists
101+ if aws ecr describe-repositories --repository-names ${REPO_NAME} --region ${REGION} & > /dev/null; then
102+ # Get the image digest for the latest tag
103+ export IMAGE_DIGEST=$( aws ecr list-images \
104+ --repository-name ${REPO_NAME} \
105+ --filter " tagStatus=TAGGED" \
106+ --query ' imageIds[?imageTag==`latest`].imageDigest' \
107+ --output text \
108+ --region ${REGION} 2> /dev/null || echo " " )
109+
110+ # Delete the image using the image digest if it exists
111+ if [ -n " $IMAGE_DIGEST " ]; then
112+ aws ecr batch-delete-image \
113+ --repository-name ${REPO_NAME} \
114+ --image-ids imageDigest=${IMAGE_DIGEST} \
115+ --region ${REGION} 2> /dev/null || true
116+ fi
117+
118+ # Delete the ECR repository
119+ aws ecr delete-repository \
120+ --repository-name ${REPO_NAME} \
121+ --force \
122+ --region ${REGION} 2> /dev/null || true
123+ else
124+ echo " ECR repository ${REPO_NAME} does not exist, skipping deletion."
125+ fi
101126
102127 echo " CHECKPOINT_3" > " $CHECKPOINT_FILE "
103128 ;;
@@ -145,14 +170,18 @@ while true; do
145170
146171 " CHECKPOINT_4" )
147172 # Delete IAM role and policy for ec2 instance
148- # Remove the role from the instance profile
149- aws iam remove-role-from-instance-profile --instance-profile-name peachycloudsecurity-ip --role-name peachycloudsecurity-redteam-${REPO_SUFFIX}
150- # Delete the instance profile
151- aws iam delete-instance-profile --instance-profile-name peachycloudsecurity-ip
152- # Delete the inline policy from the IAM role
153- aws iam delete-role-policy --role-name peachycloudsecurity-redteam-${REPO_SUFFIX} --policy-name peachycloudsecurity-policy
154- # Delete the IAM role
155- aws iam delete-role --role-name peachycloudsecurity-redteam-${REPO_SUFFIX}
173+ # Remove the role from the instance profile (if it exists)
174+ if aws iam get-instance-profile --instance-profile-name ${INSTANCE_PROFILE_NAME} & > /dev/null; then
175+ aws iam remove-role-from-instance-profile --instance-profile-name ${INSTANCE_PROFILE_NAME} --role-name peachycloudsecurity-redteam-${REPO_SUFFIX} 2> /dev/null || true
176+ # Delete the instance profile
177+ aws iam delete-instance-profile --instance-profile-name ${INSTANCE_PROFILE_NAME} 2> /dev/null || true
178+ fi
179+ # Delete the inline policy from the IAM role (if it exists)
180+ if aws iam get-role --role-name peachycloudsecurity-redteam-${REPO_SUFFIX} & > /dev/null; then
181+ aws iam delete-role-policy --role-name peachycloudsecurity-redteam-${REPO_SUFFIX} --policy-name peachycloudsecurity-policy 2> /dev/null || true
182+ # Delete the IAM role
183+ aws iam delete-role --role-name peachycloudsecurity-redteam-${REPO_SUFFIX} 2> /dev/null || true
184+ fi
156185
157186 echo " CHECKPOINT_5" > " $CHECKPOINT_FILE "
158187 ;;
@@ -161,14 +190,31 @@ while true; do
161190 # Delete IAM role and policy for eks node instance
162191 echo " Removing IAM role & policies for eks role name: ${EKS_ROLE_NAME} ..."
163192
164- aws iam detach-role-policy --role-name ${EKS_ROLE_NAME} --policy-arn arn:aws:iam::${AWS_ACCOUNT_ID} :policy/peachycloudsecurity-listSpecificS3Buckets
165- aws iam remove-role-from-instance-profile --instance-profile-name ${EKS_ROLE_NAME} -profile --role-name ${EKS_ROLE_NAME}
166- aws iam delete-instance-profile --instance-profile-name ${EKS_ROLE_NAME} -profile
167- aws iam list-attached-role-policies --role-name ${EKS_ROLE_NAME} --query ' AttachedPolicies[].PolicyArn' --output text | xargs -n1 aws iam detach-role-policy --role-name ${EKS_ROLE_NAME} --policy-arn
168-
169- aws iam delete-role --role-name ${EKS_ROLE_NAME}
193+ # Check if EKS role exists before trying to delete
194+ if aws iam get-role --role-name ${EKS_ROLE_NAME} & > /dev/null; then
195+ # Detach S3 policy if it exists
196+ aws iam detach-role-policy --role-name ${EKS_ROLE_NAME} --policy-arn arn:aws:iam::${AWS_ACCOUNT_ID} :policy/${S3_POLICY_NAME} 2> /dev/null || true
197+
198+ # Remove role from instance profile if it exists
199+ if aws iam get-instance-profile --instance-profile-name ${EKS_ROLE_NAME} -profile & > /dev/null; then
200+ aws iam remove-role-from-instance-profile --instance-profile-name ${EKS_ROLE_NAME} -profile --role-name ${EKS_ROLE_NAME} 2> /dev/null || true
201+ aws iam delete-instance-profile --instance-profile-name ${EKS_ROLE_NAME} -profile 2> /dev/null || true
202+ fi
203+
204+ # Detach all attached policies
205+ aws iam list-attached-role-policies --role-name ${EKS_ROLE_NAME} --query ' AttachedPolicies[].PolicyArn' --output text 2> /dev/null | xargs -n1 -I {} aws iam detach-role-policy --role-name ${EKS_ROLE_NAME} --policy-arn {} 2> /dev/null || true
206+
207+ # Delete the role
208+ aws iam delete-role --role-name ${EKS_ROLE_NAME} 2> /dev/null || true
209+ fi
170210
171- aws iam list-policy-versions --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/peachycloudsecurity-listSpecificS3Buckets" --query ' Versions[?IsDefaultVersion==`false`].VersionId' --output text | xargs -I {} aws iam delete-policy-version --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/peachycloudsecurity-listSpecificS3Buckets" --version-id {} && aws iam delete-policy --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/peachycloudsecurity-listSpecificS3Buckets"
211+ # Delete S3 policy if it exists
212+ if aws iam get-policy --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/${S3_POLICY_NAME} " & > /dev/null; then
213+ # Delete non-default policy versions first
214+ aws iam list-policy-versions --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/${S3_POLICY_NAME} " --query ' Versions[?IsDefaultVersion==`false`].VersionId' --output text 2> /dev/null | xargs -I {} aws iam delete-policy-version --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/${S3_POLICY_NAME} " --version-id {} 2> /dev/null || true
215+ # Delete the policy
216+ aws iam delete-policy --policy-arn " arn:aws:iam::${AWS_ACCOUNT_ID} :policy/${S3_POLICY_NAME} " 2> /dev/null || true
217+ fi
172218
173219 echo " CHECKPOINT_6" > " $CHECKPOINT_FILE "
174220 ;;
@@ -204,17 +250,22 @@ while true; do
204250
205251 " CHECKPOINT_7" )
206252 echo " Deleting the S3 Bucket..."
207- # Delete the flag.txt file from the S3 bucket
208- aws s3 rm s3://${BUCKET_NAME} /flag.txt
253+ # Delete the S3 bucket if it exists
254+ if aws s3 ls s3://${BUCKET_NAME} & > /dev/null || aws s3api head-bucket --bucket ${BUCKET_NAME} & > /dev/null 2>&1 ; then
255+ # Delete the flag.txt file from the S3 bucket
256+ aws s3 rm s3://${BUCKET_NAME} /flag.txt 2> /dev/null || true
209257
210- # Delete the S3 bucket
211- aws s3 rb s3://${BUCKET_NAME} --force
258+ # Delete the S3 bucket
259+ aws s3 rb s3://${BUCKET_NAME} --force 2> /dev/null || true
260+ else
261+ echo " S3 bucket ${BUCKET_NAME} does not exist, skipping deletion."
262+ fi
212263
213264 echo " All deployments deleted successfully."
214265
215266 # Final cleanup of checkpoint file
216- rm " $CHECKPOINT_FILE "
217- rm " $VARIABLES_FILE "
267+ rm -f " $CHECKPOINT_FILE "
268+ rm -f " $VARIABLES_FILE "
218269
219270 # Exit the loop and script
220271 break
0 commit comments