Skip to content

Commit

Permalink
setup integration tests environment (#58)
Browse files Browse the repository at this point in the history
* setup integration tests environment
  • Loading branch information
Marketen authored May 31, 2024
1 parent b87d9b6 commit f1f1c3c
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 59 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ jobs:
- name: Check out code
uses: actions/checkout@v3

- name: Build
- name: Build listener
run: |
cd ./listener
go build -a -installsuffix cgo -o bin/listener ./cmd/listener
- name: Build jwt-generator
run: |
cd ./listener
go build -a -installsuffix cgo -o bin/jwt-generator ./cmd/jwt-generator
- name: Setup Integration Test Environment
run: |
docker compose -f docker-compose-ci.yml up --build -d
- name: Test
run: |
cd ./listener
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.env
./listener/tmp/*
./listener/bin/*
jwt
private.pem
public.pem
11 changes: 11 additions & 0 deletions ci.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
MONGO_INITDB_ROOT_USERNAME=dappnode
MONGO_INITDB_ROOT_PASSWORD=dappnode
MONGO_DB_API_PORT=27017
API_PORT=8080
LOG_LEVEL=DEBUG
MAX_ENTRIES_PER_BSON=30
BEACON_NODE_URL_MAINNET=http://172.33.0.27:3500
BEACON_NODE_URL_HOLESKY=http://172.33.0.27:3500
BEACON_NODE_URL_GNOSIS=http://172.33.0.27:3500
BEACON_NODE_URL_LUKSO=http://172.33.0.27:3500
JWT_USERS_FILE=users.json.example
41 changes: 41 additions & 0 deletions docker-compose-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: "3.9"

networks:
listener-net-ci:
driver: bridge

volumes:
mongo_data_ci: {}

services:
listener:
build:
context: listener
dockerfile: Dockerfile
env_file:
- ci.env
environment:
MONGO_DB_URI: "mongodb://dappnode:dappnode@mongo:27017"
depends_on:
- mongo
container_name: listener
restart: always
volumes:
- ./jwt:/app/jwt ## listener expects /app/jwt to exist, careful when changing this path
networks:
- listener-net-ci
ports:
- "8080:8080" # should be same as API_PORT of ci.env

mongo:
build:
context: mongo
volumes:
- mongo_data_ci:/data/db
env_file:
- ci.env
command: ["mongod", "--config", "/etc/mongo/mongod.conf"]
container_name: mongo
restart: always
networks:
- listener-net-ci
4 changes: 2 additions & 2 deletions listener/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Start from the latest golang base image
FROM golang:1.22.0-alpine3.19 as builder
FROM golang:1.22.3 as builder

# Set the Current Working Directory inside the container.
WORKDIR /app
Expand All @@ -14,7 +14,7 @@ COPY internal/ ./internal/
COPY cmd/ ./cmd/

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

# Use a Docker multi-stage build to create a lean production image.
# # build-essential required by dependency github.com/herumi/bls-eth-go-binary
Expand Down
49 changes: 6 additions & 43 deletions listener/cmd/jwt-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,36 @@ import (
"flag"
"fmt"
"os"
"time"

"github.com/dappnode/validator-monitoring/listener/internal/jwt"
"github.com/dappnode/validator-monitoring/listener/internal/logger"

"github.com/golang-jwt/jwt/v5"
)

func main() {
// Define flags for the command-line input
privateKeyPath := flag.String("private-key", "", "Path to the RSA private key file (mandatory)")
subject := flag.String("sub", "", "Subject claim for the JWT (optional)")
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.")
expiration := flag.String("exp", "", "Expiration duration for the JWT in hours (optional)")
kid := flag.String("kid", "", "Key ID (kid) for the JWT (mandatory)")
outputFilePath := flag.String("output", "token.jwt", "Output file path for the JWT. Defaults to ./token.jwt")
outputFilePath := flag.String("output", "token.jwt", "Output file path for the JWT")

flag.Parse()

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

// Read the private key file
privateKeyData, err := os.ReadFile(*privateKeyPath)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to read private key file: %v", err))
}

// Parse the RSA private key
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyData)
tokenString, err := jwt.GenerateJWT(*kid, *privateKeyPath, *subject, *expiration)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to parse private key: %v", err))
}

// Prepare the claims for the JWT. These are optional
claims := jwt.MapClaims{}
if *subject != "" {
claims["sub"] = *subject
}
if *expiration != "" {
duration, err := time.ParseDuration(*expiration)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to parse expiration duration: %v", err))
}
claims["exp"] = time.Now().Add(duration).Unix()
logger.Fatal(fmt.Sprintf("Error generating JWT: %v", err))
}

// Create a new token object, specifying signing method and claims
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)

// Set the key ID (kid) in the token header
token.Header["kid"] = *kid

// Sign the token with the private key
tokenString, err := token.SignedString(privateKey)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to sign token: %v", err))
}

// Output the token to the console
fmt.Println("JWT generated successfully:")
fmt.Println(tokenString)

// Save the token to a file
err = os.WriteFile(*outputFilePath, []byte(tokenString), 0644)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to write the JWT to file: %v", err))
}

fmt.Println("JWT saved to file:", *outputFilePath)
}
35 changes: 34 additions & 1 deletion listener/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dappnode/validator-monitoring/listener
go 1.22.0

require (
github.com/gavv/httpexpect/v2 v2.16.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gorilla/mux v1.8.1
github.com/herumi/bls-eth-go-binary v1.35.0
Expand All @@ -11,14 +12,46 @@ require (
)

require (
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/stretchr/testify v1.5.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.34.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
moul.io/http2curl/v2 v2.3.0 // indirect
)
Loading

0 comments on commit f1f1c3c

Please sign in to comment.