node token: embed permissions directly in JWT

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2025-08-22 15:50:20 +02:00
parent 6bde42fc3f
commit a5dd529d88
4 changed files with 54 additions and 20 deletions

View File

@@ -264,7 +264,9 @@ func disconnectUser(username, admin, role string) {
logger.Warn(logSender, "", "unable to disconnect user %q, error getting node %q: %v", username, stat.Node, err)
continue
}
if err := n.SendDeleteRequest(admin, role, fmt.Sprintf("%s/%s", activeConnectionsPath, stat.ConnectionID)); err != nil {
perms := []string{dataprovider.PermAdminCloseConnections}
uri := fmt.Sprintf("%s/%s", activeConnectionsPath, stat.ConnectionID)
if err := n.SendDeleteRequest(admin, role, uri, perms); err != nil {
logger.Warn(logSender, "", "unable to disconnect user %q from node %q, error: %v", username, n.Name, err)
}
}

View File

@@ -217,7 +217,9 @@ func handleCloseConnection(w http.ResponseWriter, r *http.Request) {
sendAPIResponse(w, r, nil, http.StatusText(status), status)
return
}
if err := n.SendDeleteRequest(claims.Username, claims.Role, fmt.Sprintf("%s/%s", activeConnectionsPath, connectionID)); err != nil {
perms := []string{dataprovider.PermAdminCloseConnections}
uri := fmt.Sprintf("%s/%s", activeConnectionsPath, connectionID)
if err := n.SendDeleteRequest(claims.Username, claims.Role, uri, perms); err != nil {
logger.Warn(logSender, "", "unable to delete connection id %q from node %q: %v", connectionID, n.Name, err)
sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
return
@@ -243,7 +245,8 @@ func getNodesConnections(admin, role string) []common.ConnectionStatus {
defer wg.Done()
var stats []common.ConnectionStatus
if err := node.SendGetRequest(admin, role, activeConnectionsPath, &stats); err != nil {
perms := []string{dataprovider.PermAdminViewConnections}
if err := node.SendGetRequest(admin, role, activeConnectionsPath, perms, &stats); err != nil {
logger.Warn(logSender, "", "unable to get connections from node %s: %v", node.Name, err)
return
}

View File

@@ -22,6 +22,7 @@ import (
"net/url"
"slices"
"strings"
"time"
"github.com/go-chi/jwtauth/v5"
"github.com/rs/xid"
@@ -369,15 +370,22 @@ func checkNodeToken(tokenAuth *jwtauth.JWTAuth) func(next http.Handler) http.Han
if len(token) > 7 && strings.ToUpper(token[0:6]) == "BEARER" {
token = token[7:]
}
admin, role, err := dataprovider.AuthenticateNodeToken(token)
if invalidatedJWTTokens.Get(token) {
logger.Debug(logSender, "", "the node token has been invalidated")
sendAPIResponse(w, r, fmt.Errorf("the provided token is not valid"), "", http.StatusUnauthorized)
return
}
admin, role, perms, err := dataprovider.AuthenticateNodeToken(token)
if err != nil {
logger.Debug(logSender, "", "unable to authenticate node token %q: %v", token, err)
sendAPIResponse(w, r, fmt.Errorf("the provided token cannot be authenticated"), "", http.StatusUnauthorized)
return
}
defer invalidatedJWTTokens.Add(token, time.Now().Add(2*time.Minute).UTC())
c := jwtTokenClaims{
Username: admin,
Permissions: []string{dataprovider.PermAdminViewConnections, dataprovider.PermAdminCloseConnections},
Permissions: perms,
NodeID: dataprovider.GetNodeName(),
Role: role,
}