Skip to content

Commit

Permalink
Merge branch 'develop' into katana-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
rizul2108 authored Dec 22, 2023
2 parents 35ad757 + 4a7522a commit 828f99a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion katana-services
12 changes: 12 additions & 0 deletions lib/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
86 changes: 85 additions & 1 deletion lib/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"fmt"

"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion services/infrasetservice/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func BuildKatanaServices() error {
log.Fatal(errDir)
return errDir
}

katanaServicesDir := katanaDir + "/katana-services"

services, err := os.ReadDir(katanaServicesDir)
Expand Down

0 comments on commit 828f99a

Please sign in to comment.