Skip to content

Commit f1f1c3c

Browse files
authored
setup integration tests environment (#58)
* setup integration tests environment
1 parent b87d9b6 commit f1f1c3c

File tree

13 files changed

+434
-59
lines changed

13 files changed

+434
-59
lines changed

.github/workflows/go_test.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ jobs:
2020
- name: Check out code
2121
uses: actions/checkout@v3
2222

23-
- name: Build
23+
- name: Build listener
2424
run: |
2525
cd ./listener
2626
go build -a -installsuffix cgo -o bin/listener ./cmd/listener
2727
28+
- name: Build jwt-generator
29+
run: |
30+
cd ./listener
31+
go build -a -installsuffix cgo -o bin/jwt-generator ./cmd/jwt-generator
32+
33+
- name: Setup Integration Test Environment
34+
run: |
35+
docker compose -f docker-compose-ci.yml up --build -d
2836
- name: Test
2937
run: |
3038
cd ./listener

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.env
22
./listener/tmp/*
33
./listener/bin/*
4-
jwt
54
private.pem
65
public.pem

ci.env

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
MONGO_INITDB_ROOT_USERNAME=dappnode
2+
MONGO_INITDB_ROOT_PASSWORD=dappnode
3+
MONGO_DB_API_PORT=27017
4+
API_PORT=8080
5+
LOG_LEVEL=DEBUG
6+
MAX_ENTRIES_PER_BSON=30
7+
BEACON_NODE_URL_MAINNET=http://172.33.0.27:3500
8+
BEACON_NODE_URL_HOLESKY=http://172.33.0.27:3500
9+
BEACON_NODE_URL_GNOSIS=http://172.33.0.27:3500
10+
BEACON_NODE_URL_LUKSO=http://172.33.0.27:3500
11+
JWT_USERS_FILE=users.json.example

docker-compose-ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: "3.9"
2+
3+
networks:
4+
listener-net-ci:
5+
driver: bridge
6+
7+
volumes:
8+
mongo_data_ci: {}
9+
10+
services:
11+
listener:
12+
build:
13+
context: listener
14+
dockerfile: Dockerfile
15+
env_file:
16+
- ci.env
17+
environment:
18+
MONGO_DB_URI: "mongodb://dappnode:dappnode@mongo:27017"
19+
depends_on:
20+
- mongo
21+
container_name: listener
22+
restart: always
23+
volumes:
24+
- ./jwt:/app/jwt ## listener expects /app/jwt to exist, careful when changing this path
25+
networks:
26+
- listener-net-ci
27+
ports:
28+
- "8080:8080" # should be same as API_PORT of ci.env
29+
30+
mongo:
31+
build:
32+
context: mongo
33+
volumes:
34+
- mongo_data_ci:/data/db
35+
env_file:
36+
- ci.env
37+
command: ["mongod", "--config", "/etc/mongo/mongod.conf"]
38+
container_name: mongo
39+
restart: always
40+
networks:
41+
- listener-net-ci

listener/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Start from the latest golang base image
2-
FROM golang:1.22.0-alpine3.19 as builder
2+
FROM golang:1.22.3 as builder
33

44
# Set the Current Working Directory inside the container.
55
WORKDIR /app
@@ -14,7 +14,7 @@ COPY internal/ ./internal/
1414
COPY cmd/ ./cmd/
1515

1616
# Build the application, outputting the executable to /bin directory.
17-
RUN CGO_ENABLED=0 GOOS=linux go build -v -o /bin/listener ./cmd/listener/main.go
17+
RUN CGO_ENABLED=1 GOOS=linux go build -v -o /bin/listener ./cmd/listener/main.go
1818

1919
# Use a Docker multi-stage build to create a lean production image.
2020
# # build-essential required by dependency github.com/herumi/bls-eth-go-binary

listener/cmd/jwt-generator/main.go

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,36 @@ import (
44
"flag"
55
"fmt"
66
"os"
7-
"time"
87

8+
"github.com/dappnode/validator-monitoring/listener/internal/jwt"
99
"github.com/dappnode/validator-monitoring/listener/internal/logger"
10-
11-
"github.com/golang-jwt/jwt/v5"
1210
)
1311

1412
func main() {
15-
// Define flags for the command-line input
1613
privateKeyPath := flag.String("private-key", "", "Path to the RSA private key file (mandatory)")
1714
subject := flag.String("sub", "", "Subject claim for the JWT (optional)")
18-
expiration := flag.String("exp", "", "Expiration duration for the JWT in hours (optional, e.g., '24h' for 24 hours). If no value is provided, the generated token will not expire.")
15+
expiration := flag.String("exp", "", "Expiration duration for the JWT in hours (optional)")
1916
kid := flag.String("kid", "", "Key ID (kid) for the JWT (mandatory)")
20-
outputFilePath := flag.String("output", "token.jwt", "Output file path for the JWT. Defaults to ./token.jwt")
17+
outputFilePath := flag.String("output", "token.jwt", "Output file path for the JWT")
2118

2219
flag.Parse()
2320

24-
// Check for mandatory parameters
2521
if *kid == "" || *privateKeyPath == "" {
2622
logger.Fatal("Key ID (kid) and private key path must be provided")
2723
}
2824

29-
// Read the private key file
30-
privateKeyData, err := os.ReadFile(*privateKeyPath)
31-
if err != nil {
32-
logger.Fatal(fmt.Sprintf("Failed to read private key file: %v", err))
33-
}
34-
35-
// Parse the RSA private key
36-
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyData)
25+
tokenString, err := jwt.GenerateJWT(*kid, *privateKeyPath, *subject, *expiration)
3726
if err != nil {
38-
logger.Fatal(fmt.Sprintf("Failed to parse private key: %v", err))
39-
}
40-
41-
// Prepare the claims for the JWT. These are optional
42-
claims := jwt.MapClaims{}
43-
if *subject != "" {
44-
claims["sub"] = *subject
45-
}
46-
if *expiration != "" {
47-
duration, err := time.ParseDuration(*expiration)
48-
if err != nil {
49-
logger.Fatal(fmt.Sprintf("Failed to parse expiration duration: %v", err))
50-
}
51-
claims["exp"] = time.Now().Add(duration).Unix()
27+
logger.Fatal(fmt.Sprintf("Error generating JWT: %v", err))
5228
}
5329

54-
// Create a new token object, specifying signing method and claims
55-
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
56-
57-
// Set the key ID (kid) in the token header
58-
token.Header["kid"] = *kid
59-
60-
// Sign the token with the private key
61-
tokenString, err := token.SignedString(privateKey)
62-
if err != nil {
63-
logger.Fatal(fmt.Sprintf("Failed to sign token: %v", err))
64-
}
65-
66-
// Output the token to the console
6730
fmt.Println("JWT generated successfully:")
6831
fmt.Println(tokenString)
6932

70-
// Save the token to a file
7133
err = os.WriteFile(*outputFilePath, []byte(tokenString), 0644)
7234
if err != nil {
7335
logger.Fatal(fmt.Sprintf("Failed to write the JWT to file: %v", err))
7436
}
37+
7538
fmt.Println("JWT saved to file:", *outputFilePath)
7639
}

listener/go.mod

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/dappnode/validator-monitoring/listener
33
go 1.22.0
44

55
require (
6+
github.com/gavv/httpexpect/v2 v2.16.0
67
github.com/golang-jwt/jwt/v5 v5.2.1
78
github.com/gorilla/mux v1.8.1
89
github.com/herumi/bls-eth-go-binary v1.35.0
@@ -11,14 +12,46 @@ require (
1112
)
1213

1314
require (
15+
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
16+
github.com/ajg/form v1.5.1 // indirect
17+
github.com/andybalholm/brotli v1.0.4 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/fatih/color v1.15.0 // indirect
20+
github.com/fatih/structs v1.1.0 // indirect
21+
github.com/gobwas/glob v0.2.3 // indirect
1422
github.com/golang/snappy v0.0.1 // indirect
15-
github.com/klauspost/compress v1.13.6 // indirect
23+
github.com/google/go-querystring v1.1.0 // indirect
24+
github.com/gorilla/websocket v1.4.2 // indirect
25+
github.com/hpcloud/tail v1.0.0 // indirect
26+
github.com/imkira/go-interpol v1.1.0 // indirect
27+
github.com/klauspost/compress v1.15.0 // indirect
28+
github.com/mattn/go-colorable v0.1.13 // indirect
29+
github.com/mattn/go-isatty v0.0.18 // indirect
30+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
1631
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
32+
github.com/pmezard/go-difflib v1.0.0 // indirect
33+
github.com/sanity-io/litter v1.5.5 // indirect
34+
github.com/sergi/go-diff v1.0.0 // indirect
35+
github.com/stretchr/testify v1.5.0 // indirect
36+
github.com/valyala/bytebufferpool v1.0.0 // indirect
37+
github.com/valyala/fasthttp v1.34.0 // indirect
1738
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
1839
github.com/xdg-go/scram v1.1.2 // indirect
1940
github.com/xdg-go/stringprep v1.0.4 // indirect
41+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
42+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
43+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
44+
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
2045
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
46+
github.com/yudai/gojsondiff v1.0.0 // indirect
47+
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
2148
golang.org/x/crypto v0.17.0 // indirect
49+
golang.org/x/net v0.10.0 // indirect
2250
golang.org/x/sync v0.1.0 // indirect
51+
golang.org/x/sys v0.15.0 // indirect
2352
golang.org/x/text v0.14.0 // indirect
53+
gopkg.in/fsnotify.v1 v1.4.7 // indirect
54+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
55+
gopkg.in/yaml.v2 v2.4.0 // indirect
56+
moul.io/http2curl/v2 v2.3.0 // indirect
2457
)

0 commit comments

Comments
 (0)