Added ephemeral ssh keys.

This commit is contained in:
rony5394
2026-03-14 14:39:19 +01:00
parent 0f5c47ad1f
commit be06e3b87c
5 changed files with 180 additions and 36 deletions

54
docker/keys.go Normal file
View File

@@ -0,0 +1,54 @@
package docker
import (
"encoding/json"
"fmt"
"io"
"net/http"
"context"
"github.com/docker/docker/api/types/swarm"
"github.com/rony5394/blazena/shared"
)
func exchangeKeys(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodPost{
w.WriteHeader(http.StatusMethodNotAllowed);
fmt.Fprint(w, "Method Not Allowed");
return;
}
if !bearerAuth(w, r) {return;}
rawBody, err := io.ReadAll(r.Body);
if err != nil {
panic("Failed to read body!");
}
var bodyDecoded struct{
SshPkPem string `json:"sshPkPem"`
};
err = json.Unmarshal(rawBody, &bodyDecoded);
if err != nil {
panic("Failed to unmarshal json."+ err.Error());
}
sshPkPem := bodyDecoded.SshPkPem;
hostKeypair := shared.GenerateSSHKeypair();
encoded, err := json.Marshal(struct{HostPkPem string `json:"hostPkPem"`}{HostPkPem: hostKeypair.Public});
if err != nil {
panic("I wonder how. I wonder why?"+err.Error());
}
ApiClient.ConfigCreate(context.Background(), swarm.ConfigSpec{
Data: []byte(sshPkPem),
Annotations: swarm.Annotations{Name: "blazenaSSHPublicKey"},
});
ApiClient.SecretCreate(context.Background(), swarm.SecretSpec{
Data: []byte(hostKeypair.Private),
Annotations: swarm.Annotations{Name: "blazenaSSHHostPrivateKey"},
});
fmt.Fprint(w, string(encoded));
}