5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-14 03:20:28 +00:00
yggdrasil-go/contrib/ansible/genkeys.go

115 lines
2.4 KiB
Go
Raw Normal View History

2019-01-15 01:28:27 +00:00
/*
This file generates crypto keys for [ansible-yggdrasil](https://github.com/jcgruenhage/ansible-yggdrasil/)
*/
package main
import (
"crypto/ed25519"
2019-01-15 01:28:27 +00:00
"encoding/hex"
"flag"
"fmt"
"net"
"os"
"github.com/cheggaaa/pb/v3"
2019-01-15 01:28:27 +00:00
"github.com/yggdrasil-network/yggdrasil-go/src/address"
)
var numHosts = flag.Int("hosts", 1, "number of host vars to generate")
var keyTries = flag.Int("tries", 1000, "number of tries before taking the best keys")
type keySet struct {
priv []byte
pub []byte
ip string
}
func main() {
flag.Parse()
bar := pb.StartNew(*keyTries*2 + *numHosts)
2019-01-15 01:28:27 +00:00
if *numHosts > *keyTries {
println("Can't generate less keys than hosts.")
return
}
var keys []keySet
for i := 0; i < *numHosts+1; i++ {
keys = append(keys, newKey())
bar.Increment()
2019-01-15 01:28:27 +00:00
}
keys = sortKeySetArray(keys)
for i := 0; i < *keyTries-*numHosts-1; i++ {
keys[0] = newKey()
keys = bubbleUpTo(keys, 0)
bar.Increment()
2019-01-15 01:28:27 +00:00
}
os.MkdirAll("host_vars", 0755)
2019-01-15 01:28:27 +00:00
for i := 1; i <= *numHosts; i++ {
os.MkdirAll(fmt.Sprintf("host_vars/%x", i), 0755)
file, err := os.Create(fmt.Sprintf("host_vars/%x/vars", i))
2019-01-15 01:28:27 +00:00
if err != nil {
return
}
defer file.Close()
file.WriteString(fmt.Sprintf("yggdrasil_public_key: %v\n", hex.EncodeToString(keys[i].pub)))
file.WriteString("yggdrasil_private_key: \"{{ vault_yggdrasil_private_key }}\"\n")
file.WriteString(fmt.Sprintf("ansible_host: %v\n", keys[i].ip))
2019-01-15 01:28:27 +00:00
file, err = os.Create(fmt.Sprintf("host_vars/%x/vault", i))
2019-01-15 01:28:27 +00:00
if err != nil {
return
}
defer file.Close()
file.WriteString(fmt.Sprintf("vault_yggdrasil_private_key: %v\n", hex.EncodeToString(keys[i].priv)))
bar.Increment()
2019-01-15 01:28:27 +00:00
}
bar.Finish()
2019-01-15 01:28:27 +00:00
}
func newKey() keySet {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
ip := net.IP(address.AddrForKey(pub)[:]).String()
return keySet{priv[:], pub[:], ip}
2019-01-15 01:28:27 +00:00
}
func isBetter(oldID, newID []byte) bool {
for idx := range oldID {
if newID[idx] < oldID[idx] {
2019-01-15 01:28:27 +00:00
return true
}
if newID[idx] > oldID[idx] {
2019-01-15 01:28:27 +00:00
return false
}
}
return false
}
func sortKeySetArray(sets []keySet) []keySet {
for i := 0; i < len(sets); i++ {
sets = bubbleUpTo(sets, i)
}
return sets
}
func bubbleUpTo(sets []keySet, num int) []keySet {
for i := 0; i < len(sets)-num-1; i++ {
if isBetter(sets[i+1].pub, sets[i].pub) {
2019-01-15 01:28:27 +00:00
var tmp = sets[i]
sets[i] = sets[i+1]
sets[i+1] = tmp
} else {
break
2019-01-15 01:28:27 +00:00
}
}
return sets
}