AWS Security ChangesHomeSearch

AWS code-library medium security documentation change

Service: code-library · 2026-05-01 · Security-related medium

File: code-library/latest/ug/glue_example_glue_GettingStarted_024_section.md

Summary

Enhanced AWS Glue example script with improved security measures including secure temporary file handling, input validation, and credential verification

Security assessment

Added secure temporary file handling (chmod 600 + shred/rm), input validation using jq, AWS credential verification via STS, and explicit resource cleanup. These changes address security weaknesses like temporary file exposure and unauthenticated execution risks.

Diff

diff --git a/code-library/latest/ug/glue_example_glue_GettingStarted_024_section.md b/code-library/latest/ug/glue_example_glue_GettingStarted_024_section.md
index 13380bfdb..b84f06a95 100644
--- a//code-library/latest/ug/glue_example_glue_GettingStarted_024_section.md
+++ b//code-library/latest/ug/glue_example_glue_GettingStarted_024_section.md
@@ -36,0 +37,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    # Cost improvements: Reduced API calls, optimized queries, eliminated redundant operations
+    # Reliability improvements: Enhanced error handling, input validation, resource tracking
+    
+    set -euo pipefail
@@ -46 +50 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    UNIQUE_ID=$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 8)
+    UNIQUE_ID=$(openssl rand -hex 4)
@@ -48,0 +53 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    TABLE_INPUT_FILE="table-input-${UNIQUE_ID}.json"
@@ -51 +56,10 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    CREATED_RESOURCES=()
+    declare -a CREATED_RESOURCES=()
+    
+    # Set default region if not provided
+    AWS_REGION="${AWS_REGION:-us-east-1}"
+    
+    # Flag to track if database was successfully created
+    DATABASE_CREATED=false
+    
+    # Trap to ensure cleanup on exit
+    trap cleanup_resources EXIT
@@ -56,2 +70 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            echo "ERROR: $1 failed. Exiting."
-            cleanup_resources
+            echo "ERROR: $1 failed." >&2
@@ -63,0 +77 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        local exit_code=$?
@@ -76 +90,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    aws glue delete-table --database-name "$DB_NAME" --name "$resource_name"
+                    if [ "$DATABASE_CREATED" = true ]; then
+                        aws glue delete-table \
+                            --database-name "$DB_NAME" \
+                            --name "$resource_name" \
+                            --region "$AWS_REGION" \
+                            2>/dev/null || echo "Warning: Failed to delete table $resource_name"
+                    fi
@@ -79 +99,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    aws glue delete-database --name "$resource_name"
+                    aws glue delete-database \
+                        --name "$resource_name" \
+                        --region "$AWS_REGION" \
+                        2>/dev/null || echo "Warning: Failed to delete database $resource_name"
@@ -82 +105 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    echo "Unknown resource type: $resource_type"
+                    echo "Unknown resource type: $resource_type" >&2
@@ -86,0 +110,9 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        # Clean up temporary files securely
+        if [ -f "$TABLE_INPUT_FILE" ]; then
+            if command -v shred &> /dev/null; then
+                shred -vfz -n 3 "$TABLE_INPUT_FILE" 2>/dev/null || rm -f "$TABLE_INPUT_FILE"
+            else
+                rm -f "$TABLE_INPUT_FILE"
+            fi
+        fi
+        
@@ -87,0 +120,38 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        exit $exit_code
+    }
+    
+    # Function to validate prerequisites
+    validate_prerequisites() {
+        # Validate AWS CLI is available
+        if ! command -v aws &> /dev/null; then
+            echo "ERROR: AWS CLI is not installed or not in PATH" >&2
+            exit 1
+        fi
+    
+        # Validate AWS CLI version
+        local AWS_CLI_VERSION
+        AWS_CLI_VERSION=$(aws --version 2>&1 | cut -d' ' -f1 | cut -d'/' -f2 | cut -d'.' -f1)
+        if [ "$AWS_CLI_VERSION" -lt 1 ]; then
+            echo "ERROR: AWS CLI is required" >&2
+            exit 1
+        fi
+    
+        # Validate jq is available for JSON validation
+        if ! command -v jq &> /dev/null; then
+            echo "ERROR: jq is not installed or not in PATH" >&2
+            exit 1
+        fi
+    
+        # Validate AWS credentials and get account identity in single call (cost optimization)
+        local CALLER_IDENTITY
+        CALLER_IDENTITY=$(aws sts get-caller-identity --region "$AWS_REGION" --query 'Account' --output text 2>/dev/null) || {
+            echo "ERROR: Failed to get AWS caller identity. Check credentials and permissions." >&2
+            exit 1
+        }
+        
+        if [ -z "$CALLER_IDENTITY" ] || [ "$CALLER_IDENTITY" == "None" ]; then
+            echo "ERROR: Unable to determine AWS account identity" >&2
+            exit 1
+        fi
+        echo "Using AWS Account: $CALLER_IDENTITY"
+        echo "Using Region: $AWS_REGION"
@@ -90 +160,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Step 1: Create a database
+    # Function to create database with verification
+    create_database() {
@@ -92,2 +163,10 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    aws glue create-database --database-input "{\"Name\":\"$DB_NAME\",\"Description\":\"Database for AWS Glue tutorial\"}"
-    check_status "Creating database"
+        
+        if ! aws glue create-database \
+            --database-input "Name=$DB_NAME,Description=Database for AWS Glue tutorial" \
+            --region "$AWS_REGION" \
+            --output json > /dev/null 2>&1; then
+            echo "ERROR: Failed to create database $DB_NAME" >&2
+            exit 1
+        fi
+        
+        DATABASE_CREATED=true
@@ -95,0 +175 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    }
@@ -97,8 +177,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Verify the database was created
-    echo "Verifying database creation..."
-    DB_VERIFY=$(aws glue get-database --name "$DB_NAME" --query 'Database.Name' --output text)
-    check_status "Verifying database"
-    
-    if [ "$DB_VERIFY" != "$DB_NAME" ]; then
-        echo "ERROR: Database verification failed. Expected $DB_NAME but got $DB_VERIFY"
-        cleanup_resources
+    # Function to prepare table input JSON
+    prepare_table_input() {
+        # Create a temporary JSON file for table input with restricted permissions
+        if ! touch "$TABLE_INPUT_FILE" 2>/dev/null; then
+            echo "ERROR: Failed to create temporary file $TABLE_INPUT_FILE" >&2
@@ -107 +183,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    echo "Database verification successful."
@@ -109,2 +185,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Step 2: Create a table
-    echo "Step 2: Creating a table named $TABLE_NAME in database $DB_NAME"
+        if ! chmod 600 "$TABLE_INPUT_FILE" 2>/dev/null; then
+            echo "ERROR: Failed to set permissions on $TABLE_INPUT_FILE" >&2
+            rm -f "$TABLE_INPUT_FILE"
+            exit 1
+        fi
@@ -112,3 +191 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Create a temporary JSON file for table input
-    TABLE_INPUT_FILE="table-input-${UNIQUE_ID}.json"
-    cat > "$TABLE_INPUT_FILE" << EOF
+        cat > "$TABLE_INPUT_FILE" << 'EOF'
@@ -116 +193 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      "Name": "$TABLE_NAME",
+      "Name": "TABLE_NAME_PLACEHOLDER",
@@ -160,4 +237,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    aws glue create-table --database-name "$DB_NAME" --table-input file://"$TABLE_INPUT_FILE"
-    check_status "Creating table"
-    CREATED_RESOURCES+=("table:$TABLE_NAME")
-    echo "Table $TABLE_NAME created successfully."
+        # Replace placeholder with actual table name
+        if ! sed -i "s/TABLE_NAME_PLACEHOLDER/$TABLE_NAME/g" "$TABLE_INPUT_FILE" 2>/dev/null; then
+            echo "ERROR: Failed to substitute table name in JSON file" >&2
+            rm -f "$TABLE_INPUT_FILE"
+            exit 1
+        fi
@@ -165 +244,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Clean up the temporary file
+        # Validate JSON syntax before using it
+        if ! jq empty "$TABLE_INPUT_FILE" 2>/dev/null; then
+            echo "ERROR: Invalid JSON in table input file" >&2
@@ -166,0 +248,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+            exit 1
+        fi
+    }
+    
+    # Function to create table
+    create_table() {
+        echo "Step 2: Creating a table named $TABLE_NAME in database $DB_NAME"
@@ -168,4 +256 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Verify the table was created
-    echo "Verifying table creation..."
-    TABLE_VERIFY=$(aws glue get-table --database-name "$DB_NAME" --name "$TABLE_NAME" --query 'Table.Name' --output text)
-    check_status "Verifying table"
+        prepare_table_input
@@ -173,3 +258,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    if [ "$TABLE_VERIFY" != "$TABLE_NAME" ]; then
-        echo "ERROR: Table verification failed. Expected $TABLE_NAME but got $TABLE_VERIFY"
-        cleanup_resources
+        if ! aws glue create-table \
+            --database-name "$DB_NAME" \
+            --table-input "file://${TABLE_INPUT_FILE}" \
+            --region "$AWS_REGION" \
+            --output json > /dev/null 2>&1; then
+            echo "ERROR: Failed to create table $TABLE_NAME" >&2
+            rm -f "$TABLE_INPUT_FILE"
@@ -178 +266,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    echo "Table verification successful."
@@ -180 +268,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Step 3: Get table details
+        CREATED_RESOURCES+=("table:$TABLE_NAME")
+        echo "Table $TABLE_NAME created successfully."
+    }
+    
+    # Function to get and display table details