api.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. package api
  2. import (
  3. ctx "context"
  4. "encoding/json"
  5. "errors"
  6. "os"
  7. "path"
  8. "sort"
  9. "connectrpc.com/connect"
  10. "google.golang.org/protobuf/encoding/protojson"
  11. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  12. apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
  13. "github.com/google/uuid"
  14. log "github.com/sirupsen/logrus"
  15. "fmt"
  16. "net/http"
  17. "sync"
  18. "time"
  19. acl "github.com/OliveTin/OliveTin/internal/acl"
  20. auth "github.com/OliveTin/OliveTin/internal/auth"
  21. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  22. config "github.com/OliveTin/OliveTin/internal/config"
  23. entities "github.com/OliveTin/OliveTin/internal/entities"
  24. executor "github.com/OliveTin/OliveTin/internal/executor"
  25. installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
  26. "github.com/OliveTin/OliveTin/internal/tpl"
  27. connectproto "go.akshayshah.org/connectproto"
  28. )
  29. type oliveTinAPI struct {
  30. executor *executor.Executor
  31. cfg *config.Config
  32. // streamingClients is a set of currently connected clients.
  33. // The empty struct value models set semantics (keys only) and keeps add/remove O(1).
  34. // We use a map for efficient membership and deletion; ordering is not required.
  35. streamingClients map[*streamingClient]struct{}
  36. streamingClientsMutex sync.RWMutex
  37. }
  38. // This is used to avoid race conditions when iterating over the connectedClients map.
  39. // and holds the lock for as minimal time as possible to avoid blocking the API for too long.
  40. func (api *oliveTinAPI) copyOfStreamingClients() []*streamingClient {
  41. api.streamingClientsMutex.RLock()
  42. defer api.streamingClientsMutex.RUnlock()
  43. clients := make([]*streamingClient, 0, len(api.streamingClients))
  44. for client := range api.streamingClients {
  45. clients = append(clients, client)
  46. }
  47. return clients
  48. }
  49. type streamingClient struct {
  50. channel chan *apiv1.EventStreamResponse
  51. AuthenticatedUser *authpublic.AuthenticatedUser
  52. heartbeatStopOnce sync.Once
  53. heartbeatStop chan struct{}
  54. heartbeatDone chan struct{}
  55. }
  56. func (c *streamingClient) stopHeartbeat() {
  57. if c.heartbeatStop == nil || c.heartbeatDone == nil {
  58. return
  59. }
  60. c.heartbeatStopOnce.Do(func() {
  61. close(c.heartbeatStop)
  62. })
  63. <-c.heartbeatDone
  64. }
  65. // trySendEventToClient sends msg to the client's channel. Returns false if the channel is full or closed.
  66. func (api *oliveTinAPI) trySendEventToClient(client *streamingClient, msg *apiv1.EventStreamResponse) bool {
  67. if client == nil || msg == nil {
  68. return false
  69. }
  70. sent := sendToStreamingClientChannel(client.channel, msg)
  71. if !sent {
  72. log.Warnf("EventStream: client channel is full or closed, removing client")
  73. }
  74. return sent
  75. }
  76. func sendToStreamingClientChannel(ch chan *apiv1.EventStreamResponse, msg *apiv1.EventStreamResponse) (sent bool) {
  77. defer func() {
  78. if recover() != nil {
  79. sent = false
  80. }
  81. }()
  82. select {
  83. case ch <- msg:
  84. return true
  85. default:
  86. return false
  87. }
  88. }
  89. func (api *oliveTinAPI) KillAction(ctx ctx.Context, req *connect.Request[apiv1.KillActionRequest]) (*connect.Response[apiv1.KillActionResponse], error) {
  90. ret := &apiv1.KillActionResponse{
  91. ExecutionTrackingId: req.Msg.ExecutionTrackingId,
  92. }
  93. var execReqLogEntry *executor.InternalLogEntry
  94. execReqLogEntry, ret.Found = api.executor.GetLog(req.Msg.ExecutionTrackingId)
  95. if !ret.Found {
  96. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found for tracking ID %s", req.Msg.ExecutionTrackingId))
  97. }
  98. if execReqLogEntry.Binding == nil {
  99. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("log entry has no binding for tracking ID %s", req.Msg.ExecutionTrackingId))
  100. }
  101. action := execReqLogEntry.Binding.Action
  102. if action == nil {
  103. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action not found for tracking ID %s", req.Msg.ExecutionTrackingId))
  104. }
  105. log.Warnf("Killing execution request by tracking ID: %v", req.Msg.ExecutionTrackingId)
  106. user := auth.UserFromApiCall(ctx, req, api.cfg)
  107. api.killActionByTrackingId(user, action, execReqLogEntry, ret)
  108. return connect.NewResponse(ret), nil
  109. }
  110. func (api *oliveTinAPI) killActionByTrackingId(user *authpublic.AuthenticatedUser, action *config.Action, execReqLogEntry *executor.InternalLogEntry, ret *apiv1.KillActionResponse) {
  111. if !acl.IsAllowedKill(api.cfg, user, action) {
  112. log.Warnf("Killing execution request not possible - user not allowed to kill this action: %v", execReqLogEntry.ExecutionTrackingID)
  113. ret.Killed = false
  114. return
  115. }
  116. err := api.executor.Kill(execReqLogEntry)
  117. if err != nil {
  118. log.Warnf("Killing execution request err: %v", err)
  119. ret.AlreadyCompleted = true
  120. ret.Killed = false
  121. } else {
  122. ret.Killed = true
  123. }
  124. }
  125. func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *connect.Request[apiv1.StartActionRequest]) (*connect.Response[apiv1.StartActionResponse], error) {
  126. pair, err := api.findBindingByIDOrNotFound(req.Msg.BindingId)
  127. if err != nil {
  128. return nil, err
  129. }
  130. authenticatedUser := auth.UserFromApiCall(ctx, req, api.cfg)
  131. if err := validateJustificationRequired(pair.Action, req.Msg.Justification, authenticatedUser); err != nil {
  132. return nil, connectInvalidJustification(err)
  133. }
  134. execReq := executor.ExecutionRequest{
  135. Binding: pair,
  136. TrackingID: req.Msg.UniqueTrackingId,
  137. Arguments: startActionArgumentsFromProto(req.Msg.Arguments),
  138. Justification: req.Msg.Justification,
  139. AuthenticatedUser: authenticatedUser,
  140. Cfg: api.cfg,
  141. }
  142. api.executor.ExecRequest(&execReq)
  143. return connect.NewResponse(&apiv1.StartActionResponse{
  144. ExecutionTrackingId: execReq.TrackingID,
  145. }), nil
  146. }
  147. func (api *oliveTinAPI) PasswordHash(ctx ctx.Context, req *connect.Request[apiv1.PasswordHashRequest]) (*connect.Response[apiv1.PasswordHashResponse], error) {
  148. hash, err := createHash(req.Msg.Password)
  149. if err != nil {
  150. if errors.Is(err, ErrArgon2Busy) {
  151. return nil, connect.NewError(connect.CodeResourceExhausted, err)
  152. }
  153. return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("error creating hash: %w", err))
  154. }
  155. ret := &apiv1.PasswordHashResponse{
  156. Hash: hash,
  157. }
  158. return connect.NewResponse(ret), nil
  159. }
  160. func (api *oliveTinAPI) cookieSecure(header http.Header) bool {
  161. useTLS := header.Get("X-Forwarded-Proto") == "https"
  162. return useTLS || api.cfg.Security.ForceSecureCookies
  163. }
  164. func (api *oliveTinAPI) applyLocalLoginResult(req *apiv1.LocalUserLoginRequest, response *connect.Response[apiv1.LocalUserLoginResponse], match bool, secure bool) {
  165. if match {
  166. user := api.cfg.FindUserByUsername(req.Username)
  167. if user != nil {
  168. sid := uuid.NewString()
  169. auth.RegisterUserSession(api.cfg, "local", sid, user.Username)
  170. log.WithFields(log.Fields{"username": user.Username}).Info("LocalUserLogin: Session created and registered")
  171. cookie := &http.Cookie{
  172. Name: "olivetin-sid-local",
  173. Value: sid,
  174. MaxAge: 31556952,
  175. HttpOnly: true,
  176. Path: "/",
  177. Secure: secure,
  178. SameSite: http.SameSiteLaxMode,
  179. }
  180. response.Header().Set("Set-Cookie", cookie.String())
  181. log.WithFields(log.Fields{"username": user.Username}).Info("LocalUserLogin: User logged in successfully.")
  182. } else {
  183. log.WithFields(log.Fields{"username": req.Username}).Warn("LocalUserLogin: Password matched but user lookup failed.")
  184. }
  185. } else {
  186. log.WithFields(log.Fields{"username": req.Username}).Warn("LocalUserLogin: User login failed.")
  187. }
  188. }
  189. func (api *oliveTinAPI) localUserLoginEarlyReject(req *connect.Request[apiv1.LocalUserLoginRequest]) *connect.Response[apiv1.LocalUserLoginResponse] {
  190. if !api.cfg.AuthLocalUsers.Enabled {
  191. return connect.NewResponse(&apiv1.LocalUserLoginResponse{Success: false})
  192. }
  193. if isLocalInteractiveLoginDisabledForUser(api.cfg, req.Msg.Username) {
  194. log.WithFields(log.Fields{"username": req.Msg.Username}).Debug("LocalUserLogin: interactive login disabled (no password configured)")
  195. return connect.NewResponse(&apiv1.LocalUserLoginResponse{Success: false})
  196. }
  197. return nil
  198. }
  199. func (api *oliveTinAPI) LocalUserLogin(ctx ctx.Context, req *connect.Request[apiv1.LocalUserLoginRequest]) (*connect.Response[apiv1.LocalUserLoginResponse], error) {
  200. if early := api.localUserLoginEarlyReject(req); early != nil {
  201. return early, nil
  202. }
  203. match, err := checkUserPassword(api.cfg, req.Msg.Username, req.Msg.Password)
  204. if err != nil {
  205. if errors.Is(err, ErrArgon2Busy) {
  206. return nil, connect.NewError(connect.CodeResourceExhausted, err)
  207. }
  208. return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("checking password: %w", err))
  209. }
  210. response := connect.NewResponse(&apiv1.LocalUserLoginResponse{Success: match})
  211. api.applyLocalLoginResult(req.Msg, response, match, api.cookieSecure(req.Header()))
  212. return response, nil
  213. }
  214. func (api *oliveTinAPI) startActionAndWaitRun(binding *executor.ActionBinding, args map[string]string, justification string, user *authpublic.AuthenticatedUser) (*executor.InternalLogEntry, bool) {
  215. execReq := executor.ExecutionRequest{
  216. Binding: binding,
  217. TrackingID: uuid.NewString(),
  218. Arguments: args,
  219. Justification: justification,
  220. AuthenticatedUser: user,
  221. Cfg: api.cfg,
  222. }
  223. wg, _ := api.executor.ExecRequest(&execReq)
  224. wg.Wait()
  225. return api.executor.GetLog(execReq.TrackingID)
  226. }
  227. func (api *oliveTinAPI) findBindingOrNotFound(actionId string) (*executor.ActionBinding, error) {
  228. binding := api.executor.FindBindingByID(actionId)
  229. if binding == nil || binding.Action == nil {
  230. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", actionId))
  231. }
  232. return binding, nil
  233. }
  234. func (api *oliveTinAPI) findBindingByIDOrNotFound(bindingId string) (*executor.ActionBinding, error) {
  235. return api.findBindingOrNotFound(bindingId)
  236. }
  237. func (api *oliveTinAPI) StartActionAndWait(ctx ctx.Context, req *connect.Request[apiv1.StartActionAndWaitRequest]) (*connect.Response[apiv1.StartActionAndWaitResponse], error) {
  238. binding, err := api.findBindingOrNotFound(req.Msg.ActionId)
  239. if err != nil {
  240. return nil, err
  241. }
  242. user := auth.UserFromApiCall(ctx, req, api.cfg)
  243. if err := validateJustificationRequired(binding.Action, req.Msg.Justification, user); err != nil {
  244. return nil, connectInvalidJustification(err)
  245. }
  246. internalLogEntry, ok := api.startActionAndWaitRun(binding, startActionArgumentsFromProto(req.Msg.Arguments), req.Msg.Justification, user)
  247. if !ok {
  248. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found"))
  249. }
  250. return connect.NewResponse(&apiv1.StartActionAndWaitResponse{
  251. LogEntry: api.internalLogEntryToPb(internalLogEntry, user),
  252. }), nil
  253. }
  254. func (api *oliveTinAPI) StartActionByGet(ctx ctx.Context, req *connect.Request[apiv1.StartActionByGetRequest]) (*connect.Response[apiv1.StartActionByGetResponse], error) {
  255. binding := api.executor.FindBindingByID(req.Msg.ActionId)
  256. if binding == nil || binding.Action == nil {
  257. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", req.Msg.ActionId))
  258. }
  259. args := make(map[string]string)
  260. execReq := executor.ExecutionRequest{
  261. Binding: binding,
  262. TrackingID: uuid.NewString(),
  263. Arguments: args,
  264. AuthenticatedUser: auth.UserFromApiCall(ctx, req, api.cfg),
  265. Cfg: api.cfg,
  266. }
  267. _, uniqueTrackingId := api.executor.ExecRequest(&execReq)
  268. return connect.NewResponse(&apiv1.StartActionByGetResponse{
  269. ExecutionTrackingId: uniqueTrackingId,
  270. }), nil
  271. }
  272. func (api *oliveTinAPI) StartActionByGetAndWait(ctx ctx.Context, req *connect.Request[apiv1.StartActionByGetAndWaitRequest]) (*connect.Response[apiv1.StartActionByGetAndWaitResponse], error) {
  273. binding := api.executor.FindBindingByID(req.Msg.ActionId)
  274. if binding == nil || binding.Action == nil {
  275. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", req.Msg.ActionId))
  276. }
  277. args := make(map[string]string)
  278. user := auth.UserFromApiCall(ctx, req, api.cfg)
  279. execReq := executor.ExecutionRequest{
  280. Binding: binding,
  281. TrackingID: uuid.NewString(),
  282. Arguments: args,
  283. AuthenticatedUser: user,
  284. Cfg: api.cfg,
  285. }
  286. wg, _ := api.executor.ExecRequest(&execReq)
  287. wg.Wait()
  288. internalLogEntry, ok := api.executor.GetLog(execReq.TrackingID)
  289. if ok {
  290. return connect.NewResponse(&apiv1.StartActionByGetAndWaitResponse{
  291. LogEntry: api.internalLogEntryToPb(internalLogEntry, user),
  292. }), nil
  293. }
  294. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found"))
  295. }
  296. func calculateRateLimitExpires(api *oliveTinAPI, logEntry *executor.InternalLogEntry) string {
  297. if logEntry.Binding == nil || logEntry.Binding.Action == nil {
  298. return ""
  299. }
  300. expiryUnix := api.executor.GetTimeUntilAvailable(logEntry.Binding)
  301. if expiryUnix <= 0 {
  302. return ""
  303. }
  304. return time.Unix(expiryUnix, 0).Format("2006-01-02 15:04:05")
  305. }
  306. func (api *oliveTinAPI) internalLogEntryToPb(logEntry *executor.InternalLogEntry, authenticatedUser *authpublic.AuthenticatedUser) *apiv1.LogEntry {
  307. pble := &apiv1.LogEntry{
  308. ActionTitle: logEntry.ActionTitle,
  309. ActionIcon: logEntry.ActionIcon,
  310. DatetimeStarted: logEntry.DatetimeStarted.Format("2006-01-02 15:04:05"),
  311. DatetimeFinished: logEntry.DatetimeFinished.Format("2006-01-02 15:04:05"),
  312. DatetimeIndex: logEntry.Index,
  313. Output: logEntry.Output,
  314. TimedOut: logEntry.TimedOut,
  315. Blocked: logEntry.Blocked,
  316. Queued: logEntry.Queued,
  317. QueuedForGroup: logEntry.QueuedForGroup,
  318. ExitCode: logEntry.ExitCode,
  319. Tags: logEntry.Tags,
  320. ExecutionTrackingId: logEntry.ExecutionTrackingID,
  321. ExecutionStarted: logEntry.ExecutionStarted,
  322. ExecutionFinished: logEntry.ExecutionFinished,
  323. User: logEntry.Username,
  324. BindingId: logEntry.GetBindingId(),
  325. DatetimeRateLimitExpires: calculateRateLimitExpires(api, logEntry),
  326. Justification: logEntry.Justification,
  327. }
  328. if !pble.ExecutionFinished && logEntry.Binding != nil && logEntry.Binding.Action != nil {
  329. pble.CanKill = acl.IsAllowedKill(api.cfg, authenticatedUser, logEntry.Binding.Action)
  330. }
  331. return pble
  332. }
  333. func getExecutionStatusByTrackingID(api *oliveTinAPI, executionTrackingId string) *executor.InternalLogEntry {
  334. logEntry, ok := api.executor.GetLog(executionTrackingId)
  335. if !ok {
  336. return nil
  337. }
  338. return logEntry
  339. }
  340. // This is the actual action ID, not the binding ID.
  341. func getMostRecentExecutionStatusByActionId(api *oliveTinAPI, actionId string) *executor.InternalLogEntry {
  342. var ile *executor.InternalLogEntry
  343. binding := api.executor.FindBindingByID(actionId)
  344. if binding == nil {
  345. return nil
  346. }
  347. logs := api.executor.GetLogsByBindingId(binding.ID)
  348. if len(logs) == 0 {
  349. return nil
  350. }
  351. if len(logs) == 0 {
  352. return nil
  353. } else {
  354. // Get last log entry
  355. ile = logs[len(logs)-1]
  356. }
  357. return ile
  358. }
  359. func (api *oliveTinAPI) resolveExecutionStatusForView(msg *apiv1.ExecutionStatusRequest, user *authpublic.AuthenticatedUser) (*executor.InternalLogEntry, error) {
  360. ile := api.getExecutionStatusByRequest(msg)
  361. if ile == nil {
  362. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found for tracking ID %s or action ID %s", msg.ExecutionTrackingId, msg.ActionId))
  363. }
  364. if !isValidLogEntry(ile) || !api.isLogEntryAllowed(ile, user) {
  365. return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("permission denied to view this execution"))
  366. }
  367. return ile, nil
  368. }
  369. func (api *oliveTinAPI) getExecutionStatusByRequest(msg *apiv1.ExecutionStatusRequest) *executor.InternalLogEntry {
  370. if msg.ExecutionTrackingId != "" {
  371. return getExecutionStatusByTrackingID(api, msg.ExecutionTrackingId)
  372. }
  373. return getMostRecentExecutionStatusByActionId(api, msg.ActionId)
  374. }
  375. func dashboardNavigationTargetsToPb(targets []executor.DashboardNavigationTarget) []*apiv1.DashboardNavigationTarget {
  376. if len(targets) == 0 {
  377. return nil
  378. }
  379. result := make([]*apiv1.DashboardNavigationTarget, 0, len(targets))
  380. for _, target := range targets {
  381. result = append(result, &apiv1.DashboardNavigationTarget{
  382. Title: target.Title,
  383. EntityType: target.EntityType,
  384. EntityKey: target.EntityKey,
  385. Path: target.Path,
  386. })
  387. }
  388. return result
  389. }
  390. func (api *oliveTinAPI) executionStatusBackToDashboards(ile *executor.InternalLogEntry) []*apiv1.DashboardNavigationTarget {
  391. if ile == nil || ile.Binding == nil {
  392. return nil
  393. }
  394. return dashboardNavigationTargetsToPb(ile.Binding.OnDashboards)
  395. }
  396. func (api *oliveTinAPI) ExecutionStatus(ctx ctx.Context, req *connect.Request[apiv1.ExecutionStatusRequest]) (*connect.Response[apiv1.ExecutionStatusResponse], error) {
  397. user := auth.UserFromApiCall(ctx, req, api.cfg)
  398. if err := api.checkDashboardAccess(user); err != nil {
  399. return nil, err
  400. }
  401. ile, err := api.resolveExecutionStatusForView(req.Msg, user)
  402. if err != nil {
  403. return nil, err
  404. }
  405. res := &apiv1.ExecutionStatusResponse{
  406. LogEntry: api.internalLogEntryToPb(ile, user),
  407. BackToDashboards: api.executionStatusBackToDashboards(ile),
  408. }
  409. return connect.NewResponse(res), nil
  410. }
  411. func (api *oliveTinAPI) Logout(ctx ctx.Context, req *connect.Request[apiv1.LogoutRequest]) (*connect.Response[apiv1.LogoutResponse], error) {
  412. user := auth.UserFromApiCall(ctx, req, api.cfg)
  413. auth.RevokeSessionForProvider(api.cfg, user.Provider, user.SID)
  414. log.WithFields(log.Fields{
  415. "username": user.Username,
  416. "provider": user.Provider,
  417. }).Info("Logout: User logged out")
  418. response := connect.NewResponse(&apiv1.LogoutResponse{})
  419. secure := api.cookieSecure(req.Header())
  420. // Clear the local authentication cookie by setting it to expire
  421. localCookie := &http.Cookie{
  422. Name: "olivetin-sid-local",
  423. Value: "",
  424. MaxAge: -1, // This tells the browser to delete the cookie
  425. HttpOnly: true,
  426. Path: "/",
  427. Secure: secure,
  428. SameSite: http.SameSiteLaxMode,
  429. }
  430. response.Header().Set("Set-Cookie", localCookie.String())
  431. // Clear the OAuth2 authentication cookie by setting it to expire
  432. oauth2Cookie := &http.Cookie{
  433. Name: "olivetin-sid-oauth",
  434. Value: "",
  435. MaxAge: -1, // This tells the browser to delete the cookie
  436. HttpOnly: true,
  437. Path: "/",
  438. Secure: secure,
  439. SameSite: http.SameSiteLaxMode,
  440. }
  441. response.Header().Add("Set-Cookie", oauth2Cookie.String())
  442. return response, nil
  443. }
  444. func (api *oliveTinAPI) GetActionBinding(ctx ctx.Context, req *connect.Request[apiv1.GetActionBindingRequest]) (*connect.Response[apiv1.GetActionBindingResponse], error) {
  445. user := auth.UserFromApiCall(ctx, req, api.cfg)
  446. if err := api.checkDashboardAccess(user); err != nil {
  447. return nil, err
  448. }
  449. resp, err := api.getActionBindingResponse(user, req.Msg.BindingId)
  450. if err != nil {
  451. return nil, err
  452. }
  453. return connect.NewResponse(resp), nil
  454. }
  455. func (api *oliveTinAPI) getActionBindingResponse(user *authpublic.AuthenticatedUser, bindingId string) (*apiv1.GetActionBindingResponse, error) {
  456. binding := api.executor.FindBindingByID(bindingId)
  457. if binding == nil || binding.Action == nil {
  458. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", bindingId))
  459. }
  460. if !api.userCanViewAction(user, binding.Action) {
  461. return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("permission denied"))
  462. }
  463. return &apiv1.GetActionBindingResponse{
  464. Action: buildAction(binding, api.createDashboardRenderRequest(user, "", "")),
  465. BackToDashboards: dashboardNavigationTargetsToPb(binding.OnDashboards),
  466. }, nil
  467. }
  468. func (api *oliveTinAPI) userCanViewAction(user *authpublic.AuthenticatedUser, action *config.Action) bool {
  469. if user == nil {
  470. return true
  471. }
  472. return acl.IsAllowedView(api.cfg, user, action)
  473. }
  474. func (api *oliveTinAPI) GetDashboard(ctx ctx.Context, req *connect.Request[apiv1.GetDashboardRequest]) (*connect.Response[apiv1.GetDashboardResponse], error) {
  475. user := auth.UserFromApiCall(ctx, req, api.cfg)
  476. if err := api.checkDashboardAccess(user); err != nil {
  477. return nil, err
  478. }
  479. entityType := ""
  480. entityKey := ""
  481. if req.Msg != nil {
  482. entityType = req.Msg.EntityType
  483. entityKey = req.Msg.EntityKey
  484. }
  485. dashboardRenderRequest := api.createDashboardRenderRequest(user, entityType, entityKey)
  486. if api.isDefaultDashboard(req.Msg.Title) {
  487. return api.buildDefaultDashboardResponse(dashboardRenderRequest)
  488. }
  489. return api.buildCustomDashboardResponse(dashboardRenderRequest, req.Msg.Title)
  490. }
  491. func (api *oliveTinAPI) checkDashboardAccess(user *authpublic.AuthenticatedUser) error {
  492. if user.IsGuest() && api.cfg.AuthRequireGuestsToLogin {
  493. return connect.NewError(connect.CodePermissionDenied, fmt.Errorf("guests are not allowed to access the dashboard"))
  494. }
  495. return nil
  496. }
  497. func (api *oliveTinAPI) createDashboardRenderRequest(user *authpublic.AuthenticatedUser, entityType, entityKey string) *DashboardRenderRequest {
  498. rr := &DashboardRenderRequest{
  499. AuthenticatedUser: user,
  500. cfg: api.cfg,
  501. ex: api.executor,
  502. EntityType: entityType,
  503. EntityKey: entityKey,
  504. }
  505. populateActiveBindingStates(rr)
  506. return rr
  507. }
  508. func (api *oliveTinAPI) isDefaultDashboard(title string) bool {
  509. return title == "default" || title == "" || title == "Actions"
  510. }
  511. func (api *oliveTinAPI) buildDefaultDashboardResponse(rr *DashboardRenderRequest) (*connect.Response[apiv1.GetDashboardResponse], error) {
  512. db := buildDefaultDashboard(rr)
  513. res := &apiv1.GetDashboardResponse{
  514. Dashboard: db,
  515. }
  516. return connect.NewResponse(res), nil
  517. }
  518. func (api *oliveTinAPI) buildCustomDashboardResponse(rr *DashboardRenderRequest, title string) (*connect.Response[apiv1.GetDashboardResponse], error) {
  519. res := &apiv1.GetDashboardResponse{
  520. Dashboard: renderDashboard(rr, title),
  521. }
  522. return connect.NewResponse(res), nil
  523. }
  524. func resolveLogsPageSize(requestPageSize, defaultPageSize int64) int64 {
  525. if requestPageSize == 0 {
  526. return defaultPageSize
  527. }
  528. if requestPageSize < 10 {
  529. return 10
  530. }
  531. if requestPageSize > 100 {
  532. return 100
  533. }
  534. return requestPageSize
  535. }
  536. func (api *oliveTinAPI) GetLogs(ctx ctx.Context, req *connect.Request[apiv1.GetLogsRequest]) (*connect.Response[apiv1.GetLogsResponse], error) {
  537. user := auth.UserFromApiCall(ctx, req, api.cfg)
  538. if err := api.checkDashboardAccess(user); err != nil {
  539. return nil, err
  540. }
  541. pageSize := resolveLogsPageSize(req.Msg.GetPageSize(), api.cfg.LogHistoryPageSize)
  542. logEntries, paging, err := api.executor.GetLogTrackingIdsACL(api.cfg, user, req.Msg.StartOffset, pageSize, req.Msg.DateFilter, req.Msg.GetFilter())
  543. if err != nil {
  544. return nil, connect.NewError(connect.CodeInvalidArgument, err)
  545. }
  546. ret := &apiv1.GetLogsResponse{}
  547. for _, le := range logEntries {
  548. ret.Logs = append(ret.Logs, api.internalLogEntryToPb(le, user))
  549. }
  550. ret.CountRemaining = paging.CountRemaining
  551. ret.PageSize = paging.PageSize
  552. ret.TotalCount = paging.TotalCount
  553. ret.StartOffset = paging.StartOffset
  554. return connect.NewResponse(ret), nil
  555. }
  556. // isValidLogEntry checks if a log entry has all required fields populated.
  557. func isValidLogEntry(e *executor.InternalLogEntry) bool {
  558. return e != nil && e.Binding != nil && e.Binding.Action != nil
  559. }
  560. // isLogEntryAllowed checks if a log entry is allowed to be viewed by the user.
  561. func (api *oliveTinAPI) isLogEntryAllowed(e *executor.InternalLogEntry, user *authpublic.AuthenticatedUser) bool {
  562. if user == nil || !isValidLogEntry(e) {
  563. return false
  564. }
  565. return acl.IsAllowedLogs(api.cfg, user, e.Binding.Action)
  566. }
  567. // mayViewExecutionEvent returns whether the user is allowed to receive this execution event (for EventStream ACL).
  568. func (api *oliveTinAPI) mayViewExecutionEvent(entry *executor.InternalLogEntry, user *authpublic.AuthenticatedUser) bool {
  569. if user == nil {
  570. return false
  571. }
  572. return isValidLogEntry(entry) && api.isLogEntryAllowed(entry, user)
  573. }
  574. // buildEmptyPageResponse creates a response for an empty page.
  575. func buildEmptyPageResponse(page pageInfo) *apiv1.GetActionLogsResponse {
  576. return &apiv1.GetActionLogsResponse{
  577. CountRemaining: 0,
  578. PageSize: page.size,
  579. TotalCount: page.total,
  580. StartOffset: page.start,
  581. }
  582. }
  583. // calculateReversedIndices computes the reversed indices for newest-first pagination.
  584. func calculateReversedIndices(page pageInfo, filteredLen int) (int64, int64) {
  585. startIdx := page.total - page.end
  586. endIdx := page.total - page.start
  587. if startIdx < 0 {
  588. startIdx = 0
  589. }
  590. if endIdx > int64(filteredLen) {
  591. endIdx = int64(filteredLen)
  592. }
  593. return startIdx, endIdx
  594. }
  595. // buildActionLogsResponse builds the response with paginated log entries (newest first).
  596. func (api *oliveTinAPI) buildActionLogsResponse(filtered []*executor.InternalLogEntry, page pageInfo, user *authpublic.AuthenticatedUser) *apiv1.GetActionLogsResponse {
  597. startIdx, endIdx := calculateReversedIndices(page, len(filtered))
  598. ret := &apiv1.GetActionLogsResponse{}
  599. chunk := filtered[int(startIdx):int(endIdx)]
  600. for i := len(chunk) - 1; i >= 0; i-- {
  601. ret.Logs = append(ret.Logs, api.internalLogEntryToPb(chunk[i], user))
  602. }
  603. ret.CountRemaining = page.start
  604. ret.PageSize = page.size
  605. ret.TotalCount = page.total
  606. ret.StartOffset = page.start
  607. return ret
  608. }
  609. func (api *oliveTinAPI) GetActionLogs(ctx ctx.Context, req *connect.Request[apiv1.GetActionLogsRequest]) (*connect.Response[apiv1.GetActionLogsResponse], error) {
  610. user := auth.UserFromApiCall(ctx, req, api.cfg)
  611. if err := api.checkDashboardAccess(user); err != nil {
  612. return nil, err
  613. }
  614. filtered := api.filterLogsByACL(api.executor.GetLogsByBindingId(req.Msg.ActionId), user)
  615. page := paginate(int64(len(filtered)), api.cfg.LogHistoryPageSize, req.Msg.StartOffset)
  616. if page.empty {
  617. return connect.NewResponse(buildEmptyPageResponse(page)), nil
  618. }
  619. return connect.NewResponse(api.buildActionLogsResponse(filtered, page, user)), nil
  620. }
  621. func (api *oliveTinAPI) filterLogsByACL(entries []*executor.InternalLogEntry, user *authpublic.AuthenticatedUser) []*executor.InternalLogEntry {
  622. filtered := make([]*executor.InternalLogEntry, 0, len(entries))
  623. for _, e := range entries {
  624. if !isValidLogEntry(e) {
  625. continue
  626. }
  627. if api.isLogEntryAllowed(e, user) {
  628. filtered = append(filtered, e)
  629. }
  630. }
  631. return filtered
  632. }
  633. type pageInfo struct {
  634. total int64
  635. size int64
  636. start int64
  637. end int64
  638. empty bool
  639. }
  640. func paginate(total int64, size int64, start int64) pageInfo {
  641. if start < 0 {
  642. start = 0
  643. }
  644. if start >= total {
  645. return pageInfo{total: total, size: size, start: start, end: start, empty: true}
  646. }
  647. end := start + size
  648. if end > total {
  649. end = total
  650. }
  651. return pageInfo{total: total, size: size, start: start, end: end, empty: false}
  652. }
  653. /*
  654. This function is ONLY a helper for the UI - the arguments are validated properly
  655. on the StartAction -> Executor chain. This is here basically to provide helpful
  656. error messages more quickly before starting the action.
  657. It uses the same validation logic as the executor, including mangling argument
  658. values (e.g., datetime formatting, checkbox title-to-value conversion).
  659. */
  660. func (api *oliveTinAPI) argumentNotFoundForValidation(msg *apiv1.ValidateArgumentTypeRequest) bool {
  661. if msg.BindingId == "" || msg.ArgumentName == "" {
  662. return false
  663. }
  664. arg, _ := api.findArgumentForValidation(msg.BindingId, msg.ArgumentName)
  665. return arg == nil
  666. }
  667. func (api *oliveTinAPI) validateArgumentTypeBindingAccess(user *authpublic.AuthenticatedUser, msg *apiv1.ValidateArgumentTypeRequest) error {
  668. if msg == nil || msg.BindingId == "" {
  669. return nil
  670. }
  671. return api.errUnlessUserMayValidateArgumentTypeForBinding(user, msg.BindingId)
  672. }
  673. func (api *oliveTinAPI) errUnlessUserMayValidateArgumentTypeForBinding(user *authpublic.AuthenticatedUser, bindingID string) error {
  674. binding := api.executor.FindBindingByID(bindingID)
  675. if binding == nil || binding.Action == nil {
  676. return connect.NewError(connect.CodeNotFound, fmt.Errorf("action or argument not found for binding ID %s", bindingID))
  677. }
  678. if !api.userCanViewAction(user, binding.Action) {
  679. return connect.NewError(connect.CodePermissionDenied, fmt.Errorf("permission denied"))
  680. }
  681. return nil
  682. }
  683. func (api *oliveTinAPI) ValidateArgumentType(ctx ctx.Context, req *connect.Request[apiv1.ValidateArgumentTypeRequest]) (*connect.Response[apiv1.ValidateArgumentTypeResponse], error) {
  684. user := auth.UserFromApiCall(ctx, req, api.cfg)
  685. if err := api.checkDashboardAccess(user); err != nil {
  686. return nil, err
  687. }
  688. if err := api.validateArgumentTypeBindingAccess(user, req.Msg); err != nil {
  689. return nil, err
  690. }
  691. if api.argumentNotFoundForValidation(req.Msg) {
  692. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action or argument not found for binding ID %s", req.Msg.BindingId))
  693. }
  694. return api.validateArgumentTypeConnectResponse(req.Msg)
  695. }
  696. func (api *oliveTinAPI) validateArgumentTypeConnectResponse(msg *apiv1.ValidateArgumentTypeRequest) (*connect.Response[apiv1.ValidateArgumentTypeResponse], error) {
  697. err := api.validateArgumentTypeInternal(msg)
  698. desc := ""
  699. if err != nil {
  700. desc = err.Error()
  701. }
  702. return connect.NewResponse(&apiv1.ValidateArgumentTypeResponse{
  703. Valid: err == nil,
  704. Description: desc,
  705. }), nil
  706. }
  707. func (api *oliveTinAPI) validateArgumentTypeInternal(msg *apiv1.ValidateArgumentTypeRequest) error {
  708. if msg.BindingId == "" || msg.ArgumentName == "" {
  709. return executor.TypeSafetyCheck("", msg.Value, msg.Type)
  710. }
  711. arg, action := api.findArgumentForValidation(msg.BindingId, msg.ArgumentName)
  712. if arg == nil {
  713. return fmt.Errorf("argument not found")
  714. }
  715. return executor.ValidateArgument(arg, msg.Value, action)
  716. }
  717. func (api *oliveTinAPI) findArgumentForValidation(bindingId string, argumentName string) (*config.ActionArgument, *config.Action) {
  718. binding := api.executor.FindBindingByID(bindingId)
  719. if binding == nil || binding.Action == nil {
  720. return nil, nil
  721. }
  722. arg := api.findArgumentByName(binding.Action, argumentName)
  723. return arg, binding.Action
  724. }
  725. func (api *oliveTinAPI) findArgumentByName(action *config.Action, name string) *config.ActionArgument {
  726. for i := range action.Arguments {
  727. if action.Arguments[i].Name == name {
  728. return &action.Arguments[i]
  729. }
  730. }
  731. return nil
  732. }
  733. func (api *oliveTinAPI) WhoAmI(ctx ctx.Context, req *connect.Request[apiv1.WhoAmIRequest]) (*connect.Response[apiv1.WhoAmIResponse], error) {
  734. user := auth.UserFromApiCall(ctx, req, api.cfg)
  735. if err := api.checkDashboardAccess(user); err != nil {
  736. return nil, err
  737. }
  738. res := &apiv1.WhoAmIResponse{
  739. AuthenticatedUser: user.Username,
  740. Usergroup: user.UsergroupLine,
  741. Provider: user.Provider,
  742. Sid: user.SID,
  743. Acls: user.Acls,
  744. }
  745. return connect.NewResponse(res), nil
  746. }
  747. func (api *oliveTinAPI) SosReport(ctx ctx.Context, req *connect.Request[apiv1.SosReportRequest]) (*connect.Response[apiv1.SosReportResponse], error) {
  748. user := auth.UserFromApiCall(ctx, req, api.cfg)
  749. redactVersion := !user.EffectivePolicy.ShowVersionNumber
  750. sos := installationinfo.GetSosReport(redactVersion)
  751. if !api.cfg.InsecureAllowDumpSos {
  752. log.Info(sos)
  753. sos = "Your SOS Report has been logged to OliveTin logs.\n\nIf you are in a safe network, you can temporarily set `insecureAllowDumpSos: true` in your config.yaml, restart OliveTin, and refresh this page - it will put the output directly in the browser."
  754. }
  755. ret := &apiv1.SosReportResponse{
  756. Alert: sos,
  757. }
  758. return connect.NewResponse(ret), nil
  759. }
  760. func (api *oliveTinAPI) DumpVars(ctx ctx.Context, req *connect.Request[apiv1.DumpVarsRequest]) (*connect.Response[apiv1.DumpVarsResponse], error) {
  761. res := &apiv1.DumpVarsResponse{}
  762. if !api.cfg.InsecureAllowDumpVars {
  763. res.Alert = "Dumping variables is not allowed by default because it is insecure."
  764. return connect.NewResponse(res), nil
  765. }
  766. jsonstring, err := json.MarshalIndent(tpl.GetNewGeneralTemplateContext(), "", " ")
  767. if err != nil {
  768. log.WithError(err).Error("DumpVars: failed to marshal template context from GetNewGeneralTemplateContext")
  769. return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("dump vars: marshal template context: %w", err))
  770. }
  771. fmt.Printf("%s", jsonstring)
  772. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpVars = false again after you don't need it anymore"
  773. return connect.NewResponse(res), nil
  774. }
  775. func debugBindingActionTitle(binding *executor.ActionBinding) string {
  776. if binding == nil || binding.Action == nil {
  777. return ""
  778. }
  779. return binding.Action.Title
  780. }
  781. func (api *oliveTinAPI) DumpPublicIdActionMap(ctx ctx.Context, req *connect.Request[apiv1.DumpPublicIdActionMapRequest]) (*connect.Response[apiv1.DumpPublicIdActionMapResponse], error) {
  782. res := &apiv1.DumpPublicIdActionMapResponse{}
  783. res.Contents = make(map[string]*apiv1.DebugBinding)
  784. if !api.cfg.InsecureAllowDumpActionMap {
  785. res.Alert = "Dumping Public IDs is disallowed."
  786. return connect.NewResponse(res), nil
  787. }
  788. api.executor.MapActionBindingsLock.RLock()
  789. for k, v := range api.executor.MapActionBindings {
  790. res.Contents[k] = &apiv1.DebugBinding{
  791. ActionTitle: debugBindingActionTitle(v),
  792. }
  793. }
  794. api.executor.MapActionBindingsLock.RUnlock()
  795. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpActionMap = false again after you don't need it anymore"
  796. return connect.NewResponse(res), nil
  797. }
  798. func (api *oliveTinAPI) GetReadyz(ctx ctx.Context, req *connect.Request[apiv1.GetReadyzRequest]) (*connect.Response[apiv1.GetReadyzResponse], error) {
  799. res := &apiv1.GetReadyzResponse{
  800. Status: "OK",
  801. }
  802. return connect.NewResponse(res), nil
  803. }
  804. func (api *oliveTinAPI) EventStream(ctx ctx.Context, req *connect.Request[apiv1.EventStreamRequest], srv *connect.ServerStream[apiv1.EventStreamResponse]) error {
  805. log.Debugf("EventStream: %v", req.Msg)
  806. // Set X-Accel-Buffering header to disable nginx buffering for this stream
  807. // https://github.com/OliveTin/OliveTin/issues/765
  808. srv.ResponseHeader().Set("X-Accel-Buffering", "no")
  809. user := auth.UserFromApiCall(ctx, req, api.cfg)
  810. if err := api.checkDashboardAccess(user); err != nil {
  811. return err
  812. }
  813. client := &streamingClient{
  814. channel: make(chan *apiv1.EventStreamResponse, 10), // Buffered channel to hold Events
  815. AuthenticatedUser: user,
  816. heartbeatStop: make(chan struct{}),
  817. heartbeatDone: make(chan struct{}),
  818. }
  819. log.WithFields(log.Fields{
  820. "authenticatedUser": user.Username,
  821. }).Debugf("EventStream: client connected")
  822. api.streamingClientsMutex.Lock()
  823. api.streamingClients[client] = struct{}{}
  824. api.streamingClientsMutex.Unlock()
  825. go api.sendEventStreamHeartbeats(client)
  826. // loop over client channel and send events to connectedClient
  827. for msg := range client.channel {
  828. log.Debugf("Sending event to client: %v", msg)
  829. if err := srv.Send(msg); err != nil {
  830. log.Errorf("Error sending event to client: %v", err)
  831. // Remove disconnected client from the list
  832. api.removeClient(client)
  833. break
  834. }
  835. }
  836. log.Infof("EventStream: client disconnected")
  837. return nil
  838. }
  839. func (api *oliveTinAPI) sendEventStreamHeartbeats(client *streamingClient) {
  840. defer close(client.heartbeatDone)
  841. if !api.sendEventStreamHeartbeat(client) {
  842. go api.removeClient(client)
  843. return
  844. }
  845. ticker := time.NewTicker(10 * time.Second)
  846. defer ticker.Stop()
  847. api.runEventStreamHeartbeatLoop(client, ticker)
  848. }
  849. func (api *oliveTinAPI) runEventStreamHeartbeatLoop(client *streamingClient, ticker *time.Ticker) {
  850. for {
  851. if api.waitEventStreamHeartbeatOrDone(client.heartbeatStop, ticker) {
  852. return
  853. }
  854. if !api.sendEventStreamHeartbeat(client) {
  855. go api.removeClient(client)
  856. return
  857. }
  858. }
  859. }
  860. func (api *oliveTinAPI) waitEventStreamHeartbeatOrDone(done <-chan struct{}, ticker *time.Ticker) bool {
  861. select {
  862. case <-done:
  863. return true
  864. case <-ticker.C:
  865. return false
  866. }
  867. }
  868. func (api *oliveTinAPI) sendEventStreamHeartbeat(client *streamingClient) bool {
  869. msg := &apiv1.EventStreamResponse{
  870. Event: &apiv1.EventStreamResponse_Heartbeat{
  871. Heartbeat: &apiv1.EventHeartbeat{},
  872. },
  873. }
  874. return api.trySendEventToClient(client, msg)
  875. }
  876. func (api *oliveTinAPI) removeClient(clientToRemove *streamingClient) {
  877. if clientToRemove == nil {
  878. return
  879. }
  880. api.streamingClientsMutex.Lock()
  881. if _, exists := api.streamingClients[clientToRemove]; !exists {
  882. api.streamingClientsMutex.Unlock()
  883. return
  884. }
  885. delete(api.streamingClients, clientToRemove)
  886. api.streamingClientsMutex.Unlock()
  887. clientToRemove.stopHeartbeat()
  888. close(clientToRemove.channel)
  889. }
  890. func (api *oliveTinAPI) OnActionMapRebuilt() {
  891. toRemove := []*streamingClient{}
  892. for _, client := range api.copyOfStreamingClients() {
  893. msg := &apiv1.EventStreamResponse{
  894. Event: &apiv1.EventStreamResponse_ConfigChanged{
  895. ConfigChanged: &apiv1.EventConfigChanged{},
  896. },
  897. }
  898. if !api.trySendEventToClient(client, msg) {
  899. toRemove = append(toRemove, client)
  900. }
  901. }
  902. for _, client := range toRemove {
  903. api.removeClient(client)
  904. }
  905. }
  906. func (api *oliveTinAPI) OnExecutionStarted(ex *executor.InternalLogEntry) {
  907. toRemove := []*streamingClient{}
  908. for _, client := range api.copyOfStreamingClients() {
  909. api.maybeSendExecutionStarted(client, ex, &toRemove)
  910. }
  911. for _, client := range toRemove {
  912. api.removeClient(client)
  913. }
  914. }
  915. func (api *oliveTinAPI) maybeSendExecutionStarted(client *streamingClient, ex *executor.InternalLogEntry, toRemove *[]*streamingClient) {
  916. if client == nil {
  917. return
  918. }
  919. if !api.mayViewExecutionEvent(ex, client.AuthenticatedUser) {
  920. return
  921. }
  922. msg := &apiv1.EventStreamResponse{
  923. Event: &apiv1.EventStreamResponse_ExecutionStarted{
  924. ExecutionStarted: &apiv1.EventExecutionStarted{
  925. LogEntry: api.internalLogEntryToPb(ex, client.AuthenticatedUser),
  926. },
  927. },
  928. }
  929. if !api.trySendEventToClient(client, msg) {
  930. *toRemove = append(*toRemove, client)
  931. }
  932. }
  933. func (api *oliveTinAPI) OnExecutionFinished(ile *executor.InternalLogEntry) {
  934. toRemove := []*streamingClient{}
  935. for _, client := range api.copyOfStreamingClients() {
  936. api.maybeSendExecutionFinished(client, ile, &toRemove)
  937. }
  938. for _, client := range toRemove {
  939. api.removeClient(client)
  940. }
  941. }
  942. func (api *oliveTinAPI) maybeSendExecutionFinished(client *streamingClient, ile *executor.InternalLogEntry, toRemove *[]*streamingClient) {
  943. if client == nil {
  944. return
  945. }
  946. if !api.mayViewExecutionEvent(ile, client.AuthenticatedUser) {
  947. return
  948. }
  949. msg := &apiv1.EventStreamResponse{
  950. Event: &apiv1.EventStreamResponse_ExecutionFinished{
  951. ExecutionFinished: &apiv1.EventExecutionFinished{
  952. LogEntry: api.internalLogEntryToPb(ile, client.AuthenticatedUser),
  953. },
  954. },
  955. }
  956. if !api.trySendEventToClient(client, msg) {
  957. *toRemove = append(*toRemove, client)
  958. }
  959. }
  960. func (api *oliveTinAPI) GetDiagnostics(ctx ctx.Context, req *connect.Request[apiv1.GetDiagnosticsRequest]) (*connect.Response[apiv1.GetDiagnosticsResponse], error) {
  961. user := auth.UserFromApiCall(ctx, req, api.cfg)
  962. if err := api.checkDashboardAccess(user); err != nil {
  963. return nil, err
  964. }
  965. if !user.EffectivePolicy.ShowDiagnostics {
  966. return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("diagnostics are not available for your account"))
  967. }
  968. res := &apiv1.GetDiagnosticsResponse{
  969. SshFoundKey: installationinfo.Runtime.SshFoundKey,
  970. SshFoundConfig: installationinfo.Runtime.SshFoundConfig,
  971. }
  972. return connect.NewResponse(res), nil
  973. }
  974. func (api *oliveTinAPI) Init(ctx ctx.Context, req *connect.Request[apiv1.InitRequest]) (*connect.Response[apiv1.InitResponse], error) {
  975. user := auth.UserFromApiCall(ctx, req, api.cfg)
  976. loginRequired := user.IsGuest() && api.cfg.AuthRequireGuestsToLogin
  977. showVersion := user.EffectivePolicy.ShowVersionNumber
  978. currentVersion := ""
  979. availableVersion := ""
  980. if showVersion {
  981. currentVersion = installationinfo.Build.Version
  982. availableVersion = installationinfo.Runtime.AvailableVersion
  983. }
  984. res := &apiv1.InitResponse{
  985. ShowFooter: api.cfg.ShowFooter,
  986. ShowNavigation: api.cfg.ShowNavigation,
  987. ShowNewVersions: showVersion && api.cfg.ShowNewVersions,
  988. AvailableVersion: availableVersion,
  989. CurrentVersion: currentVersion,
  990. PageTitle: api.cfg.PageTitle,
  991. SectionNavigationStyle: api.cfg.SectionNavigationStyle,
  992. DefaultIconForBack: api.cfg.DefaultIconForBack,
  993. EnableCustomJs: api.cfg.EnableCustomJs,
  994. AuthLoginUrl: api.cfg.AuthLoginUrl,
  995. AuthLocalLogin: api.cfg.AuthLocalUsers.Enabled,
  996. OAuth2Providers: buildPublicOAuth2ProvidersList(api.cfg),
  997. AdditionalLinks: buildAdditionalLinks(api.cfg.AdditionalNavigationLinks),
  998. StyleMods: api.cfg.StyleMods,
  999. RootDashboards: api.buildRootDashboards(user, api.cfg.Dashboards),
  1000. AuthenticatedUser: user.Username,
  1001. AuthenticatedUserProvider: user.Provider,
  1002. EffectivePolicy: buildEffectivePolicy(user.EffectivePolicy),
  1003. BannerMessage: api.cfg.BannerMessage,
  1004. BannerCss: api.cfg.BannerCSS,
  1005. ShowDiagnostics: user.EffectivePolicy.ShowDiagnostics,
  1006. ShowLogList: user.EffectivePolicy.ShowLogList,
  1007. LoginRequired: loginRequired,
  1008. AvailableThemes: discoverAvailableThemes(api.cfg),
  1009. ShowNavigateOnStartIcons: api.cfg.ShowNavigateOnStartIcons,
  1010. }
  1011. return connect.NewResponse(res), nil
  1012. }
  1013. // discoverAvailableThemes finds all available themes in the custom-webui/themes directory.
  1014. // A theme is considered available if it has a theme.css file.
  1015. func discoverAvailableThemes(cfg *config.Config) []string {
  1016. configDir := cfg.GetDir()
  1017. if configDir == "" {
  1018. return []string{}
  1019. }
  1020. themesDir := path.Join(configDir, "custom-webui", "themes")
  1021. entries, err := os.ReadDir(themesDir)
  1022. if err != nil {
  1023. log.WithFields(log.Fields{
  1024. "themesDir": themesDir,
  1025. "error": err,
  1026. }).Tracef("Could not read themes directory")
  1027. return []string{}
  1028. }
  1029. themes := collectValidThemes(themesDir, entries)
  1030. sort.Strings(themes)
  1031. return themes
  1032. }
  1033. // collectValidThemes collects theme names from directory entries that have a theme.css file.
  1034. func collectValidThemes(themesDir string, entries []os.DirEntry) []string {
  1035. var themes []string
  1036. for _, entry := range entries {
  1037. if themeName := getValidThemeName(themesDir, entry); themeName != "" {
  1038. themes = append(themes, themeName)
  1039. }
  1040. }
  1041. return themes
  1042. }
  1043. // getValidThemeName returns the theme name if the entry is a valid theme directory with theme.css, otherwise returns empty string.
  1044. func getValidThemeName(themesDir string, entry os.DirEntry) string {
  1045. if !entry.IsDir() {
  1046. return ""
  1047. }
  1048. themeName := entry.Name()
  1049. themeCssPath := path.Join(themesDir, themeName, "theme.css")
  1050. if _, err := os.Stat(themeCssPath); err != nil {
  1051. return ""
  1052. }
  1053. return themeName
  1054. }
  1055. func (api *oliveTinAPI) buildRootDashboards(user *authpublic.AuthenticatedUser, dashboards []*config.DashboardComponent) []string {
  1056. var rootDashboards []string
  1057. dashboardRenderRequest := api.createDashboardRenderRequest(user, "", "")
  1058. api.addDefaultDashboardIfNeeded(&rootDashboards, dashboardRenderRequest)
  1059. api.addCustomDashboards(&rootDashboards, dashboards, dashboardRenderRequest)
  1060. return rootDashboards
  1061. }
  1062. func (api *oliveTinAPI) addDefaultDashboardIfNeeded(rootDashboards *[]string, rr *DashboardRenderRequest) {
  1063. defaultDashboard := buildDefaultDashboard(rr)
  1064. if defaultDashboard != nil && len(defaultDashboard.Contents) > 0 {
  1065. log.Tracef("defaultDashboard: %+v", defaultDashboard.Contents)
  1066. *rootDashboards = append(*rootDashboards, "Actions")
  1067. }
  1068. }
  1069. func (api *oliveTinAPI) addCustomDashboards(rootDashboards *[]string, dashboards []*config.DashboardComponent, rr *DashboardRenderRequest) {
  1070. for _, dashboard := range dashboards {
  1071. // We have to build the dashboard response instead of just looping over config.dashboards,
  1072. // because we need to check if the user has access to the dashboard
  1073. db := renderDashboard(rr, dashboard.Title)
  1074. if db != nil {
  1075. *rootDashboards = append(*rootDashboards, dashboard.Title)
  1076. }
  1077. }
  1078. }
  1079. func buildPublicOAuth2ProvidersList(cfg *config.Config) []*apiv1.OAuth2Provider {
  1080. var publicProviders []*apiv1.OAuth2Provider
  1081. for providerKey, provider := range cfg.AuthOAuth2Providers {
  1082. publicProviders = append(publicProviders, &apiv1.OAuth2Provider{
  1083. Title: provider.Title,
  1084. Icon: provider.Icon,
  1085. Key: providerKey,
  1086. })
  1087. }
  1088. sort.Slice(publicProviders, func(i, j int) bool {
  1089. return publicProviders[i].Key < publicProviders[j].Key
  1090. })
  1091. return publicProviders
  1092. }
  1093. func buildAdditionalLinks(links []*config.NavigationLink) []*apiv1.AdditionalLink {
  1094. var additionalLinks []*apiv1.AdditionalLink
  1095. for _, link := range links {
  1096. additionalLinks = append(additionalLinks, &apiv1.AdditionalLink{
  1097. Title: link.Title,
  1098. Url: link.Url,
  1099. })
  1100. }
  1101. return additionalLinks
  1102. }
  1103. func (api *oliveTinAPI) OnOutputChunk(content []byte, executionTrackingId string) {
  1104. entry := api.getValidLogEntryForStreaming(executionTrackingId)
  1105. if entry == nil {
  1106. return
  1107. }
  1108. msg := &apiv1.EventStreamResponse{
  1109. Event: &apiv1.EventStreamResponse_OutputChunk{
  1110. OutputChunk: &apiv1.EventOutputChunk{
  1111. Output: string(content),
  1112. ExecutionTrackingId: executionTrackingId,
  1113. },
  1114. },
  1115. }
  1116. toRemove := []*streamingClient{}
  1117. for _, client := range api.copyOfStreamingClients() {
  1118. api.maybeSendOutputChunk(client, entry, msg, &toRemove)
  1119. }
  1120. for _, client := range toRemove {
  1121. api.removeClient(client)
  1122. }
  1123. }
  1124. func (api *oliveTinAPI) getValidLogEntryForStreaming(executionTrackingId string) *executor.InternalLogEntry {
  1125. entry, ok := api.executor.GetLog(executionTrackingId)
  1126. if !ok || !isValidLogEntry(entry) {
  1127. return nil
  1128. }
  1129. return entry
  1130. }
  1131. func (api *oliveTinAPI) maybeSendOutputChunk(client *streamingClient, entry *executor.InternalLogEntry, msg *apiv1.EventStreamResponse, toRemove *[]*streamingClient) {
  1132. if client == nil {
  1133. return
  1134. }
  1135. if !api.mayViewExecutionEvent(entry, client.AuthenticatedUser) {
  1136. return
  1137. }
  1138. if !api.trySendEventToClient(client, msg) {
  1139. *toRemove = append(*toRemove, client)
  1140. }
  1141. }
  1142. func (api *oliveTinAPI) GetEntities(ctx ctx.Context, req *connect.Request[apiv1.GetEntitiesRequest]) (*connect.Response[apiv1.GetEntitiesResponse], error) {
  1143. user := auth.UserFromApiCall(ctx, req, api.cfg)
  1144. if err := api.checkDashboardAccess(user); err != nil {
  1145. return nil, err
  1146. }
  1147. entityMap := entities.GetEntities()
  1148. entityNames := make([]string, 0, len(entityMap))
  1149. for name := range entityMap {
  1150. entityNames = append(entityNames, name)
  1151. }
  1152. sort.Strings(entityNames)
  1153. entityDefinitions := make([]*apiv1.EntityDefinition, 0, len(entityNames))
  1154. for _, name := range entityNames {
  1155. def := &apiv1.EntityDefinition{
  1156. Title: name,
  1157. UsedOnDashboards: findDashboardsForEntity(name, api.cfg.Dashboards),
  1158. Instances: buildSortedEntityInstances(name, entityMap[name]),
  1159. }
  1160. entityDefinitions = append(entityDefinitions, def)
  1161. }
  1162. res := &apiv1.GetEntitiesResponse{
  1163. EntityDefinitions: entityDefinitions,
  1164. }
  1165. return connect.NewResponse(res), nil
  1166. }
  1167. func buildSortedEntityInstances(entityType string, entityInstances map[string]*entities.Entity) []*apiv1.Entity {
  1168. instanceKeys := make([]string, 0, len(entityInstances))
  1169. for key := range entityInstances {
  1170. instanceKeys = append(instanceKeys, key)
  1171. }
  1172. sort.Strings(instanceKeys)
  1173. instances := make([]*apiv1.Entity, 0, len(instanceKeys))
  1174. for _, key := range instanceKeys {
  1175. e := entityInstances[key]
  1176. instances = append(instances, &apiv1.Entity{
  1177. Title: e.Title,
  1178. UniqueKey: e.UniqueKey,
  1179. Type: entityType,
  1180. })
  1181. }
  1182. return instances
  1183. }
  1184. func findDashboardsForEntity(entityTitle string, dashboards []*config.DashboardComponent) []string {
  1185. var foundDashboards []string
  1186. seen := make(map[string]bool)
  1187. findEntityInComponents(entityTitle, "", dashboards, &foundDashboards, seen)
  1188. return foundDashboards
  1189. }
  1190. func findEntityInComponents(entityTitle string, parentTitle string, components []*config.DashboardComponent, foundDashboards *[]string, seen map[string]bool) {
  1191. for _, component := range components {
  1192. if component.Entity == entityTitle {
  1193. addEntityDashboard(component, parentTitle, foundDashboards, seen)
  1194. }
  1195. if len(component.Contents) > 0 {
  1196. findEntityInComponents(entityTitle, component.Title, component.Contents, foundDashboards, seen)
  1197. }
  1198. }
  1199. }
  1200. func addEntityDashboard(component *config.DashboardComponent, parentTitle string, foundDashboards *[]string, seen map[string]bool) {
  1201. if component.Type == "directory" {
  1202. addEntityDirectory(component, foundDashboards, seen)
  1203. } else {
  1204. addParentDashboard(parentTitle, foundDashboards, seen)
  1205. }
  1206. }
  1207. func addEntityDirectory(component *config.DashboardComponent, foundDashboards *[]string, seen map[string]bool) {
  1208. dashboardTitle := component.Title + " [Entity Directory]"
  1209. if !seen[dashboardTitle] {
  1210. *foundDashboards = append(*foundDashboards, dashboardTitle)
  1211. seen[dashboardTitle] = true
  1212. seen[component.Title] = true
  1213. }
  1214. }
  1215. func addParentDashboard(parentTitle string, foundDashboards *[]string, seen map[string]bool) {
  1216. if parentTitle != "" && !seen[parentTitle] {
  1217. *foundDashboards = append(*foundDashboards, parentTitle)
  1218. seen[parentTitle] = true
  1219. }
  1220. }
  1221. func findDirectoriesInEntityFieldsets(entityType string, dashboards []*config.DashboardComponent) []string {
  1222. var directories []string
  1223. for _, dashboard := range dashboards {
  1224. findDirectoriesInEntityFieldsetsRecursive(entityType, dashboard, &directories)
  1225. }
  1226. return directories
  1227. }
  1228. func findDirectoriesInEntityFieldsetsRecursive(entityType string, component *config.DashboardComponent, directories *[]string) {
  1229. if component.Entity == entityType {
  1230. collectDirectoriesFromComponent(component, directories)
  1231. }
  1232. if len(component.Contents) > 0 {
  1233. searchSubcomponentsForDirectories(entityType, component.Contents, directories)
  1234. }
  1235. }
  1236. func collectDirectoriesFromComponent(component *config.DashboardComponent, directories *[]string) {
  1237. for _, subitem := range component.Contents {
  1238. if subitem.Type == "directory" {
  1239. *directories = append(*directories, subitem.Title)
  1240. }
  1241. }
  1242. }
  1243. func searchSubcomponentsForDirectories(entityType string, contents []*config.DashboardComponent, directories *[]string) {
  1244. for _, subitem := range contents {
  1245. findDirectoriesInEntityFieldsetsRecursive(entityType, subitem, directories)
  1246. }
  1247. }
  1248. func (api *oliveTinAPI) GetEntity(ctx ctx.Context, req *connect.Request[apiv1.GetEntityRequest]) (*connect.Response[apiv1.Entity], error) {
  1249. user := auth.UserFromApiCall(ctx, req, api.cfg)
  1250. if err := api.checkDashboardAccess(user); err != nil {
  1251. return nil, err
  1252. }
  1253. instances := entities.GetEntityInstances(req.Msg.Type)
  1254. if len(instances) == 0 {
  1255. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity type %s not found", req.Msg.Type))
  1256. }
  1257. entity, ok := instances[req.Msg.UniqueKey]
  1258. if !ok {
  1259. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity with unique key %s not found in type %s", req.Msg.UniqueKey, req.Msg.Type))
  1260. }
  1261. res := buildEntityResponse(entity, req.Msg.Type, api.cfg.Dashboards)
  1262. return connect.NewResponse(res), nil
  1263. }
  1264. func buildEntityResponse(entity *entities.Entity, entityType string, dashboards []*config.DashboardComponent) *apiv1.Entity {
  1265. res := &apiv1.Entity{
  1266. Title: entity.Title,
  1267. UniqueKey: entity.UniqueKey,
  1268. Type: entityType,
  1269. Directories: findDirectoriesInEntityFieldsets(entityType, dashboards),
  1270. Fields: serializeEntityFields(entity.Data),
  1271. }
  1272. return res
  1273. }
  1274. func serializeEntityFields(data any) map[string]string {
  1275. if data == nil {
  1276. return nil
  1277. }
  1278. dataMap, ok := data.(map[string]any)
  1279. if !ok {
  1280. return nil
  1281. }
  1282. fields := make(map[string]string)
  1283. for k, v := range dataMap {
  1284. fields[k] = fmt.Sprintf("%v", v)
  1285. }
  1286. return fields
  1287. }
  1288. func (api *oliveTinAPI) RestartAction(ctx ctx.Context, req *connect.Request[apiv1.RestartActionRequest]) (*connect.Response[apiv1.StartActionResponse], error) {
  1289. execReqLogEntry, err := api.restartActionLogEntry(req.Msg.ExecutionTrackingId)
  1290. if err != nil {
  1291. return nil, err
  1292. }
  1293. if execReqLogEntry.Binding.Action.Justification {
  1294. return nil, restartRequiresJustificationError()
  1295. }
  1296. authenticatedUser := auth.UserFromApiCall(ctx, req, api.cfg)
  1297. execReq := executor.ExecutionRequest{
  1298. Binding: execReqLogEntry.Binding,
  1299. Arguments: make(map[string]string),
  1300. AuthenticatedUser: authenticatedUser,
  1301. Cfg: api.cfg,
  1302. }
  1303. api.executor.ExecRequest(&execReq)
  1304. return connect.NewResponse(&apiv1.StartActionResponse{
  1305. ExecutionTrackingId: execReq.TrackingID,
  1306. }), nil
  1307. }
  1308. func (api *oliveTinAPI) restartActionLogEntry(executionTrackingId string) (*executor.InternalLogEntry, error) {
  1309. execReqLogEntry, found := api.executor.GetLog(executionTrackingId)
  1310. if !found {
  1311. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found for tracking ID %s", executionTrackingId))
  1312. }
  1313. if execReqLogEntry.Binding == nil {
  1314. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("log entry has no binding for tracking ID %s", executionTrackingId))
  1315. }
  1316. if execReqLogEntry.Binding.Action == nil {
  1317. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action not found for tracking ID %s", executionTrackingId))
  1318. }
  1319. return execReqLogEntry, nil
  1320. }
  1321. var (
  1322. executorListenersMu sync.Mutex
  1323. executorListeners = map[*executor.Executor]*oliveTinAPI{}
  1324. )
  1325. // RegisterExecutorListener registers the API server as an executor listener during startup.
  1326. // Call this before background goroutines that may trigger RebuildActionMap.
  1327. func RegisterExecutorListener(ex *executor.Executor) {
  1328. ensureExecutorListener(ex)
  1329. }
  1330. func ensureExecutorListener(ex *executor.Executor) *oliveTinAPI {
  1331. executorListenersMu.Lock()
  1332. defer executorListenersMu.Unlock()
  1333. if server, ok := executorListeners[ex]; ok {
  1334. return server
  1335. }
  1336. server := newServer(ex)
  1337. executorListeners[ex] = server
  1338. return server
  1339. }
  1340. func newServer(ex *executor.Executor) *oliveTinAPI {
  1341. server := &oliveTinAPI{
  1342. cfg: ex.Cfg,
  1343. executor: ex,
  1344. streamingClients: make(map[*streamingClient]struct{}),
  1345. }
  1346. ex.AddListener(server)
  1347. return server
  1348. }
  1349. func GetNewHandler(ex *executor.Executor) (string, http.Handler) {
  1350. server := ensureExecutorListener(ex)
  1351. jsonOpt := connectproto.WithJSON(
  1352. protojson.MarshalOptions{
  1353. EmitUnpopulated: true, // https://github.com/OliveTin/OliveTin/issues/674
  1354. },
  1355. protojson.UnmarshalOptions{
  1356. DiscardUnknown: true,
  1357. },
  1358. )
  1359. return apiv1connect.NewOliveTinApiServiceHandler(server, jsonOpt)
  1360. }