hostkey is now sent from the server to the agent so that at a later

stage we can make the hostkey conrfigurable at the server level instead
of using a fixed hardcoded key.
This commit is contained in:
Erik Brakkee 2024-08-17 21:35:29 +02:00
parent b5a9ecf5e8
commit a3fe2fbcb6
6 changed files with 19 additions and 13 deletions

View File

@ -29,9 +29,6 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
) )
//go:embed hostkey.pem
var hostPrivateKey []byte
func SftpHandler(sftpSession ssh.Session) { func SftpHandler(sftpSession ssh.Session) {
sessionInfo := comms.NewSessionInfo( sessionInfo := comms.NewSessionInfo(
sftpSession.LocalAddr().String(), sftpSession.LocalAddr().String(),
@ -65,7 +62,7 @@ func SftpHandler(sftpSession ssh.Session) {
} }
} }
func sshServer(hostKeyFile string, shellCommand string, func sshServer(hostPrivateKey []byte, shellCommand string,
authorizedPublicKeys *AuthorizedPublicKeys) *ssh.Server { authorizedPublicKeys *AuthorizedPublicKeys) *ssh.Server {
ssh.Handle(func(sshSession ssh.Session) { ssh.Handle(func(sshSession ssh.Session) {
workingDirectory, _ := os.Getwd() workingDirectory, _ := os.Getwd()
@ -333,7 +330,7 @@ func main() {
var service AgentService var service AgentService
service = ListenerServer(func() *ssh.Server { service = ListenerServer(func() *ssh.Server {
return sshServer("hostkey.pem", shell, authorizedKeys) return sshServer(registration.HostPrivateKey, shell, authorizedKeys)
}) })
//service = ConnectionServer(netCatServer) //service = ConnectionServer(netCatServer)
//service = ConnectionServer(echoServer) //service = ConnectionServer(echoServer)

View File

@ -99,7 +99,6 @@ func (pubkeys *AuthorizedPublicKeys) monitorAuthorizedKeysFile(authorizedPublicK
authorizedPublicKeysFile, err) authorizedPublicKeysFile, err)
} }
defer watcher.Close() defer watcher.Close()
log.Println("XXX: monitor " + dir)
err = watcher.Add(dir) err = watcher.Add(dir)
if err != nil { if err != nil {
log.Printf("Cannot watch hold file %s, user notifications for change in expiry time will be unavailable: %v", authorizedPublicKeysFile, err) log.Printf("Cannot watch hold file %s, user notifications for change in expiry time will be unavailable: %v", authorizedPublicKeysFile, err)

View File

@ -8,7 +8,7 @@ import (
"time" "time"
) )
const PROTOCOL_VERSION = 3 const PROTOCOL_VERSION = 4
func init() { func init() {
RegisterEventsWithGob() RegisterEventsWithGob()
@ -58,7 +58,8 @@ type AgentRegistration struct {
Message string Message string
// final Id assigned by the server. Usually identical to the requested id // final Id assigned by the server. Usually identical to the requested id
// but if there is a conflict, a new id is chosen. // but if there is a conflict, a new id is chosen.
Id string Id string
HostPrivateKey []byte
} }
// Generic wrapper message required to send messages of arbitrary type // Generic wrapper message required to send messages of arbitrary type

View File

@ -105,7 +105,7 @@ func (admin *Admin) getFreeId(publicId models.RendezVousId) (models.RendezVousId
return "", fmt.Errorf("Could not allocate agent id based on requested public id '%s'", publicId) return "", fmt.Errorf("Could not allocate agent id based on requested public id '%s'", publicId)
} }
func (admin *Admin) AddAgent(publicId models.RendezVousId, agentInfo comms.EnvironmentInfo, conn io.ReadWriteCloser) (*agentConnection, error) { func (admin *Admin) AddAgent(hostKey []byte, publicId models.RendezVousId, agentInfo comms.EnvironmentInfo, conn io.ReadWriteCloser) (*agentConnection, error) {
admin.mutex.Lock() admin.mutex.Lock()
defer admin.mutex.Unlock() defer admin.mutex.Unlock()
@ -117,9 +117,10 @@ func (admin *Admin) AddAgent(publicId models.RendezVousId, agentInfo comms.Envir
} }
publicId = newPublicId publicId = newPublicId
comms.SendRegistrationMessage(conn, comms.AgentRegistration{ comms.SendRegistrationMessage(conn, comms.AgentRegistration{
Ok: true, Ok: true,
Message: message, Message: message,
Id: string(publicId), Id: string(publicId),
HostPrivateKey: hostKey,
}) })
} else { } else {
comms.SendRegistrationMessage(conn, comms.AgentRegistration{ comms.SendRegistrationMessage(conn, comms.AgentRegistration{

View File

@ -1,6 +1,7 @@
package matchmaker package matchmaker
import ( import (
_ "embed"
"fmt" "fmt"
"git.wamblee.org/converge/pkg/comms" "git.wamblee.org/converge/pkg/comms"
"git.wamblee.org/converge/pkg/models" "git.wamblee.org/converge/pkg/models"
@ -11,6 +12,13 @@ import (
"time" "time"
) )
// Use a fixed host key for all agents. Using a dynamic host key would be madness.
// An alternative would be to configure the host key on the server side and send it
// to agents before establishing a session.
//
//go:embed hostkey.pem
var hostPrivateKey []byte
type MatchMaker struct { type MatchMaker struct {
admin admin.Admin admin admin.Admin
notifier Notifier notifier Notifier
@ -34,7 +42,7 @@ func (converge *MatchMaker) Register(publicId models.RendezVousId, conn io.ReadW
return err return err
} }
agent, err := converge.admin.AddAgent(publicId, agentInfo, conn) agent, err := converge.admin.AddAgent(hostPrivateKey, publicId, agentInfo, conn)
converge.logStatus() converge.logStatus()
if err != nil { if err != nil {
return err return err