Skip to content

Commit 6a7aa3f

Browse files
authored
Merge pull request #1320 from threefoldtech/update-registrar-node
Create registrar service
2 parents e1e47d9 + abcb34d commit 6a7aa3f

File tree

20 files changed

+3981
-1258
lines changed

20 files changed

+3981
-1258
lines changed

node-registrar/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ run:
22
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
33

44
postgres:
5-
docker run --name postgres -e POSTGRES_USER=postgres POSTGRES_PASSWORD=password POSTGRES_DB=postgres -p 5432:5432 -d postgres
5+
docker run --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=postgres -p 5432:5432 -d postgres
66

77
stop-postgres:
88
docker stop postgres && docker rm postgres
@@ -12,8 +12,8 @@ build: ## Bulil the server
1212

1313
server-start:
1414
@go run cmds/registrar.go \
15-
--server-port :8080 \
16-
--log-level debug \
15+
--server-port 8080 \
16+
--debug \
1717
--domain localhost \
1818
--sql-log-level 4 \
1919
--postgres-host localhost \

node-registrar/README.md

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11

2-
## Project Documentation
2+
# Node Registrar Service
33

4-
### Overview
5-
This project provides an API for registring zos nodes using the Go Gin framework and PostgreSQL database.
4+
## Overview
5+
6+
This project provides an API for registring zos nodes using the Go Gin framework and PostgreSQL database.
67
The API supports operations like registring, listing, and updating farms and nodes, as well as reporting uptime and consumption data for nodes.
78

8-
### Endpoint Descriptions
9+
## Features
10+
11+
- **Farm Management**
12+
- Create/update farms with owner authorization
13+
- List farms with filtering/pagination
14+
- Automatic twin ID association via authentication
15+
16+
- **Node Registration**
17+
- Create/update nodes with owner authorization
18+
- Uptime reporting
19+
- Node metadata management (location, interfaces, specs)
20+
21+
- **Account System**
22+
- ED25519/SR25519 authentication
23+
- Relay management for RMB communication
24+
25+
- **Security**
26+
- Challenge-response authentication middleware
27+
- Ownership verification for mutations
28+
- Timestamp replay protection
29+
30+
## Endpoint Descriptions
31+
32+
### Farms Endpoints
933

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

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

2549
1. **Start PostgreSQL:**
50+
2651
```bash
2752
make postgres
2853
```
54+
2955
2. **Run the Server:**
56+
3057
```bash
3158
make run
3259
```
60+
3361
3. **Stop PostgreSQL:**
62+
3463
```bash
3564
make stop-postgres
3665
```
3766

38-
### Swagger Documentation
67+
## Swagger Documentation
68+
3969
Once the server is running, Swagger documentation can be accessed at:
40-
```
70+
71+
```bash
4172
http://<domain>:<port>/swagger/index.html
4273
```
74+
4375
Replace `<domain>` and `<port>` with the appropriate values.
4476

45-
### How to Use the Server
77+
## How to Use the Server
78+
4679
1. Use a tool like Postman or cURL to interact with the API.
4780
2. Refer to the Swagger documentation for detailed information about request parameters and response structures.
4881

49-
### How to run the server with docker
50-
1. use the docker file to build the docker image
51-
```
52-
docker build -t registrar:latest .
53-
```
82+
## How to run the server with docker
83+
84+
1. use the docker file to build the docker image
85+
86+
```bash
87+
docker build -t registrar:latest .
88+
```
89+
5490
2. run the image
91+
92+
```bash
93+
docker run -d \
94+
-p 8080:8080 \
95+
--name registrar \
96+
registrar:latest \
97+
./server
98+
--postgres-host=<your-postgres-host> \
99+
--postgres-port=5432 \
100+
--postgres-db=<your-db-name> \
101+
--postgres-user=<your-db-user> \
102+
--postgres-password=<your-db-password> \
103+
--ssl-mode=disable \
104+
--sql-log-level=2 \
105+
--max-open-conn=10 \
106+
--max-idle-conn=5 \
107+
--server-port=8080 \
108+
--<domain=your-domain> \
109+
--network=main\
110+
--admin_twin_id=1
111+
--debug
112+
```
113+
114+
## Authentication
115+
116+
Requests requiring authorization must include:
117+
118+
```http
119+
X-Auth: Base64(Challenge):Base64(Signature)
55120
```
56-
docker run -d \
57-
-p 8080:8080 \
58-
--name registrar \
59-
registrar:latest \
60-
./server
61-
--postgres-host=<your-postgres-host> \
62-
--postgres-port=5432 \
63-
--postgres-db=<your-db-name> \
64-
--postgres-user=<your-db-user> \
65-
--postgres-password=<your-db-password> \
66-
--ssl-mode=disable \
67-
--sql-log-level=2 \
68-
--max-open-conn=10 \
69-
--max-idle-conn=5 \
70-
--server-port=8080 \
71-
--<domain=your-domain> \
72-
--network=main\
73-
--debug
121+
122+
**Challenge Format:**
123+
`<unix_timestamp>:<twin_id>`
124+
125+
**Signature:**
126+
ED25519/SR25519 signature of challenge bytes
127+
128+
## Database Schema
129+
130+
Key Tables:
131+
132+
- `accounts` - Authentication credentials and relay configs
133+
- `farms` - Farm metadata with owner relationship
134+
- `nodes` - Node hardware/resources specification
135+
- `uptime_reports` - Historical node availability data
136+
137+
## Development
138+
139+
### Generating Swagger Docs
140+
141+
```bash
142+
swag init -g pkg/server/handlers.go --output docs --parseDependency --parseDepth 2
74143
```

node-registrar/cmds/registrar.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919

2020
type flags struct {
2121
db.Config
22-
debug bool
23-
version bool
24-
domain string
25-
serverPort uint
26-
network string
22+
debug bool
23+
version bool
24+
domain string
25+
serverPort uint
26+
network string
27+
adminTwinID uint64
2728
}
2829

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

5961
flag.Parse()
6062
f.SqlLogLevel = logger.LogLevel(sqlLogLevel)
@@ -86,7 +88,7 @@ func Run() error {
8688
}
8789
}()
8890

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

9799
log.Info().Msg("server is running on port :8080")
100+
98101
err = s.Run(quit, fmt.Sprintf("%s:%d", f.domain, f.serverPort))
99102
if err != nil {
100103
return errors.Wrap(err, "failed to run gin server")
@@ -104,15 +107,15 @@ func Run() error {
104107
}
105108

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

111114
if strings.TrimSpace(f.domain) == "" {
112115
return errors.New("invalid domain name, domain name should not be empty")
113116
}
114117
if _, err := net.LookupHost(f.domain); err != nil {
115-
return errors.Wrapf(err, "invalid domain %s", f.PostgresHost)
118+
return errors.Wrapf(err, "invalid domain %s", f.domain)
116119
}
117120

118121
return f.Config.Validate()

0 commit comments

Comments
 (0)