diff --git a/katana-services b/katana-services index 34fb474..a79e56b 160000 --- a/katana-services +++ b/katana-services @@ -1 +1 @@ -Subproject commit 34fb47432e1118a50ca0f6f0a99e858a115b320f +Subproject commit a79e56b2458f84a42fa51d293fa1bce036af57b5 diff --git a/lib/utils/crypto.go b/lib/utils/crypto.go index 66fb0d4..6c2ea3e 100644 --- a/lib/utils/crypto.go +++ b/lib/utils/crypto.go @@ -47,6 +47,12 @@ func GenerateCerts(domain string, basePath string) error { return err } + // using -traditional flag to get PKCS#1 [different header], otherwise 500 Internal Error + cmd = "openssl rsa -in " + basePath + "/ca.key -out " + basePath + "/ca.key -traditional" + if err := RunCommand(cmd); err != nil { + return err + } + // Generate ca.crt cmd = "openssl req -x509 -new -nodes -sha512 -days 3650 -subj '/C=IN/ST=Delhi/L=Delhi/O=Katana/CN=" + domain + "' -key " + basePath + "/ca.key -out " + basePath + "/ca.crt" if err := RunCommand(cmd); err != nil { @@ -60,6 +66,12 @@ func GenerateCerts(domain string, basePath string) error { return err } + // using -traditional flag to get PKCS#1 [different header], otherwise 500 Internal Error + cmd = "openssl rsa -in " + basePath + "/" + domain + ".key -out " + basePath + "/" + domain + ".key -traditional" + if err := RunCommand(cmd); err != nil { + return err + } + // Generate private key cmd = "openssl genrsa -out " + basePath + "/" + domain + ".key 4096" if err := RunCommand(cmd); err != nil { diff --git a/lib/utils/docker.go b/lib/utils/docker.go index b4591dd..b08bb88 100644 --- a/lib/utils/docker.go +++ b/lib/utils/docker.go @@ -45,6 +45,10 @@ func CheckDockerfile(_DockerfilePath string) bool { return !os.IsNotExist(err) } +func CheckDockerfile(_DockerfilePath string) bool { + _, err := os.Stat(_DockerfilePath + "/Dockerfile") + return !os.IsNotExist(err) +} func BuildDockerImage(_ChallengeName string, _DockerfilePath string) error { buf := new(bytes.Buffer) if err := Tar(_DockerfilePath, buf); err != nil { diff --git a/lib/utils/kube.go b/lib/utils/kube.go index e246cff..f9b2c43 100644 --- a/lib/utils/kube.go +++ b/lib/utils/kube.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "fmt" + + "io" "io/ioutil" "log" "os" @@ -259,7 +261,89 @@ func CopyIntoPod(podName string, containerName string, pathInPod string, localFi return nil } -func GetKatanaLoadbalancer() (string,error) { +func CopyFromPod(podName string, containerName string, pathInPod string, localFilePath string, ns ...string) error { + config, err := GetKubeConfig() + if err != nil { + return err + } + + client, err := GetKubeClient() + if err != nil { + return err + } + + namespace := "katana" + if len(ns) > 0 { + namespace = ns[0] + } + + pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) + if err != nil { + log.Printf("Error getting pod: %s\n", err) + } + + // Find the container in the pod + var container *corev1.Container + for _, c := range pod.Spec.Containers { + if c.Name == containerName { + container = &c + break + } + } + + if container == nil { + log.Printf("Container not found in pod\n") + err = fmt.Errorf("container not found in pod") + return err + } + + // Create a stream to the container + req := client.CoreV1().RESTClient().Post(). + Resource("pods"). + Name(podName). + Namespace(namespace). + SubResource("exec"). + Param("container", containerName) + + req.VersionedParams(&corev1.PodExecOptions{ + Container: containerName, + Command: []string{"cat", pathInPod}, + Stdin: false, + Stdout: true, + Stderr: true, + TTY: false, + }, scheme.ParameterCodec) + + exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) + if err != nil { + log.Printf("Error creating executor: %s\n", err) + return err + } + + localFile, err := os.Create(localFilePath) + if err != nil { + log.Printf("Error creating local file: %s\n", err) + return err + } + defer localFile.Close() + + // Stream the file + err = exec.Stream(remotecommand.StreamOptions{ + Stdin: nil, + Stdout: localFile, + Stderr: os.Stderr, + Tty: false, + }) + if err != nil { + log.Printf("Error streaming the file: %s\n", err) + return err + } + + log.Println("File copied successfully") + return nil +} + + func GetKatanaLoadbalancer() (string,error) { client, err := GetKubeClient() if err != nil { log.Fatal(err) diff --git a/services/infrasetservice/helper.go b/services/infrasetservice/helper.go index 6192645..cb5a3e7 100644 --- a/services/infrasetservice/helper.go +++ b/services/infrasetservice/helper.go @@ -121,7 +121,7 @@ func BuildKatanaServices() error { log.Fatal(errDir) return errDir } - + katanaServicesDir := katanaDir + "/katana-services" services, err := os.ReadDir(katanaServicesDir)