feat: agent rework, dynamically rotate tokens

This commit is contained in:
d34dscene
2025-05-01 00:25:34 +02:00
parent 45c4aeda54
commit 3d96a89e8e
111 changed files with 1075 additions and 914 deletions
+28 -5
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/MizuchiLabs/mantrae/internal/api/agent"
"github.com/MizuchiLabs/mantrae/internal/config"
@@ -84,9 +85,19 @@ func CreateAgent(a *config.App) http.HandlerFunc {
claims := &agent.AgentClaims{
AgentID: uuid.New().String(),
ProfileID: profileID,
ServerURL: serverUrl.Value.(string),
ServerURL: serverUrl.String("http://localhost:3000"),
}
token, err := claims.EncodeJWT(a.Config.Secret)
// Generate a JWT for the agent and let it expire based on the cleanup interval
agentInterval, err := a.SM.Get(r.Context(), settings.KeyAgentCleanupInterval)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := claims.EncodeJWT(
a.Config.Secret,
time.Now().Add(agentInterval.Duration(time.Hour*72)),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -173,10 +184,18 @@ func RotateAgentToken(a *config.App) http.HandlerFunc {
claims := &agent.AgentClaims{
AgentID: dbAgent.ID,
ProfileID: dbAgent.ProfileID,
ServerURL: serverUrl.Value.(string),
ServerURL: serverUrl.String("http://localhost:3000"),
}
token, err := claims.EncodeJWT(a.Config.Secret)
agentInterval, err := a.SM.Get(r.Context(), settings.KeyAgentCleanupInterval)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := claims.EncodeJWT(
a.Config.Secret,
time.Now().Add(agentInterval.Duration(time.Hour*72)),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -194,6 +213,10 @@ func RotateAgentToken(a *config.App) http.HandlerFunc {
Type: util.EventTypeUpdate,
Category: util.EventCategoryAgent,
}
w.WriteHeader(http.StatusNoContent)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(token); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
+15 -15
View File
@@ -7,10 +7,10 @@ import (
"strings"
"time"
"github.com/MizuchiLabs/mantrae/internal/app"
"github.com/MizuchiLabs/mantrae/internal/config"
"github.com/MizuchiLabs/mantrae/internal/db"
"github.com/MizuchiLabs/mantrae/internal/mail"
"github.com/MizuchiLabs/mantrae/internal/settings"
"github.com/MizuchiLabs/mantrae/internal/util"
"golang.org/x/crypto/bcrypt"
)
@@ -199,7 +199,7 @@ func SendResetEmail(a *config.App) http.HandlerFunc {
return
}
settings, err := q.ListSettings(r.Context())
sets, err := a.SM.GetAll(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -213,19 +213,19 @@ func SendResetEmail(a *config.App) http.HandlerFunc {
return
}
var config app.EmailConfig
for _, setting := range settings {
switch setting.Key {
case "email_host":
config.Host = setting.Value
case "email_port":
config.Port = setting.Value
case "email_user":
config.Username = setting.Value
case "email_password":
config.Password = setting.Value
case "email_from":
config.From = setting.Value
var config mail.EmailConfig
for _, s := range sets {
switch s.Value {
case settings.KeyEmailHost:
config.Host = s.Value.(string)
case settings.KeyEmailPort:
config.Port = s.Value.(string)
case settings.KeyEmailUser:
config.Username = s.Value.(string)
case settings.KeyEmailPassword:
config.Password = s.Value.(string)
case settings.KeyEmailFrom:
config.From = s.Value.(string)
}
}
data := map[string]any{