executor.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. package executor
  2. import (
  3. acl "github.com/OliveTin/OliveTin/internal/acl"
  4. "github.com/OliveTin/OliveTin/internal/auth"
  5. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/OliveTin/OliveTin/internal/entities"
  8. "github.com/google/uuid"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/prometheus/client_golang/prometheus"
  11. "github.com/prometheus/client_golang/prometheus/promauto"
  12. "gopkg.in/yaml.v3"
  13. "bytes"
  14. "context"
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "path"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. const (
  24. DefaultExitCodeNotExecuted = -1337
  25. MaxTriggerDepth = 10
  26. )
  27. var (
  28. metricActionsRequested = promauto.NewCounter(prometheus.CounterOpts{
  29. Name: "olivetin_actions_requested_count",
  30. Help: "The actions requested count",
  31. })
  32. )
  33. type ActionBinding struct {
  34. ID string
  35. Action *config.Action
  36. Entity *entities.Entity
  37. ConfigOrder int
  38. IsOnDashboard bool
  39. }
  40. // Executor represents a helper class for executing commands. It's main method
  41. // is ExecRequest
  42. type Executor struct {
  43. logs map[string]*InternalLogEntry
  44. logsTrackingIdsByDate []string
  45. LogsByBindingId map[string][]*InternalLogEntry
  46. logmutex sync.RWMutex
  47. MapActionBindings map[string]*ActionBinding
  48. MapActionBindingsLock sync.RWMutex
  49. Cfg *config.Config
  50. listeners []listener
  51. chainOfCommand []executorStepFunc
  52. }
  53. // ExecutionRequest is a request to execute an action. It's passed to an
  54. // Executor. They're created from the api.
  55. type ExecutionRequest struct {
  56. Binding *ActionBinding
  57. Arguments map[string]string
  58. TrackingID string
  59. Tags []string
  60. Cfg *config.Config
  61. AuthenticatedUser *authpublic.AuthenticatedUser
  62. TriggerDepth int
  63. logEntry *InternalLogEntry
  64. finalParsedCommand string
  65. execArgs []string
  66. useDirectExec bool
  67. executor *Executor
  68. }
  69. // InternalLogEntry objects are created by an Executor, and represent the final
  70. // state of execution (even if the command is not executed). It's designed to be
  71. // easily serializable.
  72. type InternalLogEntry struct {
  73. Binding *ActionBinding
  74. DatetimeStarted time.Time
  75. DatetimeFinished time.Time
  76. Output string
  77. TimedOut bool
  78. Blocked bool
  79. ExitCode int32
  80. Tags []string
  81. ExecutionStarted bool
  82. ExecutionFinished bool
  83. ExecutionTrackingID string
  84. Process *os.Process
  85. Username string
  86. Index int64
  87. EntityPrefix string
  88. ActionConfigTitle string // This is the title of the action as defined in the config, not the final parsed title.
  89. /*
  90. The following 3 properties are obviously on Action normally, but it's useful
  91. that logs are lightweight (so we don't need to have an action associated to
  92. logs, etc. Therefore, we duplicate those values here.
  93. */
  94. ActionTitle string
  95. ActionIcon string
  96. }
  97. type executorStepFunc func(*ExecutionRequest) bool
  98. // DefaultExecutor returns an Executor, with a sensible "chain of command" for
  99. // executing actions.
  100. func DefaultExecutor(cfg *config.Config) *Executor {
  101. e := Executor{}
  102. e.Cfg = cfg
  103. e.logs = make(map[string]*InternalLogEntry)
  104. e.logsTrackingIdsByDate = make([]string, 0)
  105. e.LogsByBindingId = make(map[string][]*InternalLogEntry)
  106. e.MapActionBindings = make(map[string]*ActionBinding)
  107. e.chainOfCommand = []executorStepFunc{
  108. stepRequestAction,
  109. stepConcurrencyCheck,
  110. stepRateCheck,
  111. stepACLCheck,
  112. stepParseArgs,
  113. stepLogStart,
  114. stepExec,
  115. stepExecAfter,
  116. stepLogFinish,
  117. stepSaveLog,
  118. stepTrigger,
  119. }
  120. return &e
  121. }
  122. type listener interface {
  123. OnExecutionStarted(logEntry *InternalLogEntry)
  124. OnExecutionFinished(logEntry *InternalLogEntry)
  125. OnOutputChunk(o []byte, executionTrackingId string)
  126. OnActionMapRebuilt()
  127. }
  128. func (e *Executor) AddListener(m listener) {
  129. e.listeners = append(e.listeners, m)
  130. }
  131. // getPagingStartIndex calculates the starting index for log pagination.
  132. // Parameters:
  133. //
  134. // startOffset: The offset from the most recent log (0 means start from the most recent)
  135. // totalLogCount: Total number of logs available
  136. // count: Number of logs to retrieve
  137. //
  138. // Returns: The calculated starting index for pagination
  139. func getPagingStartIndex(startOffset int64, totalLogCount int64) int64 {
  140. var startIndex int64
  141. if startOffset <= 0 {
  142. startIndex = totalLogCount
  143. } else {
  144. startIndex = (totalLogCount - startOffset)
  145. if startIndex < 0 {
  146. startIndex = 1
  147. }
  148. }
  149. return startIndex - 1
  150. }
  151. type PagingResult struct {
  152. CountRemaining int64
  153. PageSize int64
  154. TotalCount int64
  155. StartOffset int64
  156. }
  157. func (e *Executor) GetLogTrackingIds(startOffset int64, pageCount int64) ([]*InternalLogEntry, *PagingResult) {
  158. pagingResult := &PagingResult{
  159. CountRemaining: 0,
  160. PageSize: pageCount,
  161. TotalCount: 0,
  162. StartOffset: startOffset,
  163. }
  164. e.logmutex.RLock()
  165. totalLogCount := int64(len(e.logsTrackingIdsByDate))
  166. pagingResult.TotalCount = totalLogCount
  167. startIndex := getPagingStartIndex(startOffset, totalLogCount)
  168. pageCount = min(totalLogCount, pageCount)
  169. endIndex := max(0, (startIndex-pageCount)+1)
  170. log.WithFields(log.Fields{
  171. "startOffset": startOffset,
  172. "pageCount": pageCount,
  173. "total": totalLogCount,
  174. "startIndex": startIndex,
  175. "endIndex": endIndex,
  176. }).Tracef("GetLogTrackingIds")
  177. trackingIds := make([]*InternalLogEntry, 0, pageCount)
  178. if totalLogCount > 0 {
  179. for i := endIndex; i <= startIndex; i++ {
  180. trackingIds = append(trackingIds, e.logs[e.logsTrackingIdsByDate[i]])
  181. }
  182. }
  183. e.logmutex.RUnlock()
  184. pagingResult.CountRemaining = endIndex
  185. return trackingIds, pagingResult
  186. }
  187. // isValidLogEntryForACL checks if a log entry has all required fields for ACL checking.
  188. func isValidLogEntryForACL(entry *InternalLogEntry) bool {
  189. return entry != nil && entry.Binding != nil && entry.Binding.Action != nil
  190. }
  191. // isLogEntryAllowedByACL checks if a log entry is allowed to be viewed by the user.
  192. func isLogEntryAllowedByACL(cfg *config.Config, user *authpublic.AuthenticatedUser, entry *InternalLogEntry) bool {
  193. return acl.IsAllowedLogs(cfg, user, entry.Binding.Action)
  194. }
  195. func (e *Executor) filterLogsByACL(cfg *config.Config, user *authpublic.AuthenticatedUser, dateFilter string) []*InternalLogEntry {
  196. e.logmutex.RLock()
  197. defer e.logmutex.RUnlock()
  198. filtered := make([]*InternalLogEntry, 0, len(e.logsTrackingIdsByDate))
  199. var filterDate time.Time
  200. var hasDateFilter bool
  201. if dateFilter != "" {
  202. parsedDate, err := time.Parse("2006-01-02", dateFilter)
  203. if err != nil {
  204. log.WithFields(log.Fields{
  205. "dateFilter": dateFilter,
  206. "error": err,
  207. }).Errorf("Failed to parse date filter, expected format YYYY-MM-DD")
  208. } else {
  209. filterDate = parsedDate
  210. hasDateFilter = true
  211. }
  212. }
  213. for _, trackingId := range e.logsTrackingIdsByDate {
  214. entry := e.logs[trackingId]
  215. if !isValidLogEntryForACL(entry) {
  216. continue
  217. }
  218. if isLogEntryAllowedByACL(cfg, user, entry) {
  219. if hasDateFilter {
  220. entryDate := entry.DatetimeStarted.UTC().Truncate(24 * time.Hour)
  221. filterDateUTC := filterDate.UTC().Truncate(24 * time.Hour)
  222. if !entryDate.Equal(filterDateUTC) {
  223. continue
  224. }
  225. }
  226. filtered = append(filtered, entry)
  227. }
  228. }
  229. return filtered
  230. }
  231. // paginateFilteredLogs applies pagination to a filtered list of logs and returns
  232. // the paginated results along with pagination metadata.
  233. func paginateFilteredLogs(filtered []*InternalLogEntry, startOffset int64, pageCount int64) ([]*InternalLogEntry, *PagingResult) {
  234. total := int64(len(filtered))
  235. paging := &PagingResult{PageSize: pageCount, TotalCount: total, StartOffset: startOffset}
  236. if total == 0 {
  237. paging.CountRemaining = 0
  238. return []*InternalLogEntry{}, paging
  239. }
  240. startIndex := getPagingStartIndex(startOffset, total)
  241. pageCount = min(total, pageCount)
  242. endIndex := max(0, (startIndex-pageCount)+1)
  243. out := make([]*InternalLogEntry, 0, pageCount)
  244. for i := endIndex; i <= startIndex && i < int64(len(filtered)); i++ {
  245. out = append(out, filtered[i])
  246. }
  247. paging.CountRemaining = endIndex
  248. return out, paging
  249. }
  250. // GetLogTrackingIdsACL returns logs filtered by ACL visibility for the user and
  251. // paginated correctly based on the filtered set.
  252. // dateFilter is optional and should be in YYYY-MM-DD format. If empty, no date filtering is applied.
  253. func (e *Executor) GetLogTrackingIdsACL(cfg *config.Config, user *authpublic.AuthenticatedUser, startOffset int64, pageCount int64, dateFilter string) ([]*InternalLogEntry, *PagingResult) {
  254. filtered := e.filterLogsByACL(cfg, user, dateFilter)
  255. return paginateFilteredLogs(filtered, startOffset, pageCount)
  256. }
  257. func (e *Executor) GetLog(trackingID string) (*InternalLogEntry, bool) {
  258. e.logmutex.RLock()
  259. entry, found := e.logs[trackingID]
  260. e.logmutex.RUnlock()
  261. return entry, found
  262. }
  263. func (e *Executor) GetLogsByBindingId(bindingId string) []*InternalLogEntry {
  264. e.logmutex.RLock()
  265. logs, found := e.LogsByBindingId[bindingId]
  266. e.logmutex.RUnlock()
  267. if !found {
  268. return make([]*InternalLogEntry, 0)
  269. }
  270. return logs
  271. }
  272. // shouldCountExecution checks if a log entry should be counted for rate limiting.
  273. func shouldCountExecution(logEntry *InternalLogEntry, windowStart time.Time) bool {
  274. return !logEntry.Blocked && logEntry.DatetimeStarted.After(windowStart)
  275. }
  276. // updateOldestExecution updates the oldest execution time if this entry is older.
  277. func updateOldestExecution(oldestExecutionTime **time.Time, logEntry *InternalLogEntry) {
  278. if *oldestExecutionTime == nil {
  279. *oldestExecutionTime = &logEntry.DatetimeStarted
  280. } else if logEntry.DatetimeStarted.Before(**oldestExecutionTime) {
  281. *oldestExecutionTime = &logEntry.DatetimeStarted
  282. }
  283. }
  284. // findOldestExecutionInWindow finds the oldest execution within the time window and counts executions.
  285. // Returns the count of executions and the oldest execution time, or nil if none found.
  286. func findOldestExecutionInWindow(logs []*InternalLogEntry, windowStart time.Time) (int, *time.Time) {
  287. executions := 0
  288. var oldestExecutionTime *time.Time
  289. for _, logEntry := range logs {
  290. if !shouldCountExecution(logEntry, windowStart) {
  291. continue
  292. }
  293. executions++
  294. updateOldestExecution(&oldestExecutionTime, logEntry)
  295. }
  296. return executions, oldestExecutionTime
  297. }
  298. // calculateExpiryTime calculates when the oldest execution will fall outside the rate limit window.
  299. func calculateExpiryTime(oldestExecutionTime time.Time, duration time.Duration, now time.Time) time.Time {
  300. expiryTime := oldestExecutionTime.Add(duration)
  301. if !expiryTime.After(now) {
  302. return time.Time{}
  303. }
  304. return expiryTime
  305. }
  306. // updateMaxExpiryTime updates maxExpiryTime if expiryTime is later.
  307. func updateMaxExpiryTime(maxExpiryTime *time.Time, expiryTime time.Time) {
  308. if expiryTime.IsZero() {
  309. return
  310. }
  311. if maxExpiryTime.IsZero() || expiryTime.After(*maxExpiryTime) {
  312. *maxExpiryTime = expiryTime
  313. }
  314. }
  315. // calculateExpiryForRate calculates the expiry time for a single rate limit rule.
  316. // Returns the expiry time if the rate limit is exceeded, or zero time if not.
  317. func calculateExpiryForRate(rate config.RateSpec, logs []*InternalLogEntry, now time.Time) time.Time {
  318. duration := parseDuration(rate)
  319. if duration <= 0 {
  320. return time.Time{}
  321. }
  322. windowStart := now.Add(-duration)
  323. executions, oldestExecutionTime := findOldestExecutionInWindow(logs, windowStart)
  324. if executions < rate.Limit || oldestExecutionTime == nil {
  325. return time.Time{}
  326. }
  327. return calculateExpiryTime(*oldestExecutionTime, duration, now)
  328. }
  329. // getLogsForBinding retrieves logs for a binding ID.
  330. func (e *Executor) getLogsForBinding(bindingId string) []*InternalLogEntry {
  331. e.logmutex.RLock()
  332. logs, found := e.LogsByBindingId[bindingId]
  333. e.logmutex.RUnlock()
  334. if !found || len(logs) == 0 {
  335. return nil
  336. }
  337. return logs
  338. }
  339. // calculateMaxExpiryTimeFromRates calculates the maximum expiry time across all rate limit rules.
  340. func calculateMaxExpiryTimeFromRates(rates []config.RateSpec, logs []*InternalLogEntry, now time.Time) time.Time {
  341. var maxExpiryTime time.Time
  342. for _, rate := range rates {
  343. expiryTime := calculateExpiryForRate(rate, logs, now)
  344. updateMaxExpiryTime(&maxExpiryTime, expiryTime)
  345. }
  346. return maxExpiryTime
  347. }
  348. // GetTimeUntilAvailable calculates when an action will be available again based on rate limits.
  349. // Returns the Unix timestamp in seconds when the rate limit expires, or 0 if the action is available now.
  350. func (e *Executor) GetTimeUntilAvailable(binding *ActionBinding) int64 {
  351. if len(binding.Action.MaxRate) == 0 {
  352. return 0
  353. }
  354. logs := e.getLogsForBinding(binding.ID)
  355. if logs == nil {
  356. return 0
  357. }
  358. maxExpiryTime := calculateMaxExpiryTimeFromRates(binding.Action.MaxRate, logs, time.Now())
  359. if maxExpiryTime.IsZero() {
  360. return 0
  361. }
  362. return maxExpiryTime.Unix()
  363. }
  364. func (e *Executor) SetLog(trackingID string, entry *InternalLogEntry) {
  365. e.logmutex.Lock()
  366. entry.Index = int64(len(e.logsTrackingIdsByDate))
  367. e.logs[trackingID] = entry
  368. e.logsTrackingIdsByDate = append(e.logsTrackingIdsByDate, trackingID)
  369. e.logmutex.Unlock()
  370. }
  371. // ExecRequest processes an ExecutionRequest
  372. func (e *Executor) ExecRequest(req *ExecutionRequest) (*sync.WaitGroup, string) {
  373. if req.AuthenticatedUser == nil {
  374. req.AuthenticatedUser = auth.UserGuest(req.Cfg)
  375. }
  376. req.executor = e
  377. req.logEntry = &InternalLogEntry{
  378. Binding: req.Binding,
  379. DatetimeStarted: time.Now(),
  380. ExecutionTrackingID: req.TrackingID,
  381. Output: "",
  382. ExitCode: DefaultExitCodeNotExecuted,
  383. ExecutionStarted: false,
  384. ExecutionFinished: false,
  385. ActionTitle: "notfound",
  386. ActionIcon: "&#x1f4a9;",
  387. Username: req.AuthenticatedUser.Username,
  388. }
  389. _, isDuplicate := e.GetLog(req.TrackingID)
  390. if isDuplicate || req.TrackingID == "" {
  391. req.TrackingID = uuid.NewString()
  392. }
  393. // Update the log entry with the final tracking ID
  394. req.logEntry.ExecutionTrackingID = req.TrackingID
  395. log.Tracef("executor.ExecRequest(): %v", req)
  396. e.SetLog(req.TrackingID, req.logEntry)
  397. wg := new(sync.WaitGroup)
  398. wg.Add(1)
  399. go func() {
  400. e.execChain(req)
  401. defer wg.Done()
  402. }()
  403. return wg, req.TrackingID
  404. }
  405. func (e *Executor) execChain(req *ExecutionRequest) {
  406. for _, step := range e.chainOfCommand {
  407. if !step(req) {
  408. break
  409. }
  410. }
  411. // Ensure DatetimeFinished is set even if execution was blocked early
  412. if req.logEntry.DatetimeFinished.IsZero() {
  413. req.logEntry.DatetimeFinished = time.Now()
  414. }
  415. req.logEntry.ExecutionFinished = true
  416. // This isn't a step, because we want to notify all listeners, irrespective
  417. // of how many steps were actually executed.
  418. notifyListenersFinished(req)
  419. }
  420. func getConcurrentCount(req *ExecutionRequest) int {
  421. concurrentCount := 0
  422. req.executor.logmutex.RLock()
  423. for _, log := range req.executor.GetLogsByBindingId(req.Binding.ID) {
  424. if !log.ExecutionFinished {
  425. concurrentCount += 1
  426. }
  427. }
  428. req.executor.logmutex.RUnlock()
  429. return concurrentCount
  430. }
  431. func stepConcurrencyCheck(req *ExecutionRequest) bool {
  432. concurrentCount := getConcurrentCount(req)
  433. // Note that the current execution is counted int the logs, so when checking we +1
  434. if concurrentCount >= (req.Binding.Action.MaxConcurrent + 1) {
  435. log.WithFields(log.Fields{
  436. "actionTitle": req.logEntry.ActionTitle,
  437. "concurrentCount": concurrentCount,
  438. "maxConcurrent": req.Binding.Action.MaxConcurrent,
  439. }).Warnf("Blocked from executing due to concurrency limit")
  440. req.logEntry.Output = "Blocked from executing due to concurrency limit"
  441. req.logEntry.Blocked = true
  442. return false
  443. }
  444. return true
  445. }
  446. func parseDuration(rate config.RateSpec) time.Duration {
  447. duration, err := time.ParseDuration(rate.Duration)
  448. if err != nil {
  449. log.Warnf("Could not parse duration: %v", rate.Duration)
  450. return -1 * time.Minute
  451. }
  452. return duration
  453. }
  454. //gocyclo:ignore
  455. func getExecutionsCount(rate config.RateSpec, req *ExecutionRequest) int {
  456. executions := -1 // Because we will find ourself when checking execution logs
  457. duration := parseDuration(rate)
  458. then := time.Now().Add(-duration)
  459. for _, logEntry := range req.executor.GetLogsByBindingId(req.Binding.ID) {
  460. // FIXME
  461. /*
  462. if logEntry.EntityPrefix != req.EntityPrefix {
  463. continue
  464. }
  465. */
  466. if logEntry.DatetimeStarted.After(then) && !logEntry.Blocked {
  467. executions += 1
  468. }
  469. }
  470. return executions
  471. }
  472. func stepRateCheck(req *ExecutionRequest) bool {
  473. for _, rate := range req.Binding.Action.MaxRate {
  474. executions := getExecutionsCount(rate, req)
  475. if executions >= rate.Limit {
  476. log.WithFields(log.Fields{
  477. "actionTitle": req.logEntry.ActionTitle,
  478. "executions": executions,
  479. "limit": rate.Limit,
  480. "duration": rate.Duration,
  481. }).Infof("Blocked from executing due to rate limit")
  482. req.logEntry.Output = "Blocked from executing due to rate limit"
  483. req.logEntry.Blocked = true
  484. return false
  485. }
  486. }
  487. return true
  488. }
  489. func stepACLCheck(req *ExecutionRequest) bool {
  490. canExec := acl.IsAllowedExec(req.Cfg, req.AuthenticatedUser, req.Binding.Action)
  491. if !canExec {
  492. req.logEntry.Output = "ACL check failed. Blocked from executing."
  493. req.logEntry.Blocked = true
  494. log.WithFields(log.Fields{
  495. "actionTitle": req.logEntry.ActionTitle,
  496. }).Warnf("ACL check failed. Blocked from executing.")
  497. }
  498. return canExec
  499. }
  500. func stepParseArgs(req *ExecutionRequest) bool {
  501. ensureArgumentMap(req)
  502. injectSystemArgs(req)
  503. if !hasBindingAndAction(req) {
  504. return fail(req, fmt.Errorf("cannot parse arguments: Binding or Action is nil"))
  505. }
  506. mangleInvalidArgumentValues(req)
  507. if hasExec(req) {
  508. return handleExecBranch(req)
  509. } else {
  510. return handleShellBranch(req)
  511. }
  512. }
  513. func handleExecBranch(req *ExecutionRequest) bool {
  514. args, err := parseActionExec(req.Arguments, req.Binding.Action, req.Binding.Entity)
  515. if err != nil {
  516. return fail(req, err)
  517. }
  518. req.useDirectExec = true
  519. req.execArgs = args
  520. return true
  521. }
  522. func handleShellBranch(req *ExecutionRequest) bool {
  523. if err := checkShellArgumentSafety(req.Binding.Action); err != nil {
  524. return fail(req, err)
  525. }
  526. cmd, err := parseActionArguments(req.Arguments, req.Binding.Action, req.Binding.Entity)
  527. if err != nil {
  528. return fail(req, err)
  529. }
  530. req.useDirectExec = false
  531. req.finalParsedCommand = cmd
  532. return true
  533. }
  534. func ensureArgumentMap(req *ExecutionRequest) {
  535. if req.Arguments == nil {
  536. req.Arguments = make(map[string]string)
  537. }
  538. }
  539. func injectSystemArgs(req *ExecutionRequest) {
  540. req.Arguments["ot_executionTrackingId"] = req.TrackingID
  541. req.Arguments["ot_username"] = req.AuthenticatedUser.Username
  542. }
  543. func hasBindingAndAction(req *ExecutionRequest) bool {
  544. return !(req.Binding == nil || req.Binding.Action == nil)
  545. }
  546. func hasExec(req *ExecutionRequest) bool {
  547. return len(req.Binding.Action.Exec) > 0
  548. }
  549. func fail(req *ExecutionRequest, err error) bool {
  550. req.logEntry.Output = err.Error()
  551. log.Warn(err.Error())
  552. return false
  553. }
  554. func stepRequestAction(req *ExecutionRequest) bool {
  555. metricActionsRequested.Inc()
  556. // If there is no binding or action, do not proceed. Leave default
  557. // log entry values (icon/title/id) and stop execution gracefully.
  558. if req.Binding == nil || req.Binding.Action == nil {
  559. log.Warnf("Action request has no binding/action; skipping execution")
  560. return false
  561. }
  562. req.logEntry.Binding = req.Binding
  563. req.logEntry.ActionConfigTitle = req.Binding.Action.Title
  564. req.logEntry.ActionTitle = entities.ParseTemplateWith(req.Binding.Action.Title, req.Binding.Entity)
  565. req.logEntry.ActionIcon = req.Binding.Action.Icon
  566. req.logEntry.Tags = req.Tags
  567. req.executor.logmutex.Lock()
  568. if _, containsKey := req.executor.LogsByBindingId[req.Binding.ID]; !containsKey {
  569. req.executor.LogsByBindingId[req.Binding.ID] = make([]*InternalLogEntry, 0)
  570. }
  571. req.executor.LogsByBindingId[req.Binding.ID] = append(req.executor.LogsByBindingId[req.Binding.ID], req.logEntry)
  572. req.executor.logmutex.Unlock()
  573. log.WithFields(log.Fields{
  574. "actionTitle": req.logEntry.ActionTitle,
  575. "tags": req.Tags,
  576. }).Infof("Action requested")
  577. notifyListenersStarted(req)
  578. return true
  579. }
  580. func stepLogStart(req *ExecutionRequest) bool {
  581. log.WithFields(log.Fields{
  582. "actionTitle": req.logEntry.ActionTitle,
  583. "timeout": req.Binding.Action.Timeout,
  584. }).Infof("Action started")
  585. return true
  586. }
  587. func stepLogFinish(req *ExecutionRequest) bool {
  588. req.logEntry.ExecutionFinished = true
  589. log.WithFields(log.Fields{
  590. "actionTitle": req.logEntry.ActionTitle,
  591. "outputLength": len(req.logEntry.Output),
  592. "timedOut": req.logEntry.TimedOut,
  593. "exit": req.logEntry.ExitCode,
  594. }).Infof("Action finished")
  595. return true
  596. }
  597. func notifyListenersFinished(req *ExecutionRequest) {
  598. for _, listener := range req.executor.listeners {
  599. listener.OnExecutionFinished(req.logEntry)
  600. }
  601. }
  602. func notifyListenersStarted(req *ExecutionRequest) {
  603. for _, listener := range req.executor.listeners {
  604. listener.OnExecutionStarted(req.logEntry)
  605. }
  606. }
  607. func appendErrorToStderr(err error, logEntry *InternalLogEntry) {
  608. if err != nil {
  609. logEntry.Output = err.Error() + "\n\n" + logEntry.Output
  610. }
  611. }
  612. type OutputStreamer struct {
  613. Req *ExecutionRequest
  614. output bytes.Buffer
  615. }
  616. func (ost *OutputStreamer) Write(o []byte) (n int, err error) {
  617. for _, listener := range ost.Req.executor.listeners {
  618. listener.OnOutputChunk(o, ost.Req.TrackingID)
  619. }
  620. return ost.output.Write(o)
  621. }
  622. func (ost *OutputStreamer) String() string {
  623. return ost.output.String()
  624. }
  625. func buildEnv(args map[string]string) []string {
  626. ret := append(os.Environ(), "OLIVETIN=1")
  627. for k, v := range args {
  628. varName := fmt.Sprintf("%v", strings.TrimSpace(strings.ToUpper(k)))
  629. // Skip arguments that might not have a name (eg, confirmation), as this causes weird bugs on Windows.
  630. if varName == "" {
  631. continue
  632. }
  633. ret = append(ret, fmt.Sprintf("%v=%v", varName, v))
  634. }
  635. return ret
  636. }
  637. func stepExec(req *ExecutionRequest) bool {
  638. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(req.Binding.Action.Timeout)*time.Second)
  639. defer cancel()
  640. streamer := &OutputStreamer{Req: req}
  641. cmd := buildCommand(ctx, req)
  642. if cmd == nil {
  643. req.logEntry.Output = "Cannot execute: no command arguments provided"
  644. log.Warn("Cannot execute: no command arguments provided")
  645. return false
  646. }
  647. prepareCommand(cmd, streamer, req)
  648. runerr := cmd.Start()
  649. req.logEntry.Process = cmd.Process
  650. waiterr := cmd.Wait()
  651. req.logEntry.ExitCode = int32(cmd.ProcessState.ExitCode())
  652. req.logEntry.Output = streamer.String()
  653. appendErrorToStderr(runerr, req.logEntry)
  654. appendErrorToStderr(waiterr, req.logEntry)
  655. if ctx.Err() == context.DeadlineExceeded {
  656. log.WithFields(log.Fields{
  657. "actionTitle": req.logEntry.ActionTitle,
  658. }).Warnf("Action timed out")
  659. // The context timeout should kill the process, but let's make sure.
  660. err := req.executor.Kill(req.logEntry)
  661. if err != nil {
  662. log.WithFields(log.Fields{
  663. "actionTitle": req.logEntry.ActionTitle,
  664. }).Warnf("could not kill process: %v", err)
  665. }
  666. req.logEntry.TimedOut = true
  667. req.logEntry.Output += "OliveTin::timeout - this action timed out after " + fmt.Sprintf("%v", req.Binding.Action.Timeout) + " seconds. If you need more time for this action, set a longer timeout. See https://docs.olivetin.app/action_customization/timeouts.html for more help."
  668. }
  669. req.logEntry.DatetimeFinished = time.Now()
  670. return true
  671. }
  672. func buildCommand(ctx context.Context, req *ExecutionRequest) *exec.Cmd {
  673. if req.useDirectExec {
  674. return wrapCommandDirect(ctx, req.execArgs)
  675. }
  676. return wrapCommandInShell(ctx, req.finalParsedCommand)
  677. }
  678. func prepareCommand(cmd *exec.Cmd, streamer *OutputStreamer, req *ExecutionRequest) {
  679. cmd.Stdout = streamer
  680. cmd.Stderr = streamer
  681. cmd.Env = buildEnv(req.Arguments)
  682. req.logEntry.ExecutionStarted = true
  683. }
  684. func stepExecAfter(req *ExecutionRequest) bool {
  685. if req.Binding.Action.ShellAfterCompleted == "" {
  686. return true
  687. }
  688. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(req.Binding.Action.Timeout)*time.Second)
  689. defer cancel()
  690. var stdout bytes.Buffer
  691. var stderr bytes.Buffer
  692. args := map[string]string{
  693. "output": req.logEntry.Output,
  694. "exitCode": fmt.Sprintf("%v", req.logEntry.ExitCode),
  695. "ot_executionTrackingId": req.TrackingID,
  696. "ot_username": req.AuthenticatedUser.Username,
  697. }
  698. finalParsedCommand, err := parseCommandForReplacements(req.Binding.Action.ShellAfterCompleted, args, req.Binding.Entity)
  699. if err != nil {
  700. msg := "Could not prepare shellAfterCompleted command: " + err.Error() + "\n"
  701. req.logEntry.Output += msg
  702. log.Warn(msg)
  703. return true
  704. }
  705. cmd := wrapCommandInShell(ctx, finalParsedCommand)
  706. cmd.Stdout = &stdout
  707. cmd.Stderr = &stderr
  708. cmd.Env = buildEnv(args)
  709. runerr := cmd.Start()
  710. waiterr := cmd.Wait()
  711. req.logEntry.Output += "\n"
  712. req.logEntry.Output += "OliveTin::shellAfterCompleted stdout\n"
  713. req.logEntry.Output += stdout.String()
  714. req.logEntry.Output += "OliveTin::shellAfterCompleted stderr\n"
  715. req.logEntry.Output += stderr.String()
  716. req.logEntry.Output += "OliveTin::shellAfterCompleted errors and summary\n"
  717. appendErrorToStderr(runerr, req.logEntry)
  718. appendErrorToStderr(waiterr, req.logEntry)
  719. if ctx.Err() == context.DeadlineExceeded {
  720. req.logEntry.Output += "Your shellAfterCompleted command timed out."
  721. }
  722. req.logEntry.Output += fmt.Sprintf("Your shellAfterCompleted exited with code %v\n", cmd.ProcessState.ExitCode())
  723. req.logEntry.Output += "OliveTin::shellAfterCompleted output complete\n"
  724. return true
  725. }
  726. //gocyclo:ignore
  727. func stepTrigger(req *ExecutionRequest) bool {
  728. if req.Binding.Action.Triggers == nil {
  729. return true
  730. }
  731. if req.TriggerDepth >= MaxTriggerDepth {
  732. log.WithFields(log.Fields{
  733. "actionTitle": req.logEntry.ActionTitle,
  734. "depth": req.TriggerDepth,
  735. }).Warnf("Trigger action reached maximum depth of %v. Not triggering further actions.", MaxTriggerDepth)
  736. req.logEntry.Output += fmt.Sprintf("OliveTin::trigger - this action reached maximum trigger depth of %v. Not triggering further actions.", MaxTriggerDepth)
  737. return true
  738. }
  739. if len(req.Tags) > 0 && req.Tags[0] == "trigger" {
  740. log.Warnf("Trigger action is triggering another trigger action. This is allowed, but be careful not to create trigger loops.")
  741. }
  742. triggerLoop(req)
  743. return true
  744. }
  745. func triggerLoop(req *ExecutionRequest) {
  746. for _, triggerReq := range req.Binding.Action.Triggers {
  747. binding := req.executor.FindBindingByID(triggerReq)
  748. trigger := &ExecutionRequest{
  749. Binding: binding,
  750. TrackingID: uuid.NewString(),
  751. Tags: []string{"trigger"},
  752. AuthenticatedUser: req.AuthenticatedUser,
  753. Arguments: req.Arguments,
  754. Cfg: req.Cfg,
  755. TriggerDepth: req.TriggerDepth + 1,
  756. }
  757. req.executor.ExecRequest(trigger)
  758. }
  759. }
  760. func stepSaveLog(req *ExecutionRequest) bool {
  761. filename := fmt.Sprintf("%v.%v.%v", req.logEntry.ActionTitle, req.logEntry.DatetimeStarted.Unix(), req.logEntry.ExecutionTrackingID)
  762. saveLogResults(req, filename)
  763. saveLogOutput(req, filename)
  764. return true
  765. }
  766. func firstNonEmpty(one, two string) string {
  767. if one != "" {
  768. return one
  769. }
  770. return two
  771. }
  772. func saveLogResults(req *ExecutionRequest, filename string) {
  773. dir := firstNonEmpty(req.Binding.Action.SaveLogs.ResultsDirectory, req.Cfg.SaveLogs.ResultsDirectory)
  774. if dir != "" {
  775. data, err := yaml.Marshal(req.logEntry)
  776. if err != nil {
  777. log.Warnf("%v", err)
  778. }
  779. filepath := path.Join(dir, filename+".yaml")
  780. err = os.WriteFile(filepath, data, 0644)
  781. if err != nil {
  782. log.Warnf("%v", err)
  783. }
  784. }
  785. }
  786. func saveLogOutput(req *ExecutionRequest, filename string) {
  787. dir := firstNonEmpty(req.Binding.Action.SaveLogs.OutputDirectory, req.Cfg.SaveLogs.OutputDirectory)
  788. if dir != "" {
  789. data := req.logEntry.Output
  790. filepath := path.Join(dir, filename+".log")
  791. err := os.WriteFile(filepath, []byte(data), 0644)
  792. if err != nil {
  793. log.Warnf("%v", err)
  794. }
  795. }
  796. }