add agent version to getkey route

This commit is contained in:
Henry Dollman
2024-08-20 14:00:15 -04:00
parent 19272c05bf
commit d053f16058

View File

@@ -1,6 +1,7 @@
package hub package hub
import ( import (
"beszel"
"beszel/internal/alerts" "beszel/internal/alerts"
"beszel/internal/entities/system" "beszel/internal/entities/system"
"beszel/internal/records" "beszel/internal/records"
@@ -34,6 +35,7 @@ type Hub struct {
connectionLock *sync.Mutex connectionLock *sync.Mutex
systemConnections map[string]*ssh.Client systemConnections map[string]*ssh.Client
sshClientConfig *ssh.ClientConfig sshClientConfig *ssh.ClientConfig
pubKey string
} }
func NewHub(app *pocketbase.PocketBase) *Hub { func NewHub(app *pocketbase.PocketBase) *Hub {
@@ -127,11 +129,7 @@ func (h *Hub) Run() {
if requestData.AuthRecord == nil { if requestData.AuthRecord == nil {
return apis.NewForbiddenError("Forbidden", nil) return apis.NewForbiddenError("Forbidden", nil)
} }
key, err := os.ReadFile(h.app.DataDir() + "/id_ed25519.pub") return c.JSON(http.StatusOK, map[string]string{"key": h.pubKey, "v": beszel.Version})
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{"key": strings.TrimSuffix(string(key), "\n")})
}) })
// check if first time setup on login page // check if first time setup on login page
e.Router.GET("/api/beszel/first-run", func(c echo.Context) error { e.Router.GET("/api/beszel/first-run", func(c echo.Context) error {
@@ -384,6 +382,10 @@ func (h *Hub) getSSHKey() ([]byte, error) {
// check if the key pair already exists // check if the key pair already exists
existingKey, err := os.ReadFile(dataDir + "/id_ed25519") existingKey, err := os.ReadFile(dataDir + "/id_ed25519")
if err == nil { if err == nil {
if pubKey, err := os.ReadFile(h.app.DataDir() + "/id_ed25519.pub"); err == nil {
h.pubKey = strings.TrimSuffix(string(pubKey), "\n")
}
// return existing private key
return existingKey, nil return existingKey, nil
} }
@@ -421,6 +423,7 @@ func (h *Hub) getSSHKey() ([]byte, error) {
} }
pubKeyBytes := ssh.MarshalAuthorizedKey(publicKey) pubKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
h.pubKey = strings.TrimSuffix(string(pubKeyBytes), "\n")
// Save the public key to a file // Save the public key to a file
publicFile, err := os.Create(dataDir + "/id_ed25519.pub") publicFile, err := os.Create(dataDir + "/id_ed25519.pub")