|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +ACL_FILE=/tmp/acl-config.yaml |
| 4 | + |
| 5 | +# Parse simple key-value pairs from YAML |
| 6 | +NAME=$(grep 'name:' $ACL_FILE | sed 's/.*: //') |
| 7 | +ACTION=$(grep 'action:' $ACL_FILE | sed 's/.*: //') |
| 8 | +ALLOW_PRINCIPAL=$(grep 'allow_principal:' $ACL_FILE | sed 's/.*: //') |
| 9 | +TRANSACTIONAL_ID=$(grep 'transactional_id:' $ACL_FILE| sed 's/.*: //') |
| 10 | +RESOURCE_PATTERN_TYPE=$(grep 'resource_pattern_type:' $ACL_FILE | sed 's/.*: //') |
| 11 | + |
| 12 | +# Extract and clean operations (remove '-' and trim spaces) |
| 13 | +OPERATIONS=() |
| 14 | +while IFS= read -r line; do |
| 15 | + op=$(echo "$line" | sed -E 's/^\s*-\s*//' | xargs) # Remove '- ' and trim whitespace |
| 16 | + [[ -n "$op" ]] && OPERATIONS+=("$op") # Add only if non-empty |
| 17 | +done < <(awk '/operations:/ {flag=1; next} /^[^ ]/ {flag=0} flag' $ACL_FILE) |
| 18 | + |
| 19 | +# Extract and clean topics (remove '-' and trim spaces) |
| 20 | +TOPICS=() |
| 21 | +while IFS= read -r line; do |
| 22 | + topic=$(echo "$line" | sed -E 's/^\s*-\s*//' | xargs) # Remove '- ' and trim whitespace |
| 23 | + [[ -n "$topic" ]] && TOPICS+=("$topic") # Add only if non-empty |
| 24 | +done < <(awk '/topics:/ {flag=1; next} /^[^ ]/ {flag=0} flag' $ACL_FILE) |
| 25 | + |
| 26 | +# Display the ACL name for better logging |
| 27 | +echo "Running ACL Setup: $NAME" |
| 28 | + |
| 29 | +# Loop through each topic and apply ACLs |
| 30 | +for topic in "${TOPICS[@]}"; do |
| 31 | + # Construct the base command |
| 32 | + CMD="/opt/kafka/bin/kafka-acls.sh --bootstrap-server broker:9092 --command-config /tmp/admin.properties $ACTION --allow-principal \"$ALLOW_PRINCIPAL\"" |
| 33 | + |
| 34 | + # Add each operation correctly |
| 35 | + for op in "${OPERATIONS[@]}"; do |
| 36 | + CMD+=" --operation $op" |
| 37 | + done |
| 38 | + |
| 39 | + # Add the topic and resource pattern type |
| 40 | + CMD+=" --topic $topic" |
| 41 | + CMD+=" --resource-pattern-type $RESOURCE_PATTERN_TYPE" |
| 42 | + |
| 43 | + # Include transactional ID if provided |
| 44 | + if [[ -n "$TRANSACTIONAL_ID" ]]; then |
| 45 | + CMD+=" --transactional-id $TRANSACTIONAL_ID" |
| 46 | + fi |
| 47 | + |
| 48 | + # Print and execute the command |
| 49 | + echo "Executing: $CMD" |
| 50 | + eval "$CMD" |
| 51 | +done |
0 commit comments