From 4fca210846b28caf1372edb5ec0abe8193d3ff8b Mon Sep 17 00:00:00 2001
From: "kali kaneko (leap communications)" <kali@leap.se>
Date: Wed, 24 Nov 2021 16:18:51 +0100
Subject: [feat] hook motd during bootstrap

some refactor, plus fix docs
---
 branding/motd-cli/README.md         |   6 +-
 branding/motd-cli/main.go           | 123 +++---------------------------------
 branding/motd-cli/motd-example.json |  15 -----
 branding/motd-cli/motd_test.go      |  83 ------------------------
 4 files changed, 11 insertions(+), 216 deletions(-)
 delete mode 100644 branding/motd-cli/motd-example.json
 delete mode 100644 branding/motd-cli/motd_test.go

(limited to 'branding/motd-cli')

diff --git a/branding/motd-cli/README.md b/branding/motd-cli/README.md
index 5d00f10..5e66cee 100644
--- a/branding/motd-cli/README.md
+++ b/branding/motd-cli/README.md
@@ -15,8 +15,8 @@ The structure of the `motd.json` file is like follows:
 ```
 {
     "motd": [{
-        "begin":    "Jan 1 2021 00:00:00",
-        "end":      "Dec 31 2021 23:59:00",
+        "begin":    "01 Nov 21 00:00 -0700",
+        "end":      "31 Jan 22 00:00 -0700",
         "type":     "daily",
         "platform": "all",
         "urgency":  "normal",
@@ -32,7 +32,7 @@ The structure of the `motd.json` file is like follows:
 
 Valid values are: 
 
-* Begin, End are date strings, like "Jan 1 2021 00:00:00".
+* Begin, End are date strings, in the format: "01 Jan 21 00:00:00 -0700".
 * Type: "once" for a one-shot message, "daily" for a message that is displayed daily during the specified duration.
 * Platform: one of "windows", "osx", "snap", "linux", or "all".
 * Urgency: either "normal" or "critical".
diff --git a/branding/motd-cli/main.go b/branding/motd-cli/main.go
index 0ac1316..cea5910 100644
--- a/branding/motd-cli/main.go
+++ b/branding/motd-cli/main.go
@@ -1,110 +1,19 @@
 package main
 
 import (
-	"encoding/json"
 	"flag"
 	"fmt"
 	"io"
 	"io/ioutil"
-	"log"
 	"net/http"
 	"os"
-	"time"
-)
+	"path/filepath"
 
-/* TODO move structs to pkg/config/motd module, import from there */
+	"0xacab.org/leap/bitmask-vpn/pkg/motd"
+)
 
-const defaultFile = "motd-example.json"
 const OK = "✓"
 const WRONG = "☓"
-const TimeString = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
-
-type Messages struct {
-	Messages []Message `json:"motd"`
-}
-
-func (m *Messages) Length() int {
-	return len(m.Messages)
-}
-
-type Message struct {
-	Begin    string          `json:"begin"`
-	End      string          `json:"end"`
-	Type     string          `json:"type"`
-	Platform string          `json:"platform"`
-	Urgency  string          `json:"urgency"`
-	Text     []LocalizedText `json:"text"`
-}
-
-func (m *Message) IsValid() bool {
-	valid := (m.IsValidBegin() && m.IsValidEnd() &&
-		m.IsValidType() && m.IsValidPlatform() && m.IsValidUrgency() &&
-		m.HasLocalizedText())
-	return valid
-}
-
-func (m *Message) IsValidBegin() bool {
-	_, err := time.Parse(TimeString, m.Begin)
-	if err != nil {
-		log.Println(err)
-		return false
-	}
-	return true
-}
-
-func (m *Message) IsValidEnd() bool {
-	endTime, err := time.Parse(TimeString, m.End)
-	if err != nil {
-		log.Println(err)
-		return false
-	}
-	beginTime, err := time.Parse(TimeString, m.Begin)
-	if err != nil {
-		log.Println(err)
-		return false
-	}
-	if !beginTime.Before(endTime) {
-		log.Println("begin ts should be before end")
-		return false
-	}
-	return true
-}
-
-func (m *Message) IsValidType() bool {
-	switch m.Type {
-	case "once", "daily":
-		return true
-	default:
-		return false
-	}
-}
-
-func (m *Message) IsValidPlatform() bool {
-	switch m.Platform {
-	case "windows", "linux", "osx", "all":
-		return true
-	default:
-		return false
-	}
-}
-
-func (m *Message) IsValidUrgency() bool {
-	switch m.Urgency {
-	case "normal", "critical":
-		return true
-	default:
-		return false
-	}
-}
-
-func (m *Message) HasLocalizedText() bool {
-	return len(m.Text) > 0
-}
-
-type LocalizedText struct {
-	Lang string `json:"lang"`
-	Str  string `json:"str"`
-}
 
 func main() {
 	file := flag.String("file", "", "file to validate")
@@ -114,16 +23,19 @@ func main() {
 	f := *file
 	u := *url
 
+	var m motd.Messages
+	var err error
+
 	if u != "" {
 		fmt.Println("url:", u)
 		f = downloadToTempFile(u)
 	} else {
 		if f == "" {
-			f = defaultFile
+			f = filepath.Join("../../pkg/motd/", motd.ExampleFile)
 		}
 		fmt.Println("file:", f)
 	}
-	m, err := parseFile(f)
+	m, err = motd.ParseFile(f)
 	if err != nil {
 		panic(err)
 	}
@@ -159,25 +71,6 @@ func downloadToTempFile(url string) string {
 	return out.Name()
 }
 
-func parseFile(f string) (Messages, error) {
-	jsonFile, err := os.Open(f)
-	if err != nil {
-		panic(err)
-	}
-	defer jsonFile.Close()
-	byteVal, err := ioutil.ReadAll(jsonFile)
-	if err != nil {
-		panic(err)
-	}
-	return parseJsonStr(byteVal)
-}
-
-func parseJsonStr(b []byte) (Messages, error) {
-	var m Messages
-	json.Unmarshal(b, &m)
-	return m, nil
-}
-
 func mark(val bool) string {
 	if val {
 		return OK
diff --git a/branding/motd-cli/motd-example.json b/branding/motd-cli/motd-example.json
deleted file mode 100644
index f33c2b9..0000000
--- a/branding/motd-cli/motd-example.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
-    "motd": [{
-        "begin":    "02 Jan 21 15:04 -0700",
-        "end":      "31 Jan 22 15:04 -0700",
-        "type":     "daily",
-        "platform": "all",
-        "urgency":  "normal",
-        "text": [
-          { "lang": "en",
-            "str": "This is a <a href='https://leap.se'>test!</a>"},
-          { "lang": "es",
-            "str": "Esto es una <a href='https://leap.se'>pruebita!</a>"}
-        ]}
-    ]
-}
diff --git a/branding/motd-cli/motd_test.go b/branding/motd-cli/motd_test.go
deleted file mode 100644
index ed11661..0000000
--- a/branding/motd-cli/motd_test.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package main
-
-import (
-	"testing"
-)
-
-func TestGoodMotd(t *testing.T) {
-	m, err := parseFile(defaultFile)
-	if err != nil {
-		t.Errorf("error parsing default file")
-	}
-	if m.Length() == 0 {
-		t.Errorf("zero messages in file")
-	}
-	for _, msg := range m.Messages {
-		if !msg.IsValid() {
-			t.Errorf("invalid motd json at %s", defaultFile)
-		}
-	}
-}
-
-const emptyDate = `
-{
-    "motd": [{
-        "begin":    "",
-        "end":      "",
-        "type":     "daily",
-        "platform": "all",
-        "urgency":  "normal",
-        "text": [
-          { "lang": "en",
-            "str": "test"
-	  }]
-    }]
-}`
-
-func TestEmptyDateFails(t *testing.T) {
-	m, err := parseJsonStr([]byte(emptyDate))
-	if err != nil {
-		t.Errorf("error parsing json")
-	}
-	if allValid(t, m) {
-		t.Errorf("empty string should not be valid")
-	}
-}
-
-const badEnd = `
-{
-    "motd": [{
-	"begin":    "02 Jan 21 00:00 +0100",
-	"end":      "01 Jan 21 00:00 +0100",
-        "type":     "daily",
-        "platform": "all",
-        "urgency":  "normal",
-        "text": [
-          { "lang": "en",
-            "str": "test"
-	  }]
-    }]
-}`
-
-func TestBadEnd(t *testing.T) {
-	m, err := parseJsonStr([]byte(badEnd))
-	if err != nil {
-		t.Errorf("error parsing json")
-	}
-	if allValid(t, m) {
-		t.Errorf("begin > end must fail")
-	}
-}
-
-func allValid(t *testing.T, m Messages) bool {
-	if m.Length() == 0 {
-		t.Errorf("expected at least one message")
-
-	}
-	for _, msg := range m.Messages {
-		if !msg.IsValid() {
-			return false
-		}
-	}
-	return true
-}
-- 
cgit v1.2.3