Initial support for JWT tokens in agents checks
parent
ed319c0291
commit
19285c3215
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module jilo-server
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
||||||
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
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=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|
|
@ -4,14 +4,17 @@ servers:
|
||||||
agents:
|
agents:
|
||||||
agent1:
|
agent1:
|
||||||
endpoint: "https://meet.lindeas.com:8081/jvb"
|
endpoint: "https://meet.lindeas.com:8081/jvb"
|
||||||
|
secret: "mysecretkey"
|
||||||
check_period: 5
|
check_period: 5
|
||||||
agent2:
|
agent2:
|
||||||
endpoint: "https://meet.lindeas.com:8081/jicofo"
|
endpoint: "https://meet.lindeas.com:8081/jicofo"
|
||||||
|
secret: "mysecretkey"
|
||||||
check_period: 5
|
check_period: 5
|
||||||
meet.example.com:
|
meet.example.com:
|
||||||
agents:
|
agents:
|
||||||
agent1:
|
agent1:
|
||||||
endpoint: "https://meet.example.com:8081/jvb"
|
endpoint: "https://meet.example.com:8081/jvb"
|
||||||
|
secret: "mysecret"
|
||||||
check_period: 10
|
check_period: 10
|
||||||
agent2:
|
agent2:
|
||||||
endpoint: "https://meet.example.com:8081/jicofo"
|
endpoint: "https://meet.example.com:8081/jicofo"
|
||||||
|
|
42
main.go
42
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
// Structures
|
// Structures
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
Endpoint string `yaml:"endpoint"`
|
Endpoint string `yaml:"endpoint"`
|
||||||
|
Secret string `yaml:"secret"`
|
||||||
CheckPeriod int `yaml:"check_period"`
|
CheckPeriod int `yaml:"check_period"`
|
||||||
}
|
}
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
@ -101,11 +103,43 @@ func checkTableExists(db *sql.DB) bool {
|
||||||
return err == nil && name == "endpoint_data"
|
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
|
// Check agent endpoint
|
||||||
func checkEndpoint(endpoint string) (int, int64, string) {
|
func checkEndpoint(agent Agent) (int, int64, string) {
|
||||||
log.Println("Sending HTTP get request to Jilo agent:", endpoint)
|
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()
|
start := time.Now()
|
||||||
resp, err := http.Get(endpoint)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to check the endpoint:", err)
|
log.Println("Failed to check the endpoint:", err)
|
||||||
return 0, 0, ""
|
return 0, 0, ""
|
||||||
|
@ -167,7 +201,7 @@ func main() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
log.Printf("Checking agent [%s - %s]: %s", serverName, agentName, agent.Endpoint)
|
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)
|
log.Printf("Agent [%s - %s]: Status code: %d, Response time: %d ms", serverName, agentName, statusCode, responseTime)
|
||||||
|
|
||||||
saveData(db, statusCode, responseTime, responseContent)
|
saveData(db, statusCode, responseTime, responseContent)
|
||||||
|
|
Loading…
Reference in New Issue