Skip to content

Commit

Permalink
api: add report timescale db
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Aug 21, 2024
1 parent e806b15 commit 79f9f14
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 4 deletions.
9 changes: 9 additions & 0 deletions api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Configuration struct {
CacheTTL string `json:"cache_ttl"`

ValidSubscription bool `json:"valid_subscription"`

ReportDbUri string `json:"report_db_uri"`
}

var Config = Configuration{}
Expand Down Expand Up @@ -242,4 +244,11 @@ func Init() {
} else {
Config.ValidSubscription = false
}

if os.Getenv("REPORT_DB_URI") != "" {
Config.ReportDbUri = os.Getenv("REPORT_DB_URI")
} else {
logs.Logs.Println("[CRITICAL][ENV] REPORT_DB_URI variable is empty")
os.Exit(1)
}
}
12 changes: 8 additions & 4 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.9.1
github.com/jackc/pgx/v5 v5.6.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/nqd/flat v0.2.0
golang.org/x/crypto v0.17.0
)

require (
Expand All @@ -25,23 +28,24 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jellydator/ttlcache/v3 v3.2.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jellydator/ttlcache/v3 v3.2.0 h1:6lqVJ8X3ZaUwvzENqPAobDsXNExfUJd61u++uW8a3LE=
github.com/jellydator/ttlcache/v3 v3.2.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
Expand Down Expand Up @@ -276,6 +284,8 @@ golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -342,6 +352,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
Expand All @@ -359,6 +371,8 @@ golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190606050223-4d9ae51c2468/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190611222205-d73e1c7e250b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
Expand Down
8 changes: 8 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {

// init storage
storage.Init()
storage.InitReportDb()

// init socket connection
socket.Init()
Expand Down Expand Up @@ -131,6 +132,13 @@ func main() {
}
}

// report APIs
reports := router.Group("/reports")
reports.Use(middleware.ReportAuth())
{
reports.POST("/mwan-series", methods.UpdateMwanSeries)
}

// handle missing endpoint
router.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, structs.Map(response.StatusNotFound{
Expand Down
32 changes: 32 additions & 0 deletions api/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -257,3 +258,34 @@ func InitJWT() *jwt.GinJWTMiddleware {
// return object
return authMiddleware
}

func ReportAuth() gin.HandlerFunc {
return func(c *gin.Context) {

token := c.GetHeader("RegistrationToken")
unit_id := c.GetHeader("UnitId")
if token == "" || unit_id == "" {
c.JSON(http.StatusUnauthorized, structs.Map(response.StatusUnauthorized{
Code: 401,
Message: "missing token or unit id",
Data: nil,
}))
c.Abort()
return
}
valid := true
// TODO: check if token is valid
if !valid {
c.JSON(http.StatusUnauthorized, structs.Map(response.StatusUnauthorized{
Code: 401,
Message: "invalid token or unit id",
Data: nil,
}))
c.Abort()
return
}
c.Set("UnitId", unit_id)
c.Next()
}

}
26 changes: 26 additions & 0 deletions api/storage/report_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 Nethesis S.r.l.
* http://www.nethesis.it - [email protected]
*
* SPDX-License-Identifier: GPL-2.0-only
*
* author: Giacomo Sanchietti <[email protected]>
*/
CREATE TABLE IF NOT EXISTS units (
id SERIAL PRIMARY KEY,
uuid TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS mwan_events (
time TIMESTAMPTZ NOT NULL,
unit_id INTEGER NOT NULL references units(id),
wan TEXT NOT NULL,
event TEXT NOT NULL,
interface TEXT,
UNIQUE (time, unit_id),
CONSTRAINT fk_unit FOREIGN KEY(unit_id) REFERENCES units(id) ON DELETE CASCADE
);

SELECT
create_hypertable('mwan_events', by_range('time'), if_not_exists => TRUE);
39 changes: 39 additions & 0 deletions api/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package storage

import (
"context"
"database/sql"
_ "embed"
"os"
Expand All @@ -19,16 +20,22 @@ import (
"github.com/NethServer/nethsecurity-controller/api/logs"
"github.com/NethServer/nethsecurity-controller/api/models"
"github.com/NethServer/nethsecurity-controller/api/utils"
"github.com/jackc/pgx/v5/pgxpool"

_ "github.com/mattn/go-sqlite3"
)

var db *sql.DB
var dbpool *pgxpool.Pool
var dbctx context.Context
var err error

//go:embed schema.sql
var schemaSQL string

//go:embed report_schema.sql
var reportSchemaSQL string

func Instance() *sql.DB {
if db == nil {
db = Init()
Expand Down Expand Up @@ -262,3 +269,35 @@ func UpdatePassword(accountUsername string, newPassword string) error {

return err
}

func InitReportDb() (*pgxpool.Pool, context.Context) {
dbctx = context.Background()
dbpool, err = pgxpool.New(dbctx, configuration.Config.ReportDbUri)
if err != nil {
logs.Logs.Println("[CRITICAL][DB] error in db connection:" + err.Error())
os.Exit(1)
}

err = dbpool.Ping(dbctx)
if err != nil {
logs.Logs.Println("[CRITICAL][DB] error in db connection:" + err.Error())
os.Exit(1)
}

// execute create tables
_, errExecute := dbpool.Exec(dbctx, reportSchemaSQL)
if errExecute != nil {
logs.Logs.Println("[ERR][STORAGE] error in storage file schema init:" + errExecute.Error())
} else {
logs.Logs.Println("[INFO] Report schema initialized")
}

return dbpool, dbctx
}

func ReportInstance() (*pgxpool.Pool, context.Context) {
if dbpool == nil {
dbpool, dbctx = InitReportDb()
}
return dbpool, dbctx
}

0 comments on commit 79f9f14

Please sign in to comment.