From 29cbbd67d48d793ba800ff11a4c26b600621d86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?yJan=20Christian=20Gr=C3=BCnhage?= Date: Mon, 10 Apr 2017 18:04:54 +0200 Subject: [PATCH] Added source code --- README.md | 10 +++++++++- example.json | 13 +++++++++++++ main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 example.json create mode 100644 main.go diff --git a/README.md b/README.md index cec08bf..31ff9a3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ -# borg-gen-auth-keys +### Generate an authorized_keys file: +When working with borg serve in an automated environment, you should use an authorized_keys file that restricts the ssh sessions to use only borg, and only in the directory where the repo they should use lies in. This binary takes in a json file specifying the needed info and prints out the wanted authorized_keys file. + +To generate that file, do this (assuming you have set up everything needed for go get, and $GOPATH/bin is in your $PATH): + +```bash +go get git.jcg.re/jcgruenhage/borg-gen-auth-keys +borg-gen-auth-keys yourjsonfile.json > ~/.ssh/authorized_keys +``` diff --git a/example.json b/example.json new file mode 100644 index 0000000..3713236 --- /dev/null +++ b/example.json @@ -0,0 +1,13 @@ +{ + "Folder": "/mnt/backupmount", + "Hosts":[ + { + "Name": "host1", + "SSHKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC7ZvLoWZgh5eMDQXLEdWobkt/B8mnM2osEynF0boorifRNwztQHY3bKXTHjUlMnnWH+UvZ3aXH16M1QmyuNi4kLUTSMu4GSVsXMfknHRjgJ927Y/j5TCGXfOcfB0B4pcgHS3fVi1VMXXiZ1S/X8cbt1llXHfEC5eKutDeJkRer3w== SSH_KEY" + }, + { + "Name": "host2", + "SSHKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDIgm31x6ETn4HwxBthe3m14s1Bp9p8sT5kC/XSijN6uPpUmuF2ciRGc5QLzsBJR6MAD45zg6Q9nLBZGBnSIZDNfSGA5sqAeoZVHm4vjQWxenqG7dlNFXlC1DIHzMxxDsDUPneyXGBemAvVwL9h4HEC4xhMQIAJfaZqSWuNzzqYWw== SSH_KEY" + } + ] +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..411caba --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "os" + "io/ioutil" + "encoding/json" + "fmt" +) + +type Host struct { + Name string + SSHKey string +} + +type HostList struct { + Folder string + Hosts []Host +} + +func main() { + inputFilePath := os.Args[1] + dat, err := ioutil.ReadFile(inputFilePath) + check(err) + var hosts HostList + err = json.Unmarshal(dat, &hosts) + check(err) + for i := 0; i < len(hosts.Hosts); i++ { + fmt.Print("command=\"borg serve --restrict-to-path ") + fmt.Print(hosts.Folder) + fmt.Print("/") + fmt.Print(hosts.Hosts[i].Name) + fmt.Print("\",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc ") + fmt.Print(hosts.Hosts[i].SSHKey) + fmt.Print("\n") + } + +} +func check(e error) { + if e != nil { + panic(e) + } +}