Skip to content

Commit 92c11e3

Browse files
committed
feat(sgx): add sidecar dockerfile
1 parent 77fb2d1 commit 92c11e3

File tree

17 files changed

+942
-4
lines changed

17 files changed

+942
-4
lines changed

sgx_network_simulation/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM golang:1.16 AS go
2+
3+
RUN apt-get update && \
4+
apt-get install -y make g++ libgmp-dev libglib2.0-dev libssl-dev && \
5+
apt-get install -y protobuf-compiler && \
6+
apt-get clean
7+
8+
WORKDIR /app
9+
COPY tools/tcp_grpc_proxy ./
10+
RUN make build
11+
12+
FROM python:3.6.8
13+
14+
RUN echo "deb http://archive.debian.org/debian stretch main contrib non-free" > /etc/apt/sources.list
15+
16+
RUN apt-get update && \
17+
apt-get install -y curl vim make nginx && \
18+
apt-get clean
19+
20+
# upgrade nginx
21+
RUN echo "deb http://nginx.org/packages/mainline/debian/ stretch nginx deb-src http://nginx.org/packages/mainline/debian/ stretch nginx" > /etc/apt/sources.list.d/nginx.list
22+
RUN wget -qO - https://nginx.org/keys/nginx_signing.key | apt-key add -
23+
RUN apt update && \
24+
apt remove nginx-common -y && \
25+
apt install nginx
26+
27+
COPY sgx_network_simulation/ /app/
28+
WORKDIR /app
29+
COPY --from=go /app/tcp2grpc ./
30+
COPY --from=go /app/grpc2tcp ./
31+
RUN pip3 install -r requirements.txt && make protobuf
32+
33+
ENTRYPOINT ["bash", "docker_entrypoint.sh"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Forwards all traffic to nginx controller
2+
server {
3+
listen 32102 http2;
4+
5+
# No limits
6+
client_max_body_size 0;
7+
grpc_read_timeout 3600s;
8+
grpc_send_timeout 3600s;
9+
client_body_timeout 3600s;
10+
# grpc_socket_keepalive is recommended but not required
11+
# grpc_socket_keepalive is supported after nginx 1.15.6
12+
grpc_socket_keepalive on;
13+
14+
grpc_set_header Authority fl-bytedance-client-auth.com;
15+
grpc_set_header Host fl-bytedance-client-auth.com;
16+
grpc_set_header X-Host sgx-test.fl-cmcc.com;
17+
18+
location / {
19+
# Redirects to nginx controller
20+
grpc_pass grpc://fedlearner-stack-ingress-nginx-controller.default.svc:80;
21+
}
22+
}

sgx_network_simulation/sidecar.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
FILE_PATH="/pod-data/listen_port"
5+
while [ ! -s "$FILE_PATH" ]; do
6+
echo "wait for $FILE_PATH ..."
7+
sleep 1
8+
done
9+
WORKER_LISTEN_PORT=$(cat "$FILE_PATH")
10+
11+
echo "# Forwards all traffic to nginx controller
12+
server {
13+
listen 32102 http2;
14+
15+
# No limits
16+
client_max_body_size 0;
17+
grpc_read_timeout 3600s;
18+
grpc_send_timeout 3600s;
19+
client_body_timeout 3600s;
20+
# grpc_socket_keepalive is recommended but not required
21+
# grpc_socket_keepalive is supported after nginx 1.15.6
22+
grpc_socket_keepalive on;
23+
24+
grpc_set_header Authority ${EGRESS_HOST};
25+
grpc_set_header Host ${EGRESS_HOST};
26+
grpc_set_header X-Host ${SERVICE_ID}.${EGRESS_DOMAIN};
27+
28+
location / {
29+
# Redirects to nginx controller
30+
grpc_pass grpc://fedlearner-stack-ingress-nginx-controller.default.svc:80;
31+
}
32+
}
33+
" > nginx/sidecar.conf
34+
35+
if [ -z "$PORT0" ]; then
36+
PORT0=32001
37+
fi
38+
39+
if [ -z "$PORT2" ]; then
40+
PORT2=32102
41+
fi
42+
43+
sed -i "s/listen [0-9]* http2;/listen $PORT2 http2;/" nginx/sidecar.conf
44+
45+
cp nginx/sidecar.conf /etc/nginx/conf.d/
46+
service nginx restart
47+
48+
# Server sidecar: grpc to tcp, 5001 is the server port of main container
49+
echo "Starting server sidecar"
50+
./grpc2tcp --grpc_server_port=$PORT0 \
51+
--target_tcp_address="localhost:$WORKER_LISTEN_PORT" &
52+
53+
echo "Starting client sidecar"
54+
./tcp2grpc --tcp_server_port="$PROXY_LOCAL_PORT" \
55+
--target_grpc_address="localhost:$PORT2" &
56+
57+
echo "===========Sidecar started!!============="
58+
59+
while true
60+
do
61+
if [[ -f "/pod-data/main-terminated" ]]
62+
then
63+
exit 0
64+
fi
65+
sleep 5
66+
done

tools/tcp_grpc_proxy/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
install:
2+
go get tcp_grpc_proxy
3+
go mod download
4+
5+
protobuf: install
6+
go install google.golang.org/protobuf/cmd/[email protected]
7+
go install google.golang.org/grpc/cmd/[email protected]
8+
PATH="${PATH}:$(shell go env GOPATH)/bin" \
9+
protoc -I=proto --go_out=. --go-grpc_out=. proto/*.proto
10+
11+
build: protobuf
12+
go build -o tcp2grpc cmd/tcp2grpc/main.go
13+
go build -o grpc2tcp cmd/grpc2tcp/main.go

tools/tcp_grpc_proxy/cmd/grpc2tcp/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fedlearner.net/tools/tcp_grpc_proxy/pkg/proxy"
54
"flag"
65
"fmt"
6+
"tcp_grpc_proxy/proxy"
77
)
88

99
func main() {
@@ -14,6 +14,6 @@ func main() {
1414
flag.Parse()
1515
grpcServerAddress := fmt.Sprintf("0.0.0.0:%d", grpcServerPort)
1616

17-
grpc2tcpServer := proxy.NewGrpc2TcpServer(grpcServerAddress, targetTCPAddress)
17+
grpc2tcpServer := proxy.NewGrpc2TCPServer(grpcServerAddress, targetTCPAddress)
1818
grpc2tcpServer.Run()
1919
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"os"
7+
"time"
8+
9+
"tcp_grpc_proxy/proto"
10+
11+
"github.com/sirupsen/logrus"
12+
"google.golang.org/grpc"
13+
)
14+
15+
func main() {
16+
// Set up a connection to the server.
17+
grpcServer := "127.0.0.1:7766"
18+
conn, err := grpc.Dial(grpcServer, grpc.WithInsecure())
19+
if err != nil {
20+
logrus.Fatalf("did not connect: %v", err)
21+
}
22+
defer conn.Close()
23+
tsc := proto.NewTunnelServiceClient(conn)
24+
25+
tc, err := tsc.Tunnel(context.Background())
26+
if err != nil {
27+
logrus.Fatalln(err)
28+
}
29+
30+
sendPacket := func(data []byte) error {
31+
return tc.Send(&proto.Chunk{Data: data})
32+
}
33+
34+
go func() {
35+
for {
36+
chunk, err := tc.Recv()
37+
if err != nil {
38+
logrus.Println("Recv terminated:", err)
39+
os.Exit(0)
40+
}
41+
logrus.Println(string(chunk.Data))
42+
}
43+
44+
}()
45+
46+
for {
47+
time.Sleep(time.Duration(2) * time.Second)
48+
buf := bytes.NewBufferString("************Hello World**********").Bytes()
49+
sendPacket(buf)
50+
}
51+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"tcp_grpc_proxy/grpc2tcp"
5+
)
6+
7+
func main() {
8+
grpcServerAddress := "0.0.0.0:7766"
9+
targetTCPAddress := "127.0.0.1:17766"
10+
grpc2tcp.RunServer(grpcServerAddress, targetTCPAddress)
11+
}
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
package main
22

33
import (
4-
"fedlearner.net/tools/tcp_grpc_proxy/pkg/proxy"
54
"flag"
65
"fmt"
6+
"io"
7+
"net"
8+
"os"
9+
"tcp_grpc_proxy/proxy"
710
)
811

12+
func test() {
13+
client, err := net.Dial("tcp", "127.0.0.1:17767")
14+
if err != nil {
15+
fmt.Println("err:", err)
16+
return
17+
}
18+
defer client.Close()
19+
20+
go func() {
21+
input := make([]byte, 1024)
22+
for {
23+
n, err := os.Stdin.Read(input)
24+
if err != nil {
25+
fmt.Println("input err:", err)
26+
continue
27+
}
28+
client.Write([]byte(input[:n]))
29+
}
30+
}()
31+
32+
buf := make([]byte, 1024)
33+
for {
34+
n, err := client.Read(buf)
35+
if err != nil {
36+
if err == io.EOF {
37+
return
38+
}
39+
fmt.Println("read err:", err)
40+
continue
41+
}
42+
fmt.Println(string(buf[:n]))
43+
44+
}
45+
}
46+
947
func main() {
1048
var tcpServerPort int
1149
var targetGrpcAddress string
@@ -14,6 +52,6 @@ func main() {
1452
flag.Parse()
1553
tcpServerAddress := fmt.Sprintf("0.0.0.0:%d", tcpServerPort)
1654

17-
tcp2grpcServer := proxy.NewTcp2GrpcServer(tcpServerAddress, targetGrpcAddress)
55+
tcp2grpcServer := proxy.NewTCP2GrpcServer(tcpServerAddress, targetGrpcAddress)
1856
tcp2grpcServer.Run()
1957
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"net"
6+
"time"
7+
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func main() {
12+
var tcpServerAddress string
13+
flag.StringVar(&tcpServerAddress, "tcp_server_address", "127.0.0.1:17767",
14+
"TCP server address which the client connects to.")
15+
16+
conn, err := net.Dial("tcp", tcpServerAddress)
17+
if err != nil {
18+
logrus.Fatalf("Dail to tcp target %s error: %v", tcpServerAddress, err)
19+
}
20+
logrus.Infoln("Connected to", tcpServerAddress)
21+
// Makes sure the connection gets closed
22+
defer conn.Close()
23+
defer logrus.Infoln("Connection closed to ", tcpServerAddress)
24+
25+
for {
26+
conn.Write([]byte("hello world"))
27+
logrus.Infof("Sent 'hello world' to server %s", tcpServerAddress)
28+
29+
tcpData := make([]byte, 64*1024)
30+
_, err := conn.Read(tcpData)
31+
if err != nil {
32+
logrus.Fatalln("Read from tcp error: ", err)
33+
}
34+
logrus.Infof("Received '%s' from server", string(tcpData))
35+
36+
time.Sleep(time.Duration(5) * time.Second)
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"net"
7+
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func handleTCPConn(conn net.Conn) {
12+
for {
13+
tcpData := make([]byte, 64*1024)
14+
bytesRead, err := conn.Read(tcpData)
15+
if err != nil {
16+
logrus.Fatalln("Read from tcp error: ", err)
17+
}
18+
logrus.Infof("TCP server got %d bytes", bytesRead)
19+
conn.Write([]byte("This is a string from TCP server"))
20+
}
21+
}
22+
23+
func main() {
24+
var tcpServerPort int
25+
flag.IntVar(&tcpServerPort, "tcp_server_port", 17766, "TCP server port")
26+
flag.Parse()
27+
tcpServerAddress := fmt.Sprintf("0.0.0.0:%d", tcpServerPort)
28+
29+
listener, err := net.Listen("tcp", tcpServerAddress)
30+
if err != nil {
31+
logrus.Fatalln("Listen TCP error: ", err)
32+
}
33+
defer listener.Close()
34+
logrus.Infoln("Run TCPServer at ", tcpServerAddress)
35+
36+
for {
37+
conn, err := listener.Accept()
38+
if err != nil {
39+
logrus.Errorln("TCP listener error:", err)
40+
continue
41+
}
42+
43+
logrus.Infoln("Got tcp connection")
44+
go handleTCPConn(conn)
45+
}
46+
}

0 commit comments

Comments
 (0)