From 382a6afe074d8267faa4ad5000841a2495640b21 Mon Sep 17 00:00:00 2001 From: Jessica Frazelle Date: Wed, 9 Mar 2016 18:29:05 -0800 Subject: [PATCH 1/3] move flag parsing into init Signed-off-by: Jessica Frazelle --- redoctober.go | 58 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/redoctober.go b/redoctober.go index 02f5ef8..cd02c39 100644 --- a/redoctober.go +++ b/redoctober.go @@ -219,7 +219,21 @@ multi-cert example: redoctober -vaultpath diskrecord.json -addr localhost:8080 -certs cert1.pem,cert2.pem -keys cert1.key,cert2.key ` -func main() { +var ( + addr string + caPath string + certsPath string + hcHost string + hcKey string + hcRoom string + keysPath string + roHost string + staticPath string + useSystemdSocket bool + vaultPath string +) + +func init() { flag.Usage = func() { fmt.Fprint(os.Stderr, "main usage dump\n") fmt.Fprint(os.Stderr, usage) @@ -227,33 +241,37 @@ func main() { os.Exit(2) } - var staticPath = flag.String("static", "", "Path to override built-in index.html") - var vaultPath = flag.String("vaultpath", "diskrecord.json", "Path to the the disk vault") - var addr = flag.String("addr", "localhost:8080", "Server and port separated by :") - var useSystemdSocket = flag.Bool("systemdfds", false, "Use systemd socket activation to listen on a file. Useful for binding privileged sockets.") - var certsPathString = flag.String("certs", "", "Path(s) of TLS certificate in PEM format, comma-separated") - var keysPathString = flag.String("keys", "", "Path(s) of TLS private key in PEM format, comma-separated, must me in the same order as the certs") - var caPath = flag.String("ca", "", "Path of TLS CA for client authentication (optional)") - var hcKey = flag.String("hckey", "", "Hipchat API Key") - var hcRoom = flag.String("hcroom", "", "Hipchat Room Id") - var hcHost = flag.String("hchost", "", "Hipchat Url Base (ex: hipchat.com)") - var roHost = flag.String("rohost", "", "RedOctober Url Base (ex: localhost:8080)") - flag.Parse() + flag.StringVar(&addr, "addr", "localhost:8080", "Server and port separated by :") + flag.StringVar(&caPath, "ca", "", "Path of TLS CA for client authentication (optional)") + flag.StringVar(&certsPath, "certs", "", "Path(s) of TLS certificate in PEM format, comma-separated") + flag.StringVar(&hcHost, "hchost", "", "Hipchat Url Base (ex: hipchat.com)") + flag.StringVar(&hcKey, "hckey", "", "Hipchat API Key") + flag.StringVar(&hcRoom, "hcroom", "", "Hipchat Room Id") + flag.StringVar(&keysPath, "keys", "", "Path(s) of TLS private key in PEM format, comma-separated, must me in the same order as the certs") + flag.StringVar(&roHost, "rohost", "", "RedOctober Url Base (ex: localhost:8080)") + flag.StringVar(&staticPath, "static", "", "Path to override built-in index.html") + flag.BoolVar(&useSystemdSocket, "systemdfds", false, "Use systemd socket activation to listen on a file. Useful for binding privileged sockets.") + flag.StringVar(&vaultPath, "vaultpath", "diskrecord.json", "Path to the the disk vault") - if *vaultPath == "" || *certsPathString == "" || *keysPathString == "" || (*addr == "" && *useSystemdSocket == false) { + flag.Parse() +} + +func main() { + if vaultPath == "" || certsPath == "" || keysPath == "" || + (addr == "" && useSystemdSocket == false) { fmt.Fprint(os.Stderr, usage) flag.PrintDefaults() os.Exit(2) } - certPaths := strings.Split(*certsPathString, ",") - keyPaths := strings.Split(*keysPathString, ",") + certPaths := strings.Split(certsPath, ",") + keyPaths := strings.Split(keysPath, ",") - if err := core.Init(*vaultPath, *hcKey, *hcRoom, *hcHost, *roHost); err != nil { - log.Fatalf(err.Error()) + if err := core.Init(vaultPath, hcKey, hcRoom, hcHost, roHost); err != nil { + log.Fatal(err) } - s, l, err := NewServer(*staticPath, *addr, *caPath, certPaths, keyPaths, *useSystemdSocket) + s, l, err := NewServer(staticPath, addr, caPath, certPaths, keyPaths, useSystemdSocket) if err != nil { log.Fatalf("Error starting redoctober server: %s\n", err) } @@ -1079,7 +1097,7 @@ var indexHtml = []byte(` evt.preventDefault(); createLink(); }); - + // Init from query string if possible. var queryParams = document.location.search; var queryParts = queryParams.split('&'); From 4340039d5b6b1ed3a3497bbb2bc0b62c6b180873 Mon Sep 17 00:00:00 2001 From: Jessica Frazelle Date: Wed, 9 Mar 2016 18:40:09 -0800 Subject: [PATCH 2/3] use go generate so index.html contents are not in redoctober.go Signed-off-by: Jessica Frazelle --- generate.go | 36 ++ redoctober.go | 891 +------------------------------ static.go | 893 ++++++++++++++++++++++++++++++++ index.html => static/index.html | 2 +- 4 files changed, 933 insertions(+), 889 deletions(-) create mode 100644 generate.go create mode 100644 static.go rename index.html => static/index.html (99%) diff --git a/generate.go b/generate.go new file mode 100644 index 0000000..56b3ae3 --- /dev/null +++ b/generate.go @@ -0,0 +1,36 @@ +// +build ignore + +package main + +import ( + "io" + "os" + "path/filepath" +) + +// Reads static/index.html and saves as a constant in static.go +func main() { + wd, err := os.Getwd() + if err != nil { + panic(err) + } + out, err := os.Create(filepath.Join(wd, "static.go")) + if err != nil { + panic(err) + } + indexPath := filepath.Join(wd, "static", "index.html") + + out.Write([]byte("// This file is autogenerated; DO NOT EDIT DIRECTLY\n// See generate.go for more info\npackage main\n\nconst (\n")) + out.Write([]byte("\tindexHtml = `")) + f, err := os.Open(indexPath) + if err != nil { + panic(err) + } + defer f.Close() + if _, err := io.Copy(out, f); err != nil { + panic(err) + } + + out.Write([]byte("`\n")) + out.Write([]byte(")\n")) +} diff --git a/redoctober.go b/redoctober.go index cd02c39..1151006 100644 --- a/redoctober.go +++ b/redoctober.go @@ -198,7 +198,7 @@ func (this *indexHandler) handle(w http.ResponseWriter, r *http.Request) { defer f.Close() body = f } else { - body = bytes.NewReader(indexHtml) + body = bytes.NewReader([]byte(indexHtml)) } header := w.Header() @@ -256,6 +256,8 @@ func init() { flag.Parse() } +//go:generate go run generate.go + func main() { if vaultPath == "" || certsPath == "" || keysPath == "" || (addr == "" && useSystemdSocket == false) { @@ -277,890 +279,3 @@ func main() { } s.Serve(l) } - -var indexHtml = []byte(` - - - Red October - Two Man Rule File Encryption & Decryption - - - - - - - - - - - -
-

Red October Management

-
-
-

Delegate

- -
- - -
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- -
-
-
- -
- -
-
-

User summary / delegation list

- -
- - -
- - -
-
- - -
- -
- -
-

Current Delegations

-
    - -

    All Users

    -
      -
      -
      -
      - -
      - -
      -
      -

      Create vault

      -
      - - -
      - - -
      -
      - - -
      - -
      - -
      - -

      Create User

      - -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      -
      -
      - - -
      -
      - -
      -
      -
      - -
      - -
      -
      -

      Change account

      - -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      - - -
      -
      - - -
      - -
      - -

      Admin Controls

      - -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - - -
      -
      - -
      -
      -
      -
      -
      -
      -

      Encrypt data

      - -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      -
      -
      - - -
      -
      -
      - - -
      - -
      -
      -
      -
      -
      -
      -

      Decrypt data

      - -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      - - -
      - -
      -
      -
      -
      -
      -
      -

      Get owners

      - -
      - - -
      - - -
      - -
      -
      -
      -
      -
      -
      -

      Create Order

      - -
      - -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - -
      -
      -
      -
      -
      -
      -

      Order Info

      - -
      - -
      -
      - - -
      -
      - - -
      -
      -
      -
      - - -
      -
      - -
      -
      -
      -
      -
      -
      -

      Outstanding Orders

      - -
      - -
      -
      -
      - - -
      -
      - - -
      -
      - - -
      -
      -
      -
      -

      Order Cancel

      - -
      - -
      -
      -
      - - -
      -
      - - -
      -
      -
      -
      -
      -
      - - -
      -
      -
      - -
      -
      -
      -
      -
      -

      Create Delegation Link

      - -
      - -
      - -
      - - -
      -

      Red October. CloudFlare

      -
      - - - -`) diff --git a/static.go b/static.go new file mode 100644 index 0000000..ba01954 --- /dev/null +++ b/static.go @@ -0,0 +1,893 @@ +// This file is autogenerated; DO NOT EDIT DIRECTLY +// See generate.go for more info +package main + +const ( + indexHtml = ` + + + Red October - Two Man Rule File Encryption & Decryption + + + + + + + + + + + +
      +

      Red October Management

      +
      +
      +

      Delegate

      + +
      + + +
      +
      + + +
      +
      + + +
      +
      +
      +
      + + +
      +
      + + +
      +
      +
      +
      + + +
      +
      + + +
      +
      +
      +
      + + +
      +
      + +
      +
      +
      + +
      + +
      +
      +

      User summary / delegation list

      + +
      + + +
      + + +
      +
      + + +
      + +
      + +
      +

      Current Delegations

      +
        + +

        All Users

        +
          +
          +
          +
          + +
          + +
          +
          +

          Create vault

          +
          + + +
          + + +
          +
          + + +
          + +
          + +
          + +

          Create User

          + +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          +
          +
          + + +
          +
          + +
          +
          +
          + +
          + +
          +
          +

          Change account

          + +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          + + +
          +
          + + +
          + +
          + +

          Admin Controls

          + +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + + +
          +
          + +
          +
          +
          +
          +
          +
          +

          Encrypt data

          + +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          +
          +
          + + +
          +
          +
          + + +
          + +
          +
          +
          +
          +
          +
          +

          Decrypt data

          + +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          + + +
          + +
          +
          +
          +
          +
          +
          +

          Get owners

          + +
          + + +
          + + +
          + +
          +
          +
          +
          +
          +
          +

          Create Order

          + +
          + +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + +
          +
          +
          +
          +
          +
          +

          Order Info

          + +
          + +
          +
          + + +
          +
          + + +
          +
          +
          +
          + + +
          +
          + +
          +
          +
          +
          +
          +
          +

          Outstanding Orders

          + +
          + +
          +
          +
          + + +
          +
          + + +
          +
          + + +
          +
          +
          +
          +

          Order Cancel

          + +
          + +
          +
          +
          + + +
          +
          + + +
          +
          +
          +
          +
          +
          + + +
          +
          +
          + +
          +
          +
          +
          +
          +

          Create Delegation Link

          + +
          + +
          + +
          + + +
          +

          Red October. CloudFlare

          +
          + + + + +` +) diff --git a/index.html b/static/index.html similarity index 99% rename from index.html rename to static/index.html index 610075f..76a2575 100644 --- a/index.html +++ b/static/index.html @@ -817,7 +817,7 @@ evt.preventDefault(); createLink(); }); - + // Init from query string if possible. var queryParams = document.location.search; var queryParts = queryParams.split('&'); From 2e6fdf15c60b8b0b2802dd0c2e3e84b306cf356c Mon Sep 17 00:00:00 2001 From: Jessica Frazelle Date: Wed, 9 Mar 2016 18:52:58 -0800 Subject: [PATCH 3/3] add script to validate nothing was changed that needed to re-run go generate Signed-off-by: Jessica Frazelle --- .travis.yml | 1 + scripts/.validate | 33 ++++++++++++++++++++++++++++++++ scripts/validate-html-generation | 28 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 scripts/.validate create mode 100755 scripts/validate-html-generation diff --git a/.travis.yml b/.travis.yml index 075bdd3..a3d0800 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ script: - go get github.com/cloudflare/redoctober - go test github.com/cloudflare/redoctober... - go vet github.com/cloudflare/redoctober... + - ./scripts/validate-html-generation - go list -f '{{if len .TestGoFiles}}"go test -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' ./... | xargs -i sh -c {} - gover . coverprofile.txt after_success: diff --git a/scripts/.validate b/scripts/.validate new file mode 100644 index 0000000..dd3f38e --- /dev/null +++ b/scripts/.validate @@ -0,0 +1,33 @@ +#!/bin/bash + +if [ -z "$VALIDATE_UPSTREAM" ]; then + # this is kind of an expensive check, so let's not do this twice if we + # are running more than one validate bundlescript + + VALIDATE_REPO='https://github.com/cloudflare/redoctober.git' + VALIDATE_BRANCH='master' + + if [ "$TRAVIS" = 'true' -a "$TRAVIS_PULL_REQUEST" != 'false' ]; then + VALIDATE_REPO="https://github.com/${TRAVIS_REPO_SLUG}.git" + VALIDATE_BRANCH="${TRAVIS_BRANCH}" + fi + + VALIDATE_HEAD="$(git rev-parse --verify HEAD)" + + git fetch -q "$VALIDATE_REPO" "refs/heads/$VALIDATE_BRANCH" + VALIDATE_UPSTREAM="$(git rev-parse --verify FETCH_HEAD)" + + VALIDATE_COMMIT_LOG="$VALIDATE_UPSTREAM..$VALIDATE_HEAD" + VALIDATE_COMMIT_DIFF="$VALIDATE_UPSTREAM...$VALIDATE_HEAD" + + validate_diff() { + if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then + git diff "$VALIDATE_COMMIT_DIFF" "$@" + fi + } + validate_log() { + if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then + git log "$VALIDATE_COMMIT_LOG" "$@" + fi + } +fi diff --git a/scripts/validate-html-generation b/scripts/validate-html-generation new file mode 100755 index 0000000..c58633e --- /dev/null +++ b/scripts/validate-html-generation @@ -0,0 +1,28 @@ +#!/bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source "${DIR}/.validate" + +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- 'static/index.html' || true) ) +unset IFS + +if [ ${#files[@]} -gt 0 ]; then + # We run go generate to and see if we have a diff afterwards + go generate >/dev/null + # Let see if the working directory is clean + diffs="$(git status --porcelain -- static.go 2>/dev/null)" + if [ "$diffs" ]; then + { + echo 'The result of go generate differs' + echo + echo "$diffs" + echo + echo 'Please re-run go generate' + echo + } >&2 + false + else + echo 'Congratulations! File generation is done correctly.' + fi +fi