yosaictl.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net"
  9. "os"
  10. "strings"
  11. "git.aetherial.dev/aeth/yosai/pkg/cloud/linode"
  12. "git.aetherial.dev/aeth/yosai/pkg/daemon"
  13. "git.aetherial.dev/aeth/yosai/pkg/secrets/hashicorp"
  14. "git.aetherial.dev/aeth/yosai/pkg/semaphore"
  15. )
  16. const UNIX_DOMAIN_SOCK_PATH = "/tmp/yosaid.sock"
  17. /*
  18. Build a JSON request to send the yosaid daemon
  19. :param v: a struct to serialize for a request
  20. :param value: a string to put into the request
  21. */
  22. func jsonBuilder(v interface{}, value string) []byte {
  23. addLn, ok := v.(linode.AddLinodeRequest)
  24. if ok {
  25. addLn = linode.AddLinodeRequest{
  26. Name: value,
  27. }
  28. b, _ := json.Marshal(addLn)
  29. return b
  30. }
  31. delLn, ok := v.(linode.DeleteLinodeRequest)
  32. if ok {
  33. delLn = linode.DeleteLinodeRequest{
  34. Id: value,
  35. }
  36. b, _ := json.Marshal(delLn)
  37. return b
  38. }
  39. pollLn, ok := v.(linode.PollLinodeRequest)
  40. if ok {
  41. pollLn = linode.PollLinodeRequest{
  42. Address: value,
  43. }
  44. b, _ := json.Marshal(pollLn)
  45. return b
  46. }
  47. semReq, ok := v.(semaphore.SemaphoreRequest)
  48. if ok {
  49. semReq = semaphore.SemaphoreRequest{
  50. Target: value,
  51. }
  52. b, _ := json.Marshal(semReq)
  53. return b
  54. }
  55. vaultReq, ok := v.(hashicorp.VaultItem)
  56. if ok {
  57. vals := strings.Split(value, ",")
  58. if len(vals) != 4 {
  59. log.Fatal("To add a key, you must pass the <name>,<type>,<public>,<private>. TODO: this interface needs to be improved.")
  60. }
  61. vaultReq = hashicorp.VaultItem{
  62. Name: vals[0],
  63. Type: vals[1],
  64. Public: vals[2],
  65. Secret: vals[3],
  66. }
  67. b, _ := json.Marshal(vaultReq)
  68. return b
  69. }
  70. return []byte("{\"data\":\"test\"}")
  71. }
  72. func main() {
  73. if len(os.Args) < 4 {
  74. log.Fatal("Not enough arguments!")
  75. }
  76. var args []string
  77. args = os.Args[1:]
  78. var rb = bytes.NewBuffer([]byte{})
  79. switch args[0] {
  80. case "cloud":
  81. switch args[1] {
  82. case "delete":
  83. rb.Write(jsonBuilder(linode.DeleteLinodeRequest{}, args[2]))
  84. case "add":
  85. rb.Write(jsonBuilder(linode.AddLinodeRequest{}, args[2]))
  86. case "poll":
  87. rb.Write(jsonBuilder(linode.PollLinodeRequest{}, args[2]))
  88. }
  89. case "ansible-hosts":
  90. rb.Write(jsonBuilder(semaphore.SemaphoreRequest{}, args[2]))
  91. case "ansible-job":
  92. rb.Write(jsonBuilder(semaphore.SemaphoreRequest{}, args[2]))
  93. case "ansible-projects":
  94. rb.Write(jsonBuilder(semaphore.SemaphoreRequest{}, args[2]))
  95. case "ansible":
  96. rb.Write(jsonBuilder(semaphore.SemaphoreRequest{}, args[2]))
  97. case "keyring":
  98. rb.Write(jsonBuilder(hashicorp.VaultItem{}, fmt.Sprintf("%s,,,", args[2])))
  99. case "vault":
  100. rb.Write(jsonBuilder(hashicorp.VaultItem{}, args[2]))
  101. }
  102. msg := daemon.Marshal(daemon.SockMessage{
  103. Type: daemon.MsgRequest,
  104. TypeLen: int8(len(daemon.MsgRequest)),
  105. StatusMsg: "",
  106. StatusCode: 0,
  107. Version: daemon.SockMsgVers,
  108. Body: rb.Bytes(),
  109. Target: args[0],
  110. Method: args[1],
  111. })
  112. conn, err := net.Dial("unix", UNIX_DOMAIN_SOCK_PATH)
  113. if err != nil {
  114. log.Fatal(err)
  115. }
  116. defer conn.Close()
  117. buf := bytes.NewBuffer(msg)
  118. _, err = io.Copy(conn, buf)
  119. if err != nil {
  120. log.Fatal("write error:", err)
  121. }
  122. resp := bytes.NewBuffer([]byte{})
  123. _, err = io.Copy(resp, conn)
  124. if err != nil {
  125. if err == io.EOF {
  126. fmt.Println("exited ok.")
  127. os.Exit(0)
  128. }
  129. log.Fatal(err)
  130. }
  131. responseMsg := daemon.Unmarshal(resp.Bytes())
  132. var outbuf = bytes.NewBuffer([]byte{})
  133. err = json.Indent(outbuf, responseMsg.Body, " ", " ")
  134. if err != nil {
  135. fmt.Println(string(responseMsg.Body))
  136. os.Exit(0)
  137. }
  138. fmt.Println(string(outbuf.Bytes()))
  139. }