Initial support for JWT tokens in agents checks

main
Yasen Pramatarov 2024-10-14 18:55:48 +03:00
parent ed319c0291
commit 19285c3215
4 changed files with 44 additions and 4 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module jilo-server
go 1.23.2
require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -4,14 +4,17 @@ servers:
agents:
agent1:
endpoint: "https://meet.lindeas.com:8081/jvb"
secret: "mysecretkey"
check_period: 5
agent2:
endpoint: "https://meet.lindeas.com:8081/jicofo"
secret: "mysecretkey"
check_period: 5
meet.example.com:
agents:
agent1:
endpoint: "https://meet.example.com:8081/jvb"
secret: "mysecret"
check_period: 10
agent2:
endpoint: "https://meet.example.com:8081/jicofo"

42
main.go
View File

@ -10,6 +10,7 @@ import (
"os"
"time"
"github.com/golang-jwt/jwt/v5"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
)
@ -17,6 +18,7 @@ import (
// Structures
type Agent struct {
Endpoint string `yaml:"endpoint"`
Secret string `yaml:"secret"`
CheckPeriod int `yaml:"check_period"`
}
type Server struct {
@ -101,11 +103,43 @@ func checkTableExists(db *sql.DB) bool {
return err == nil && name == "endpoint_data"
}
// JWT token generation
func generateJWT(secret string) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iat": time.Now().Unix(),
})
tokenString, err := token.SignedString([]byte(secret))
if err != nil {
return "", err
}
return tokenString, nil
}
// Check agent endpoint
func checkEndpoint(endpoint string) (int, int64, string) {
log.Println("Sending HTTP get request to Jilo agent:", endpoint)
func checkEndpoint(agent Agent) (int, int64, string) {
log.Println("Sending HTTP get request to Jilo agent:", agent.Endpoint)
// Generate the JWT token
token, err := generateJWT(agent.Secret)
if err != nil {
log.Println("Failed to generate JWT token:", err)
return 0, 0, ""
}
// Create the http request
req, err := http.NewRequest("GET", agent.Endpoint, nil)
if err != nil {
log.Println("Failed to create the HTTP request:", err)
return 0, 0, ""
}
// Set Authorization header
req.Header.Set("Authorization", "Bearer "+token)
start := time.Now()
resp, err := http.Get(endpoint)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("Failed to check the endpoint:", err)
return 0, 0, ""
@ -167,7 +201,7 @@ func main() {
for {
log.Printf("Checking agent [%s - %s]: %s", serverName, agentName, agent.Endpoint)
statusCode, responseTime, responseContent := checkEndpoint(agent.Endpoint)
statusCode, responseTime, responseContent := checkEndpoint(agent)
log.Printf("Agent [%s - %s]: Status code: %d, Response time: %d ms", serverName, agentName, statusCode, responseTime)
saveData(db, statusCode, responseTime, responseContent)