yosaid.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. lnRouter := linode.NewLinodeRouter()
  64. lnRouter.Register(daemon.ADD, lnConn.AddLinodeHandler)
  65. lnRouter.Register(daemon.SHOW, lnConn.ShowLinodeHandler)
  66. lnRouter.Register(daemon.DELETE, lnConn.DeleteLinodeHandler)
  67. lnRouter.Register(daemon.POLL, lnConn.PollLinodeHandler)
  68. semHostsRouter := semaphore.NewSemaphoreRouter()
  69. semHostsRouter.Register(daemon.ADD, semaphoreConn.AddHostHandler)
  70. semHostsRouter.Register(daemon.DELETE, semaphoreConn.DeleteHostHandler)
  71. semHostsRouter.Register(daemon.SHOW, semaphoreConn.ShowHostHandler)
  72. semProjRouter := semaphore.NewSemaphoreRouter()
  73. semProjRouter.Register(daemon.ADD, semaphoreConn.AddProjectHandler)
  74. semProjRouter.Register(daemon.SHOW, semaphoreConn.ShowProjectHandler)
  75. semTaskRouter := semaphore.NewSemaphoreRouter()
  76. semTaskRouter.Register(daemon.RUN, semaphoreConn.RunTaskHandler)
  77. semTaskRouter.Register(daemon.POLL, semaphoreConn.PollTaskHandler)
  78. semTaskRouter.Register(daemon.SHOW, semaphoreConn.ShowTaskHandler)
  79. semBootstrapRouter := semaphore.NewSemaphoreRouter()
  80. semBootstrapRouter.Register(daemon.BOOTSTRAP, semaphoreConn.BootstrapHandler)
  81. configPeerRouter := daemon.NewConfigRouter()
  82. configPeerRouter.Register(daemon.ADD, conf.AddPeerHandler)
  83. configPeerRouter.Register(daemon.DELETE, conf.DeletePeerHandler)
  84. configServerRouter := daemon.NewConfigRouter()
  85. configServerRouter.Register(daemon.ADD, conf.AddServerHandler)
  86. configServerRouter.Register(daemon.DELETE, conf.DeleteServerHandler)
  87. configRouter := daemon.NewConfigRouter()
  88. configRouter.Register(daemon.SHOW, conf.ShowConfigHandler)
  89. configRouter.Register(daemon.SAVE, conf.SaveConfigHandler)
  90. configRouter.Register(daemon.RELOAD, conf.ReloadConfigHandler)
  91. keyringRouter := daemon.NewKeyRingRouter()
  92. keyringRouter.Register(daemon.SHOW, apikeyring.ShowKeyringHandler)
  93. keyringRouter.Register(daemon.BOOTSTRAP, apikeyring.BootstrapKeyringHandler)
  94. keyringRouter.Register(daemon.RELOAD, apikeyring.ReloadKeyringHandler)
  95. vpnRouter := daemon.NewVpnRouter()
  96. vpnRouter.Register(daemon.SHOW, ctx.VpnShowHandler)
  97. vpnRouter.Register(daemon.SAVE, ctx.VpnSaveHandler)
  98. ctxRouter := daemon.NewContextRouter()
  99. ctxRouter.Register(daemon.SHOW, ctx.ShowRoutesHandler)
  100. ctx.Register("cloud", lnRouter)
  101. ctx.Register("keyring", keyringRouter)
  102. ctx.Register("config", configRouter)
  103. ctx.Register("config-peer", configPeerRouter)
  104. ctx.Register("config-server", configServerRouter)
  105. ctx.Register("ansible", semBootstrapRouter)
  106. ctx.Register("ansible-hosts", semHostsRouter)
  107. ctx.Register("ansible-projects", semProjRouter)
  108. ctx.Register("ansible-task", semTaskRouter)
  109. ctx.Register("vpn-config", vpnRouter)
  110. ctx.Register("routes", ctxRouter)
  111. ctx.ListenAndServe()
  112. }