From 2704caa5e4aaaa023b5e2b81f47c6dda679343d1 Mon Sep 17 00:00:00 2001 From: Akilan Date: Fri, 15 Dec 2023 03:42:06 +0000 Subject: [PATCH] added public-private keys for all docker containers --- config/generate/generateFiles.go | 311 ++++++++++++++++--------------- server/docker/docker.go | 36 ++-- server/server.go | 1 - 3 files changed, 180 insertions(+), 168 deletions(-) diff --git a/config/generate/generateFiles.go b/config/generate/generateFiles.go index 1b5585a..3eff648 100644 --- a/config/generate/generateFiles.go +++ b/config/generate/generateFiles.go @@ -1,171 +1,171 @@ package generate import ( - "crypto/rand" - "crypto/rsa" - "crypto/x509" - "encoding/pem" - "github.com/Akilan1999/p2p-rendering-computation/p2p" - "github.com/go-git/go-git/v5" - "golang.org/x/crypto/ssh" - "io/ioutil" - "os" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "github.com/go-git/go-git/v5" + "golang.org/x/crypto/ssh" + "io/ioutil" + "os" ) // GenerateFiles Generates all the files needed to setup P2PRC func GenerateFiles(rootNodes ...p2p.IpAddress) (err error) { - err = GenerateIPTableFile(rootNodes) - err = GenerateDockerFiles() - err = GeneratePluginDirectory() - err = GenerateClientTrackContainers() - return + err = GenerateIPTableFile(rootNodes) + err = GenerateDockerFiles() + err = GeneratePluginDirectory() + err = GenerateClientTrackContainers() + return } // GenerateIPTableFile Generates the IPTable file with the appropirate root node func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) { - var rootnodes p2p.IpAddresses - var rootnode p2p.IpAddress + var rootnodes p2p.IpAddresses + var rootnode p2p.IpAddress - err = CreateIPTableFolderStructure() - if err != nil { - return err - } + err = CreateIPTableFolderStructure() + if err != nil { + return err + } - // If root node addresses are not provided as optional parameters - if len(rootNodes) <= 0 { - rootnode.Name = "Node1" - rootnode.ServerPort = "8078" - rootnode.NAT = "False" - rootnode.Ipv4 = "139.59.162.154" + // If root node addresses are not provided as optional parameters + if len(rootNodes) <= 0 { + rootnode.Name = "Node1" + rootnode.ServerPort = "8078" + rootnode.NAT = "False" + rootnode.Ipv4 = "139.59.162.154" - rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode) - } else { - // if root nodes are provided then override them as the optional parameter - for i := range rootNodes { - rootnodes.IpAddress = append(rootnodes.IpAddress, rootNodes[i]) - } - } + rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode) + } else { + // if root nodes are provided then override them as the optional parameter + for i := range rootNodes { + rootnodes.IpAddress = append(rootnodes.IpAddress, rootNodes[i]) + } + } - err = rootnodes.WriteIpTable() - return + err = rootnodes.WriteIpTable() + return } // CreateIPTableFolderStructure Create folder structure for IPTable func CreateIPTableFolderStructure() (err error) { - path, err := GetCurrentPath() - if err != nil { - return err - } - if _, err = os.Stat(path + "p2p"); os.IsNotExist(err) { - if err = os.Mkdir(path+"p2p", os.ModePerm); err != nil { - return err - } - } - if _, err = os.Stat(path + "p2p/iptable"); os.IsNotExist(err) { - if err = os.Mkdir(path+"p2p/iptable", os.ModePerm); err != nil { - return err - } - } - if _, err = os.Stat(path + "p2p/iptable/ip_table.json"); os.IsNotExist(err) { - _, err = os.Create(path + "p2p/iptable/ip_table.json") - if err != nil { - return err - } - } + path, err := GetCurrentPath() + if err != nil { + return err + } + if _, err = os.Stat(path + "p2p"); os.IsNotExist(err) { + if err = os.Mkdir(path+"p2p", os.ModePerm); err != nil { + return err + } + } + if _, err = os.Stat(path + "p2p/iptable"); os.IsNotExist(err) { + if err = os.Mkdir(path+"p2p/iptable", os.ModePerm); err != nil { + return err + } + } + if _, err = os.Stat(path + "p2p/iptable/ip_table.json"); os.IsNotExist(err) { + _, err = os.Create(path + "p2p/iptable/ip_table.json") + if err != nil { + return err + } + } - return + return } // GenerateDockerFiles Generate default docker files func GenerateDockerFiles() (err error) { - path, err := GetCurrentPath() - if err != nil { - return err - } - if _, err = os.Stat(path + "server"); os.IsNotExist(err) { - if err = os.Mkdir(path+"server", os.ModePerm); err != nil { - return err - } - if _, err = os.Stat(path + "server/docker"); os.IsNotExist(err) { - if err = os.Mkdir(path+"server/docker", os.ModePerm); err != nil { - return err - } - } - if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) { - if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil { - return err - } - } - if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) { - if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil { - return err - } - } - if _, err = os.Stat(path + "server/docker/containers/docker-ubuntu-sshd"); os.IsNotExist(err) { - if err = os.Mkdir(path+"server/docker/containers/docker-ubuntu-sshd", os.ModePerm); err != nil { - return err - } - } + path, err := GetCurrentPath() + if err != nil { + return err + } + if _, err = os.Stat(path + "server"); os.IsNotExist(err) { + if err = os.Mkdir(path+"server", os.ModePerm); err != nil { + return err + } + if _, err = os.Stat(path + "server/docker"); os.IsNotExist(err) { + if err = os.Mkdir(path+"server/docker", os.ModePerm); err != nil { + return err + } + } + if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) { + if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil { + return err + } + } + if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) { + if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil { + return err + } + } + if _, err = os.Stat(path + "server/docker/containers/docker-ubuntu-sshd"); os.IsNotExist(err) { + if err = os.Mkdir(path+"server/docker/containers/docker-ubuntu-sshd", os.ModePerm); err != nil { + return err + } + } - } + } - // Clone base docker image - _, err = git.PlainClone(path+"server/docker/containers/docker-ubuntu-sshd", false, &git.CloneOptions{ - URL: "https://github.com/Akilan1999/docker-ubuntu-sshd", - Progress: os.Stdout, - }) - if err != nil { - return err - } + // Clone base docker image + _, err = git.PlainClone(path+"server/docker/containers/docker-ubuntu-sshd", false, &git.CloneOptions{ + URL: "https://github.com/Akilan1999/docker-ubuntu-sshd", + Progress: os.Stdout, + }) + if err != nil { + return err + } - return + return } // GeneratePluginDirectory Generates plugin directory structure func GeneratePluginDirectory() (err error) { - path, err := GetCurrentPath() - if err != nil { - return err - } - if _, err = os.Stat(path + "plugin"); os.IsNotExist(err) { - if err = os.Mkdir(path+"plugin", os.ModePerm); err != nil { - return err - } - if _, err = os.Stat(path + "plugin/deploy"); os.IsNotExist(err) { - if err = os.Mkdir(path+"plugin/deploy", os.ModePerm); err != nil { - return err - } - } - } - return + path, err := GetCurrentPath() + if err != nil { + return err + } + if _, err = os.Stat(path + "plugin"); os.IsNotExist(err) { + if err = os.Mkdir(path+"plugin", os.ModePerm); err != nil { + return err + } + if _, err = os.Stat(path + "plugin/deploy"); os.IsNotExist(err) { + if err = os.Mkdir(path+"plugin/deploy", os.ModePerm); err != nil { + return err + } + } + } + return } func GenerateClientTrackContainers() (err error) { - path, err := GetCurrentPath() - if err != nil { - return err - } - if _, err = os.Stat(path + "client"); os.IsNotExist(err) { - if err = os.Mkdir(path+"client", os.ModePerm); err != nil { - return err - } - if err = os.Mkdir(path+"client/trackcontainers", os.ModePerm); err != nil { - return err - } + path, err := GetCurrentPath() + if err != nil { + return err + } + if _, err = os.Stat(path + "client"); os.IsNotExist(err) { + if err = os.Mkdir(path+"client", os.ModePerm); err != nil { + return err + } + if err = os.Mkdir(path+"client/trackcontainers", os.ModePerm); err != nil { + return err + } - if _, err = os.Stat(path + "client/trackcontainers/trackcontainers.json"); os.IsNotExist(err) { - _, err = os.Create(path + "client/trackcontainers/trackcontainers.json") - if err != nil { - return err - } - _, err = os.Create(path + "client/trackcontainers/grouptrackcontainers.json") - if err != nil { - return err - } - } + if _, err = os.Stat(path + "client/trackcontainers/trackcontainers.json"); os.IsNotExist(err) { + _, err = os.Create(path + "client/trackcontainers/trackcontainers.json") + if err != nil { + return err + } + _, err = os.Create(path + "client/trackcontainers/grouptrackcontainers.json") + if err != nil { + return err + } + } - } - return + } + return } // MakeSSHKeyPair make a pair of public and private keys for SSH access. @@ -173,24 +173,31 @@ func GenerateClientTrackContainers() (err error) { // Private Key generated is PEM encoded // source: https://gist.github.com/goliatone/e9c13e5f046e34cef6e150d06f20a34c func MakeSSHKeyPair(pubKeyPath, privateKeyPath string) error { - privateKey, err := rsa.GenerateKey(rand.Reader, 1024) - if err != nil { - return err - } - // generate and write private key as PEM - privateKeyFile, err := os.Create(privateKeyPath) - defer privateKeyFile.Close() - if err != nil { - return err - } - privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)} - if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil { - return err - } - // generate and write public key - pub, err := ssh.NewPublicKey(&privateKey.PublicKey) - if err != nil { - return err - } - return ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0655) + privateKey, err := rsa.GenerateKey(rand.Reader, 1024) + if err != nil { + return err + } + // generate and write private key as PEM + privateKeyFile, err := os.Create(privateKeyPath) + defer privateKeyFile.Close() + if err != nil { + return err + } + + // Set permission to private key to SSH into docker machine + err = os.Chmod("privateKeyPath", 600) + if err != nil { + return err + } + + privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)} + if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil { + return err + } + // generate and write public key + pub, err := ssh.NewPublicKey(&privateKey.PublicKey) + if err != nil { + return err + } + return ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0655) } diff --git a/server/docker/docker.go b/server/docker/docker.go index 4eb5731..d0d9473 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -20,16 +20,18 @@ import ( ) type DockerVM struct { - SSHUsername string `json:"SSHUsername"` - SSHPassword string `json:"SSHPassword"` - ID string `json:"ID"` - TagName string `json:"TagName"` - ImagePath string `json:"ImagePath"` - Ports Ports `json:"Ports"` - GPU string `json:"GPU"` - TempPath string - BaseImage string - LogsPath string + SSHUsername string `json:"SSHUsername"` + SSHPublcKey string `json:"SSHPublicKey"` + SSHPrivateKey string `json:"SSHPrivateKey"` + ID string `json:"ID"` + TagName string `json:"TagName"` + ImagePath string `json:"ImagePath"` + Ports Ports `json:"Ports"` + GPU string `json:"GPU"` + TempPath string + BaseImage string + LogsPath string + SSHCommand string `json:"SSHCommand"` } type DockerContainers struct { @@ -73,13 +75,14 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string, baseImage // Sets if GPU is selected or not RespDocker.GPU = GPU + // Get config informatopn + // Sets Free port to Struct //RespDocker.SSHPort = Ports[0] //RespDocker.VNCPort = Ports[1] // Sets appropriate username and password to the // variables in the struct - RespDocker.SSHUsername = "master" - RespDocker.SSHPassword = "password" + RespDocker.SSHUsername = "root" //RespDocker.BaseImage = "ubuntu:20.04" //RespDocker.VNCPassword = "vncpassword" @@ -92,6 +95,8 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string, baseImage } RespDocker.ImagePath = config.DefaultDockerFile RespDocker.LogsPath = config.DockerRunLogs + RespDocker.SSHPublcKey = config.PublicKeyFile + RespDocker.SSHPrivateKey = config.PrivateKeyFile // We are checking if the container name is not nil and not equal to the default one used // which is docker-ubuntu-sshd @@ -193,11 +198,12 @@ func (d *DockerVM) imageBuild(dockerClient *client.Client) error { //defer cancel() var cmd bytes.Buffer - - cmd.WriteString("docker build -t " + d.TagName + " " + d.ImagePath + "/" + d.TagName) + cmd.WriteString("docker build -t " + d.TagName + " " + d.ImagePath + "/" + d.TagName + ` --build-arg SSH_KEY="$(cat ` + d.SSHPublcKey + `)"`) //"-v=/opt/data:/data p2p-ubuntu /start > /dev/null" cmdStr := cmd.String() - _, err := exec.Command("/bin/sh", "-c", cmdStr).Output() + fmt.Println(cmdStr) + output, err := exec.Command("/bin/sh", "-c", cmdStr).Output() + fmt.Printf("%s", output) if err != nil { return err } diff --git a/server/server.go b/server/server.go index 3c752d5..802b549 100644 --- a/server/server.go +++ b/server/server.go @@ -127,7 +127,6 @@ func Server() (*gin.Engine, error) { if err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) } - fmt.Println(resp) } c.JSON(http.StatusOK, resp)