api.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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 (newest first).
  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. chunk := filtered[int(startIdx):int(endIdx)]
  538. for i := len(chunk) - 1; i >= 0; i-- {
  539. ret.Logs = append(ret.Logs, api.internalLogEntryToPb(chunk[i], user))
  540. }
  541. ret.CountRemaining = page.start
  542. ret.PageSize = page.size
  543. ret.TotalCount = page.total
  544. ret.StartOffset = page.start
  545. return ret
  546. }
  547. func (api *oliveTinAPI) GetActionLogs(ctx ctx.Context, req *connect.Request[apiv1.GetActionLogsRequest]) (*connect.Response[apiv1.GetActionLogsResponse], error) {
  548. user := auth.UserFromApiCall(ctx, req, api.cfg)
  549. if err := api.checkDashboardAccess(user); err != nil {
  550. return nil, err
  551. }
  552. filtered := api.filterLogsByACL(api.executor.GetLogsByBindingId(req.Msg.ActionId), user)
  553. page := paginate(int64(len(filtered)), api.cfg.LogHistoryPageSize, req.Msg.StartOffset)
  554. if page.empty {
  555. return connect.NewResponse(buildEmptyPageResponse(page)), nil
  556. }
  557. return connect.NewResponse(api.buildActionLogsResponse(filtered, page, user)), nil
  558. }
  559. func (api *oliveTinAPI) filterLogsByACL(entries []*executor.InternalLogEntry, user *authpublic.AuthenticatedUser) []*executor.InternalLogEntry {
  560. filtered := make([]*executor.InternalLogEntry, 0, len(entries))
  561. for _, e := range entries {
  562. if !isValidLogEntry(e) {
  563. continue
  564. }
  565. if api.isLogEntryAllowed(e, user) {
  566. filtered = append(filtered, e)
  567. }
  568. }
  569. return filtered
  570. }
  571. type pageInfo struct {
  572. total int64
  573. size int64
  574. start int64
  575. end int64
  576. empty bool
  577. }
  578. func paginate(total int64, size int64, start int64) pageInfo {
  579. if start < 0 {
  580. start = 0
  581. }
  582. if start >= total {
  583. return pageInfo{total: total, size: size, start: start, end: start, empty: true}
  584. }
  585. end := start + size
  586. if end > total {
  587. end = total
  588. }
  589. return pageInfo{total: total, size: size, start: start, end: end, empty: false}
  590. }
  591. /*
  592. This function is ONLY a helper for the UI - the arguments are validated properly
  593. on the StartAction -> Executor chain. This is here basically to provide helpful
  594. error messages more quickly before starting the action.
  595. It uses the same validation logic as the executor, including mangling argument
  596. values (e.g., datetime formatting, checkbox title-to-value conversion).
  597. */
  598. func (api *oliveTinAPI) argumentNotFoundForValidation(msg *apiv1.ValidateArgumentTypeRequest) bool {
  599. if msg.BindingId == "" || msg.ArgumentName == "" {
  600. return false
  601. }
  602. arg, _ := api.findArgumentForValidation(msg.BindingId, msg.ArgumentName)
  603. return arg == nil
  604. }
  605. func (api *oliveTinAPI) ValidateArgumentType(ctx ctx.Context, req *connect.Request[apiv1.ValidateArgumentTypeRequest]) (*connect.Response[apiv1.ValidateArgumentTypeResponse], error) {
  606. if api.argumentNotFoundForValidation(req.Msg) {
  607. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action or argument not found for binding ID %s", req.Msg.BindingId))
  608. }
  609. err := api.validateArgumentTypeInternal(req.Msg)
  610. desc := ""
  611. if err != nil {
  612. desc = err.Error()
  613. }
  614. return connect.NewResponse(&apiv1.ValidateArgumentTypeResponse{
  615. Valid: err == nil,
  616. Description: desc,
  617. }), nil
  618. }
  619. func (api *oliveTinAPI) validateArgumentTypeInternal(msg *apiv1.ValidateArgumentTypeRequest) error {
  620. if msg.BindingId == "" || msg.ArgumentName == "" {
  621. return executor.TypeSafetyCheck("", msg.Value, msg.Type)
  622. }
  623. arg, action := api.findArgumentForValidation(msg.BindingId, msg.ArgumentName)
  624. if arg == nil {
  625. return fmt.Errorf("argument not found")
  626. }
  627. return executor.ValidateArgument(arg, msg.Value, action)
  628. }
  629. func (api *oliveTinAPI) findArgumentForValidation(bindingId string, argumentName string) (*config.ActionArgument, *config.Action) {
  630. binding := api.executor.FindBindingByID(bindingId)
  631. if binding == nil || binding.Action == nil {
  632. return nil, nil
  633. }
  634. arg := api.findArgumentByName(binding.Action, argumentName)
  635. return arg, binding.Action
  636. }
  637. func (api *oliveTinAPI) findArgumentByName(action *config.Action, name string) *config.ActionArgument {
  638. for i := range action.Arguments {
  639. if action.Arguments[i].Name == name {
  640. return &action.Arguments[i]
  641. }
  642. }
  643. return nil
  644. }
  645. func (api *oliveTinAPI) WhoAmI(ctx ctx.Context, req *connect.Request[apiv1.WhoAmIRequest]) (*connect.Response[apiv1.WhoAmIResponse], error) {
  646. user := auth.UserFromApiCall(ctx, req, api.cfg)
  647. if err := api.checkDashboardAccess(user); err != nil {
  648. return nil, err
  649. }
  650. res := &apiv1.WhoAmIResponse{
  651. AuthenticatedUser: user.Username,
  652. Usergroup: user.UsergroupLine,
  653. Provider: user.Provider,
  654. Sid: user.SID,
  655. Acls: user.Acls,
  656. }
  657. return connect.NewResponse(res), nil
  658. }
  659. func (api *oliveTinAPI) SosReport(ctx ctx.Context, req *connect.Request[apiv1.SosReportRequest]) (*connect.Response[apiv1.SosReportResponse], error) {
  660. user := auth.UserFromApiCall(ctx, req, api.cfg)
  661. redactVersion := !user.EffectivePolicy.ShowVersionNumber
  662. sos := installationinfo.GetSosReport(redactVersion)
  663. if !api.cfg.InsecureAllowDumpSos {
  664. log.Info(sos)
  665. 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."
  666. }
  667. ret := &apiv1.SosReportResponse{
  668. Alert: sos,
  669. }
  670. return connect.NewResponse(ret), nil
  671. }
  672. func (api *oliveTinAPI) DumpVars(ctx ctx.Context, req *connect.Request[apiv1.DumpVarsRequest]) (*connect.Response[apiv1.DumpVarsResponse], error) {
  673. res := &apiv1.DumpVarsResponse{}
  674. if !api.cfg.InsecureAllowDumpVars {
  675. res.Alert = "Dumping variables is not allowed by default because it is insecure."
  676. return connect.NewResponse(res), nil
  677. }
  678. jsonstring, err := json.MarshalIndent(tpl.GetNewGeneralTemplateContext(), "", " ")
  679. if err != nil {
  680. log.WithError(err).Error("DumpVars: failed to marshal template context from GetNewGeneralTemplateContext")
  681. return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("dump vars: marshal template context: %w", err))
  682. }
  683. fmt.Printf("%s", jsonstring)
  684. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpVars = false again after you don't need it anymore"
  685. return connect.NewResponse(res), nil
  686. }
  687. func debugBindingActionTitle(binding *executor.ActionBinding) string {
  688. if binding == nil || binding.Action == nil {
  689. return ""
  690. }
  691. return binding.Action.Title
  692. }
  693. func (api *oliveTinAPI) DumpPublicIdActionMap(ctx ctx.Context, req *connect.Request[apiv1.DumpPublicIdActionMapRequest]) (*connect.Response[apiv1.DumpPublicIdActionMapResponse], error) {
  694. res := &apiv1.DumpPublicIdActionMapResponse{}
  695. res.Contents = make(map[string]*apiv1.DebugBinding)
  696. if !api.cfg.InsecureAllowDumpActionMap {
  697. res.Alert = "Dumping Public IDs is disallowed."
  698. return connect.NewResponse(res), nil
  699. }
  700. api.executor.MapActionBindingsLock.RLock()
  701. for k, v := range api.executor.MapActionBindings {
  702. res.Contents[k] = &apiv1.DebugBinding{
  703. ActionTitle: debugBindingActionTitle(v),
  704. }
  705. }
  706. api.executor.MapActionBindingsLock.RUnlock()
  707. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpActionMap = false again after you don't need it anymore"
  708. return connect.NewResponse(res), nil
  709. }
  710. func (api *oliveTinAPI) GetReadyz(ctx ctx.Context, req *connect.Request[apiv1.GetReadyzRequest]) (*connect.Response[apiv1.GetReadyzResponse], error) {
  711. res := &apiv1.GetReadyzResponse{
  712. Status: "OK",
  713. }
  714. return connect.NewResponse(res), nil
  715. }
  716. func (api *oliveTinAPI) EventStream(ctx ctx.Context, req *connect.Request[apiv1.EventStreamRequest], srv *connect.ServerStream[apiv1.EventStreamResponse]) error {
  717. log.Debugf("EventStream: %v", req.Msg)
  718. // Set X-Accel-Buffering header to disable nginx buffering for this stream
  719. // https://github.com/OliveTin/OliveTin/issues/765
  720. srv.ResponseHeader().Set("X-Accel-Buffering", "no")
  721. user := auth.UserFromApiCall(ctx, req, api.cfg)
  722. if err := api.checkDashboardAccess(user); err != nil {
  723. return err
  724. }
  725. client := &streamingClient{
  726. channel: make(chan *apiv1.EventStreamResponse, 10), // Buffered channel to hold Events
  727. AuthenticatedUser: user,
  728. }
  729. log.WithFields(log.Fields{
  730. "authenticatedUser": user.Username,
  731. }).Debugf("EventStream: client connected")
  732. api.streamingClientsMutex.Lock()
  733. api.streamingClients[client] = struct{}{}
  734. api.streamingClientsMutex.Unlock()
  735. // loop over client channel and send events to connectedClient
  736. for msg := range client.channel {
  737. log.Debugf("Sending event to client: %v", msg)
  738. if err := srv.Send(msg); err != nil {
  739. log.Errorf("Error sending event to client: %v", err)
  740. // Remove disconnected client from the list
  741. api.removeClient(client)
  742. break
  743. }
  744. }
  745. log.Infof("EventStream: client disconnected")
  746. return nil
  747. }
  748. func (api *oliveTinAPI) removeClient(clientToRemove *streamingClient) {
  749. if clientToRemove == nil {
  750. return
  751. }
  752. api.streamingClientsMutex.Lock()
  753. delete(api.streamingClients, clientToRemove)
  754. api.streamingClientsMutex.Unlock()
  755. close(clientToRemove.channel)
  756. }
  757. func (api *oliveTinAPI) OnActionMapRebuilt() {
  758. toRemove := []*streamingClient{}
  759. for _, client := range api.copyOfStreamingClients() {
  760. select {
  761. case client.channel <- &apiv1.EventStreamResponse{
  762. Event: &apiv1.EventStreamResponse_ConfigChanged{
  763. ConfigChanged: &apiv1.EventConfigChanged{},
  764. },
  765. }:
  766. default:
  767. log.Warnf("EventStream: client channel is full, removing client")
  768. toRemove = append(toRemove, client)
  769. }
  770. }
  771. for _, client := range toRemove {
  772. api.removeClient(client)
  773. }
  774. }
  775. func (api *oliveTinAPI) OnExecutionStarted(ex *executor.InternalLogEntry) {
  776. toRemove := []*streamingClient{}
  777. for _, client := range api.copyOfStreamingClients() {
  778. api.maybeSendExecutionStarted(client, ex, &toRemove)
  779. }
  780. for _, client := range toRemove {
  781. api.removeClient(client)
  782. }
  783. }
  784. func (api *oliveTinAPI) maybeSendExecutionStarted(client *streamingClient, ex *executor.InternalLogEntry, toRemove *[]*streamingClient) {
  785. if client == nil {
  786. return
  787. }
  788. if !api.mayViewExecutionEvent(ex, client.AuthenticatedUser) {
  789. return
  790. }
  791. msg := &apiv1.EventStreamResponse{
  792. Event: &apiv1.EventStreamResponse_ExecutionStarted{
  793. ExecutionStarted: &apiv1.EventExecutionStarted{
  794. LogEntry: api.internalLogEntryToPb(ex, client.AuthenticatedUser),
  795. },
  796. },
  797. }
  798. if !api.trySendEventToClient(client, msg) {
  799. *toRemove = append(*toRemove, client)
  800. }
  801. }
  802. func (api *oliveTinAPI) OnExecutionFinished(ile *executor.InternalLogEntry) {
  803. toRemove := []*streamingClient{}
  804. for _, client := range api.copyOfStreamingClients() {
  805. api.maybeSendExecutionFinished(client, ile, &toRemove)
  806. }
  807. for _, client := range toRemove {
  808. api.removeClient(client)
  809. }
  810. }
  811. func (api *oliveTinAPI) maybeSendExecutionFinished(client *streamingClient, ile *executor.InternalLogEntry, toRemove *[]*streamingClient) {
  812. if client == nil {
  813. return
  814. }
  815. if !api.mayViewExecutionEvent(ile, client.AuthenticatedUser) {
  816. return
  817. }
  818. msg := &apiv1.EventStreamResponse{
  819. Event: &apiv1.EventStreamResponse_ExecutionFinished{
  820. ExecutionFinished: &apiv1.EventExecutionFinished{
  821. LogEntry: api.internalLogEntryToPb(ile, client.AuthenticatedUser),
  822. },
  823. },
  824. }
  825. if !api.trySendEventToClient(client, msg) {
  826. *toRemove = append(*toRemove, client)
  827. }
  828. }
  829. func (api *oliveTinAPI) GetDiagnostics(ctx ctx.Context, req *connect.Request[apiv1.GetDiagnosticsRequest]) (*connect.Response[apiv1.GetDiagnosticsResponse], error) {
  830. user := auth.UserFromApiCall(ctx, req, api.cfg)
  831. if err := api.checkDashboardAccess(user); err != nil {
  832. return nil, err
  833. }
  834. if !user.EffectivePolicy.ShowDiagnostics {
  835. return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("diagnostics are not available for your account"))
  836. }
  837. res := &apiv1.GetDiagnosticsResponse{
  838. SshFoundKey: installationinfo.Runtime.SshFoundKey,
  839. SshFoundConfig: installationinfo.Runtime.SshFoundConfig,
  840. }
  841. return connect.NewResponse(res), nil
  842. }
  843. func (api *oliveTinAPI) Init(ctx ctx.Context, req *connect.Request[apiv1.InitRequest]) (*connect.Response[apiv1.InitResponse], error) {
  844. user := auth.UserFromApiCall(ctx, req, api.cfg)
  845. loginRequired := user.IsGuest() && api.cfg.AuthRequireGuestsToLogin
  846. showVersion := user.EffectivePolicy.ShowVersionNumber
  847. currentVersion := ""
  848. availableVersion := ""
  849. if showVersion {
  850. currentVersion = installationinfo.Build.Version
  851. availableVersion = installationinfo.Runtime.AvailableVersion
  852. }
  853. res := &apiv1.InitResponse{
  854. ShowFooter: api.cfg.ShowFooter,
  855. ShowNavigation: api.cfg.ShowNavigation,
  856. ShowNewVersions: showVersion && api.cfg.ShowNewVersions,
  857. AvailableVersion: availableVersion,
  858. CurrentVersion: currentVersion,
  859. PageTitle: api.cfg.PageTitle,
  860. SectionNavigationStyle: api.cfg.SectionNavigationStyle,
  861. DefaultIconForBack: api.cfg.DefaultIconForBack,
  862. EnableCustomJs: api.cfg.EnableCustomJs,
  863. AuthLoginUrl: api.cfg.AuthLoginUrl,
  864. AuthLocalLogin: api.cfg.AuthLocalUsers.Enabled,
  865. OAuth2Providers: buildPublicOAuth2ProvidersList(api.cfg),
  866. AdditionalLinks: buildAdditionalLinks(api.cfg.AdditionalNavigationLinks),
  867. StyleMods: api.cfg.StyleMods,
  868. RootDashboards: api.buildRootDashboards(user, api.cfg.Dashboards),
  869. AuthenticatedUser: user.Username,
  870. AuthenticatedUserProvider: user.Provider,
  871. EffectivePolicy: buildEffectivePolicy(user.EffectivePolicy),
  872. BannerMessage: api.cfg.BannerMessage,
  873. BannerCss: api.cfg.BannerCSS,
  874. ShowDiagnostics: user.EffectivePolicy.ShowDiagnostics,
  875. ShowLogList: user.EffectivePolicy.ShowLogList,
  876. LoginRequired: loginRequired,
  877. AvailableThemes: discoverAvailableThemes(api.cfg),
  878. ShowNavigateOnStartIcons: api.cfg.ShowNavigateOnStartIcons,
  879. }
  880. return connect.NewResponse(res), nil
  881. }
  882. // discoverAvailableThemes finds all available themes in the custom-webui/themes directory.
  883. // A theme is considered available if it has a theme.css file.
  884. func discoverAvailableThemes(cfg *config.Config) []string {
  885. configDir := cfg.GetDir()
  886. if configDir == "" {
  887. return []string{}
  888. }
  889. themesDir := path.Join(configDir, "custom-webui", "themes")
  890. entries, err := os.ReadDir(themesDir)
  891. if err != nil {
  892. log.WithFields(log.Fields{
  893. "themesDir": themesDir,
  894. "error": err,
  895. }).Tracef("Could not read themes directory")
  896. return []string{}
  897. }
  898. themes := collectValidThemes(themesDir, entries)
  899. sort.Strings(themes)
  900. return themes
  901. }
  902. // collectValidThemes collects theme names from directory entries that have a theme.css file.
  903. func collectValidThemes(themesDir string, entries []os.DirEntry) []string {
  904. var themes []string
  905. for _, entry := range entries {
  906. if themeName := getValidThemeName(themesDir, entry); themeName != "" {
  907. themes = append(themes, themeName)
  908. }
  909. }
  910. return themes
  911. }
  912. // getValidThemeName returns the theme name if the entry is a valid theme directory with theme.css, otherwise returns empty string.
  913. func getValidThemeName(themesDir string, entry os.DirEntry) string {
  914. if !entry.IsDir() {
  915. return ""
  916. }
  917. themeName := entry.Name()
  918. themeCssPath := path.Join(themesDir, themeName, "theme.css")
  919. if _, err := os.Stat(themeCssPath); err != nil {
  920. return ""
  921. }
  922. return themeName
  923. }
  924. func (api *oliveTinAPI) buildRootDashboards(user *authpublic.AuthenticatedUser, dashboards []*config.DashboardComponent) []string {
  925. var rootDashboards []string
  926. dashboardRenderRequest := api.createDashboardRenderRequest(user, "", "")
  927. api.addDefaultDashboardIfNeeded(&rootDashboards, dashboardRenderRequest)
  928. api.addCustomDashboards(&rootDashboards, dashboards, dashboardRenderRequest)
  929. return rootDashboards
  930. }
  931. func (api *oliveTinAPI) addDefaultDashboardIfNeeded(rootDashboards *[]string, rr *DashboardRenderRequest) {
  932. defaultDashboard := buildDefaultDashboard(rr)
  933. if defaultDashboard != nil && len(defaultDashboard.Contents) > 0 {
  934. log.Tracef("defaultDashboard: %+v", defaultDashboard.Contents)
  935. *rootDashboards = append(*rootDashboards, "Actions")
  936. }
  937. }
  938. func (api *oliveTinAPI) addCustomDashboards(rootDashboards *[]string, dashboards []*config.DashboardComponent, rr *DashboardRenderRequest) {
  939. for _, dashboard := range dashboards {
  940. // We have to build the dashboard response instead of just looping over config.dashboards,
  941. // because we need to check if the user has access to the dashboard
  942. db := renderDashboard(rr, dashboard.Title)
  943. if db != nil {
  944. *rootDashboards = append(*rootDashboards, dashboard.Title)
  945. }
  946. }
  947. }
  948. func buildPublicOAuth2ProvidersList(cfg *config.Config) []*apiv1.OAuth2Provider {
  949. var publicProviders []*apiv1.OAuth2Provider
  950. for providerKey, provider := range cfg.AuthOAuth2Providers {
  951. publicProviders = append(publicProviders, &apiv1.OAuth2Provider{
  952. Title: provider.Title,
  953. Icon: provider.Icon,
  954. Key: providerKey,
  955. })
  956. }
  957. sort.Slice(publicProviders, func(i, j int) bool {
  958. return publicProviders[i].Key < publicProviders[j].Key
  959. })
  960. return publicProviders
  961. }
  962. func buildAdditionalLinks(links []*config.NavigationLink) []*apiv1.AdditionalLink {
  963. var additionalLinks []*apiv1.AdditionalLink
  964. for _, link := range links {
  965. additionalLinks = append(additionalLinks, &apiv1.AdditionalLink{
  966. Title: link.Title,
  967. Url: link.Url,
  968. })
  969. }
  970. return additionalLinks
  971. }
  972. func (api *oliveTinAPI) OnOutputChunk(content []byte, executionTrackingId string) {
  973. entry := api.getValidLogEntryForStreaming(executionTrackingId)
  974. if entry == nil {
  975. return
  976. }
  977. msg := &apiv1.EventStreamResponse{
  978. Event: &apiv1.EventStreamResponse_OutputChunk{
  979. OutputChunk: &apiv1.EventOutputChunk{
  980. Output: string(content),
  981. ExecutionTrackingId: executionTrackingId,
  982. },
  983. },
  984. }
  985. toRemove := []*streamingClient{}
  986. for _, client := range api.copyOfStreamingClients() {
  987. api.maybeSendOutputChunk(client, entry, msg, &toRemove)
  988. }
  989. for _, client := range toRemove {
  990. api.removeClient(client)
  991. }
  992. }
  993. func (api *oliveTinAPI) getValidLogEntryForStreaming(executionTrackingId string) *executor.InternalLogEntry {
  994. entry, ok := api.executor.GetLog(executionTrackingId)
  995. if !ok || !isValidLogEntry(entry) {
  996. return nil
  997. }
  998. return entry
  999. }
  1000. func (api *oliveTinAPI) maybeSendOutputChunk(client *streamingClient, entry *executor.InternalLogEntry, msg *apiv1.EventStreamResponse, toRemove *[]*streamingClient) {
  1001. if client == nil {
  1002. return
  1003. }
  1004. if !api.mayViewExecutionEvent(entry, client.AuthenticatedUser) {
  1005. return
  1006. }
  1007. if !api.trySendEventToClient(client, msg) {
  1008. *toRemove = append(*toRemove, client)
  1009. }
  1010. }
  1011. func (api *oliveTinAPI) GetEntities(ctx ctx.Context, req *connect.Request[apiv1.GetEntitiesRequest]) (*connect.Response[apiv1.GetEntitiesResponse], error) {
  1012. user := auth.UserFromApiCall(ctx, req, api.cfg)
  1013. if err := api.checkDashboardAccess(user); err != nil {
  1014. return nil, err
  1015. }
  1016. entityMap := entities.GetEntities()
  1017. entityNames := make([]string, 0, len(entityMap))
  1018. for name := range entityMap {
  1019. entityNames = append(entityNames, name)
  1020. }
  1021. sort.Strings(entityNames)
  1022. entityDefinitions := make([]*apiv1.EntityDefinition, 0, len(entityNames))
  1023. for _, name := range entityNames {
  1024. def := &apiv1.EntityDefinition{
  1025. Title: name,
  1026. UsedOnDashboards: findDashboardsForEntity(name, api.cfg.Dashboards),
  1027. Instances: buildSortedEntityInstances(name, entityMap[name]),
  1028. }
  1029. entityDefinitions = append(entityDefinitions, def)
  1030. }
  1031. res := &apiv1.GetEntitiesResponse{
  1032. EntityDefinitions: entityDefinitions,
  1033. }
  1034. return connect.NewResponse(res), nil
  1035. }
  1036. func buildSortedEntityInstances(entityType string, entityInstances map[string]*entities.Entity) []*apiv1.Entity {
  1037. instanceKeys := make([]string, 0, len(entityInstances))
  1038. for key := range entityInstances {
  1039. instanceKeys = append(instanceKeys, key)
  1040. }
  1041. sort.Strings(instanceKeys)
  1042. instances := make([]*apiv1.Entity, 0, len(instanceKeys))
  1043. for _, key := range instanceKeys {
  1044. e := entityInstances[key]
  1045. instances = append(instances, &apiv1.Entity{
  1046. Title: e.Title,
  1047. UniqueKey: e.UniqueKey,
  1048. Type: entityType,
  1049. })
  1050. }
  1051. return instances
  1052. }
  1053. func findDashboardsForEntity(entityTitle string, dashboards []*config.DashboardComponent) []string {
  1054. var foundDashboards []string
  1055. seen := make(map[string]bool)
  1056. findEntityInComponents(entityTitle, "", dashboards, &foundDashboards, seen)
  1057. return foundDashboards
  1058. }
  1059. func findEntityInComponents(entityTitle string, parentTitle string, components []*config.DashboardComponent, foundDashboards *[]string, seen map[string]bool) {
  1060. for _, component := range components {
  1061. if component.Entity == entityTitle {
  1062. addEntityDashboard(component, parentTitle, foundDashboards, seen)
  1063. }
  1064. if len(component.Contents) > 0 {
  1065. findEntityInComponents(entityTitle, component.Title, component.Contents, foundDashboards, seen)
  1066. }
  1067. }
  1068. }
  1069. func addEntityDashboard(component *config.DashboardComponent, parentTitle string, foundDashboards *[]string, seen map[string]bool) {
  1070. if component.Type == "directory" {
  1071. addEntityDirectory(component, foundDashboards, seen)
  1072. } else {
  1073. addParentDashboard(parentTitle, foundDashboards, seen)
  1074. }
  1075. }
  1076. func addEntityDirectory(component *config.DashboardComponent, foundDashboards *[]string, seen map[string]bool) {
  1077. dashboardTitle := component.Title + " [Entity Directory]"
  1078. if !seen[dashboardTitle] {
  1079. *foundDashboards = append(*foundDashboards, dashboardTitle)
  1080. seen[dashboardTitle] = true
  1081. seen[component.Title] = true
  1082. }
  1083. }
  1084. func addParentDashboard(parentTitle string, foundDashboards *[]string, seen map[string]bool) {
  1085. if parentTitle != "" && !seen[parentTitle] {
  1086. *foundDashboards = append(*foundDashboards, parentTitle)
  1087. seen[parentTitle] = true
  1088. }
  1089. }
  1090. func findDirectoriesInEntityFieldsets(entityType string, dashboards []*config.DashboardComponent) []string {
  1091. var directories []string
  1092. for _, dashboard := range dashboards {
  1093. findDirectoriesInEntityFieldsetsRecursive(entityType, dashboard, &directories)
  1094. }
  1095. return directories
  1096. }
  1097. func findDirectoriesInEntityFieldsetsRecursive(entityType string, component *config.DashboardComponent, directories *[]string) {
  1098. if component.Entity == entityType {
  1099. collectDirectoriesFromComponent(component, directories)
  1100. }
  1101. if len(component.Contents) > 0 {
  1102. searchSubcomponentsForDirectories(entityType, component.Contents, directories)
  1103. }
  1104. }
  1105. func collectDirectoriesFromComponent(component *config.DashboardComponent, directories *[]string) {
  1106. for _, subitem := range component.Contents {
  1107. if subitem.Type == "directory" {
  1108. *directories = append(*directories, subitem.Title)
  1109. }
  1110. }
  1111. }
  1112. func searchSubcomponentsForDirectories(entityType string, contents []*config.DashboardComponent, directories *[]string) {
  1113. for _, subitem := range contents {
  1114. findDirectoriesInEntityFieldsetsRecursive(entityType, subitem, directories)
  1115. }
  1116. }
  1117. func (api *oliveTinAPI) GetEntity(ctx ctx.Context, req *connect.Request[apiv1.GetEntityRequest]) (*connect.Response[apiv1.Entity], error) {
  1118. user := auth.UserFromApiCall(ctx, req, api.cfg)
  1119. if err := api.checkDashboardAccess(user); err != nil {
  1120. return nil, err
  1121. }
  1122. instances := entities.GetEntityInstances(req.Msg.Type)
  1123. if len(instances) == 0 {
  1124. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity type %s not found", req.Msg.Type))
  1125. }
  1126. entity, ok := instances[req.Msg.UniqueKey]
  1127. if !ok {
  1128. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity with unique key %s not found in type %s", req.Msg.UniqueKey, req.Msg.Type))
  1129. }
  1130. res := buildEntityResponse(entity, req.Msg.Type, api.cfg.Dashboards)
  1131. return connect.NewResponse(res), nil
  1132. }
  1133. func buildEntityResponse(entity *entities.Entity, entityType string, dashboards []*config.DashboardComponent) *apiv1.Entity {
  1134. res := &apiv1.Entity{
  1135. Title: entity.Title,
  1136. UniqueKey: entity.UniqueKey,
  1137. Type: entityType,
  1138. Directories: findDirectoriesInEntityFieldsets(entityType, dashboards),
  1139. Fields: serializeEntityFields(entity.Data),
  1140. }
  1141. return res
  1142. }
  1143. func serializeEntityFields(data any) map[string]string {
  1144. if data == nil {
  1145. return nil
  1146. }
  1147. dataMap, ok := data.(map[string]any)
  1148. if !ok {
  1149. return nil
  1150. }
  1151. fields := make(map[string]string)
  1152. for k, v := range dataMap {
  1153. fields[k] = fmt.Sprintf("%v", v)
  1154. }
  1155. return fields
  1156. }
  1157. func (api *oliveTinAPI) RestartAction(ctx ctx.Context, req *connect.Request[apiv1.RestartActionRequest]) (*connect.Response[apiv1.StartActionResponse], error) {
  1158. ret := &apiv1.StartActionResponse{
  1159. ExecutionTrackingId: req.Msg.ExecutionTrackingId,
  1160. }
  1161. execReqLogEntry, found := api.executor.GetLog(req.Msg.ExecutionTrackingId)
  1162. if !found {
  1163. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found for tracking ID %s", req.Msg.ExecutionTrackingId))
  1164. }
  1165. if execReqLogEntry.Binding == nil {
  1166. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("log entry has no binding for tracking ID %s", req.Msg.ExecutionTrackingId))
  1167. }
  1168. action := execReqLogEntry.Binding.Action
  1169. if action == nil {
  1170. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action not found for tracking ID %s", req.Msg.ExecutionTrackingId))
  1171. }
  1172. authenticatedUser := auth.UserFromApiCall(ctx, req, api.cfg)
  1173. // TrackingID is deliberately not passed to the executor, so that it generates a new one for the restarted execution.
  1174. // This is because the old execution (identified by the old TrackingID) is already used.
  1175. execReq := executor.ExecutionRequest{
  1176. Binding: execReqLogEntry.Binding,
  1177. Arguments: make(map[string]string),
  1178. AuthenticatedUser: authenticatedUser,
  1179. Cfg: api.cfg,
  1180. }
  1181. api.executor.ExecRequest(&execReq)
  1182. ret.ExecutionTrackingId = execReq.TrackingID
  1183. return connect.NewResponse(ret), nil
  1184. }
  1185. func newServer(ex *executor.Executor) *oliveTinAPI {
  1186. server := oliveTinAPI{}
  1187. server.cfg = ex.Cfg
  1188. server.executor = ex
  1189. server.streamingClients = make(map[*streamingClient]struct{})
  1190. ex.AddListener(&server)
  1191. return &server
  1192. }
  1193. func GetNewHandler(ex *executor.Executor) (string, http.Handler) {
  1194. server := newServer(ex)
  1195. jsonOpt := connectproto.WithJSON(
  1196. protojson.MarshalOptions{
  1197. EmitUnpopulated: true, // https://github.com/OliveTin/OliveTin/issues/674
  1198. },
  1199. protojson.UnmarshalOptions{
  1200. DiscardUnknown: true,
  1201. },
  1202. )
  1203. return apiv1connect.NewOliveTinApiServiceHandler(server, jsonOpt)
  1204. }