api.go 48 KB

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