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"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -350,7 +349,6 @@ func (h *MachineHandler) DeployKeys(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverKeyPath := h.cfg.SSHDir() + "/id_ed25519"
|
serverKeyPath := h.cfg.SSHDir() + "/id_ed25519"
|
||||||
serverPubKeyPath := h.cfg.SSHDir() + "/id_ed25519.pub"
|
|
||||||
|
|
||||||
if m.SSHKeyID != nil {
|
if m.SSHKeyID != nil {
|
||||||
sshKeyRepo := models.NewSSHKeyRepository(h.db)
|
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()
|
allMachines, err := repo.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("failed to fetch machines for auto-detect", "error", err)
|
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(
|
result, err := sshmanager.DeployKeysToMachine(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
serverKeyPath,
|
serverKeyPath,
|
||||||
serverPubKey,
|
|
||||||
m.Host,
|
m.Host,
|
||||||
m.Port,
|
m.Port,
|
||||||
m.SSHUser,
|
m.SSHUser,
|
||||||
keys,
|
keys,
|
||||||
knownHostsHosts,
|
knownHostsHosts,
|
||||||
req.IncludeServerKey,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, err.Error())
|
writeError(w, http.StatusInternalServerError, err.Error())
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
@@ -26,7 +25,7 @@ type DeployResult struct {
|
|||||||
Errors []string
|
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{}}
|
result := &DeployResult{Success: true, Messages: []string{}, Errors: []string{}}
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", host, port)
|
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))
|
slog.Info("deploy keys result", "host", host, "success", result.Success, "messages", len(result.Messages), "errors", len(result.Errors))
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,12 +125,11 @@ export interface DeployKeysResponse {
|
|||||||
|
|
||||||
export interface DeployKeysOptions {
|
export interface DeployKeysOptions {
|
||||||
known_hosts_host?: string;
|
known_hosts_host?: string;
|
||||||
include_server_key?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deployKeys(machineId: number, options?: DeployKeysOptions): Promise<DeployKeysResponse> {
|
export async function deployKeys(machineId: number, options?: DeployKeysOptions): Promise<DeployKeysResponse> {
|
||||||
return api<DeployKeysResponse>(`/api/machines/${machineId}/deploy-keys`, {
|
return api<DeployKeysResponse>(`/api/machines/${machineId}/deploy-keys`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: options ?? { include_server_key: true },
|
body: options ?? {},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ export default function Machines() {
|
|||||||
async function handleDeployKeys(m: Machine) {
|
async function handleDeployKeys(m: Machine) {
|
||||||
setDeployModal({ machine: m, result: null, loading: true });
|
setDeployModal({ machine: m, result: null, loading: true });
|
||||||
try {
|
try {
|
||||||
const result = await deployKeys(m.id, { include_server_key: true });
|
const result = await deployKeys(m.id);
|
||||||
setDeployModal({ machine: m, result, loading: false });
|
setDeployModal({ machine: m, result, loading: false });
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
setDeployModal({ machine: m, result: { success: false, messages: [], errors: [(e as Error).message] }, loading: false });
|
setDeployModal({ machine: m, result: { success: false, messages: [], errors: [(e as Error).message] }, loading: false });
|
||||||
|
|||||||
Reference in New Issue
Block a user