about summary refs log tree commit diff
path: root/src/seed.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-08-14 17:28:47 +0200
committermaride <maride@darknebu.la>2018-08-14 17:28:47 +0200
commit6a66ed60af5ba83d3e9c064d41dbd1b7a0f23468 (patch)
tree5c940674cfbcc93b52eb9ed87c78f1236976a859 /src/seed.go
parent728e93e07648adb84c83f77687077d18987e0319 (diff)
Add challenges
Diffstat (limited to 'src/seed.go')
-rw-r--r--src/seed.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/seed.go b/src/seed.go
new file mode 100644
index 0000000..8256929
--- /dev/null
+++ b/src/seed.go
@@ -0,0 +1,86 @@
+package main
+
+import (
+	"flag"
+	"io/ioutil"
+	"log"
+	"encoding/json"
+)
+
+var (
+	challenges = []Challenge{}
+	seedFilePath* string
+)
+
+func registerSeedFlags() {
+	seedFilePath = flag.String("seedFile", "/etc/companion.json", "Path to seedfile")
+}
+
+// Read a given seed file and return their containing challenges
+func readSeedFile(path string) ([]Challenge) {
+	var jsonContents map[string]interface{}
+
+	// Read file
+	rawContents, readError := ioutil.ReadFile(path)
+	if readError != nil {
+		log.Printf("Failed to read seed file at %s: %s", *seedFilePath, readError.Error())
+		return nil
+	}
+
+	// Convert JSON String to map
+	unmarshalError := json.Unmarshal(rawContents, &jsonContents)
+	if unmarshalError != nil {
+		log.Printf("Failed to parse JSON in seed file at %s: %s", *seedFilePath, unmarshalError.Error())
+		return nil
+	}
+
+	tmpChallenges := []Challenge{}
+
+	// Iterate over challenges
+	for index, challengeObject := range jsonContents["challenges"].([]interface{}) {
+		challenge := challengeObject.(map[string]interface{})
+		// add our parsed challenge to array
+
+		name, nameOK := challenge["name"].(string)
+		desc, descOK := challenge["description"].(string)
+		flag, flagOK := challenge["flag"].(string)
+		cont, contOK := challenge["container"].(string)
+		category, categoryOK := challenge["category"].(string)
+
+		if nameOK && descOK && flagOK && contOK && categoryOK {
+			tmpChallenges = append(tmpChallenges, Challenge{
+				Name: name,
+				Description: desc,
+				Flag: flag,
+				Container: cont,
+				Category: category,
+			})
+		} else {
+			log.Printf("Ignoring challenge at position %d: Not all values are parseable (name: %b, desc: %b, flag: %b, container: %b, category: %b", index, nameOK, descOK, flagOK, contOK, categoryOK)
+		}
+	}
+
+	return tmpChallenges
+}
+
+// Read the file we set up using flags and store the returned challenge array
+func getChallengesFromSeedFile() {
+	tmpChallenges := readSeedFile(*seedFilePath)
+
+	if tmpChallenges != nil {
+		challenges = tmpChallenges
+	}
+}
+
+// Generate a JSON string from the stored challenges, stripped from content which might spoil the user
+func generateJSONFromChallenges() (string, error) {
+	// To avoid leakage of container name or the flag towards the user, we need to strip the challenges
+	var strippedChallenges []StrippedChallenge
+
+	for _, challenge := range challenges {
+		strippedChallenges = append(strippedChallenges, stripChallenge(challenge))
+	}
+
+	marshalled, marshalError := json.Marshal(strippedChallenges)
+	return string(marshalled), marshalError
+}