From 8d035ab3a055c06d55f07b210da8363938b4c2a0 Mon Sep 17 00:00:00 2001 From: postmannen Date: Tue, 5 Oct 2021 11:50:54 +0200 Subject: [PATCH] added generate nkeys to scripts --- .../nats-server/generate-nats-config/go.mod | 3 - .../nats-server/generate-nats-config/main.go | 56 ----------------- .../done.log | 0 .../generatenatsconfig | Bin .../generate-nats-node-auth-config/main.go | 10 +++ scripts/nats-server/generate-nkeys/main.go | 59 ++++++++++++++++++ 6 files changed, 69 insertions(+), 59 deletions(-) delete mode 100644 scripts/nats-server/generate-nats-config/go.mod delete mode 100644 scripts/nats-server/generate-nats-config/main.go rename scripts/nats-server/{generate-nats-config => generate-nats-node-auth-config}/done.log (100%) rename scripts/nats-server/{generate-nats-config => generate-nats-node-auth-config}/generatenatsconfig (100%) create mode 100644 scripts/nats-server/generate-nkeys/main.go diff --git a/scripts/nats-server/generate-nats-config/go.mod b/scripts/nats-server/generate-nats-config/go.mod deleted file mode 100644 index 23c0f3f..0000000 --- a/scripts/nats-server/generate-nats-config/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module test - -go 1.17 diff --git a/scripts/nats-server/generate-nats-config/main.go b/scripts/nats-server/generate-nats-config/main.go deleted file mode 100644 index 4bd83ea..0000000 --- a/scripts/nats-server/generate-nats-config/main.go +++ /dev/null @@ -1,56 +0,0 @@ -package main - -import ( - "bufio" - "os" - "strings" - "text/template" - - "log" -) - -type data struct { - IP string - Name string - Nkey string -} - -const tmpNats string = ` -{ - # {{.Name}} - nkey: {{.Nkey}} - permissions: { - publish: { - allow: ["central.>","errorCentral.>","{{.Name}}.>"] - } - subscribe: { - allow: ["central.>","errorCentral.>","{{.Name}}.>"] - } - } -} -` - -func main() { - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - ss := strings.Split(scanner.Text(), ",") - if len(ss) < 3 { - continue - } - - nkey := strings.Split(ss[2], "nkey-user=") - - d := data{ - IP: ss[0], - Name: ss[1], - Nkey: nkey[1], - } - - tmp, err := template.New("myTemplate").Parse(tmpNats) - if err != nil { - log.Printf("error: template parse failed: %v\n", err) - } - - tmp.Execute(os.Stdout, d) - } -} diff --git a/scripts/nats-server/generate-nats-config/done.log b/scripts/nats-server/generate-nats-node-auth-config/done.log similarity index 100% rename from scripts/nats-server/generate-nats-config/done.log rename to scripts/nats-server/generate-nats-node-auth-config/done.log diff --git a/scripts/nats-server/generate-nats-config/generatenatsconfig b/scripts/nats-server/generate-nats-node-auth-config/generatenatsconfig similarity index 100% rename from scripts/nats-server/generate-nats-config/generatenatsconfig rename to scripts/nats-server/generate-nats-node-auth-config/generatenatsconfig diff --git a/scripts/nats-server/generate-nats-node-auth-config/main.go b/scripts/nats-server/generate-nats-node-auth-config/main.go index 4bd83ea..649523e 100644 --- a/scripts/nats-server/generate-nats-node-auth-config/main.go +++ b/scripts/nats-server/generate-nats-node-auth-config/main.go @@ -1,3 +1,13 @@ +// Will generate the node specific config to be used in the +// authentication section of the nats-server config. +// It's given it's input via stdin +// +// Example: +// ./generatenatsconfig < done.log +// +// The format of the input should be: +// ,,nkey-user= + package main import ( diff --git a/scripts/nats-server/generate-nkeys/main.go b/scripts/nats-server/generate-nkeys/main.go new file mode 100644 index 0000000..ba4deca --- /dev/null +++ b/scripts/nats-server/generate-nkeys/main.go @@ -0,0 +1,59 @@ +// Generate both public and private nkeys +// nk -gen user -pubout + +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "log" + "os" + "os/exec" + "path" + "strings" +) + +func main() { + fileDir := flag.String("fileDir", "./", "the directory path for where to store the files") + flag.Parse() + + cmd := exec.Command("nk", "-gen", "user", "-pubout") + out, err := cmd.CombinedOutput() + if err != nil { + log.Printf("error: check if the nk tool is installed on the system: %v\n", err) + } + + fmt.Printf("out: %v\n", string(out)) + + scanner := bufio.NewScanner(bytes.NewReader(out)) + for scanner.Scan() { + text := scanner.Text() + + if strings.HasPrefix(text, "S") { + p := path.Join(*fileDir, "seed.txt") + writekey(p, []byte(text)) + } + if strings.HasPrefix(text, "U") { + p := path.Join(*fileDir, "user.txt") + writekey(p, []byte(text)) + } + + } +} + +func writekey(fileName string, b []byte) error { + fh, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600) + if err != nil { + return fmt.Errorf("error: failed to open create/open file for writing: %v", err) + } + defer fh.Close() + + _, err = fh.Write(b) + if err != nil { + return fmt.Errorf("error: failed to write file: %v", err) + } + + return nil +}