api.go 64 KB

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