yosaid.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "git.aetherial.dev/aeth/yosai/pkg/cloud/linode"
  11. "git.aetherial.dev/aeth/yosai/pkg/config"
  12. "git.aetherial.dev/aeth/yosai/pkg/daemon"
  13. daemonproto "git.aetherial.dev/aeth/yosai/pkg/daemon-proto"
  14. "git.aetherial.dev/aeth/yosai/pkg/keytags"
  15. "git.aetherial.dev/aeth/yosai/pkg/secrets/hashicorp"
  16. "git.aetherial.dev/aeth/yosai/pkg/secrets/keyring"
  17. "git.aetherial.dev/aeth/yosai/pkg/semaphore"
  18. "github.com/joho/godotenv"
  19. )
  20. func GetSshKeyPrompt(daemonKeyring keyring.DaemonKeyRing, conf config.Configuration) {
  21. fmt.Print("Enter the full path of the ssh key to use for your daemon: ")
  22. reader := bufio.NewReader(os.Stdin)
  23. line, err := reader.ReadString('\n')
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. keypath := strings.Trim(line, "\n")
  28. pubkeyBytes, err := os.ReadFile(keypath + ".pub")
  29. if err != nil {
  30. log.Fatal("Error reading the public key: ", err)
  31. }
  32. privkeyBytes, err := os.ReadFile(keypath)
  33. if err != nil {
  34. log.Fatal("Error reading the private key: ", err)
  35. }
  36. daemonKeyring.AddKey(keytags.SYSTEM_SSH_KEYNAME, keyring.SshKey{
  37. PublicKey: strings.Trim(string(pubkeyBytes), "\n"),
  38. PrivateKey: strings.Trim(string(privkeyBytes), "\n"),
  39. Username: conf.Username,
  40. })
  41. }
  42. const UNIX_DOMAIN_SOCK_PATH = "/tmp/yosaid.sock"
  43. func main() {
  44. configMode := flag.String("config-mode", "", "Specify the configuration mode to run the daemon as, i.e. 'server' or 'host'")
  45. username := flag.String("username", "", "This is the username to run the daemon as. Usually only applicable if using a configuration server")
  46. secretsBackendKey := flag.String("secrets-backend-key", "", "This is the API key for the secret backend")
  47. envFile := flag.String("env", "./.env", "Pass an environment variable file to this flag. Default's to '.env' in the CWD")
  48. configInit := flag.Bool("config-init", false, "pass this to create a blank config at ./.config.tmpl")
  49. envInit := flag.Bool("env-init", false, "pass this to create a blank env file at ./.env.tmpl")
  50. flag.Parse()
  51. if *configInit {
  52. err := config.BlankConfig("./.config.tmpl")
  53. if err != nil {
  54. log.Fatal("Couldnt create blank config: ", err)
  55. }
  56. fmt.Println("Blank config created at ./.config.tmpl")
  57. os.Exit(0)
  58. }
  59. if *envInit {
  60. err := config.BlankEnv("./.env.tmpl")
  61. if err != nil {
  62. log.Fatal("Couldnt create blank env file: ", err)
  63. }
  64. fmt.Println("Blank env file created at ./.env.tmpl")
  65. os.Exit(0)
  66. }
  67. err := godotenv.Load(*envFile)
  68. if err != nil {
  69. fmt.Println("Couldn't find an env file in the current directory. Relying on program startup sequence to provide initial configuration")
  70. }
  71. startupArgs := map[config.StartupArgKeyname]string{
  72. config.ConfigModeArg: *configMode,
  73. config.UsernameArg: *username,
  74. config.SecretsBackendKeyArg: *secretsBackendKey,
  75. }
  76. var configIO config.DaemonConfigIO
  77. startupData := config.Turnkey(startupArgs)
  78. switch startupData.ConfigurationMode {
  79. case "server":
  80. configIO = config.NewConfigServerImpl("192.168.50.35:8080", "http")
  81. case "host":
  82. configIO = config.NewConfigHostImpl("/home/aeth/.config/yosai.json")
  83. default:
  84. fmt.Println("unknown configuration mode: ", startupData.ConfigurationMode, " passed.")
  85. os.Exit(199)
  86. }
  87. conf := config.NewConfiguration(os.Stdout, config.ValidateUsername(startupData.Username))
  88. configIO.Propogate(conf)
  89. conf.SetConfigIO(configIO)
  90. conf.SetStreamIO(os.Stdout)
  91. apikeyring := keyring.NewKeyRing(conf, keytags.ConstKeytag{})
  92. // Here we are demonstrating how you add a key to a keyring, in this
  93. // case it is the top level keyring.
  94. apikeyring.AddKey(keytags.HASHICORP_VAULT_KEYNAME, keyring.BearerAuth{
  95. Secret: startupData.SecretsBackendKey,
  96. Username: config.ValidateUsername(startupData.Username),
  97. })
  98. hashiConn := hashicorp.VaultConnection{
  99. VaultUrl: conf.Service.SecretsBackendUrl,
  100. HttpProto: "https",
  101. KeyRing: apikeyring,
  102. Client: &http.Client{},
  103. }
  104. apikeyring.Rungs = append(apikeyring.Rungs, hashiConn)
  105. _, err = apikeyring.GetKey(keytags.SYSTEM_SSH_KEYNAME)
  106. if err != nil {
  107. GetSshKeyPrompt(apikeyring, *conf)
  108. }
  109. err = apikeyring.Bootstrap()
  110. if err != nil {
  111. log.Fatal(err)
  112. }
  113. for _, key := range apikeyring.Keys {
  114. fmt.Println(key.GetType(), key.Prepare())
  115. }
  116. // creating the connection client with Hashicorp vault, and using the keyring we created above
  117. // as this clients keyring. This allows the API key we added earlier to be used when calling the API
  118. lnConn := linode.LinodeConnection{Client: &http.Client{}, Keyring: apikeyring, Config: conf, KeyTagger: keytags.ConstKeytag{}}
  119. semaphoreConn := semaphore.NewSemaphoreClient(conf.Service.AnsibleBackendUrl, "https", apikeyring, conf, keytags.ConstKeytag{})
  120. apikeyring.Rungs = append(apikeyring.Rungs, semaphoreConn)
  121. ctx := daemon.NewContext(UNIX_DOMAIN_SOCK_PATH, os.Stdout, apikeyring, conf)
  122. lnRouter := linode.NewLinodeRouter()
  123. lnRouter.Register(daemonproto.ADD, lnConn.AddLinodeHandler)
  124. lnRouter.Register(daemonproto.SHOW, lnConn.ShowLinodeHandler)
  125. lnRouter.Register(daemonproto.DELETE, lnConn.DeleteLinodeHandler)
  126. lnRouter.Register(daemonproto.POLL, lnConn.PollLinodeHandler)
  127. semHostsRouter := semaphore.NewSemaphoreRouter()
  128. semHostsRouter.Register(daemonproto.ADD, semaphoreConn.AddHostHandler)
  129. semHostsRouter.Register(daemonproto.DELETE, semaphoreConn.DeleteHostHandler)
  130. semHostsRouter.Register(daemonproto.SHOW, semaphoreConn.ShowHostHandler)
  131. semProjRouter := semaphore.NewSemaphoreRouter()
  132. semProjRouter.Register(daemonproto.ADD, semaphoreConn.AddProjectHandler)
  133. semProjRouter.Register(daemonproto.SHOW, semaphoreConn.ShowProjectHandler)
  134. semTaskRouter := semaphore.NewSemaphoreRouter()
  135. semTaskRouter.Register(daemonproto.RUN, semaphoreConn.RunTaskHandler)
  136. semTaskRouter.Register(daemonproto.POLL, semaphoreConn.PollTaskHandler)
  137. semTaskRouter.Register(daemonproto.SHOW, semaphoreConn.ShowTaskHandler)
  138. semBootstrapRouter := semaphore.NewSemaphoreRouter()
  139. semBootstrapRouter.Register(daemonproto.BOOTSTRAP, semaphoreConn.BootstrapHandler)
  140. configPeerRouter := config.NewConfigRouter()
  141. configPeerRouter.Register(daemonproto.ADD, conf.AddPeerHandler)
  142. configPeerRouter.Register(daemonproto.DELETE, conf.DeletePeerHandler)
  143. configServerRouter := config.NewConfigRouter()
  144. configServerRouter.Register(daemonproto.ADD, conf.AddServerHandler)
  145. configServerRouter.Register(daemonproto.DELETE, conf.DeleteServerHandler)
  146. configRouter := config.NewConfigRouter()
  147. configRouter.Register(daemonproto.SHOW, conf.ShowConfigHandler)
  148. configRouter.Register(daemonproto.SAVE, conf.SaveConfigHandler)
  149. configRouter.Register(daemonproto.RELOAD, conf.ReloadConfigHandler)
  150. keyringRouter := keyring.NewKeyRingRouter()
  151. keyringRouter.Register(daemonproto.SHOW, apikeyring.ShowKeyringHandler)
  152. keyringRouter.Register(daemonproto.BOOTSTRAP, apikeyring.BootstrapKeyringHandler)
  153. keyringRouter.Register(daemonproto.RELOAD, apikeyring.ReloadKeyringHandler)
  154. vpnRouter := daemon.NewVpnRouter()
  155. vpnRouter.Register(daemonproto.SHOW, ctx.VpnShowHandler)
  156. vpnRouter.Register(daemonproto.SAVE, ctx.VpnSaveHandler)
  157. ctxRouter := daemon.NewContextRouter()
  158. ctxRouter.Register(daemonproto.SHOW, ctx.ShowRoutesHandler)
  159. ctx.Register("cloud", lnRouter)
  160. ctx.Register("keyring", keyringRouter)
  161. ctx.Register("config", configRouter)
  162. ctx.Register("config-peer", configPeerRouter)
  163. ctx.Register("config-server", configServerRouter)
  164. ctx.Register("ansible", semBootstrapRouter)
  165. ctx.Register("ansible-hosts", semHostsRouter)
  166. ctx.Register("ansible-projects", semProjRouter)
  167. ctx.Register("ansible-task", semTaskRouter)
  168. ctx.Register("vpn-config", vpnRouter)
  169. ctx.Register("routes", ctxRouter)
  170. ctx.ListenAndServe()
  171. }