4
0

executor_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. package executor
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/OliveTin/OliveTin/internal/auth"
  8. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  9. config "github.com/OliveTin/OliveTin/internal/config"
  10. "github.com/OliveTin/OliveTin/internal/entities"
  11. )
  12. func testingExecutor() (*Executor, *config.Config) {
  13. cfg := config.DefaultConfig()
  14. e := DefaultExecutor(cfg)
  15. a1 := &config.Action{
  16. Title: "Do some tickles",
  17. Shell: "echo 'Tickling {{ person }}'",
  18. Arguments: []config.ActionArgument{
  19. {
  20. Name: "person",
  21. Type: "ascii",
  22. },
  23. },
  24. }
  25. cfg.Actions = append(cfg.Actions, a1)
  26. cfg.Sanitize()
  27. return e, cfg
  28. }
  29. func TestCreateExecutorAndExec(t *testing.T) {
  30. e, cfg := testingExecutor()
  31. req := ExecutionRequest{
  32. AuthenticatedUser: &authpublic.AuthenticatedUser{Username: "MrTickle"},
  33. Cfg: cfg,
  34. Arguments: map[string]string{
  35. "person": "yourself",
  36. },
  37. }
  38. // Ensure bindings are available and set the binding to the only configured action
  39. e.RebuildActionMap()
  40. if len(cfg.Actions) > 0 {
  41. req.Binding = e.FindBindingWithNoEntity(cfg.Actions[0])
  42. }
  43. assert.NotNil(t, e, "Create an executor")
  44. wg, _ := e.ExecRequest(&req)
  45. wg.Wait()
  46. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  47. }
  48. func TestStepRequestActionPopulateLogEntryResolvesEntityTemplates(t *testing.T) {
  49. req := &ExecutionRequest{
  50. logEntry: &InternalLogEntry{},
  51. Binding: &ActionBinding{
  52. Action: &config.Action{
  53. Title: "Do something with {{ project.name }}",
  54. Icon: "{{ project.icon }}",
  55. },
  56. Entity: &entities.Entity{
  57. Data: map[string]any{
  58. "name": "foo",
  59. "icon": "🐰",
  60. },
  61. UniqueKey: "foo-key",
  62. },
  63. },
  64. }
  65. stepRequestActionPopulateLogEntry(req)
  66. assert.Equal(t, "Do something with foo", req.logEntry.ActionTitle)
  67. assert.Equal(t, "🐰", req.logEntry.ActionIcon)
  68. assert.Equal(t, "Do something with {{ project.name }}", req.logEntry.ActionConfigTitle)
  69. assert.Equal(t, "foo-key", req.logEntry.EntityPrefix)
  70. }
  71. func TestExecNonExistant(t *testing.T) {
  72. e, cfg := testingExecutor()
  73. req := ExecutionRequest{
  74. // Binding: e.FindBindingWithNoEntity("waffles"),
  75. logEntry: &InternalLogEntry{},
  76. Cfg: cfg,
  77. }
  78. wg, _ := e.ExecRequest(&req)
  79. wg.Wait()
  80. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  81. assert.Equal(t, "💩", req.logEntry.ActionIcon, "Log entry icon is a poop (not found)")
  82. }
  83. func TestArgumentNameCamelCase(t *testing.T) {
  84. req := newExecRequest()
  85. req.Binding.Action = &config.Action{
  86. Title: "Do some tickles",
  87. Shell: "echo 'Tickling {{ personName }}'",
  88. Arguments: []config.ActionArgument{
  89. {
  90. Name: "personName",
  91. Type: "ascii",
  92. },
  93. },
  94. }
  95. req.Arguments = map[string]string{
  96. "personName": "Fred",
  97. }
  98. out, err := parseActionArguments(req)
  99. assert.Equal(t, "echo 'Tickling Fred'", out)
  100. assert.Nil(t, err)
  101. }
  102. func TestArgumentNameSnakeCase(t *testing.T) {
  103. req := newExecRequest()
  104. req.Binding.Action = &config.Action{
  105. Title: "Do some tickles",
  106. Shell: "echo 'Tickling {{ person_name }}'",
  107. Arguments: []config.ActionArgument{
  108. {
  109. Name: "person_name",
  110. Type: "ascii",
  111. },
  112. },
  113. }
  114. req.Arguments = map[string]string{
  115. "person_name": "Fred",
  116. }
  117. out, err := parseActionArguments(req)
  118. assert.Equal(t, "echo 'Tickling Fred'", out)
  119. assert.Nil(t, err)
  120. }
  121. func TestGetLogsEmpty(t *testing.T) {
  122. e, cfg := testingExecutor()
  123. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  124. logs, paging := e.GetLogTrackingIds(0, 10)
  125. assert.NotNil(t, logs, "Logs should not be nil")
  126. assert.Equal(t, 0, len(logs), "No logs yet")
  127. assert.Equal(t, int64(0), paging.CountRemaining, "There should be no remaining logs")
  128. }
  129. func TestGetLogsLessThanPageSize(t *testing.T) {
  130. e, cfg := testingExecutor()
  131. cfg.Actions = append(cfg.Actions, &config.Action{
  132. Title: "blat",
  133. Shell: "date",
  134. })
  135. cfg.Sanitize()
  136. // Rebuild action map to include newly added action
  137. e.RebuildActionMap()
  138. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  139. logEntries, paging := e.GetLogTrackingIds(0, 10)
  140. assert.Equal(t, 0, len(logEntries), "There should be 0 logs")
  141. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  142. execNewReqAndWait(e, "blat", cfg)
  143. execNewReqAndWait(e, "blat", cfg)
  144. execNewReqAndWait(e, "blat", cfg)
  145. execNewReqAndWait(e, "blat", cfg)
  146. execNewReqAndWait(e, "blat", cfg)
  147. execNewReqAndWait(e, "blat", cfg)
  148. execNewReqAndWait(e, "blat", cfg)
  149. logEntries, paging = e.GetLogTrackingIds(0, 10)
  150. assert.Equal(t, 7, len(logEntries), "There should be 7 logs")
  151. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  152. execNewReqAndWait(e, "blat", cfg)
  153. execNewReqAndWait(e, "blat", cfg)
  154. execNewReqAndWait(e, "blat", cfg)
  155. execNewReqAndWait(e, "blat", cfg)
  156. execNewReqAndWait(e, "blat", cfg)
  157. logEntries, paging = e.GetLogTrackingIds(0, 10)
  158. assert.Equal(t, 10, len(logEntries), "There should be 10 logs")
  159. assert.Equal(t, int64(2), paging.CountRemaining, "There should be 1 remaining logs")
  160. }
  161. func execNewReqAndWait(e *Executor, title string, cfg *config.Config) {
  162. req := &ExecutionRequest{
  163. // ActionTitle: title,
  164. Cfg: cfg,
  165. }
  166. // Ensure we have a binding for the requested title
  167. e.RebuildActionMap()
  168. var action *config.Action
  169. for _, a := range cfg.Actions {
  170. if a.Title == title {
  171. action = a
  172. break
  173. }
  174. }
  175. if action != nil {
  176. req.Binding = e.FindBindingWithNoEntity(action)
  177. }
  178. wg, _ := e.ExecRequest(req)
  179. wg.Wait()
  180. }
  181. func TestGetPagingIndexes(t *testing.T) {
  182. assert.Zero(t, getPagingStartIndex(5, 0), "Testing start index from empty list")
  183. assert.Equal(t, int64(4), getPagingStartIndex(5, 10), "Testing start index from mid point")
  184. assert.Equal(t, int64(9), getPagingStartIndex(-1, 10), "Testing start index with negative offset")
  185. assert.Equal(t, int64(0), getPagingStartIndex(15, 10), "Testing start index with large offset")
  186. assert.Equal(t, int64(9), getPagingStartIndex(0, 10), "Testing start index with zero count")
  187. }
  188. func TestUnsetRequiredArgument(t *testing.T) {
  189. req := newExecRequest()
  190. req.Binding.Action = &config.Action{
  191. Title: "Print your name",
  192. Shell: "echo 'Your name is: {{ name }}'",
  193. Arguments: []config.ActionArgument{
  194. {
  195. Name: "name",
  196. Type: "ascii",
  197. },
  198. },
  199. }
  200. req.Arguments = map[string]string{}
  201. out, err := parseActionArguments(req)
  202. assert.Equal(t, "", out)
  203. assert.NotNil(t, err)
  204. }
  205. func TestUnusedArgumentStillPassesTypeSafetyCheck(t *testing.T) {
  206. req := newExecRequest()
  207. req.Binding.Action = &config.Action{
  208. Title: "Print your name",
  209. Shell: "echo 'Your name is: {{ name }}'",
  210. Arguments: []config.ActionArgument{
  211. {
  212. Name: "name",
  213. Type: "ascii",
  214. },
  215. {
  216. Name: "age",
  217. Type: "int",
  218. },
  219. },
  220. }
  221. req.Arguments = map[string]string{
  222. "name": "Fred",
  223. "age": "Not an integer",
  224. }
  225. out, err := parseActionArguments(req)
  226. assert.Equal(t, "", out)
  227. assert.NotNil(t, err)
  228. }
  229. // https://github.com/OliveTin/OliveTin/issues/564
  230. func TestMangleInvalidArgumentValues(t *testing.T) {
  231. e, cfg := testingExecutor()
  232. a1 := &config.Action{
  233. Title: "Validate my date without seconds because I am from an Android phone",
  234. Shell: "echo 'The date is: {{ date }}'",
  235. Arguments: []config.ActionArgument{
  236. {
  237. Name: "date",
  238. Type: "datetime",
  239. },
  240. },
  241. }
  242. cfg.Actions = append(cfg.Actions, a1)
  243. cfg.Sanitize()
  244. // Build bindings for newly added action
  245. e.RebuildActionMap()
  246. req := ExecutionRequest{
  247. // Action: a1,
  248. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  249. Cfg: cfg,
  250. Arguments: map[string]string{
  251. "date": "1990-01-10T12:00", // Invalid format, should be without seconds
  252. },
  253. }
  254. // Set binding to our appended action
  255. req.Binding = e.FindBindingWithNoEntity(a1)
  256. wg, _ := e.ExecRequest(&req)
  257. wg.Wait()
  258. assert.NotNil(t, req.logEntry, "Log entry should not be nil")
  259. assert.Equal(t, req.logEntry.Output, "The date is: 1990-01-10T12:00:00\n", "Date should be mangled to a valid format")
  260. }
  261. func TestWebhookRejectsShellExecution(t *testing.T) {
  262. cfg := config.DefaultConfig()
  263. e := DefaultExecutor(cfg)
  264. a1 := &config.Action{
  265. Title: "Webhook Shell Reject",
  266. Shell: "echo '{{ msg }}'",
  267. Arguments: []config.ActionArgument{
  268. {Name: "msg", Type: "ascii"},
  269. },
  270. }
  271. cfg.Actions = append(cfg.Actions, a1)
  272. cfg.Sanitize()
  273. e.RebuildActionMap()
  274. req := ExecutionRequest{
  275. Tags: []string{"webhook"},
  276. AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
  277. Cfg: cfg,
  278. Arguments: map[string]string{"msg": "hello"},
  279. Binding: e.FindBindingWithNoEntity(a1),
  280. }
  281. wg, _ := e.ExecRequest(&req)
  282. wg.Wait()
  283. assert.NotNil(t, req.logEntry)
  284. assert.Equal(t, int32(-1337), req.logEntry.ExitCode)
  285. assert.Contains(t, req.logEntry.Output, "webhooks cannot use Shell execution")
  286. }
  287. func TestWebhookAllowsExecExecution(t *testing.T) {
  288. cfg := config.DefaultConfig()
  289. e := DefaultExecutor(cfg)
  290. a1 := &config.Action{
  291. Title: "Webhook Exec OK",
  292. Exec: []string{"echo", "{{ msg }}"},
  293. Arguments: []config.ActionArgument{
  294. {Name: "msg", Type: "ascii"},
  295. },
  296. }
  297. cfg.Actions = append(cfg.Actions, a1)
  298. cfg.Sanitize()
  299. e.RebuildActionMap()
  300. req := ExecutionRequest{
  301. Tags: []string{"webhook"},
  302. AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
  303. Cfg: cfg,
  304. Arguments: map[string]string{"msg": "hello"},
  305. Binding: e.FindBindingWithNoEntity(a1),
  306. }
  307. wg, _ := e.ExecRequest(&req)
  308. wg.Wait()
  309. assert.NotNil(t, req.logEntry)
  310. assert.Equal(t, int32(0), req.logEntry.ExitCode)
  311. assert.Contains(t, req.logEntry.Output, "hello")
  312. }
  313. func TestFilterToDefinedArgumentsOnly(t *testing.T) {
  314. req := newExecRequest()
  315. req.Binding.Action = &config.Action{
  316. Title: "Filter test",
  317. Shell: "echo '{{ name }}'",
  318. Arguments: []config.ActionArgument{
  319. {Name: "name", Type: "ascii"},
  320. },
  321. }
  322. req.Arguments = map[string]string{
  323. "name": "Alice",
  324. "webhook_path": "/malicious/$(id)",
  325. "extra_undefined": "ignored",
  326. }
  327. filterToDefinedArgumentsOnly(req)
  328. assert.Equal(t, "Alice", req.Arguments["name"])
  329. assert.Empty(t, req.Arguments["webhook_path"])
  330. assert.Empty(t, req.Arguments["extra_undefined"])
  331. }
  332. func TestFilterToDefinedArgumentsDropsReservedPrefixArgs(t *testing.T) {
  333. req := newExecRequest()
  334. req.Binding.Action = &config.Action{
  335. Title: "Filter test",
  336. Shell: "echo test",
  337. Arguments: []config.ActionArgument{},
  338. }
  339. req.Arguments = map[string]string{
  340. "ot_executionTrackingId": "track-123",
  341. "ot_username": "webhook",
  342. }
  343. filterToDefinedArgumentsOnly(req)
  344. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  345. assert.Empty(t, req.Arguments["ot_username"])
  346. }
  347. func TestStepParseArgsInjectsSystemArgsAfterFiltering(t *testing.T) {
  348. req := newExecRequest()
  349. req.TrackingID = "server-track-456"
  350. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  351. req.Binding.Action = &config.Action{
  352. Title: "Filter then inject",
  353. Shell: "echo test",
  354. Arguments: []config.ActionArgument{
  355. {Name: "name", Type: "ascii"},
  356. },
  357. }
  358. req.Arguments = map[string]string{
  359. "name": "Alice",
  360. "ot_executionTrackingId": "attacker-track",
  361. "ot_username": "mallory",
  362. "ot_custom": "polluted",
  363. }
  364. assert.True(t, stepParseArgs(req))
  365. assert.Equal(t, "Alice", req.Arguments["name"])
  366. assert.Equal(t, "server-track-456", req.Arguments["ot_executionTrackingId"])
  367. assert.Equal(t, "alice", req.Arguments["ot_username"])
  368. assert.Empty(t, req.Arguments["ot_custom"])
  369. }
  370. func TestStepParseArgsDropsReservedPrefixArgsFromEnvironment(t *testing.T) {
  371. req := newExecRequest()
  372. req.TrackingID = "server-track-456"
  373. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  374. req.Binding.Action = &config.Action{
  375. Title: "No reserved prefix pollution",
  376. Shell: "echo test",
  377. Arguments: []config.ActionArgument{},
  378. }
  379. req.Arguments = map[string]string{
  380. "ot_custom": "polluted",
  381. }
  382. assert.True(t, stepParseArgs(req))
  383. env := buildEnv(req.Arguments)
  384. assert.False(t, containsEnvPrefix(env, "OT_CUSTOM="))
  385. assert.True(t, containsEnvPrefix(env, "OT_USERNAME=alice@example.com"))
  386. assert.True(t, containsEnvPrefix(env, "OT_EXECUTIONTRACKINGID=server-track-456"))
  387. }
  388. func TestSystemArgumentDefinitionsAreReservedAndShellSafe(t *testing.T) {
  389. unsafeTypes := map[string]struct{}{
  390. "email": {},
  391. "password": {},
  392. "raw_string_multiline": {},
  393. "url": {},
  394. "very_dangerous_raw_string": {},
  395. }
  396. seen := map[string]struct{}{}
  397. for _, arg := range systemArgumentDefinitions {
  398. assert.True(t, strings.HasPrefix(arg.Name, config.ReservedArgumentNamePrefix))
  399. assert.NotEmpty(t, arg.Type)
  400. assert.True(t, arg.RejectNull)
  401. _, duplicate := seen[arg.Name]
  402. assert.False(t, duplicate, "duplicate system argument definition %q", arg.Name)
  403. seen[arg.Name] = struct{}{}
  404. _, unsafe := unsafeTypes[arg.Type]
  405. assert.False(t, unsafe, "system argument %q uses unsafe type %q", arg.Name, arg.Type)
  406. }
  407. }
  408. func TestValidatedSystemArgsMatchesSystemArgumentDefinitions(t *testing.T) {
  409. req := newExecRequest()
  410. req.TrackingID = "server-track-456"
  411. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  412. args, err := validatedSystemArgs(req)
  413. assert.Nil(t, err)
  414. assert.Len(t, args, len(systemArgumentDefinitions))
  415. for _, arg := range systemArgumentDefinitions {
  416. assert.Contains(t, args, arg.Name)
  417. }
  418. }
  419. func TestBuildShellAfterArgsOnlyAddsExpectedNonSystemArgs(t *testing.T) {
  420. req := newExecRequest()
  421. req.logEntry = &InternalLogEntry{
  422. Output: "hello",
  423. ExitCode: 7,
  424. }
  425. req.TrackingID = "server-track-456"
  426. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  427. req.Binding.Action = &config.Action{ShellAfterCompleted: "echo test"}
  428. args, err := buildShellAfterArgs(req)
  429. assert.Nil(t, err)
  430. assert.Len(t, args, len(systemArgumentDefinitions)+2)
  431. assert.Contains(t, args, "output")
  432. assert.Contains(t, args, "exitCode")
  433. for _, arg := range systemArgumentDefinitions {
  434. assert.Contains(t, args, arg.Name)
  435. }
  436. }
  437. func TestStepParseArgsAllowsEmailUsernameSystemArg(t *testing.T) {
  438. req := newExecRequest()
  439. req.logEntry = &InternalLogEntry{}
  440. req.TrackingID = "server-track-456"
  441. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  442. req.Binding.Action = &config.Action{
  443. Title: "Email username",
  444. Shell: "echo test",
  445. Arguments: []config.ActionArgument{},
  446. }
  447. assert.True(t, stepParseArgs(req))
  448. assert.Equal(t, "alice@example.com", req.Arguments["ot_username"])
  449. }
  450. func TestStepParseArgsFailsWhenUsernameSystemArgIsInvalid(t *testing.T) {
  451. req := newExecRequest()
  452. req.logEntry = &InternalLogEntry{}
  453. req.TrackingID = "server-track-456"
  454. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  455. req.Binding.Action = &config.Action{
  456. Title: "Invalid system arg",
  457. Shell: "echo test",
  458. Arguments: []config.ActionArgument{},
  459. }
  460. assert.False(t, stepParseArgs(req))
  461. assert.Contains(t, req.logEntry.Output, `system argument "ot_username" failed validation`)
  462. assert.Empty(t, req.Arguments["ot_username"])
  463. }
  464. func TestStepParseArgsFailsWhenTrackingIDSystemArgIsInvalid(t *testing.T) {
  465. req := newExecRequest()
  466. req.logEntry = &InternalLogEntry{}
  467. req.TrackingID = "track/../../bad"
  468. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  469. req.Binding.Action = &config.Action{
  470. Title: "Invalid tracking ID",
  471. Shell: "echo test",
  472. Arguments: []config.ActionArgument{},
  473. }
  474. assert.False(t, stepParseArgs(req))
  475. assert.Contains(t, req.logEntry.Output, `system argument "ot_executionTrackingId" failed validation`)
  476. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  477. }
  478. func TestBuildShellAfterArgsUsesValidatedSystemArgs(t *testing.T) {
  479. req := newExecRequest()
  480. req.logEntry = &InternalLogEntry{
  481. Output: "hello",
  482. ExitCode: 7,
  483. }
  484. req.TrackingID = "server-track-456"
  485. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  486. req.Binding.Action = &config.Action{
  487. Title: "Shell after",
  488. ShellAfterCompleted: "echo test",
  489. }
  490. args, err := buildShellAfterArgs(req)
  491. assert.Nil(t, err)
  492. assert.Equal(t, "alice@example.com", args["ot_username"])
  493. assert.Equal(t, "server-track-456", args["ot_executionTrackingId"])
  494. assert.Equal(t, "hello", args["output"])
  495. assert.Equal(t, "7", args["exitCode"])
  496. }
  497. func TestBuildShellAfterArgsFailsWhenSystemArgIsInvalid(t *testing.T) {
  498. req := newExecRequest()
  499. req.logEntry = &InternalLogEntry{}
  500. req.TrackingID = "server-track-456"
  501. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  502. req.Binding.Action = &config.Action{
  503. Title: "Shell after invalid username",
  504. ShellAfterCompleted: "echo test",
  505. }
  506. args, err := buildShellAfterArgs(req)
  507. assert.Nil(t, args)
  508. assert.NotNil(t, err)
  509. assert.Contains(t, err.Error(), `system argument "ot_username" failed validation`)
  510. }
  511. func containsEnvPrefix(env []string, prefix string) bool {
  512. for _, item := range env {
  513. if strings.HasPrefix(item, prefix) {
  514. return true
  515. }
  516. }
  517. return false
  518. }
  519. func TestTriggerExecutesTriggeredAction(t *testing.T) {
  520. cfg := config.DefaultConfig()
  521. e := DefaultExecutor(cfg)
  522. helloAction := &config.Action{
  523. Title: "Hello world",
  524. Shell: "echo 'Hello World!'",
  525. }
  526. triggerAction := &config.Action{
  527. Title: "Simple action that triggers another action",
  528. Shell: "echo 'Hi'",
  529. Triggers: []string{"Hello world"},
  530. }
  531. cfg.Actions = append(cfg.Actions, helloAction, triggerAction)
  532. cfg.Sanitize()
  533. e.RebuildActionMap()
  534. finishedTitles := make(chan string, 4)
  535. collector := &executionFinishedCollector{ch: finishedTitles}
  536. e.AddListener(collector)
  537. req := &ExecutionRequest{
  538. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  539. Cfg: cfg,
  540. Binding: e.FindBindingWithNoEntity(triggerAction),
  541. }
  542. wg, _ := e.ExecRequest(req)
  543. wg.Wait()
  544. var got []string
  545. for i := 0; i < 2; i++ {
  546. select {
  547. case title := <-finishedTitles:
  548. got = append(got, title)
  549. case <-time.After(2 * time.Second):
  550. t.Fatalf("timed out waiting for execution %d; got %v", i+1, got)
  551. }
  552. }
  553. assert.Contains(t, got, "Hello world", "triggered action must run")
  554. assert.Contains(t, got, "Simple action that triggers another action", "triggering action must run")
  555. }
  556. func TestTriggerUnknownActionTitleSkipsWithoutPanic(t *testing.T) {
  557. cfg := config.DefaultConfig()
  558. e := DefaultExecutor(cfg)
  559. triggerAction := &config.Action{
  560. Title: "Action with bad trigger",
  561. Shell: "echo 'ok'",
  562. Triggers: []string{"Nonexistent action"},
  563. }
  564. cfg.Actions = append(cfg.Actions, triggerAction)
  565. cfg.Sanitize()
  566. e.RebuildActionMap()
  567. finishedTitles := make(chan string, 4)
  568. collector := &executionFinishedCollector{ch: finishedTitles}
  569. e.AddListener(collector)
  570. req := &ExecutionRequest{
  571. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  572. Cfg: cfg,
  573. Binding: e.FindBindingWithNoEntity(triggerAction),
  574. }
  575. wg, _ := e.ExecRequest(req)
  576. wg.Wait()
  577. var got []string
  578. select {
  579. case title := <-finishedTitles:
  580. got = append(got, title)
  581. case <-time.After(500 * time.Millisecond):
  582. }
  583. assert.Len(t, got, 1, "only the triggering action runs; unknown trigger is skipped")
  584. if len(got) > 0 {
  585. assert.Equal(t, "Action with bad trigger", got[0])
  586. }
  587. }
  588. type executionFinishedCollector struct {
  589. ch chan string
  590. }
  591. func (c *executionFinishedCollector) OnExecutionStarted(_ *InternalLogEntry) {}
  592. func (c *executionFinishedCollector) OnExecutionFinished(entry *InternalLogEntry) {
  593. c.ch <- entry.ActionTitle
  594. }
  595. func (c *executionFinishedCollector) OnOutputChunk(_ []byte, _ string) {}
  596. func (c *executionFinishedCollector) OnActionMapRebuilt() {}