Skip to content

Commit

Permalink
[Traffic Control] Introduce script to aid in configuration (#20693)
Browse files Browse the repository at this point in the history
## Description 

A script to somewhat simplify the process of determining how to
configure TrafficController in cases where nodes are running behind
proxies or other infra. See usage comment in code for more context.

## Test plan 

```yaml
# after changing x-forwarded-for configuration to 0 and restarting sui-node on the host...
ubuntu@jnb-tnt-val-00:~$ journalctl -fu sui-node | ./config-traffic-control.sh "95.217.193.168"
x-forwarded-for contents: 95.217.193.168
Configuration:
  client-id-source:
    x-forwarded-for: 1
```

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
williampsmith authored Dec 19, 2024
1 parent 1921cb4 commit d040de4
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions scripts/config-traffic-control.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0

usage() {
echo "Usage: $0 <search_string>"
echo
echo "Provides a recommended configuration for the client-id-source "
echo "field of the sui-node policy-config config for traffic controller."
echo "To use, do the following:"
echo
echo "1. Set the following sui-node config:"
echo
echo " client-id-source:"
echo " x-forwarded-for: 0"
echo
echo "2. Start the node"
echo "3. Run this script, piping sui-node logs to it and providing the known client IP address as an argument."
echo "4. The script will output the recommended configuration for the client-id-source field."
echo "5. Set the client-id-source field to the recommended configuration."
echo "6. Restart the node."
echo "7. The node will now use the recommended configuration for the client-id-source field."
echo
echo "NOTE: If the node is not running behind a proxy, this script will not yield any results."
echo " In such a case, set the client-id-source field to the default value of 'socket-addr'."
echo
echo "Example 1: journalctl -fu sui-node | $0 1.2.3.4"
echo "Example 2: echo 'x-forwarded-for contents: [\"1.2.3.4\", \"5.6.7.8\", \"4.5.6.7\"]' | $0 1.2.3.4"
}

# Check for help flag
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
fi

# Check that a search string is provided
if [ $# -ne 1 ]; then
usage
exit 1
fi

search="$1"

while IFS= read -r line; do
# Check if the line matches the pattern for x-forwarded-for
# Using a regex with a capturing group to extract the contents inside the brackets.
if [[ $line =~ x-forwarded-for[[:space:]]+contents:[[:space:]]+\[(.*)\]\. ]]; then
inside_brackets="${BASH_REMATCH[1]}"

# Replace '", "' with newlines to split into multiple lines, then read into an array
IFS=$'\n' read -d '' -r -a items < <(echo "$inside_brackets" | sed 's/", "/\n/g')

# Strip any non-integer characters from start and end of each item
for i in "${!items[@]}"; do
items[$i]=$(echo "${items[$i]}" | sed 's/^[^0-9]*//; s/[^0-9]*$//')
done

# Store the entire array into a variable (space-separated)
contents_var="${items[@]}"

# Find the index of the search element
found_index=-1
for i in "${!items[@]}"; do
if [ "${items[$i]}" = "$search" ]; then
found_index=$i
break
fi
done

if [ $found_index -ge 0 ]; then
# Calculate how many elements come after the found element
elements_after=$(( ${#items[@]} - (found_index + 1) ))
result=$(( 1 + elements_after ))

# Print the contents array and the recommended configuration
echo "x-forwarded-for contents: $contents_var"
echo "Configuration:"
echo " client-id-source:"
echo " x-forwarded-for: $result"

exit 0
fi
fi
done

# If we get here, no match was found
exit 1

0 comments on commit d040de4

Please sign in to comment.