yosaid.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "git.aetherial.dev/aeth/yosai/pkg/cloud/linode"
  9. "git.aetherial.dev/aeth/yosai/pkg/daemon"
  10. "git.aetherial.dev/aeth/yosai/pkg/keytags"
  11. "git.aetherial.dev/aeth/yosai/pkg/secrets/hashicorp"
  12. "git.aetherial.dev/aeth/yosai/pkg/semaphore"
  13. )
  14. const UNIX_DOMAIN_SOCK_PATH = "/tmp/yosaid.sock"
  15. func main() {
  16. config := flag.Bool("config-init", false, "pass this to create a blank config at ./.config.tmpl")
  17. envInit := flag.Bool("env-init", false, "pass this to create a blank env file at ./.env.tmpl")
  18. flag.Parse()
  19. if *config {
  20. err := daemon.BlankConfig("./.config.tmpl")
  21. if err != nil {
  22. log.Fatal("Couldnt create blank config: ", err)
  23. }
  24. fmt.Println("Blank config created at ./.config.tmpl")
  25. os.Exit(0)
  26. }
  27. if *envInit {
  28. err := daemon.BlankEnv("./.env.tmpl")
  29. if err != nil {
  30. log.Fatal("Couldnt create blank env file: ", err)
  31. }
  32. fmt.Println("Blank env file created at ./.env.tmpl")
  33. os.Exit(0)
  34. }
  35. err := daemon.LoadAndVerifyEnv("./.env", daemon.EnvironmentVariables)
  36. if err != nil {
  37. log.Fatal("Error loading env file: ", err)
  38. }
  39. conf := daemon.ReadConfig(daemon.DefaultConfigLoc)
  40. apikeyring := daemon.NewKeyRing(conf, keytags.ConstKeytag{})
  41. // Here we are demonstrating how you add a key to a keyring, in this
  42. // case it is the top level keyring.
  43. apikeyring.AddKey(keytags.HASHICORP_VAULT_KEYNAME, daemon.BearerAuth{
  44. Secret: os.Getenv(keytags.HASHICORP_VAULT_KEYNAME),
  45. })
  46. hashiConn := hashicorp.VaultConnection{
  47. VaultUrl: conf.Service.SecretsBackendUrl,
  48. HttpProto: "https",
  49. KeyRing: apikeyring,
  50. Client: &http.Client{},
  51. }
  52. apikeyring.Rungs = append(apikeyring.Rungs, hashiConn)
  53. err = apikeyring.Bootstrap()
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. // creating the connection client with Hashicorp vault, and using the keyring we created above
  58. // as this clients keyring. This allows the API key we added earlier to be used when calling the API
  59. lnConn := linode.LinodeConnection{Client: &http.Client{}, Keyring: apikeyring, Config: conf, KeyTagger: keytags.ConstKeytag{}}
  60. semaphoreConn := semaphore.NewSemaphoreClient(conf.Service.AnsibleBackendUrl, "https", apikeyring, conf, keytags.ConstKeytag{})
  61. apikeyring.Rungs = append(apikeyring.Rungs, semaphoreConn)
  62. ctx := daemon.NewContext(UNIX_DOMAIN_SOCK_PATH, os.Stdout, apikeyring, conf)
  63. ctx.Register("keyring", apikeyring.KeyringRouter)
  64. ctx.Register("config", conf.ConfigRouter)
  65. ctx.Register("config-peer", conf.PeerRouter)
  66. ctx.Register("config-server", conf.ServerRouter)
  67. ctx.Register("cloud", lnConn.LinodeRouter)
  68. ctx.Register("ansible", semaphoreConn.BootstrapHandler)
  69. ctx.Register("ansible-hosts", semaphoreConn.HostHandler)
  70. ctx.Register("ansible-projects", semaphoreConn.ProjectHandler)
  71. ctx.Register("ansible-job", semaphoreConn.TaskHandler)
  72. ctx.Register("vault", hashiConn.VaultRouter)
  73. ctx.Register("daemon", ctx.DaemonRouter)
  74. ctx.ListenAndServe()
  75. }