Skip to content

Commit

Permalink
Merge pull request #16 from NethServer/improve_unit_info
Browse files Browse the repository at this point in the history
Improve unit info management
  • Loading branch information
gsanchietti authored Apr 3, 2024
2 parents 9664d19 + 61f9287 commit 554ac3b
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 245 deletions.
33 changes: 21 additions & 12 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ CGO_ENABLED=0 go build

- `FQDN`: fully qualified domain name of the machine - *default*: `hostname -f`

- `CACHE_TTL`: cache time to live for unit information in seconds - *default*: `7200` (2 hours)
Unit information are fetched from the connected units. The cache is refreshed every hour.

## APIs
### Auth
- `POST /login`
Expand Down Expand Up @@ -121,7 +124,6 @@ CGO_ENABLED=0 go build
"ipaddress": "172.23.21.3",
"id": "<unit_id>",
"netmask": "255.255.255.0",
"registered": true,
"vpn": {
"bytes_rcvd": "21830",
"bytes_sent": "5641",
Expand All @@ -130,34 +132,37 @@ CGO_ENABLED=0 go build
"virtual_address": "172.23.21.3"
},
"info": {
"unit_id": "fba703c1-6c2d-4d3d-9dab-5998c7b66700",
"unit_name": "fw.local",
"unit_name": "myfw1",
"version": "8-23.05.2-ns.0.0.2-beta2-37-g6e74afc",
"subscription_type": "enterprise",
"system_id": "XXXXXXXX-XXXX",
"created": "2024-03-14T15:18:08Z"
"ssh_port": 22,
"fqdn": "fw.local",
}
},
...
{
"ipaddress": "",
"id": "<unit_id>",
"netmask": "",
"registered": false,
"vpn": {},
"info": {
"unit_id": "zzzzzzzz-d9f3-44b7-b277-36d65cf139e6",
"unit_name": "fw.nethsecurity.local",
"version": "8-23.05.2-ns.0.0.2-beta2-37-g6e74afc",
"unit_name": "",
"version": "",
"subscription_type": "",
"system_id": "",
"created": "2024-03-14T15:16:02Z"
"ssh_port": 0,
"fqdn": "",
}
}
],
"message": "units listed successfully"
}
```

The API takes a query parameter `cache`. If `cache` is set to `true`, the API will return the cached data, if data are fresh enough.
If `cache` is set to `false`, the API will always fetch the data from the connected units.

- `GET /units/<unit_id>`

REQ
Expand Down Expand Up @@ -186,18 +191,22 @@ CGO_ENABLED=0 go build
"virtual_address": "172.23.21.3"
},
"info": {
"unit_id": "fba703c1-6c2d-4d3d-9dab-5998c7b66700",
"unit_name": "fw.local",
"unit_name": "myfw1",
"version": "8-23.05.2-ns.0.0.2-beta2-37-g6e74afc",
"subscription_type": "enterprise",
"system_id": "XXXXXXXX-XXXX",
"created": "2024-03-14T15:18:08Z"
"ssh_port": 22,
"fqdn": "fw.local",
},
"join_code": "eyJmcWRuIjoiY29udHJvbGxlci5ncy5uZXRoc2VydmVyLm5ldCIsInRva2VuIjoiMTIzNCIsInVuaXRfaWQiOiI5Njk0Y2Y4ZC03ZmE5LTRmN2EtYjFjNC1iY2Y0MGUzMjhjMDIifQ=="
},
"message": "unit listed successfully"
}
```

The API takes a query parameter `cache`. If `cache` is set to `true`, the API will return the cached data, if data are fresh enough.
If `cache` is set to `false`, the API will always fetch the data from the connected units.

- `GET /units/<unit_id>/token`

REQ
Expand Down
5 changes: 1 addition & 4 deletions api/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,24 @@ func Init() {
value = 3600
}

Cache = ttlcache.New[string, string](
Cache = ttlcache.New(
ttlcache.WithTTL[string, string](time.Duration(value) * time.Second),
)
go Cache.Start() // starts automatic expired item deletion
}

func SetUnitInfo(unitId string, unitInfo models.UnitInfo) {
print("SET_CACHE" + unitId + "\n")
value, err := strconv.Atoi(configuration.Config.CacheTTL)
if err != nil {
value = 60
}
b, err := json.Marshal(unitInfo)
print("SET_CACHE" + string(b) + "\n")
if err == nil {
Cache.Set(unitId, string(b), time.Duration(value)*time.Second)
}
}

func GetUnitInfo(unitId string) (models.UnitInfo, error) {
print("searching for unit info in cache " + unitId + "\n")
if Cache.Has(unitId) {
data := models.UnitInfo{}
item := Cache.Get(unitId)
Expand Down
2 changes: 1 addition & 1 deletion api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,6 @@ func Init() {
if os.Getenv("CACHE_TTL") != "" {
Config.CacheTTL = os.Getenv("CACHE_TTL")
} else {
Config.CacheTTL = "3600"
Config.CacheTTL = "7200"
}
}
21 changes: 21 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package main
import (
"io/ioutil"
"net/http"
"time"

"github.com/fatih/structs"
"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -42,6 +43,24 @@ import (
// @schemes http
// @BasePath /api

func refreshCacheLoop() {
ticker := time.NewTicker(60 * time.Minute)
for range ticker.C {
// load all units info into cache
units, err := methods.ListUnits()
if err != nil {
return
}

for _, unit := range units {
unitInfo, err := methods.GetRemoteInfo(unit)
if err == nil {
cache.SetUnitInfo(unit, unitInfo)
}
}
}
}

func main() {
// init logs with syslog
logs.Init("nethsecurity_controller")
Expand All @@ -58,6 +77,8 @@ func main() {
// init cache
cache.Init()

go refreshCacheLoop() // starts cache refresh loop

// disable log to stdout when running in release mode
if gin.Mode() == gin.ReleaseMode {
gin.DefaultWriter = ioutil.Discard
Expand Down
Loading

0 comments on commit 554ac3b

Please sign in to comment.