Skip to content

Commit

Permalink
Merge pull request #1320 from threefoldtech/update-registrar-node
Browse files Browse the repository at this point in the history
Create registrar service
  • Loading branch information
xmonader authored Feb 11, 2025
2 parents e1e47d9 + abcb34d commit 6a7aa3f
Show file tree
Hide file tree
Showing 20 changed files with 3,981 additions and 1,258 deletions.
6 changes: 3 additions & 3 deletions node-registrar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ run:
go run cmds/registrar.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --domain localhost --server-port 8080

postgres:
docker run --name postgres -e POSTGRES_USER=postgres POSTGRES_PASSWORD=password POSTGRES_DB=postgres -p 5432:5432 -d postgres
docker run --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=postgres -p 5432:5432 -d postgres

stop-postgres:
docker stop postgres && docker rm postgres
Expand All @@ -12,8 +12,8 @@ build: ## Bulil the server

server-start:
@go run cmds/registrar.go \
--server-port :8080 \
--log-level debug \
--server-port 8080 \
--debug \
--domain localhost \
--sql-log-level 4 \
--postgres-host localhost \
Expand Down
133 changes: 101 additions & 32 deletions node-registrar/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@

## Project Documentation
# Node Registrar Service

### Overview
This project provides an API for registring zos nodes using the Go Gin framework and PostgreSQL database.
## Overview

This project provides an API for registring zos nodes using the Go Gin framework and PostgreSQL database.
The API supports operations like registring, listing, and updating farms and nodes, as well as reporting uptime and consumption data for nodes.

### Endpoint Descriptions
## Features

- **Farm Management**
- Create/update farms with owner authorization
- List farms with filtering/pagination
- Automatic twin ID association via authentication

- **Node Registration**
- Create/update nodes with owner authorization
- Uptime reporting
- Node metadata management (location, interfaces, specs)

- **Account System**
- ED25519/SR25519 authentication
- Relay management for RMB communication

- **Security**
- Challenge-response authentication middleware
- Ownership verification for mutations
- Timestamp replay protection

## Endpoint Descriptions

### Farms Endpoints

#### Farms Endpoints
1. **GET /farms/** - List all farms, or use FarmFilter to list specific set of farms.
2. **GET /farms/:farm_id** - Get a specific farm by ID.
3. **POST /farms/** - Create a new farm.
4. **PATCH /farms/** - Update an existing farm.

#### Nodes Endpoints
### Nodes Endpoints

1. **GET /nodes/** - List all nodes, or use NodeFilter to list specific set of nodes.
2. **GET /nodes/:node_id** - Get a specific node by ID.
3. **POST /nodes/** - Register a new node.
Expand All @@ -23,52 +47,97 @@ The API supports operations like registring, listing, and updating farms and nod
## Setup Instructions

1. **Start PostgreSQL:**

```bash
make postgres
```

2. **Run the Server:**

```bash
make run
```

3. **Stop PostgreSQL:**

```bash
make stop-postgres
```

### Swagger Documentation
## Swagger Documentation

Once the server is running, Swagger documentation can be accessed at:
```

```bash
http://<domain>:<port>/swagger/index.html
```

Replace `<domain>` and `<port>` with the appropriate values.

### How to Use the Server
## How to Use the Server

1. Use a tool like Postman or cURL to interact with the API.
2. Refer to the Swagger documentation for detailed information about request parameters and response structures.

### How to run the server with docker
1. use the docker file to build the docker image
```
docker build -t registrar:latest .
```
## How to run the server with docker

1. use the docker file to build the docker image

```bash
docker build -t registrar:latest .
```

2. run the image

```bash
docker run -d \
-p 8080:8080 \
--name registrar \
registrar:latest \
./server
--postgres-host=<your-postgres-host> \
--postgres-port=5432 \
--postgres-db=<your-db-name> \
--postgres-user=<your-db-user> \
--postgres-password=<your-db-password> \
--ssl-mode=disable \
--sql-log-level=2 \
--max-open-conn=10 \
--max-idle-conn=5 \
--server-port=8080 \
--<domain=your-domain> \
--network=main\
--admin_twin_id=1
--debug
```

## Authentication

Requests requiring authorization must include:

```http
X-Auth: Base64(Challenge):Base64(Signature)
```
docker run -d \
-p 8080:8080 \
--name registrar \
registrar:latest \
./server
--postgres-host=<your-postgres-host> \
--postgres-port=5432 \
--postgres-db=<your-db-name> \
--postgres-user=<your-db-user> \
--postgres-password=<your-db-password> \
--ssl-mode=disable \
--sql-log-level=2 \
--max-open-conn=10 \
--max-idle-conn=5 \
--server-port=8080 \
--<domain=your-domain> \
--network=main\
--debug

**Challenge Format:**
`<unix_timestamp>:<twin_id>`

**Signature:**
ED25519/SR25519 signature of challenge bytes

## Database Schema

Key Tables:

- `accounts` - Authentication credentials and relay configs
- `farms` - Farm metadata with owner relationship
- `nodes` - Node hardware/resources specification
- `uptime_reports` - Historical node availability data

## Development

### Generating Swagger Docs

```bash
swag init -g pkg/server/handlers.go --output docs --parseDependency --parseDepth 2
```
19 changes: 11 additions & 8 deletions node-registrar/cmds/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (

type flags struct {
db.Config
debug bool
version bool
domain string
serverPort uint
network string
debug bool
version bool
domain string
serverPort uint
network string
adminTwinID uint64
}

var (
Expand Down Expand Up @@ -55,6 +56,7 @@ func Run() error {
flag.UintVar(&f.serverPort, "server-port", 8080, "server port")
flag.StringVar(&f.domain, "domain", "", "domain on which the server will be served")
flag.StringVar(&f.network, "network", "dev", "the registrar network")
flag.Uint64Var(&f.adminTwinID, "admin-twin-id", 0, "admin twin ID")

flag.Parse()
f.SqlLogLevel = logger.LogLevel(sqlLogLevel)
Expand Down Expand Up @@ -86,7 +88,7 @@ func Run() error {
}
}()

s, err := server.NewServer(db, f.network)
s, err := server.NewServer(db, f.network, f.adminTwinID)
if err != nil {
return errors.Wrap(err, "failed to start gin server")
}
Expand All @@ -95,6 +97,7 @@ func Run() error {
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)

log.Info().Msg("server is running on port :8080")

err = s.Run(quit, fmt.Sprintf("%s:%d", f.domain, f.serverPort))
if err != nil {
return errors.Wrap(err, "failed to run gin server")
Expand All @@ -104,15 +107,15 @@ func Run() error {
}

func (f flags) validate() error {
if f.serverPort < 1 && f.serverPort > 65535 {
if f.serverPort < 1 || f.serverPort > 65535 {
return errors.Errorf("invalid port %d, server port should be in the valid port range 1–65535", f.serverPort)
}

if strings.TrimSpace(f.domain) == "" {
return errors.New("invalid domain name, domain name should not be empty")
}
if _, err := net.LookupHost(f.domain); err != nil {
return errors.Wrapf(err, "invalid domain %s", f.PostgresHost)
return errors.Wrapf(err, "invalid domain %s", f.domain)
}

return f.Config.Validate()
Expand Down
Loading

0 comments on commit 6a7aa3f

Please sign in to comment.