api.go 47 KB

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