Skip to content

Commit

Permalink
add ticker, modify teams collection to include flags
Browse files Browse the repository at this point in the history
  • Loading branch information
P3rcy-8685 committed Jul 12, 2023
1 parent 3e6b831 commit 3a5e8f6
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
15 changes: 9 additions & 6 deletions lib/mongo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ func AddAdmin(ctx context.Context, admin types.AdminUser) error {
return UpsertOne(ctx, AdminCollection, bson.M{UsernameKey: admin.Username}, admin)
}

func AddChallenge(challenge types.Challenge, teamName string ) error {
filter := bson.M{"name": teamName}
update := bson.M{"$push": bson.M{"challenges": challenge}}
_, err := link.Collection(TeamsCollection).UpdateMany(context.Background(), filter, update)
return err
}
func AddChallenge(challenge types.Challenge, teamName string) error {
teamFilter := bson.M{"username":teamName}
update := bson.M{"$push": bson.M{"challenges": challenge}}
_, err := link.Collection(TeamsCollection).UpdateOne(context.TODO(), teamFilter, update)
if err != nil {
return err
}
return nil
}
16 changes: 11 additions & 5 deletions manifests/templates/cluster-admin-role.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-admin-role
namespace: katana
rules:
- apiGroups: [""]
resources: ["pods", "pods/exec"]
- apiGroups: ["apps"]
resources: ["pods", "pods/exec", "services", "statefulsets","statefulsets/scale"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cluster-admin-role-binding
namespace: katana
subjects:
- kind: ServiceAccount
name: cluster-admin
namespace: default
namespace: katana
roleRef:
kind: ClusterRole
name: cluster-admin-role
apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ServiceAccount
metadata:
name: cluster-admin
namespace: katana
13 changes: 9 additions & 4 deletions manifests/templates/kashira.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: v1
kind: PersistentVolume
metadata:
name: kashira-pv
namespace: katana
spec:
capacity:
storage: 1Gi
Expand All @@ -17,6 +18,7 @@ apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: kashira-pvc
namespace: katana
spec:
accessModes:
- ReadWriteOnce
Expand All @@ -30,6 +32,7 @@ apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kashira
namespace: katana
annotations:
tick: "false"
spec:
Expand All @@ -40,18 +43,20 @@ spec:
replicas: 1
template:
metadata:
namespace: katana
labels:
app: kashira
spec:
serviceAccountName: cluster-admin
containers:
- name: kashira
image: core.harbor.domain/katana/kashira:latest
image: percy477/kashira:latest
ports:
- containerPort: 80
volumeClaimTemplates:
- metadata:
name: data
namespace: katana
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: standard
Expand All @@ -64,11 +69,11 @@ apiVersion: v1
kind: Service
metadata:
name: kashira-svc
namespace: katana
spec:
clusterIP: None
selector:
app: example-app
app: kashira
ports:
- protocol: TCP
port: 80
targetPort: 80
targetPort: 80
14 changes: 6 additions & 8 deletions services/flaghandlerservice/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var namespace string = "katana"
var podName string = "kashira-0"
var podName string = "kashira"
var containerName string = "kashira"

func SendFlagCheckerAndUpdaterToKashira(localFilePath string) {
Expand All @@ -29,20 +29,18 @@ func Server() {

go func() {
for range ticker.C {
// Get the pod
pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
pod, err := client.AppsV1().StatefulSets(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
return
}

// Modify annotations
pod.Annotations["tick"] = "true"

// Update the pod
_, err = client.CoreV1().Pods(namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
_, err = client.AppsV1().StatefulSets(namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
if err != nil {
fmt.Println(err)
return
}

}
}()
}
7 changes: 6 additions & 1 deletion services/master/controllers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -131,7 +132,11 @@ func Deploy(c *fiber.Ctx) error {
Defenses:0,
Flag:"flag{test}",
}
mongo.AddChallenge(challenge,teamName)
err := mongo.AddChallenge(challenge,teamName)
if err != nil {
fmt.Println("Error in adding challenge to mongo")
log.Println(err)
}
utils.DeployChallenge(folderName, teamName, patch, replicas)
url, err := deployer.CreateService(folderName, teamName)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions services/master/controllers/ticker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package controllers

import (
"github.com/gofiber/fiber/v2"
t"github.com/sdslabs/katana/services/flaghandlerservice"
)

func StartTicker(c *fiber.Ctx) error {
t.Server()
return c.SendString("Ticker Started")
}
3 changes: 2 additions & 1 deletion services/master/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func Server() error {

utils.InitTicker(5 * time.Minute)
utils.InitTicker(10 * time.Second)

fiberConfig := fiber.Config{
ReadTimeout: 5 * time.Second,
Expand Down Expand Up @@ -51,6 +51,7 @@ func Server() error {
admin.Post("/deploy", c.Deploy)
admin.Get("/gitServer", c.GitServer)
admin.Get("/cluster/:id", c.ClusterInfo)
admin.Get("/startTicker",c.StartTicker)
admin.Get("/deleteChallenge/:chall_name", c.DeleteChallenge)
fmt.Printf("Listening on %s:%d\n", cfg.APIConfig.Host, cfg.APIConfig.Port)
return app.Listen(fmt.Sprintf("%s:%d", cfg.APIConfig.Host, cfg.APIConfig.Port))
Expand Down
14 changes: 7 additions & 7 deletions services/sshproviderservice/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ func CreateTeams(teamnumber int) error {
if err != nil {
return err
}
podNamespace := "katana-team-" + fmt.Sprint(i) + "-ns"
// start watching for container events
go envVariables(gogs, pwd, podNamespace)
podNamespace := "katana-team-" + fmt.Sprint(i)
team := types.CTFTeam{
Index: i,
Name: podNamespace,
PodName: podName,
Password: hashed,
Index: i,
Name: podNamespace,
PodName: podName,
Password: hashed,
Challenges: []types.Challenge{},
}
podNamespace = "katana-team-" + fmt.Sprint(i) + "-ns"
go envVariables(gogs, pwd, podNamespace)
mysql.CreateGogsUser(team.Name, pwd)
mysql.CreateAccessToken(team.Name, pwd)
fmt.Fprintf(credsFile, "Team: %d, Username: %s, Password: %s\n", i, team.Name, pwd)
Expand Down

0 comments on commit 3a5e8f6

Please sign in to comment.