Skip to content

Create registrar service #1320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
86f5b0d
update registrar node
Eslam-Nawara Jan 23, 2025
29fcee9
add model and handlers for creating and getting accounts
sameh-farouk Jan 26, 2025
e8aa001
update swagger doc
sameh-farouk Jan 26, 2025
f7769dd
support track uptime reports historical data
sameh-farouk Jan 27, 2025
1eee850
Add support for storing and updating twin's Relays and RMB_PK
sameh-farouk Jan 27, 2025
f4a72a0
Update models' Constraints
sameh-farouk Jan 27, 2025
8b720e2
Add AuthMiddleware
sameh-farouk Jan 28, 2025
3d09942
use nodeID param in uptime handler instead of having it in the request
sameh-farouk Jan 28, 2025
59bcb04
removing FarmFreeIps for now
sameh-farouk Jan 28, 2025
f8cdaef
fix RegisterNode
sameh-farouk Jan 28, 2025
1bc47bf
revert to go 1.21.0
sameh-farouk Jan 28, 2025
63b1f50
update comments
sameh-farouk Jan 28, 2025
3f049d5
"add endpoint to update node"
sameh-farouk Jan 28, 2025
353f913
Switch from url param to query param in getAccount route/handler and …
sameh-farouk Jan 28, 2025
e25bac2
add zos version endpoints to set and get zOS version
sameh-farouk Jan 28, 2025
9a6fbe8
update swagger doc for zos endpoints
sameh-farouk Jan 29, 2025
90baab3
generate swagger docs
sameh-farouk Jan 29, 2025
b8f9213
update docs
sameh-farouk Jan 29, 2025
3627353
fix swagger docs path
sameh-farouk Jan 29, 2025
a5197b8
fix missing API version from routes url and missing auth haeder from …
sameh-farouk Jan 29, 2025
4992bf1
add 401 response swagger documentation
sameh-farouk Jan 29, 2025
e3bc75a
Uses GORM's serializer instead of using custom Scan/Value implementation
sameh-farouk Jan 29, 2025
7f69d6f
update json names in node registrar
Eslam-Nawara Feb 3, 2025
9bbc96c
return account from get accout
Eslam-Nawara Feb 4, 2025
bf2f7a8
remove print filter
Eslam-Nawara Feb 4, 2025
e401f1a
update updateNodeRequest rules
Eslam-Nawara Feb 4, 2025
b9db013
use Node struct to update instead on map
Omarabdul3ziz Feb 5, 2025
7815aa2
fix inconsistencies in server returns
Eslam-Nawara Feb 5, 2025
54484cd
update swagger docs
Eslam-Nawara Feb 5, 2025
cf8922b
fix set zos version
sameh-farouk Feb 10, 2025
8420ab4
update swagger files
sameh-farouk Feb 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions go.work.sum

Large diffs are not rendered by default.

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
Loading