Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SomyaChawla0250 committed Sep 10, 2023
1 parent 6daf53d commit 9166a63
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 169 deletions.
4 changes: 1 addition & 3 deletions kubernetes/manifests/kashira.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@ spec:
serviceAccountName: cluster-admin
containers:
- name: kashira
image: ashpect/kashira
image: harbor.katana.local/katana/kashira
ports:
- containerPort: 80
volumeMounts:
- name: kashira-data
mountPath: /opt/kashira/flag-data

env:
- name: DAEMON_PORT
valueFrom:
configMapKeyRef:
name: kashira-config
key: daemon_port

volumeClaimTemplates:
- metadata:
name: kashira-data
Expand Down
3 changes: 1 addition & 2 deletions kubernetes/manifests/kissaki.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
serviceAccountName: cluster-admin
containers:
- name: kissaki
image: ashpect/kissaki
image: harbor.katana.local/katana/kissaki
ports:
- containerPort: 80
volumeMounts:
Expand All @@ -40,7 +40,6 @@ spec:
configMapKeyRef:
name: kashira-config
key: daemon_port

volumeClaimTemplates:
- metadata:
name: kissaki-data
Expand Down
148 changes: 0 additions & 148 deletions lib/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,154 +63,6 @@ func GenerateCerts(domain string, basePath string) error {
return nil
}

// [DO NOT REMOVE....WIP....]

// Generating certificates without cmd commands
// func GenerateCerts(domain string, basePath string) error {
// log.Println("Cert 1")
// // Generate ca.key in harbor directory
// caKey, err := rsa.GenerateKey(rand.Reader, 4096)
// if err != nil {
// return err
// }
// caKeyBytes := x509.MarshalPKCS1PrivateKey(caKey)
// caKeyFile, err := os.Create(basePath + "/ca.key")
// if err != nil {
// return err
// }
// defer caKeyFile.Close()
// if err := pem.Encode(caKeyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: caKeyBytes}); err != nil {
// return err
// }

// log.Println("Cert 2")
// // Generate ca.crt
// caTemplate := &x509.Certificate{
// SerialNumber: big.NewInt(1),
// Subject: pkix.Name{
// Country: []string{"IN"},
// Organization: []string{"Katana"},
// OrganizationalUnit: []string{"Katana CA"},
// Locality: []string{"Delhi"},
// Province: []string{"Delhi"},
// CommonName: domain,
// },
// NotBefore: time.Now(),
// NotAfter: time.Now().AddDate(10, 0, 0), // 10 years validity
// KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
// ExtKeyUsage: []x509.ExtKeyUsage{
// x509.ExtKeyUsageServerAuth,
// },
// BasicConstraintsValid: true,
// IsCA: true,
// }
// caBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, &caKey.PublicKey, caKey)
// if err != nil {
// return err
// }
// caCertFile, err := os.Create(basePath + "/ca.crt")
// if err != nil {
// return err
// }
// defer caCertFile.Close()
// if err := pem.Encode(caCertFile, &pem.Block{Type: "CERTIFICATE", Bytes: caBytes}); err != nil {
// return err
// }

// log.Println("Cert 3")
// // Generate private key
// privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
// if err != nil {
// return err
// }
// privateKeyFile, err := os.Create(basePath + "/" + domain + ".key")
// if err != nil {
// return err
// }
// defer privateKeyFile.Close()
// privateKeyBytes, err := x509.MarshalECPrivateKey(privateKey)
// if err != nil {
// return err
// }
// if err := pem.Encode(privateKeyFile, &pem.Block{Type: "EC PRIVATE KEY", Bytes: privateKeyBytes}); err != nil {
// return err
// }

// log.Println("Cert 4")
// // Generate certificate signing request
// csrTemplate := &x509.CertificateRequest{
// Subject: pkix.Name{
// Country: []string{"IN"},
// Organization: []string{"Katana"},
// OrganizationalUnit: []string{"Katana"},
// Locality: []string{"Delhi"},
// Province: []string{"Delhi"},
// CommonName: domain,
// },
// DNSNames: []string{domain},
// }
// csrBytes, err := x509.CreateCertificateRequest(rand.Reader, csrTemplate, privateKey)
// if err != nil {
// return err
// }
// csrFile, err := os.Create(basePath + "/" + domain + ".csr")
// if err != nil {
// return err
// }
// defer csrFile.Close()
// if err := pem.Encode(csrFile, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}); err != nil {
// return err
// }

// log.Println("Cert 5")
// // Generate v3.ext file
// extFileContent := fmt.Sprintf("authorityKeyIdentifier=keyid,issuer\nbasicConstraints=CA:FALSE\nkeyUsage=digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\nsubjectAltName=DNS:%s", domain)
// extFile, err := os.Create(basePath + "/v3.ext")
// if err != nil {
// return err
// }
// defer extFile.Close()
// if _, err := extFile.WriteString(extFileContent); err != nil {
// return err
// }

// log.Println("Cert 6")
// // Generate certificate
// certTemplate := &x509.Certificate{
// SerialNumber: big.NewInt(2),
// Subject: pkix.Name{
// Country: []string{"IN"},
// Organization: []string{"Katana"},
// OrganizationalUnit: []string{"Katana"},
// Locality: []string{"Delhi"},
// Province: []string{"Delhi"},
// CommonName: domain,
// },
// NotBefore: time.Now(),
// NotAfter: time.Now().AddDate(10, 0, 0), // 10 years validity
// KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
// ExtKeyUsage: []x509.ExtKeyUsage{
// x509.ExtKeyUsageServerAuth,
// },
// BasicConstraintsValid: true,
// }
// certBytes, err := x509.CreateCertificate(rand.Reader, certTemplate, caTemplate, &privateKey.PublicKey, caKey)
// if err != nil {
// return err
// }
// certFile, err := os.Create(basePath + "/" + domain + ".crt")
// if err != nil {
// return err
// }
// defer certFile.Close()
// if err := pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}); err != nil {
// return err
// }

// log.Println("Cert 7")
// return nil
// }

func HashPassword(password string) (string, error) {
pass := []byte(password)
hash, err := bcrypt.GenerateFromPassword(pass, bcrypt.MinCost)
Expand Down
5 changes: 0 additions & 5 deletions lib/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ func dockerLogin(username string, password string) {
log.Println("Logged into Harbor successfully")
}

func CheckDockerfile(_DockerfilePath string) bool {
_, err := os.Stat(_DockerfilePath + "/Dockerfile")
return !os.IsNotExist(err)
}

func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {
buf := new(bytes.Buffer)
if err := Tar(_DockerfilePath, buf); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions lib/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package utils
import (
"bytes"
"context"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"io"

g "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/types"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -353,7 +354,7 @@ func GetNodes(clientset *kubernetes.Clientset) ([]corev1.Node, error) {
return nodes.Items, nil
}

func CopyTarIntoPod(podName string, containerName string, pathInPod string, localFilePath string, ns ...string) error {
func CopyIntoPod(podName string, containerName string, pathInPod string, localFilePath string, ns ...string) error {
config, err := GetKubeConfig()
if err != nil {
return err
Expand All @@ -370,7 +371,7 @@ func CopyTarIntoPod(podName string, containerName string, pathInPod string, loca
}

reader, writer := io.Pipe()

go func() {
defer writer.Close()
err := Tar(localFilePath, writer)
Expand All @@ -394,7 +395,7 @@ func CopyTarIntoPod(podName string, containerName string, pathInPod string, loca
if container == nil {
log.Printf("Container not found in pod\n")
}

// Create a stream to the container
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Expand Down Expand Up @@ -433,4 +434,3 @@ func CopyTarIntoPod(podName string, containerName string, pathInPod string, loca
log.Println("File copied successfully")
return nil
}

10 changes: 5 additions & 5 deletions services/challengedeployerservice/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"log"
"os"
"strconv"

git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
g "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/lib/utils"
v1 "k8s.io/api/core/v1"
)


func copyChallengeIntoTsuka(dirPath string, challengeName string, challengeType string) error {
localFilePath := dirPath + "/" + challengeName
pathInPod := "/opt/katana/katana_" + challengeType + "_" + challengeName + ".tar.gz"
Expand Down Expand Up @@ -53,7 +53,7 @@ func copyChallengeIntoTsuka(dirPath string, challengeName string, challengeType
// Loop over pods
for _, pod := range pods {
// Copy file into pod
if err := utils.CopyTarIntoPod(pod.Name, g.TeamVmConfig.ContainerName, pathInPod, localFilePath, pod.Namespace); err != nil {
if err := utils.CopyIntoPod(pod.Name, g.TeamVmConfig.ContainerName, pathInPod, localFilePath, pod.Namespace); err != nil {
log.Println(err)
return err
}
Expand Down Expand Up @@ -115,9 +115,9 @@ func createFolder(challengeName string) (message int, challengePath string) {
func copyChallengeCheckerIntoKissaki(dirPath string, challengeName string) error {
srcFilePath := dirPath + "/" + challengeName + "-challenge-checker"
pathInPod := "/opt/kissaki/kissaki_" + challengeName + ".tar.gz"
log.Println("Testing... sourceFilePath is " + srcFilePath + "....and... pathInPod is " + pathInPod)
log.Println("Testing... sourceFilePath is " + srcFilePath + "....and... pathInPod is " + pathInPod)

if err := utils.CopyTarIntoPod("kissaki-0", "kissaki", pathInPod, srcFilePath, "katana"); err != nil {
if err := utils.CopyIntoPod("kissaki-0", "kissaki", pathInPod, srcFilePath, "katana"); err != nil {
log.Println(err)
return err
}
Expand All @@ -129,7 +129,7 @@ func copyFlagDataIntoKashira(dirPath string, challengeName string) error {
pathInPod := "/opt/kashira/kashira_" + challengeName + ".tar.gz"
log.Println("Testing... sourceFilePath is " + srcFilePath + "....and... pathInPod is" + pathInPod)

if err := utils.CopyTarIntoPod("kashira-0", "kashira", pathInPod, srcFilePath, "katana"); err != nil {
if err := utils.CopyIntoPod("kashira-0", "kashira", pathInPod, srcFilePath, "katana"); err != nil {
log.Println(err)
return err
}
Expand Down
1 change: 0 additions & 1 deletion services/flaghandlerservice/helper.go

This file was deleted.

0 comments on commit 9166a63

Please sign in to comment.