client.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. package semaphore
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "git.aetherial.dev/aeth/yosai/pkg/config"
  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/keyring"
  16. "gopkg.in/yaml.v3"
  17. )
  18. const ProjectsPath = "api/projects"
  19. const ProjectPath = "api/project"
  20. const YosaiProject = "Yosai VPN Sentinel"
  21. const YosaiServerInventory = "Yosai VPN Servers"
  22. const YosaiVpnRotationJob = "VPN Rotation playbook"
  23. const YosaiEnvironment = "VPN Server configuration environment variables"
  24. type SemaphoreConnection struct {
  25. Client *http.Client
  26. Keyring keyring.DaemonKeyRing
  27. KeyTagger keytags.Keytagger
  28. Config *config.Configuration
  29. ServerUrl string
  30. HttpProto string
  31. ProjectId int
  32. }
  33. type TaskInfo struct {
  34. ID int `json:"id"`
  35. TemplateID int `json:"template_id"`
  36. Status string `json:"status"`
  37. Debug bool `json:"debug"`
  38. Playbook string `json:"playbook"`
  39. Environment string `json:"environment"`
  40. Secret string `json:"secret"`
  41. Limit string `json:"limit"`
  42. }
  43. type TaskOutput struct {
  44. TaskID int `json:"task_id"`
  45. Task string `json:"task"`
  46. Time time.Time `json:"time"`
  47. Output string `json:"output"`
  48. }
  49. type NewTemplateRequest struct {
  50. ProjectId int `json:"project_id"`
  51. Name string `json:"name"`
  52. InventoryId int `json:"inventory_id"`
  53. RepositoryId int `json:"repository_id"`
  54. EnvironmentId int `json:"environment_id"`
  55. Playbook string `json:"playbook"`
  56. App string `json:"app"`
  57. Type string `json:"type"`
  58. }
  59. type JobTemplate struct {
  60. Id int `json:"id"`
  61. ProjectId int `json:"project_id"`
  62. Name string `json:"name"`
  63. InventoryId int `json:"inventory_id"`
  64. RepositoryId int `json:"repository_id"`
  65. EnvironmentId int `json:"environment_id"`
  66. App string `json:"app"`
  67. Playbook string `json:"playbook"`
  68. }
  69. type StartTaskRequest struct {
  70. TemplateID int `json:"template_id"`
  71. ProjectId int `json:"project_id"`
  72. }
  73. type StartTaskResponse struct {
  74. Id int `json:"id"`
  75. TemplateID int `json:"template_id"`
  76. Debug bool `json:"debug"`
  77. DryRun bool `json:"dry_run"`
  78. Diff bool `json:"diff"`
  79. Playbook string `json:"playbook"`
  80. Environment string `json:"environment"`
  81. Limit string `json:"limit"`
  82. }
  83. type EnvironmentVariables struct {
  84. SecretsProviderUrl string `json:"SECRETS_PROVIDER_URL"`
  85. SecretsProviderApiKey string `json:"SECRETS_PROVIDER_API_KEY"`
  86. }
  87. type AddEnvironmentRequest struct {
  88. Name string `json:"name"`
  89. ProjectID int `json:"project_id"`
  90. Password string `json:"password"`
  91. JSON string `json:"json"`
  92. Env string `json:"env"`
  93. }
  94. type EnvironmentResponse struct {
  95. Id int `json:"id"`
  96. Name string `json:"name"`
  97. ProjectID int `json:"project_id"`
  98. Password string `json:"password"`
  99. JSON string `json:"json"`
  100. Env string `json:"env"`
  101. }
  102. type ProjectsResponse struct {
  103. Id int `json:"id"`
  104. Name string `json:"name"`
  105. Created string `json:"created"`
  106. Alert bool `json:"alert"`
  107. AlertChat string `json:"alert_chat"`
  108. MaxParallelTasks int `json:"max_parallel_tasks"`
  109. }
  110. type NewProjectReqeust struct {
  111. Name string `json:"name"`
  112. Alert bool `json:"alert"`
  113. AlertChat string `json:"alert_chat"`
  114. MaxParallelTasks int `json:"max_parallel_tasks"`
  115. }
  116. type NewRepoRequest struct {
  117. Name string `json:"name"` // name of the project
  118. ProjectId int `json:"project_id"` // the numerical ID of the project as per /api/project/<project id>
  119. GitUrl string `json:"git_url"` // the URL of the git repo (SSH address)
  120. GitBranch string `json:"git_branch"` // the branch to pull down
  121. SshKeyId int `json:"ssh_key_id"` // the numerical ID of the ssh key for the repository, as per /api/project/<project id>/keys
  122. }
  123. type NewRepoResponse struct {
  124. Id int `json:"id"` // the numerical ID assigned to the repo by Semaphore
  125. Name string `json:"name"` // name of the project
  126. ProjectId int `json:"project_id"` // the numerical ID of the project as per /api/project/<project id>
  127. GitUrl string `json:"git_url"` // the URL of the git repo (SSH address)
  128. GitBranch string `json:"git_branch"` // the branch to pull down
  129. SshKeyId int `json:"ssh_key_id"` // the numerical ID of the ssh key for the repository, as per /api/project/<project id>/keys
  130. }
  131. type AddKeyRequest struct {
  132. Name string `json:"name"`
  133. Type keyring.KeyType `json:"type"`
  134. Username config.Username `json:"username"`
  135. ProjectId int `json:"project_id"`
  136. LoginPassword loginPassword `json:"login_password"`
  137. Ssh sshKeyAdd `json:"ssh"`
  138. }
  139. func (a AddKeyRequest) GetPublic() string {
  140. if a.Type == "ssh" {
  141. return a.Ssh.PublicKey
  142. } else {
  143. return a.LoginPassword.Login
  144. }
  145. }
  146. func (a AddKeyRequest) GetSecret() string {
  147. if a.Type == "ssh" {
  148. return a.Ssh.PrivateKey
  149. } else {
  150. return a.LoginPassword.Password
  151. }
  152. }
  153. func (a AddKeyRequest) Prepare() string {
  154. b, err := json.Marshal(a)
  155. if err != nil {
  156. return err.Error()
  157. }
  158. return string(b)
  159. }
  160. func (a AddKeyRequest) Owner() config.Username {
  161. return a.Username
  162. }
  163. func (a AddKeyRequest) GetType() keyring.KeyType {
  164. return a.Type
  165. }
  166. type KeyItemResponse struct {
  167. Id int `json:"id"`
  168. Name string `json:"name"`
  169. Type keyring.KeyType `json:"type"`
  170. Username config.Username `json:"username"`
  171. ProjectId int `json:"project_id"`
  172. LoginPassword loginPassword `json:"login_password"`
  173. Ssh sshKeyAdd `json:"ssh"`
  174. }
  175. type NewInventoryRequest struct {
  176. Name string `json:"name"`
  177. ProjectId int `json:"project_id"`
  178. Inventory string `json:"inventory"` // This field is where the YAML inventory file gets put, as a string (not a filepath!)
  179. Type string `json:"type"`
  180. SshKeyId int `json:"ssh_key_id"`
  181. BecomeKeyId int `json:"become_key_id"`
  182. }
  183. type InventoryResponse struct {
  184. Id int `json:"id"`
  185. Inventory string `json:"inventory"`
  186. Name string `json:"name"`
  187. ProjectId int `json:"project_id"`
  188. Type string `json:"type"`
  189. SshKeyId int `json:"ssh_key_id"`
  190. BecomeKeyId int `json:"become_key_id"`
  191. }
  192. /*
  193. ####################################################################
  194. ############ IMPLEMENTING keyring.Key FOR KeyItemResponse ###########
  195. ####################################################################
  196. */
  197. func (k KeyItemResponse) GetPublic() string {
  198. return k.Ssh.PublicKey
  199. }
  200. func (k KeyItemResponse) GetSecret() string {
  201. return k.Ssh.PrivateKey
  202. }
  203. func (k KeyItemResponse) Prepare() string {
  204. return k.Name
  205. }
  206. func (k KeyItemResponse) GetType() keyring.KeyType {
  207. return k.Type
  208. }
  209. func (k KeyItemResponse) Owner() config.Username {
  210. return k.Username
  211. }
  212. type loginPassword struct {
  213. Password string `json:"password"`
  214. Login string `json:"login"`
  215. }
  216. type sshKeyAdd struct {
  217. PrivateKey string `json:"private_key"`
  218. PublicKey string `json:"public_key"`
  219. Login config.Username `json:"login"`
  220. }
  221. /*
  222. ###################################################################
  223. ########### IMPLEMENTING THE DaemonKeyRing INTERFACE ##############
  224. ###################################################################
  225. */
  226. /*
  227. Get SSH key by its name
  228. */
  229. func (s SemaphoreConnection) GetKey(name string) (keyring.Key, error) {
  230. var key KeyItemResponse
  231. keys, err := s.GetAllKeys()
  232. if err != nil {
  233. return key, keyring.KeyRingError
  234. }
  235. for i := range keys {
  236. if keys[i].Name == name {
  237. return keys[i], nil
  238. }
  239. }
  240. return key, keyring.KeyNotFound
  241. }
  242. /*
  243. Add SSH Key to a project
  244. :param name: the name to assign the key in the project
  245. :param keyring: a keyring.DaemonKeyRing implementer that can return the API key for Semaphore
  246. :param key: a keyring.Key implementer wrapping the SSH key
  247. */
  248. func (s SemaphoreConnection) AddKey(name string, key keyring.Key) error {
  249. _, err := s.GetKeyId(name)
  250. if err == nil { // return if the key exists
  251. return nil
  252. }
  253. path := fmt.Sprintf("%s/%v/keys", ProjectPath, s.ProjectId)
  254. _, err = s.Post(path, bytes.NewReader([]byte(key.Prepare())))
  255. if err != nil {
  256. return err
  257. }
  258. return nil
  259. }
  260. /*
  261. Drop a key from the Semaphore secret store
  262. */
  263. func (s SemaphoreConnection) RemoveKey(name string) error {
  264. _, err := s.Delete(name)
  265. return err
  266. }
  267. // Return the resource name for logging purposes
  268. func (s SemaphoreConnection) Source() string {
  269. return "Semaphore Keystore"
  270. }
  271. // NewKeyRequest builder function
  272. func (s SemaphoreConnection) NewKeyRequestBuilder(name string, key keyring.Key) keyring.Key {
  273. if key.GetType() == keyring.SSH_KEY {
  274. return AddKeyRequest{
  275. Name: name,
  276. Type: key.GetType(),
  277. ProjectId: s.ProjectId,
  278. Ssh: sshKeyAdd{
  279. Login: key.Owner(),
  280. PrivateKey: key.GetSecret(),
  281. },
  282. }
  283. } else {
  284. return AddKeyRequest{
  285. Name: name,
  286. Type: key.GetType(),
  287. ProjectId: s.ProjectId,
  288. LoginPassword: loginPassword{
  289. Login: key.GetPublic(),
  290. Password: key.GetSecret(),
  291. },
  292. }
  293. }
  294. }
  295. /*
  296. Create a new semaphore client
  297. :param url: the base url of the semaphore server, without the HTTP/S prefix
  298. :param proto: either HTTP or HTTPS, depending on the server's SSL setup
  299. :param log: an io.Writer to write logfile to
  300. :param keyring: a keyring.DaemonKeyRing implementer to get the Semaphore API key from
  301. */
  302. func NewSemaphoreClient(url string, proto string, keyring keyring.DaemonKeyRing, conf *config.Configuration, keytagger keytags.Keytagger) SemaphoreConnection {
  303. client := &http.Client{}
  304. semaphoreBootstrap := SemaphoreConnection{Client: client, ServerUrl: url, HttpProto: proto, Keyring: keyring, KeyTagger: keytagger, Config: conf}
  305. semaphoreBootstrap.Log("Using mode ", proto)
  306. id, err := semaphoreBootstrap.GetProjectByName(YosaiProject)
  307. if err != nil {
  308. semaphoreBootstrap.Log(YosaiProject, "Not found in semaphore. Creating...")
  309. err = semaphoreBootstrap.NewProject(YosaiProject)
  310. if err != nil {
  311. semaphoreBootstrap.Log("Fatal error creating the project in semaphore: ", err.Error(), "exiting.")
  312. os.Exit(127)
  313. }
  314. id, err := semaphoreBootstrap.GetProjectByName(YosaiProject)
  315. if err != nil {
  316. semaphoreBootstrap.Log("Error finding the project: ", YosaiProject, err.Error(), "exiting.")
  317. os.Exit(127)
  318. }
  319. semaphoreBootstrap.Log(YosaiProject, "found with ID: ", fmt.Sprint(id))
  320. semaphoreBootstrap.Log("OK! Semaphore connection established.")
  321. return SemaphoreConnection{
  322. Client: client,
  323. ServerUrl: url,
  324. HttpProto: proto,
  325. ProjectId: id,
  326. Keyring: keyring,
  327. Config: conf,
  328. KeyTagger: keytagger,
  329. }
  330. }
  331. semaphoreBootstrap.Log("OK! Semaphore connection established.")
  332. return SemaphoreConnection{
  333. Client: &http.Client{},
  334. ServerUrl: url,
  335. HttpProto: proto,
  336. ProjectId: id,
  337. Keyring: keyring,
  338. Config: conf,
  339. KeyTagger: keytagger,
  340. }
  341. }
  342. // logging wrapper
  343. func (s *SemaphoreConnection) Log(msg ...string) {
  344. semMsg := []string{
  345. "SemaphoreConnection:",
  346. }
  347. semMsg = append(semMsg, msg...)
  348. s.Config.Log(semMsg...)
  349. }
  350. /*
  351. Create a new 'Project' in Semaphore
  352. :param name: the name to assign the project
  353. :param keyring: a keyring.DaemonKeyRing implementer to get the Semaphore API key from
  354. */
  355. func (s SemaphoreConnection) NewProject(name string) error {
  356. _, err := s.GetProjectByName(name)
  357. if err == nil {
  358. return nil // return nil of project already exists
  359. }
  360. var b []byte
  361. newProj := NewProjectReqeust{
  362. Name: name,
  363. Alert: false,
  364. AlertChat: "",
  365. MaxParallelTasks: 0,
  366. }
  367. b, err = json.Marshal(&newProj)
  368. if err != nil {
  369. return &SemaphoreClientError{Msg: err.Error()}
  370. }
  371. _, err = s.Post(ProjectsPath, bytes.NewReader(b))
  372. if err != nil {
  373. return err
  374. }
  375. return nil
  376. }
  377. /*
  378. Add a repository to the project designated for the Yosai service
  379. :param giturl: the url for the git repo containing the ansible scripts for VPN server config
  380. :param branch: the branch to target on the git repo
  381. */
  382. func (s SemaphoreConnection) AddRepository(giturl string, branch string) error {
  383. _, err := s.GetRepoByName(fmt.Sprintf("%s:%s", giturl, branch))
  384. if err == nil { // return if the repo exists
  385. return nil
  386. }
  387. sshKeyId, err := s.GetKeyId(s.KeyTagger.SystemSshKeyname())
  388. if err != nil {
  389. return err
  390. }
  391. repoAddRequest := NewRepoRequest{
  392. Name: fmt.Sprintf("%s:%s", giturl, branch),
  393. ProjectId: s.ProjectId,
  394. GitUrl: giturl,
  395. GitBranch: branch,
  396. SshKeyId: sshKeyId,
  397. }
  398. b, err := json.Marshal(&repoAddRequest)
  399. if err != nil {
  400. return &SemaphoreClientError{Msg: err.Error()}
  401. }
  402. _, err = s.Post(fmt.Sprintf("%s/%v/repositories", ProjectPath, s.ProjectId), bytes.NewReader(b))
  403. if err != nil {
  404. return err
  405. }
  406. return nil
  407. }
  408. /*
  409. Generic POST Request to sent to the Semaphore server
  410. :param path: the path to the API to POST. Preceeding slashes will be trimmed
  411. :param body: an io.Reader implementer to use as the POST body. Must comply with application/json Content-Type
  412. */
  413. func (s SemaphoreConnection) Put(path string, body io.Reader) ([]byte, error) {
  414. var b []byte
  415. apikey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
  416. if err != nil {
  417. return b, &SemaphoreClientError{Msg: err.Error()}
  418. }
  419. req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), body)
  420. if err != nil {
  421. return b, &SemaphoreClientError{Msg: err.Error()}
  422. }
  423. req.Header.Add("Authorization", apikey.Prepare())
  424. req.Header.Add("Content-Type", "application/json")
  425. resp, err := s.Client.Do(req)
  426. if err != nil {
  427. return b, &SemaphoreClientError{Msg: err.Error()}
  428. }
  429. defer resp.Body.Close()
  430. if resp.StatusCode >= 400 {
  431. return b, &SemaphoreClientError{Msg: resp.Status}
  432. }
  433. b, err = io.ReadAll(resp.Body)
  434. if err != nil {
  435. return b, &SemaphoreClientError{Msg: err.Error()}
  436. }
  437. return b, nil
  438. }
  439. /*
  440. Generic POST Request to sent to the Semaphore server
  441. :param path: the path to the API to POST. Preceeding slashes will be trimmed
  442. :param body: an io.Reader implementer to use as the POST body. Must comply with application/json Content-Type
  443. */
  444. func (s SemaphoreConnection) Post(path string, body io.Reader) ([]byte, error) {
  445. var b []byte
  446. apikey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
  447. if err != nil {
  448. return b, &SemaphoreClientError{Msg: err.Error()}
  449. }
  450. req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), body)
  451. if err != nil {
  452. return b, &SemaphoreClientError{Msg: err.Error()}
  453. }
  454. req.Header.Add("Authorization", apikey.Prepare())
  455. req.Header.Add("Content-Type", "application/json")
  456. resp, err := s.Client.Do(req)
  457. if err != nil {
  458. return b, &SemaphoreClientError{Msg: err.Error()}
  459. }
  460. defer resp.Body.Close()
  461. b, err = io.ReadAll(resp.Body)
  462. if err != nil {
  463. return b, &SemaphoreClientError{Msg: err.Error()}
  464. }
  465. if resp.StatusCode >= 400 {
  466. return b, &SemaphoreClientError{Msg: resp.Status}
  467. }
  468. return b, nil
  469. }
  470. /*
  471. Agnostic GET method for calling the upstream Semaphore server
  472. :param path: the path to GET, added into the base API url
  473. */
  474. func (s SemaphoreConnection) Get(path string) ([]byte, error) {
  475. var b []byte
  476. apiKey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
  477. if err != nil {
  478. return b, &SemaphoreClientError{Msg: err.Error()}
  479. }
  480. req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), nil)
  481. if err != nil {
  482. return b, &SemaphoreClientError{Msg: err.Error()}
  483. }
  484. req.Header.Add("Authorization", apiKey.Prepare())
  485. resp, err := s.Client.Do(req)
  486. if err != nil {
  487. return b, &SemaphoreClientError{Msg: err.Error()}
  488. }
  489. defer resp.Body.Close()
  490. b, err = io.ReadAll(resp.Body)
  491. if err != nil {
  492. return b, &SemaphoreClientError{Msg: err.Error()}
  493. }
  494. return b, nil
  495. }
  496. /*
  497. Generic DELETE method for calling the Semaphore server
  498. */
  499. func (s SemaphoreConnection) Delete(path string) ([]byte, error) {
  500. return []byte{}, nil
  501. }
  502. /*
  503. Retrieve the projects in Semaphore
  504. :param keyring: a keyring.DaemonKeyRing implementer to get the API key from for Semaphore
  505. */
  506. func (s SemaphoreConnection) GetProjects() ([]ProjectsResponse, error) {
  507. var projectsResp []ProjectsResponse
  508. b, err := s.Get(ProjectsPath)
  509. if err != nil {
  510. return projectsResp, err
  511. }
  512. err = json.Unmarshal(b, &projectsResp)
  513. if err != nil {
  514. return projectsResp, &SemaphoreClientError{Msg: err.Error()}
  515. }
  516. return projectsResp, nil
  517. }
  518. /*
  519. Get Project by its name, and return its ID
  520. */
  521. func (s SemaphoreConnection) GetProjectByName(name string) (int, error) {
  522. projects, err := s.GetProjects()
  523. if err != nil {
  524. return 0, err
  525. }
  526. for i := range projects {
  527. if projects[i].Name == name {
  528. return projects[i].Id, nil
  529. }
  530. }
  531. return 0, &SemaphoreClientError{Msg: fmt.Sprintf("Project with name: '%s' not found.", name)}
  532. }
  533. /*
  534. Get SSH Keys from the current project
  535. */
  536. func (s SemaphoreConnection) GetAllKeys() ([]KeyItemResponse, error) {
  537. var keys []KeyItemResponse
  538. b, err := s.Get(fmt.Sprintf("%s/%v/keys", ProjectPath, s.ProjectId))
  539. if err != nil {
  540. return keys, err
  541. }
  542. err = json.Unmarshal(b, &keys)
  543. if err != nil {
  544. return keys, &SemaphoreClientError{Msg: err.Error()}
  545. }
  546. return keys, nil
  547. }
  548. /*
  549. Return a key ID from the Semaphore keystore by it's name
  550. :param keyname: the name of the key in Semaphore
  551. */
  552. func (s SemaphoreConnection) GetKeyId(keyname string) (int, error) {
  553. keys, err := s.GetAllKeys()
  554. if err != nil {
  555. return 0, err
  556. }
  557. for i := range keys {
  558. if keys[i].Name == keyname {
  559. return keys[i].Id, nil
  560. }
  561. }
  562. return 0, &KeyNotFound{Keyname: keyname}
  563. }
  564. /*
  565. Get the output of a task
  566. :param taskId: the ID of the task that was ran
  567. */
  568. func (s SemaphoreConnection) GetTaskOutput(taskId int) ([]TaskOutput, error) {
  569. var taskout []TaskOutput
  570. b, err := s.Get(fmt.Sprintf("%s/%v/tasks/%v/output", ProjectPath, s.ProjectId, taskId))
  571. if err != nil {
  572. return taskout, err
  573. }
  574. err = json.Unmarshal(b, &taskout)
  575. if err != nil {
  576. return taskout, &SemaphoreClientError{Msg: "Could not unmarshall the response from getting task output." + err.Error()}
  577. }
  578. return taskout, nil
  579. }
  580. /*
  581. Get information relating to a task
  582. :param taskId: the ID of the task that was ran
  583. */
  584. func (s SemaphoreConnection) GetTaskInfo(taskId int) (TaskInfo, error) {
  585. var taskout TaskInfo
  586. b, err := s.Get(fmt.Sprintf("%s/%v/tasks/%v", ProjectPath, s.ProjectId, taskId))
  587. if err != nil {
  588. return taskout, err
  589. }
  590. err = json.Unmarshal(b, &taskout)
  591. if err != nil {
  592. return taskout, &SemaphoreClientError{Msg: "Could not unmarshall the response from getting task output." + err.Error()}
  593. }
  594. return taskout, nil
  595. }
  596. /*
  597. Poll for task completion
  598. :param taskId: the ID of the task to be polled
  599. :param max_tries: the number of times to poll the running task before timing out
  600. */
  601. func (s SemaphoreConnection) PollTask(taskId int, max_tries int) error {
  602. var attempts int
  603. for {
  604. attempts = attempts + 1
  605. s.Log("Polling task: ", fmt.Sprint(taskId), " for ", fmt.Sprint(attempts), " times.")
  606. if attempts > max_tries {
  607. s.Log("Polling for job completion timed out after: ", fmt.Sprint(attempts), " attempts.")
  608. return &SemaphoreTimeout{Tries: attempts}
  609. }
  610. resp, err := s.GetTaskInfo(taskId)
  611. if err != nil {
  612. return err
  613. }
  614. s.Log("Job: ", fmt.Sprint(taskId), " is marked with status: ", resp.Status)
  615. if resp.Status == "success" {
  616. return nil
  617. }
  618. if resp.Status == "error" {
  619. return &SemaphoreTimeout{Tries: attempts}
  620. }
  621. time.Sleep(time.Second * 5)
  622. }
  623. }
  624. /*
  625. Add an inventory to semaphore
  626. :param hosts: a list of IP addresses to add to the inventory
  627. */
  628. func (s SemaphoreConnection) AddInventory(name string) error {
  629. _, err := s.GetInventoryByName(name)
  630. if err == nil { // Returning on nil error because that means the inventory exists
  631. return &SemaphoreClientError{Msg: "Inventory Exists! Please update rather than create a new."}
  632. }
  633. sshKeyId, err := s.GetKeyId(s.KeyTagger.SystemSshKeyname())
  634. if err != nil {
  635. return err
  636. }
  637. becomeKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccKeyname())
  638. if err != nil {
  639. return &SemaphoreClientError{Msg: err.Error()}
  640. }
  641. inv := YamlInventory{All: yamlInvAll{Hosts: map[string]yamlVars{}}}
  642. b, err := yaml.Marshal(inv)
  643. if err != nil {
  644. s.Config.Log("failed to generate yaml inv stub, using default fallback values. Error: ", err.Error())
  645. b = []byte("all:\n hosts: {}\n")
  646. }
  647. body := NewInventoryRequest{
  648. Name: name,
  649. ProjectId: s.ProjectId,
  650. Inventory: string(b),
  651. SshKeyId: sshKeyId,
  652. BecomeKeyId: becomeKeyId,
  653. Type: "static-yaml",
  654. }
  655. requestBody, err := json.Marshal(&body)
  656. if err != nil {
  657. return &SemaphoreClientError{Msg: err.Error()}
  658. }
  659. _, err = s.Post(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "inventory"), bytes.NewReader(requestBody))
  660. return err
  661. }
  662. /*
  663. Get Inventory by name and return its ID
  664. :param name: the name of the inventory to find
  665. */
  666. func (s SemaphoreConnection) GetInventoryByName(name string) (InventoryResponse, error) {
  667. var out InventoryResponse
  668. resp, err := s.GetAllInventories()
  669. if err != nil {
  670. return out, err
  671. }
  672. for i := range resp {
  673. if resp[i].Name == name {
  674. return resp[i], nil
  675. }
  676. }
  677. return out, &KeyNotFound{Keyname: name}
  678. }
  679. /*
  680. Get all inventories from Semaphore
  681. */
  682. func (s SemaphoreConnection) GetAllInventories() ([]InventoryResponse, error) {
  683. var resp []InventoryResponse
  684. b, err := s.Get(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "inventory"))
  685. if err != nil {
  686. return resp, err
  687. }
  688. err = json.Unmarshal(b, &resp)
  689. if err != nil {
  690. return resp, &SemaphoreClientError{Msg: err.Error()}
  691. }
  692. return resp, nil
  693. }
  694. /*
  695. Update an inventory
  696. */
  697. func (s SemaphoreConnection) UpdateInventory(name string, inv YamlInventory) error {
  698. sshKeyId, err := s.GetKeyId(s.KeyTagger.SystemSshKeyname())
  699. if err != nil {
  700. return err
  701. }
  702. becomeKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccKeyname())
  703. if err != nil {
  704. return err
  705. }
  706. b, err := yaml.Marshal(inv)
  707. if err != nil {
  708. return &SemaphoreClientError{Msg: "Error unmarshalling YAML inventory payload: " + err.Error()}
  709. }
  710. targetInv, err := s.GetInventoryByName(name)
  711. if err != nil {
  712. return &SemaphoreClientError{Msg: "Target inventory: " + name + " was not found."}
  713. }
  714. body := InventoryResponse{
  715. Id: targetInv.Id,
  716. Name: name,
  717. ProjectId: s.ProjectId,
  718. Inventory: string(b),
  719. SshKeyId: sshKeyId,
  720. BecomeKeyId: becomeKeyId,
  721. Type: "static-yaml",
  722. }
  723. req, err := json.Marshal(body)
  724. if err != nil {
  725. return &SemaphoreClientError{Msg: "There was an error marshalling the JSON payload: " + err.Error()}
  726. }
  727. _, err = s.Put(fmt.Sprintf("%s/%v/inventory/%v", ProjectPath, s.ProjectId, targetInv.Id), bytes.NewReader(req))
  728. return err
  729. }
  730. /*
  731. Remove host from an inventory
  732. */
  733. func (s SemaphoreConnection) RemoveHostFromInv(name string, host ...string) error {
  734. resp, err := s.GetInventoryByName(name)
  735. if err != nil {
  736. return err
  737. }
  738. var inv YamlInventory
  739. err = yaml.Unmarshal([]byte(resp.Inventory), &inv)
  740. if err != nil {
  741. return &SemaphoreClientError{Msg: "Error unmarshalling inventory from server: " + resp.Inventory + err.Error()}
  742. }
  743. for i := range host {
  744. _, ok := inv.All.Hosts[host[i]]
  745. if !ok {
  746. return &SemaphoreClientError{Msg: "Host: " + host[i] + " not found in the inventory: " + resp.Inventory}
  747. }
  748. delete(inv.All.Hosts, host[i])
  749. }
  750. return s.UpdateInventory(name, inv)
  751. }
  752. /*
  753. Add hosts to inventory
  754. */
  755. func (s SemaphoreConnection) AddHostToInv(name string, host ...config.VpnServer) error {
  756. resp, err := s.GetInventoryByName(name)
  757. if err != nil {
  758. return err
  759. }
  760. var inv YamlInventory
  761. err = yaml.Unmarshal([]byte(resp.Inventory), &inv)
  762. if err != nil {
  763. return &SemaphoreClientError{Msg: "Error unmarshalling inventory from server: " + resp.Inventory + err.Error()}
  764. }
  765. newHosts := s.YamlInventoryBuilder(host)
  766. for addr, host := range newHosts.All.Hosts {
  767. inv.All.Hosts[addr] = host
  768. }
  769. return s.UpdateInventory(name, inv)
  770. }
  771. /*
  772. Get a repo ID by its name
  773. :param name: the name of the repo
  774. */
  775. func (s SemaphoreConnection) GetRepoByName(name string) (int, error) {
  776. resp, err := s.GetAllRepos()
  777. if err != nil {
  778. return 0, err
  779. }
  780. for i := range resp {
  781. if resp[i].Name == name {
  782. return resp[i].Id, nil
  783. }
  784. }
  785. return 0, &KeyNotFound{Keyname: name}
  786. }
  787. /*
  788. Get all repositories from Semaphore
  789. */
  790. func (s SemaphoreConnection) GetAllRepos() ([]NewRepoResponse, error) {
  791. var resp []NewRepoResponse
  792. b, err := s.Get(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "repositories"))
  793. if err != nil {
  794. return resp, &SemaphoreClientError{Msg: err.Error()}
  795. }
  796. err = json.Unmarshal(b, &resp)
  797. if err != nil {
  798. return resp, &SemaphoreClientError{Msg: err.Error()}
  799. }
  800. return resp, nil
  801. }
  802. // Create an environment variable configuration, currently unimplemented
  803. func (s SemaphoreConnection) AddEnvironment(envVars EnvironmentVariables) error {
  804. envBytes, err := json.Marshal(envVars)
  805. if err != nil {
  806. return &SemaphoreClientError{Msg: "Couldnt unmarshall the environment variable payload: " + err.Error()}
  807. }
  808. _, err = s.GetEnvironmentId(YosaiEnvironment)
  809. if err == nil {
  810. return nil // environment exists, dont add another with same name
  811. }
  812. var body AddEnvironmentRequest
  813. body = AddEnvironmentRequest{
  814. Name: YosaiEnvironment,
  815. ProjectID: s.ProjectId,
  816. JSON: "{}",
  817. Env: string(envBytes),
  818. }
  819. b, err := json.Marshal(body)
  820. if err != nil {
  821. return &SemaphoreClientError{Msg: "couldnt marshal the JSON payload"}
  822. }
  823. _, err = s.Post(fmt.Sprintf("%s/%v/environment", ProjectPath, s.ProjectId), bytes.NewBuffer(b))
  824. return err
  825. }
  826. // Get an environment configuration ID by name.
  827. func (s SemaphoreConnection) GetEnvironmentId(name string) (int, error) {
  828. var env []EnvironmentResponse
  829. b, err := s.Get(fmt.Sprintf("%s/%v/environment", ProjectPath, s.ProjectId))
  830. if err != nil {
  831. return 0, err
  832. }
  833. err = json.Unmarshal(b, &env)
  834. if err != nil {
  835. return 0, &SemaphoreClientError{Msg: "Couldnt unmarshall the response"}
  836. }
  837. for i := range env {
  838. if env[i].Name == name {
  839. return env[i].Id, nil
  840. }
  841. }
  842. return 0, &KeyNotFound{Keyname: "Couldnt find environment: " + name}
  843. }
  844. /*
  845. Add job template to the Yosai project on Semaphore
  846. :param playbook: the name of the playbook file
  847. :param repoName: the name of the repo that the playbook belongs to
  848. */
  849. func (s SemaphoreConnection) AddJobTemplate(playbook string, repoName string) error {
  850. _, err := s.JobTemplateByName(YosaiVpnRotationJob)
  851. if err == nil {
  852. return nil // return nil because template exists
  853. }
  854. repoId, err := s.GetRepoByName(repoName)
  855. if err != nil {
  856. return err
  857. }
  858. InventoryItem, err := s.GetInventoryByName(YosaiServerInventory)
  859. if err != nil {
  860. return err
  861. }
  862. envId, err := s.GetEnvironmentId(YosaiEnvironment)
  863. if err != nil {
  864. return err
  865. }
  866. templ := NewTemplateRequest{
  867. ProjectId: s.ProjectId,
  868. Name: YosaiVpnRotationJob,
  869. InventoryId: InventoryItem.Id,
  870. RepositoryId: repoId,
  871. EnvironmentId: envId,
  872. Playbook: playbook,
  873. App: "ansible",
  874. Type: "",
  875. }
  876. b, err := json.Marshal(templ)
  877. if err != nil {
  878. return &SemaphoreClientError{Msg: err.Error()}
  879. }
  880. b, err = s.Post(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "templates"), bytes.NewReader(b))
  881. if err != nil {
  882. return &SemaphoreClientError{Msg: fmt.Sprintf("Error: %s\nServer Response: %s", err.Error(), string(b))}
  883. }
  884. return nil
  885. }
  886. /*
  887. Start a task in Semaphore by the template name
  888. :param name: the name of the job template to start
  889. */
  890. func (s SemaphoreConnection) StartJob(name string) (StartTaskResponse, error) {
  891. var resp StartTaskResponse
  892. template, err := s.JobTemplateByName(name)
  893. if err != nil {
  894. return resp, &SemaphoreClientError{Msg: "Could not start job template: " + name + "Error: " + err.Error()}
  895. }
  896. var jobReq StartTaskRequest
  897. jobReq = StartTaskRequest{
  898. TemplateID: template.Id,
  899. ProjectId: s.ProjectId,
  900. }
  901. b, err := json.Marshal(&jobReq)
  902. if err != nil {
  903. return resp, &SemaphoreClientError{Msg: "Couldnt marshal data into byte array: " + err.Error()}
  904. }
  905. rb, err := s.Post(fmt.Sprintf("%s/%v/tasks", ProjectPath, s.ProjectId), bytes.NewReader(b))
  906. if err != nil {
  907. return resp, err
  908. }
  909. err = json.Unmarshal(rb, &resp)
  910. if err != nil {
  911. return resp, &SemaphoreClientError{Msg: "Couldnt unmarshal the response from semaphore: " + err.Error()}
  912. }
  913. return resp, nil
  914. }
  915. /*
  916. Get a job template ID by name
  917. :param name: the name of the job template ID
  918. */
  919. func (s SemaphoreConnection) GetAllTemplates() ([]JobTemplate, error) {
  920. var jobs []JobTemplate
  921. resp, err := s.Get(fmt.Sprintf("%s/%v/templates", ProjectPath, s.ProjectId))
  922. if err != nil {
  923. return jobs, err
  924. }
  925. err = json.Unmarshal(resp, &jobs)
  926. if err != nil {
  927. return jobs, &SemaphoreClientError{Msg: "Error unmarshalling payload response: " + err.Error()}
  928. }
  929. return jobs, nil
  930. }
  931. /*
  932. Bootstrap the Semaphore environment
  933. */
  934. /*
  935. Get a job template ID by name
  936. :param name: the name of the job template ID
  937. */
  938. func (s SemaphoreConnection) JobTemplateByName(name string) (JobTemplate, error) {
  939. var job JobTemplate
  940. jobs, err := s.GetAllTemplates()
  941. if err != nil {
  942. return job, err
  943. }
  944. for i := range jobs {
  945. if jobs[i].Name == name {
  946. return jobs[i], nil
  947. }
  948. }
  949. return job, &SemaphoreClientError{Msg: "Job with name" + name + "not found"}
  950. }
  951. /*
  952. ##########################################################
  953. ################## DAEMON ROUTE HANDLERS #################
  954. ##########################################################
  955. */
  956. type SemaphoreRequest struct {
  957. Target string `json:"target"`
  958. }
  959. /*
  960. Wrapping the functioanlity of the keyring bootstrapper for top level cleanliness
  961. */
  962. func (s SemaphoreConnection) keyBootstrapper() daemonproto.SockMessage {
  963. reqKeys := s.KeyTagger.GetAnsibleKeys()
  964. for i := range reqKeys {
  965. kn := reqKeys[i]
  966. key, err := s.Keyring.GetKey(kn)
  967. if err != nil {
  968. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  969. }
  970. err = s.AddKey(kn, s.NewKeyRequestBuilder(kn, key))
  971. if err != nil {
  972. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  973. }
  974. }
  975. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte("Daemon keyring successfuly bootstrapped."))
  976. }
  977. /*
  978. Wrapping the functionality of the Project bootstrapper for top level cleanliness
  979. */
  980. func (s SemaphoreConnection) projectBootstrapper() daemonproto.SockMessage {
  981. s.Config.Log("Entered Semaphore bootstrapping ...")
  982. err := s.NewProject(YosaiProject)
  983. if err != nil {
  984. s.Config.Log("Error creating the project: ", err.Error())
  985. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  986. }
  987. err = s.AddRepository(s.Config.Ansible.Repo, s.Config.Ansible.Branch)
  988. if err != nil {
  989. s.Config.Log("Error creating the repository: ", err.Error())
  990. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  991. }
  992. hashiKey, err := s.Keyring.GetKey(s.KeyTagger.HashicorpVaultKeyname())
  993. if err != nil {
  994. s.Config.Log("Error getting the hashicorp keyring from the keyring: ", err.Error())
  995. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  996. }
  997. err = s.AddEnvironment(EnvironmentVariables{SecretsProviderUrl: s.Config.Service.SecretsBackendUrl, SecretsProviderApiKey: hashiKey.GetSecret()})
  998. if err != nil {
  999. s.Config.Log("Error creating the environment: ", err.Error())
  1000. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1001. }
  1002. err = s.AddJobTemplate(s.Config.Ansible.PlaybookName, fmt.Sprintf("%s:%s", s.Config.Ansible.Repo, s.Config.Ansible.Branch))
  1003. if err != nil {
  1004. s.Config.Log("Error creating the job template: ", err.Error())
  1005. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1006. }
  1007. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte("Project successfuly bootstrapped."))
  1008. }
  1009. /*
  1010. Wrapping the inventory bootstrap functionality for top level cleanliness
  1011. */
  1012. func (s SemaphoreConnection) inventoryBootstrapper() daemonproto.SockMessage {
  1013. err := s.AddInventory(YosaiServerInventory)
  1014. if err != nil {
  1015. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1016. }
  1017. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte("Inventory successfuly bootstrapped."))
  1018. }
  1019. func (s SemaphoreConnection) BootstrapHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1020. bootstrapFuncs := []func() daemonproto.SockMessage{
  1021. s.keyBootstrapper,
  1022. s.inventoryBootstrapper,
  1023. s.projectBootstrapper,
  1024. }
  1025. successMsg := ""
  1026. for i := range bootstrapFuncs {
  1027. call := bootstrapFuncs[i]
  1028. resp := call()
  1029. if resp.StatusCode != daemonproto.REQUEST_OK {
  1030. s.Log("Error bootstrapping components: ", resp.Target, string(resp.Body))
  1031. continue
  1032. }
  1033. successMsg = successMsg + resp.StatusMsg + "\n"
  1034. }
  1035. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte(successMsg))
  1036. }
  1037. /*
  1038. Wrapping the add project function in a route friendly interface
  1039. :param msg: a message to parse that was recieved from the daemon socket
  1040. */
  1041. func (s SemaphoreConnection) AddProjectHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1042. var req SemaphoreRequest
  1043. err := json.Unmarshal(msg.Body, &req)
  1044. if err != nil {
  1045. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1046. }
  1047. err = s.NewProject(req.Target)
  1048. if err != nil {
  1049. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1050. }
  1051. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte("Project: "+req.Target+" successfully added."))
  1052. }
  1053. /*
  1054. Wrapping the show project function in a route friendly interface
  1055. :param msg: a message to parse that was recieved from the daemon socket
  1056. */
  1057. func (s SemaphoreConnection) ShowProjectHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1058. proj, err := s.GetProjects()
  1059. if err != nil {
  1060. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1061. }
  1062. b, err := json.MarshalIndent(proj, " ", " ")
  1063. if err != nil {
  1064. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1065. }
  1066. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, b)
  1067. }
  1068. /*
  1069. Wrapping the run task function in a route friendly interface
  1070. :param msg: a message to parse that was recieved from the daemon socket
  1071. */
  1072. func (s SemaphoreConnection) RunTaskHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1073. var req SemaphoreRequest
  1074. err := json.Unmarshal(msg.Body, &req)
  1075. if err != nil {
  1076. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1077. }
  1078. resp, err := s.StartJob(req.Target)
  1079. if err != nil {
  1080. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1081. }
  1082. b, err := json.MarshalIndent(resp, " ", " ")
  1083. if err != nil {
  1084. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1085. }
  1086. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, b)
  1087. }
  1088. /*
  1089. Wrapping the show task function in a route friendly interface
  1090. :param msg: a message to parse that was recieved from the daemon socket
  1091. */
  1092. func (s SemaphoreConnection) ShowTaskHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1093. var req SemaphoreRequest
  1094. err := json.Unmarshal(msg.Body, &req)
  1095. if err != nil {
  1096. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1097. }
  1098. taskid, err := strconv.Atoi(req.Target)
  1099. if err != nil {
  1100. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1101. }
  1102. taskout, err := s.GetTaskOutput(taskid)
  1103. if err != nil {
  1104. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1105. }
  1106. b, err := json.MarshalIndent(taskout, " ", " ")
  1107. if err != nil {
  1108. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1109. }
  1110. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, b)
  1111. }
  1112. /*
  1113. Wrapping the poll task function in a route friendly interface
  1114. :param msg: a message to parse that was recieved from the daemon socket
  1115. */
  1116. func (s SemaphoreConnection) PollTaskHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1117. var req SemaphoreRequest
  1118. err := json.Unmarshal(msg.Body, &req)
  1119. if err != nil {
  1120. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1121. }
  1122. taskId, err := strconv.Atoi(req.Target)
  1123. if err != nil {
  1124. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_TIMEOUT, []byte(err.Error()))
  1125. }
  1126. err = s.PollTask(taskId, 60)
  1127. if err != nil {
  1128. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_TIMEOUT, []byte(err.Error()))
  1129. }
  1130. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, []byte("Task: "+req.Target+" completed."))
  1131. }
  1132. /*
  1133. Wrapping the show hosts functions in a route friendly interface
  1134. :param msg: a message to parse that was recieved from the daemon socket
  1135. */
  1136. func (s SemaphoreConnection) ShowHostHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1137. inv, err := s.GetAllInventories()
  1138. if err != nil {
  1139. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1140. }
  1141. b, err := json.Marshal(inv)
  1142. if err != nil {
  1143. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1144. }
  1145. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_OK, b)
  1146. }
  1147. /*
  1148. Wrapping the delete host function in a route friendly interface
  1149. :param msg: a message to parse that was recieved from the daemon socket
  1150. */
  1151. func (s SemaphoreConnection) DeleteHostHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1152. var req SemaphoreRequest
  1153. err := json.Unmarshal(msg.Body, &req)
  1154. if err != nil {
  1155. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1156. }
  1157. hosts := strings.Split(strings.Trim(req.Target, ","), ",")
  1158. err = s.RemoveHostFromInv(YosaiServerInventory, hosts...)
  1159. if err != nil {
  1160. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1161. }
  1162. return *daemonproto.NewSockMessage(daemonproto.MsgRequest, daemonproto.REQUEST_OK, []byte(fmt.Sprintf("Host: %v removed from the inventory", hosts)))
  1163. }
  1164. /*
  1165. Wrapping the add host function in a route friendly interface
  1166. :param msg: a message to parse that was recieved from the daemon socket
  1167. */
  1168. func (s SemaphoreConnection) AddHostHandler(msg daemonproto.SockMessage) daemonproto.SockMessage {
  1169. var req SemaphoreRequest
  1170. err := json.Unmarshal(msg.Body, &req)
  1171. if err != nil {
  1172. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1173. }
  1174. hosts := strings.Split(strings.Trim(req.Target, ","), ",")
  1175. vpnHosts := []config.VpnServer{}
  1176. for i := range hosts {
  1177. server, err := s.Config.GetServer(hosts[i])
  1178. if err != nil {
  1179. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1180. }
  1181. vpnHosts = append(vpnHosts, server)
  1182. }
  1183. err = s.AddHostToInv(YosaiServerInventory, vpnHosts...)
  1184. if err != nil {
  1185. return *daemonproto.NewSockMessage(daemonproto.MsgResponse, daemonproto.REQUEST_FAILED, []byte(err.Error()))
  1186. }
  1187. return *daemonproto.NewSockMessage(daemonproto.MsgRequest, daemonproto.REQUEST_OK, []byte(fmt.Sprintf("Host: %v added to the inventory", hosts)))
  1188. }
  1189. type SemaphoreRouter struct {
  1190. routes map[daemonproto.Method]func(daemonproto.SockMessage) daemonproto.SockMessage
  1191. }
  1192. func (s *SemaphoreRouter) Register(method daemonproto.Method, callable func(daemonproto.SockMessage) daemonproto.SockMessage) {
  1193. s.routes[method] = callable
  1194. }
  1195. func (s *SemaphoreRouter) Routes() map[daemonproto.Method]func(daemonproto.SockMessage) daemonproto.SockMessage {
  1196. return s.routes
  1197. }
  1198. func NewSemaphoreRouter() *SemaphoreRouter {
  1199. return &SemaphoreRouter{routes: map[daemonproto.Method]func(daemonproto.SockMessage) daemonproto.SockMessage{}}
  1200. }
  1201. /*
  1202. ######################################################
  1203. ############# YAML INVENTORY STRUCTS #################
  1204. ######################################################
  1205. */
  1206. type YamlInventory struct {
  1207. All yamlInvAll `yaml:"all"`
  1208. }
  1209. type yamlInvAll struct {
  1210. Hosts map[string]yamlVars `yaml:"hosts"`
  1211. }
  1212. type yamlVars struct {
  1213. AnsibleSshCommonArgs string `yaml:"ansible_ssh_common_args"`
  1214. MachineType string `yaml:"machine_type"`
  1215. MachineSubType string `yaml:"machine_subtype"`
  1216. VpnNetworkAddress string `yaml:"vpn_network_address"`
  1217. VpnServerPort int `yaml:"vpn_server_port"`
  1218. Clients map[string]yamlVpnClient `yaml:"clients"`
  1219. SecretsProvider string `yaml:"secrets_provider"`
  1220. VpnNetMask int `yaml:"vpn_netmask"`
  1221. Name string `yaml:"name"`
  1222. }
  1223. type yamlVpnClient struct {
  1224. Name string `yaml:"name"`
  1225. Ipv4 string `yaml:"ipv4"`
  1226. Pubkey string `yaml:"pubkey"`
  1227. }
  1228. /*
  1229. YAML inventory builder function
  1230. :param hosts: a list of host IP addresses to add to the VPN server inventory
  1231. */
  1232. func (s SemaphoreConnection) YamlInventoryBuilder(hosts []config.VpnServer) YamlInventory {
  1233. hostmap := map[string]yamlVars{}
  1234. clientmap := map[string]yamlVpnClient{}
  1235. clients := s.Config.VpnClients()
  1236. for i := range clients {
  1237. client := clients[i]
  1238. clientmap[client.Name] = yamlVpnClient{Name: client.Name, Ipv4: client.VpnIpv4.String(), Pubkey: client.Pubkey}
  1239. }
  1240. for i := range hosts {
  1241. server := hosts[i]
  1242. hostmap[hosts[i].WanIpv4] = yamlVars{
  1243. AnsibleSshCommonArgs: "-o StrictHostKeyChecking=no",
  1244. MachineType: "vpn",
  1245. MachineSubType: "server",
  1246. VpnNetworkAddress: server.VpnIpv4.String(),
  1247. VpnServerPort: s.Config.Service.VpnServerPort,
  1248. Clients: clientmap,
  1249. SecretsProvider: s.Config.Service.SecretsBackend,
  1250. VpnNetMask: s.Config.Service.VpnMask,
  1251. Name: server.Name}
  1252. }
  1253. return YamlInventory{
  1254. All: yamlInvAll{
  1255. Hosts: hostmap,
  1256. },
  1257. }
  1258. }
  1259. /*
  1260. ##########################################
  1261. ################ ERRORS ##################
  1262. ##########################################
  1263. */
  1264. type SemaphoreClientError struct {
  1265. Msg string
  1266. }
  1267. // Implementing error interface
  1268. func (s *SemaphoreClientError) Error() string {
  1269. return fmt.Sprintf("There was an error with the call to the semaphore server: '%s'", s.Msg)
  1270. }
  1271. type KeyNotFound struct{ Keyname string }
  1272. func (k *KeyNotFound) Error() string {
  1273. return fmt.Sprintf("Key '%s' was not found in the Semaphore Keystore", k.Keyname)
  1274. }
  1275. type SemaphoreTimeout struct {
  1276. Tries int
  1277. }
  1278. func (s *SemaphoreTimeout) Error() string {
  1279. return "Semaphore job execution poll timed out after: " + fmt.Sprint(s.Tries) + " calls to the server."
  1280. }