Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnus major #83

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"foxundermoon.shell-format"
]
}
Empty file removed challenges/.gitkeep
Empty file.
6 changes: 3 additions & 3 deletions config.sample.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kubeconfig = ""
kubeconfig = "/home/ujjwal/.kube/config"
kubehost = "0.0.0.0"
backendurl = "http://10.25.1.15:15528"
backendurl = "http://10.25.1.15:3000"
kubenamespace = "default"
timeout = 20 # in seconds

Expand All @@ -19,7 +19,7 @@ templated_manifests = [

[services.api]
host = "0.0.0.0"
port = 15528
port = 3000

[teamvm]
teampodname = "katana-team-master-pod"
Expand Down
2 changes: 1 addition & 1 deletion katana-services
6 changes: 4 additions & 2 deletions lib/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ func DeployCluster(kubeconfig *rest.Config, kubeclientset *kubernetes.Clientset)

clientset, _ := utils.GetKubeClient()

nodes, _ := utils.GetNodes(clientset)

nodes, err := utils.GetNodes(clientset)
if err != nil {
log.Println(err)
}
deploymentConfig.NodeAffinityValue = nodes[0].Name

for _, m := range clusterConfig.TemplatedManifests {
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var link *mongo.Database

func setupAdmin() error {
adminUser := configs.AdminConfig
pwd:= utils.SHA256(adminUser.Password)
pwd := utils.SHA256(adminUser.Password)
admin := types.AdminUser{
Username: adminUser.Username,
Password: pwd,
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func setup() error {

func setupGogs() error {
if err := CreateDatabase(gogsDatabase); err != nil {
return fmt.Errorf("cannot create database: %w", err)
log.Println("cannot create database: %w", err)
}
if err := CreateGogsAdmin(configs.AdminConfig.Username, configs.AdminConfig.Password); err != nil {
return fmt.Errorf("cannot create gogs admin: %w", err)
Expand Down
71 changes: 71 additions & 0 deletions lib/retrieve_data/jsonSaver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package retrieve_data

import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"github.com/sdslabs/katana/lib/mongo"
"github.com/sdslabs/katana/lib/utils"
"go.mongodb.org/mongo-driver/bson"
)

var ticker *time.Ticker
var tick int

func saveJSON() {
for range ticker.C {
data := mongo.FetchDocs(context.Background(), "teams", bson.M{})
path := fmt.Sprintf("./json_data/data-tick-%d.json", tick)
jsonData, err := convertBSONArrayToJSONArray(data)
if err != nil {
fmt.Println(err)
return
}
storeJSONToFile(jsonData, path)
tick++
}
}

func StartSaving() {
ticker = utils.GetTicker()
utils.CreateDirectoryIfNotExists("json_data")
go saveJSON()
}

func convertBSONArrayToJSONArray(bsonArray []bson.M) ([]byte, error) {
var jsonArray []map[string]interface{}

for _, bsonDoc := range bsonArray {
delete(bsonDoc, "publicKey")
delete(bsonDoc, "password")

jsonArray = append(jsonArray, bsonDoc)
}

jsonData, err := json.Marshal(jsonArray)
if err != nil {
return nil, fmt.Errorf("failed to marshal JSON array: %w", err)
}

return jsonData, nil
}

func storeJSONToFile(jsonData []byte, filePath string) error {
jsonMap := map[string]interface{}{"data": json.RawMessage(jsonData)}

finalJSONData, err := json.MarshalIndent(jsonMap, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal final JSON: %w", err)
}

err = os.WriteFile(filePath, finalJSONData, 0644)
if err != nil {
return fmt.Errorf("failed to write JSON to file: %w", err)
}

fmt.Printf("JSON data stored in file: %s\n", filePath)
return nil
}
21 changes: 20 additions & 1 deletion lib/utils/os.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

import "os/exec"
import (
"fmt"
"os"
"os/exec"
)

func RunCommand(cmd string) error {
out := exec.Command("bash", "-c", cmd)
Expand All @@ -11,3 +15,18 @@ func RunCommand(cmd string) error {

return nil
}

func CreateDirectoryIfNotExists(dirPath string) error {
if _, err := os.Stat(dirPath); !os.IsNotExist(err) {
if err := os.RemoveAll(dirPath); err != nil {
return fmt.Errorf("failed to delete existing directory: %w", err)
}
fmt.Printf("Directory '%s' deleted.\n", dirPath)
}

if err := os.MkdirAll(dirPath, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
fmt.Printf("Directory '%s' created.\n", dirPath)
return nil
}
147 changes: 98 additions & 49 deletions services/challengedeployerservice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,48 @@ import (
"github.com/sdslabs/katana/lib/utils"
"github.com/sdslabs/katana/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/BurntSushi/toml"
)

type ChallengeToml struct {
Challenge Challenge `toml:"challenge"`
}

type Challenge struct {
Author Author `toml:"author"`
Metadata Metadata `toml:"metadata"`
Env Env `toml:"env"`
}

type Author struct {
Name string `toml:"name"`
Email string `toml:"email"`
}

type Metadata struct {
Name string `toml:"name"`
Flag string `toml:"flag"`
Description string `toml:"description"`
Type string `toml:"type"`
Points int `toml:"points"`
}

type Env struct {
PortMappings []string `toml:"port_mappings"`
}

func LoadConfiguration(configFile string) ChallengeToml {
var config ChallengeToml
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
return ChallengeToml{}
}
return config
}

func DeployChallenge(c *fiber.Ctx) error {

challengeType := "web"
challengeType := ""
folderName := ""
patch := false
replicas := int32(1)
Expand Down Expand Up @@ -69,6 +106,9 @@ func DeployChallenge(c *fiber.Ctx) error {
return c.SendString("Error in unarchiving")
}

data := LoadConfiguration("./challenges/"+folderName+"/config.toml")
challengeType = data.Challenge.Metadata.Type

//Update challenge path to get dockerfile
utils.BuildDockerImage(folderName, challengePath+"/"+folderName)

Expand All @@ -84,16 +124,20 @@ func DeployChallenge(c *fiber.Ctx) error {
Uptime: 0,
Attacks: 0,
Defences: 0,
Points: 0,
Flag: "flag{test}",
Points: data.Challenge.Metadata.Points,
Flag: data.Challenge.Metadata.Flag,
}
err := mongo.AddChallenge(challenge, teamName)
if err != nil {
fmt.Println("Error in adding challenge to mongo")
log.Println(err)
}
deployment.DeployChallengeToCluster(folderName, teamName, patch, replicas)
url, err := createServiceForChallenge(folderName, teamName, 3000, i)
port, err := strconv.ParseInt(strings.Split(data.Challenge.Env.PortMappings[0], ":")[1], 10, 32)
if err != nil {
log.Println("Error occured")
}
url, err := createServiceForChallenge(folderName, teamName, int32(port), i)
if err != nil {
res = append(res, []string{teamName, err.Error()})
} else {
Expand Down Expand Up @@ -124,59 +168,64 @@ func ChallengeUpdate(c *fiber.Ctx) error {
return err
}

dir := p.Repository.FullName
s := strings.Split(dir, "/")
challengeName := s[1]
teamName := s[0]
namespace := teamName + "-ns"
log.Println("Challenge update request received for", challengeName, "by", teamName)
repo, err := git.PlainOpen("teams/" + dir)
if err != nil {
log.Println(err)
}
if p.Ref != "refs/heads/master" {
return c.SendString("Push not on master branch. Ignoring")
} else {

auth := &http.BasicAuth{
Username: g.AdminConfig.Username,
Password: g.AdminConfig.Password,
}
dir := p.Repository.FullName
s := strings.Split(dir, "/")
challengeName := s[1]
teamName := s[0]
namespace := teamName + "-ns"
log.Println("Challenge update request received for", challengeName, "by", teamName)
repo, err := git.PlainOpen("teams/" + dir)
if err != nil {
log.Println(err)
}

worktree, err := repo.Worktree()
worktree.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
})
auth := &http.BasicAuth{
Username: g.AdminConfig.Username,
Password: g.AdminConfig.Password,
}

if err != nil {
log.Println("Error pulling changes:", err)
}
worktree, err := repo.Worktree()
worktree.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
})

imageName := strings.Replace(dir, "/", "-", -1)
if err != nil {
log.Println("Error pulling changes:", err)
}

log.Println("Pull successful for", teamName, ". Building image...")
firstPatch := !utils.DockerImageExists(imageName)
utils.BuildDockerImage(imageName, "./teams/"+dir)
imageName := strings.Replace(dir, "/", "-", -1)

if firstPatch {
log.Println("First Patch for", teamName)
deployment.DeployChallengeToCluster(challengeName, teamName, patch, replicas)
} else {
log.Println("Not the first patch for", teamName, ". Simply deploying the image...")
labelSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
"app": challengeName,
},
}
// Delete the challenge pod
err = client.CoreV1().Pods(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&labelSelector),
})
if err != nil {
log.Println("Error")
log.Println(err)
log.Println("Pull successful for", teamName, ". Building image...")
firstPatch := !utils.DockerImageExists(imageName)
utils.BuildDockerImage(imageName, "./teams/"+dir)

if firstPatch {
log.Println("First Patch for", teamName)
deployment.DeployChallengeToCluster(challengeName, teamName, patch, replicas)
} else {
log.Println("Not the first patch for", teamName, ". Simply deploying the image...")
labelSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
"app": challengeName,
},
}
// Delete the challenge pod
err = client.CoreV1().Pods(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&labelSelector),
})
if err != nil {
log.Println("Error")
log.Println(err)
}
}
log.Println("Image built for", teamName)
return c.SendString("Challenge updated")
}
log.Println("Image built for", teamName)
return c.SendString("Challenge updated")
}

func DeleteChallenge(c *fiber.Ctx) error {
Expand Down
1 change: 0 additions & 1 deletion services/flaghandlerservice/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func Server() {
fmt.Println(err)
return
}

}
}()
}
Loading
Loading