1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

added generate nkeys to scripts

This commit is contained in:
postmannen 2021-10-05 11:50:54 +02:00
parent 6378f2b86c
commit 8d035ab3a0
6 changed files with 69 additions and 59 deletions

View file

@ -1,3 +0,0 @@
module test
go 1.17

View file

@ -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)
}
}

View file

@ -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:
// <ip address>,<full nodename>,nkey-user=<user key of node>
package main package main
import ( import (

View file

@ -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
}