fix: remove public key deployment from deploy-keys
Deploy-keys now only: - Creates /var/lib/syncserver/ssh/keys on remote - Uploads private keys of all other machines to that path - Populates known_hosts with all other machine hosts No longer touches authorized_keys or reads server public key.
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -341,8 +340,8 @@ func (h *MachineHandler) DeployKeys(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var req struct {
|
||||
KnownHostsHost string `json:"known_hosts_host"`
|
||||
IncludeServerKey bool `json:"include_server_key"`
|
||||
KnownHostsHost string `json:"known_hosts_host"`
|
||||
IncludeServerKey bool `json:"include_server_key"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
@@ -350,7 +349,6 @@ func (h *MachineHandler) DeployKeys(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
serverKeyPath := h.cfg.SSHDir() + "/id_ed25519"
|
||||
serverPubKeyPath := h.cfg.SSHDir() + "/id_ed25519.pub"
|
||||
|
||||
if m.SSHKeyID != nil {
|
||||
sshKeyRepo := models.NewSSHKeyRepository(h.db)
|
||||
@@ -360,14 +358,6 @@ func (h *MachineHandler) DeployKeys(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
serverPubKey := ""
|
||||
if req.IncludeServerKey {
|
||||
data, err := os.ReadFile(serverPubKeyPath)
|
||||
if err == nil {
|
||||
serverPubKey = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
allMachines, err := repo.GetAll()
|
||||
if err != nil {
|
||||
slog.Warn("failed to fetch machines for auto-detect", "error", err)
|
||||
@@ -406,13 +396,11 @@ func (h *MachineHandler) DeployKeys(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := sshmanager.DeployKeysToMachine(
|
||||
context.Background(),
|
||||
serverKeyPath,
|
||||
serverPubKey,
|
||||
m.Host,
|
||||
m.Port,
|
||||
m.SSHUser,
|
||||
keys,
|
||||
knownHostsHosts,
|
||||
req.IncludeServerKey,
|
||||
)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
@@ -26,7 +25,7 @@ type DeployResult struct {
|
||||
Errors []string
|
||||
}
|
||||
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath, serverPubKey string, host string, port int, user string, keys []DeployKey, knownHostsHosts []string, addServerPubKey bool) (*DeployResult, error) {
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath string, host string, port int, user string, keys []DeployKey, knownHostsHosts []string) (*DeployResult, error) {
|
||||
result := &DeployResult{Success: true, Messages: []string{}, Errors: []string{}}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
@@ -163,27 +162,6 @@ func DeployKeysToMachine(ctx context.Context, serverKeyPath, serverPubKey string
|
||||
}
|
||||
}
|
||||
|
||||
if addServerPubKey && serverPubKey != "" {
|
||||
session3, err := conn.NewSession()
|
||||
if err != nil {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("session for authorized_keys: %v", err))
|
||||
result.Success = false
|
||||
} else {
|
||||
session3.Stdout = &stdout
|
||||
session3.Stderr = &stderr
|
||||
pubKeyClean := strings.TrimSpace(serverPubKey)
|
||||
err := session3.Run(fmt.Sprintf("mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '%s' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys", pubKeyClean))
|
||||
session3.Close()
|
||||
if err != nil {
|
||||
slog.Warn("deploy: adding to authorized_keys failed", "host", host, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("adding to authorized_keys: %v (stderr: %s)", err, stderr.String()))
|
||||
result.Success = false
|
||||
} else {
|
||||
result.Messages = append(result.Messages, fmt.Sprintf("Added server public key to %s@%s:~/.ssh/authorized_keys", user, host))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slog.Info("deploy keys result", "host", host, "success", result.Success, "messages", len(result.Messages), "errors", len(result.Errors))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -125,12 +125,11 @@ export interface DeployKeysResponse {
|
||||
|
||||
export interface DeployKeysOptions {
|
||||
known_hosts_host?: string;
|
||||
include_server_key?: boolean;
|
||||
}
|
||||
|
||||
export async function deployKeys(machineId: number, options?: DeployKeysOptions): Promise<DeployKeysResponse> {
|
||||
return api<DeployKeysResponse>(`/api/machines/${machineId}/deploy-keys`, {
|
||||
method: 'POST',
|
||||
body: options ?? { include_server_key: true },
|
||||
body: options ?? {},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ export default function Machines() {
|
||||
async function handleDeployKeys(m: Machine) {
|
||||
setDeployModal({ machine: m, result: null, loading: true });
|
||||
try {
|
||||
const result = await deployKeys(m.id, { include_server_key: true });
|
||||
const result = await deployKeys(m.id);
|
||||
setDeployModal({ machine: m, result, loading: false });
|
||||
} catch (e: unknown) {
|
||||
setDeployModal({ machine: m, result: { success: false, messages: [], errors: [(e as Error).message] }, loading: false });
|
||||
|
||||
Reference in New Issue
Block a user