qui<\/a>. <\/p>\n\n\n\nDando per scontata la creazione di una nuova istanza ed una AMI, partiamo con la compilazione del tool per la gestione del tunnel e l’installazione. <\/p>\n\n\n\n
Installeremo anche Suricata<\/strong>, un Intrusion Detection System open-source. Useremo la configurazione di default<\/p>\n\n\n\napt update\napt install -y build-essential \"Development Tools\"\napt install -y cmake g++ suricata\nsnap install aws-cli --classic\nsuricata-update #update rules for suricata\ncd \/opt\ngit clone https:\/\/github.com\/aws-samples\/aws-gateway-load-balancer-tunnel-handler\ncd aws-gateway-load-balancer-tunnel-handler\ncmake .\nmake<\/code><\/pre>\n\n\n\nAl termine di queste operazioni il tunnel handler sar\u00e0 pronto, viene creato un file eseguibile con nome “gwlbtun”. Eseguendolo con il parametro “-h” viene visualizzata la pagina di help:<\/p>\n\n\n\n
root@ip-10-101-5-238:\/opt\/aws-gateway-load-balancer-tunnel-handler# .\/gwlbtun -h\nAWS Gateway Load Balancer Tunnel Handler\nUsage: .\/gwlbtun [options]\nExample: .\/gwlbtun\n\n -h Print this help\n -c FILE Command to execute when a new tunnel has been built. See below for arguments passed.\n -r FILE Command to execute when a tunnel times out and is about to be destroyed. See below for arguments passed.\n -t TIME Minimum time in seconds between last packet seen and to consider the tunnel timed out. Set to 0 (the default) to never time out tunnels.\n Note the actual time between last packet and the destroy call may be longer than this time.\n -p PORT Listen to TCP port PORT and provide a health status report on it.\n -s Only return simple health check status (only the HTTP response code), instead of detailed statistics.\n -d Enable debugging output.\n -x Enable dumping the hex payload of packets being processed.\n\n---------------------------------------------------------------------------------------------------------\nTunnel command arguments:\nThe commands will be called with the following arguments:\n1: The string 'CREATE' or 'DESTROY', depending on which operation is occurring.\n2: The interface name of the ingress interface (gwi-<X>).\n3: The interface name of the egress interface (gwo-<X>). Packets can be sent out via in the ingress\n as well, but having two different interfaces makes routing and iptables easier.\n4: The GWLBE ENI ID in base 16 (e.g. '2b8ee1d4db0c51c4') associated with this tunnel.\n\nThe <X> in the interface name is replaced with the base 60 encoded ENI ID (to fit inside the 15 character\ndevice name limit).<\/code><\/pre>\n\n\n\nOltre a stabilire la connessione con il Gateway Load Balancer e creare il tunnel GENEVE, gwlbtun mette a disposizione una porta utilizzabile per permettere al Target Group di effettuare gli health check, cos\u00ec da non dover implementare controlli ad hoc.<\/p>\n\n\n\n
In aggiunta a questo, quando il tunnel viene creato o terminato, gwlbtun pu\u00f2 eseguire uno script. Useremo questa funzione per fare in modo che uno script bash possa abilitare il routing IP, usando iptables per aggiungere e rimuovare le regole di NAT.<\/p>\n\n\n\n
Nota:<\/strong> per far funzionare la nostra istanza \u00e8 necessario disabilitare una feature di sicurezza chiamata “source\/destination check”. Questa impostazione fa in modo che il traffico non originato o diretto da o per l’istanza venga automaticamente bloccato. <\/p>\n\n\n\nL’istanza stessa deve essere in grado di farlo, per cui vedremo che dovremo creare ed assegnare un instance role con una autorizzazione specifica.<\/p>\n\n\n\n
Lo script che segue p\u00f9o essere memorizzato nella directory<\/p>\n\n\n\n
\/opt\/aws-gateway-load-balancer-tunnel-handler<\/code><\/pre>\n\n\n\ncon il nome<\/p>\n\n\n\n
tunnel-handler.sh<\/code><\/pre>\n\n\n\n#!\/bin\/bash\n\n# Note: This requires this instance to have Source\/Dest check disabled; we need to assign a role to the ec2 instance to enable and disable it\n\n\necho \"Running tunnel handler script... \"\necho Mode is $1, In Int is $2, Out Int is $3, ENI is $4\n\niptables -F\niptables -t nat -F\nINSTANCE_ID=$(curl 169.254.169.254\/latest\/meta-data\/instance-id\n\ncase $1 in\n \tCREATE)\n\t\t\techo \"Disabling source and destination check.\"\n\t\t\taws ec2 modify-instance-attribute --instance-id=$INSTANCE_ID --source-dest-check\n\n \techo \"Setting up NAT and IP FORWARD\"\n \tiptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\n \tiptables -A FORWARD -i $2 -o $2 -j ACCEPT\n \techo 1 > \/proc\/sys\/net\/ipv4\/ip_forward\n \techo 0 > \/proc\/sys\/net\/ipv4\/conf\/all\/rp_filter\n \techo 0 > \/proc\/sys\/net\/ipv4\/conf\/$2\/rp_filter\n \t;;\n \tDESTROY)\n\t\t\techo \"Enabling source and destination check.\"\n\t\t\taws ec2 modify-instance-attribute --instance-id=$INSTANCE_ID --no-source-dest-check\n \techo \"Removing IP FORWARD\"\n \techo 0 > \/proc\/sys\/net\/ipv4\/ip_forward\n \techo 1 > \/proc\/sys\/net\/ipv4\/conf\/all\/rp_filter\n \techo 1 > \/proc\/sys\/net\/ipv4\/conf\/$2\/rp_filter\n \t;;\n \t*)\n \techo \"invalid action.\"\n \texit 1\n \t;;\nesac<\/code><\/pre>\n\n\n\nA questo punto non ci resta che scrive una unit systemd per avviare gwlbtun. Useremo aws-gwlb.service come nome, mettendo il file nella directory<\/p>\n\n\n\n
\/lib\/systemd\/system\/<\/code><\/pre>\n\n\n\n[Unit]\nDescription=AWS GWLB Tunnel Handler\nAfter=network.target\n\n[Service] \nExecStart=\/opt\/aws-gateway-load-balancer-tunnel-handler\/gwlbtun -c \/opt\/aws-gateway-load-balancer-tunnel-handler\/tunnel-handler.sh -r \/opt\/aws-gateway-load-balancer-tunnel-handler\/tunnel-handler.sh -p 80\nRestart=always\nRestartSec=5s\n\n[Install]\nWantedBy=multi-user.target\nAlias=aws-gwlb<\/code><\/pre>\n\n\n\nCon i comandi che seguono abilitiamo il servizio (possiamo non avviarlo perch\u00e9 l’istanza su cui stiamo lavorando verr\u00e0 usata come template)<\/p>\n\n\n\n
systemctl daemon-reload\nsystemctl enable aws-gwlb<\/code><\/pre>\n\n\n\nPossiamo ora creare una AMI e iniziare a creare il Gateway Load Balancer.<\/p>\n\n\n\n
Configurazione del Load Balancer<\/h2>\n\n\n\n
Per prima cosa va creato un Target Group, cliccando sulla sezione “Target Group<\/strong>“<\/p>\n\n\n\nIl tipo di target sar\u00e0 “Instances”, il nome \u00e8 libero. Il protocollo deve essere “GENEVE”. Siccome nella unit systemd abbiamo specificato il flag “-p 80” useremo la porta 80 per l’health check<\/p>\n\n\n\n
<\/p>\n\n\n