api.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. package api
  2. import (
  3. ctx "context"
  4. "encoding/json"
  5. "sort"
  6. "connectrpc.com/connect"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  9. apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
  10. "github.com/google/uuid"
  11. log "github.com/sirupsen/logrus"
  12. "fmt"
  13. "net/http"
  14. "sync"
  15. acl "github.com/OliveTin/OliveTin/internal/acl"
  16. auth "github.com/OliveTin/OliveTin/internal/auth"
  17. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  18. config "github.com/OliveTin/OliveTin/internal/config"
  19. entities "github.com/OliveTin/OliveTin/internal/entities"
  20. executor "github.com/OliveTin/OliveTin/internal/executor"
  21. installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
  22. connectproto "go.akshayshah.org/connectproto"
  23. )
  24. type oliveTinAPI struct {
  25. executor *executor.Executor
  26. cfg *config.Config
  27. // streamingClients is a set of currently connected clients.
  28. // The empty struct value models set semantics (keys only) and keeps add/remove O(1).
  29. // We use a map for efficient membership and deletion; ordering is not required.
  30. streamingClients map[*streamingClient]struct{}
  31. streamingClientsMutex sync.RWMutex
  32. }
  33. // This is used to avoid race conditions when iterating over the connectedClients map.
  34. // and holds the lock for as minimal time as possible to avoid blocking the API for too long.
  35. func (api *oliveTinAPI) copyOfStreamingClients() []*streamingClient {
  36. api.streamingClientsMutex.RLock()
  37. defer api.streamingClientsMutex.RUnlock()
  38. clients := make([]*streamingClient, 0, len(api.streamingClients))
  39. for client := range api.streamingClients {
  40. clients = append(clients, client)
  41. }
  42. return clients
  43. }
  44. type streamingClient struct {
  45. channel chan *apiv1.EventStreamResponse
  46. AuthenticatedUser *authpublic.AuthenticatedUser
  47. }
  48. func (api *oliveTinAPI) KillAction(ctx ctx.Context, req *connect.Request[apiv1.KillActionRequest]) (*connect.Response[apiv1.KillActionResponse], error) {
  49. ret := &apiv1.KillActionResponse{
  50. ExecutionTrackingId: req.Msg.ExecutionTrackingId,
  51. }
  52. var execReqLogEntry *executor.InternalLogEntry
  53. execReqLogEntry, ret.Found = api.executor.GetLog(req.Msg.ExecutionTrackingId)
  54. if !ret.Found {
  55. log.Warnf("Killing execution request not possible - not found by tracking ID: %v", req.Msg.ExecutionTrackingId)
  56. return connect.NewResponse(ret), nil
  57. }
  58. log.Warnf("Killing execution request by tracking ID: %v", req.Msg.ExecutionTrackingId)
  59. action := execReqLogEntry.Binding.Action
  60. if action == nil {
  61. log.Warnf("Killing execution request not possible - action not found: %v", execReqLogEntry.ActionTitle)
  62. ret.Killed = false
  63. return connect.NewResponse(ret), nil
  64. }
  65. user := auth.UserFromApiCall(ctx, req, api.cfg)
  66. api.killActionByTrackingId(user, action, execReqLogEntry, ret)
  67. return connect.NewResponse(ret), nil
  68. }
  69. func (api *oliveTinAPI) killActionByTrackingId(user *authpublic.AuthenticatedUser, action *config.Action, execReqLogEntry *executor.InternalLogEntry, ret *apiv1.KillActionResponse) {
  70. if !acl.IsAllowedKill(api.cfg, user, action) {
  71. log.Warnf("Killing execution request not possible - user not allowed to kill this action: %v", execReqLogEntry.ExecutionTrackingID)
  72. ret.Killed = false
  73. return
  74. }
  75. err := api.executor.Kill(execReqLogEntry)
  76. if err != nil {
  77. log.Warnf("Killing execution request err: %v", err)
  78. ret.AlreadyCompleted = true
  79. ret.Killed = false
  80. } else {
  81. ret.Killed = true
  82. }
  83. }
  84. func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *connect.Request[apiv1.StartActionRequest]) (*connect.Response[apiv1.StartActionResponse], error) {
  85. args := make(map[string]string)
  86. for _, arg := range req.Msg.Arguments {
  87. args[arg.Name] = arg.Value
  88. }
  89. pair := api.executor.FindBindingByID(req.Msg.BindingId)
  90. if pair == nil || pair.Action == nil {
  91. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", req.Msg.BindingId))
  92. }
  93. authenticatedUser := auth.UserFromApiCall(ctx, req, api.cfg)
  94. execReq := executor.ExecutionRequest{
  95. Binding: pair,
  96. TrackingID: req.Msg.UniqueTrackingId,
  97. Arguments: args,
  98. AuthenticatedUser: authenticatedUser,
  99. Cfg: api.cfg,
  100. }
  101. api.executor.ExecRequest(&execReq)
  102. ret := &apiv1.StartActionResponse{
  103. ExecutionTrackingId: execReq.TrackingID,
  104. }
  105. return connect.NewResponse(ret), nil
  106. }
  107. func (api *oliveTinAPI) PasswordHash(ctx ctx.Context, req *connect.Request[apiv1.PasswordHashRequest]) (*connect.Response[apiv1.PasswordHashResponse], error) {
  108. hash, err := createHash(req.Msg.Password)
  109. if err != nil {
  110. return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("error creating hash: %w", err))
  111. }
  112. ret := &apiv1.PasswordHashResponse{
  113. Hash: hash,
  114. }
  115. return connect.NewResponse(ret), nil
  116. }
  117. func (api *oliveTinAPI) LocalUserLogin(ctx ctx.Context, req *connect.Request[apiv1.LocalUserLoginRequest]) (*connect.Response[apiv1.LocalUserLoginResponse], error) {
  118. // Check if local user authentication is enabled
  119. if !api.cfg.AuthLocalUsers.Enabled {
  120. return connect.NewResponse(&apiv1.LocalUserLoginResponse{
  121. Success: false,
  122. }), nil
  123. }
  124. match := checkUserPassword(api.cfg, req.Msg.Username, req.Msg.Password)
  125. response := connect.NewResponse(&apiv1.LocalUserLoginResponse{
  126. Success: match,
  127. })
  128. if match {
  129. // Set authentication cookie for successful login
  130. user := api.cfg.FindUserByUsername(req.Msg.Username)
  131. if user != nil {
  132. sid := uuid.NewString()
  133. // Register the session in the session storage
  134. auth.RegisterUserSession(api.cfg, "local", sid, user.Username)
  135. log.WithFields(log.Fields{
  136. "username": user.Username,
  137. }).Info("LocalUserLogin: Session created and registered")
  138. // Set the authentication cookie in the response headers
  139. cookie := &http.Cookie{
  140. Name: "olivetin-sid-local",
  141. Value: sid,
  142. MaxAge: 31556952, // 1 year
  143. HttpOnly: true,
  144. Path: "/",
  145. }
  146. response.Header().Set("Set-Cookie", cookie.String())
  147. }
  148. log.WithFields(log.Fields{
  149. "username": req.Msg.Username,
  150. }).Info("LocalUserLogin: User logged in successfully.")
  151. } else {
  152. log.WithFields(log.Fields{
  153. "username": req.Msg.Username,
  154. }).Warn("LocalUserLogin: User login failed.")
  155. }
  156. return response, nil
  157. }
  158. func (api *oliveTinAPI) StartActionAndWait(ctx ctx.Context, req *connect.Request[apiv1.StartActionAndWaitRequest]) (*connect.Response[apiv1.StartActionAndWaitResponse], error) {
  159. args := make(map[string]string)
  160. for _, arg := range req.Msg.Arguments {
  161. args[arg.Name] = arg.Value
  162. }
  163. user := auth.UserFromApiCall(ctx, req, api.cfg)
  164. execReq := executor.ExecutionRequest{
  165. Binding: api.executor.FindBindingByID(req.Msg.ActionId),
  166. TrackingID: uuid.NewString(),
  167. Arguments: args,
  168. AuthenticatedUser: user,
  169. Cfg: api.cfg,
  170. }
  171. wg, _ := api.executor.ExecRequest(&execReq)
  172. wg.Wait()
  173. internalLogEntry, ok := api.executor.GetLog(execReq.TrackingID)
  174. if ok {
  175. return connect.NewResponse(&apiv1.StartActionAndWaitResponse{
  176. LogEntry: api.internalLogEntryToPb(internalLogEntry, user),
  177. }), nil
  178. } else {
  179. return nil, fmt.Errorf("execution not found")
  180. }
  181. }
  182. func (api *oliveTinAPI) StartActionByGet(ctx ctx.Context, req *connect.Request[apiv1.StartActionByGetRequest]) (*connect.Response[apiv1.StartActionByGetResponse], error) {
  183. args := make(map[string]string)
  184. execReq := executor.ExecutionRequest{
  185. Binding: api.executor.FindBindingByID(req.Msg.ActionId),
  186. TrackingID: uuid.NewString(),
  187. Arguments: args,
  188. AuthenticatedUser: auth.UserFromApiCall(ctx, req, api.cfg),
  189. Cfg: api.cfg,
  190. }
  191. _, uniqueTrackingId := api.executor.ExecRequest(&execReq)
  192. return connect.NewResponse(&apiv1.StartActionByGetResponse{
  193. ExecutionTrackingId: uniqueTrackingId,
  194. }), nil
  195. }
  196. func (api *oliveTinAPI) StartActionByGetAndWait(ctx ctx.Context, req *connect.Request[apiv1.StartActionByGetAndWaitRequest]) (*connect.Response[apiv1.StartActionByGetAndWaitResponse], error) {
  197. args := make(map[string]string)
  198. user := auth.UserFromApiCall(ctx, req, api.cfg)
  199. execReq := executor.ExecutionRequest{
  200. Binding: api.executor.FindBindingByID(req.Msg.ActionId),
  201. TrackingID: uuid.NewString(),
  202. Arguments: args,
  203. AuthenticatedUser: user,
  204. Cfg: api.cfg,
  205. }
  206. wg, _ := api.executor.ExecRequest(&execReq)
  207. wg.Wait()
  208. internalLogEntry, ok := api.executor.GetLog(execReq.TrackingID)
  209. if ok {
  210. return connect.NewResponse(&apiv1.StartActionByGetAndWaitResponse{
  211. LogEntry: api.internalLogEntryToPb(internalLogEntry, user),
  212. }), nil
  213. } else {
  214. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found"))
  215. }
  216. }
  217. func (api *oliveTinAPI) internalLogEntryToPb(logEntry *executor.InternalLogEntry, authenticatedUser *authpublic.AuthenticatedUser) *apiv1.LogEntry {
  218. pble := &apiv1.LogEntry{
  219. ActionTitle: logEntry.ActionTitle,
  220. ActionIcon: logEntry.ActionIcon,
  221. ActionId: logEntry.ActionId,
  222. DatetimeStarted: logEntry.DatetimeStarted.Format("2006-01-02 15:04:05"),
  223. DatetimeFinished: logEntry.DatetimeFinished.Format("2006-01-02 15:04:05"),
  224. DatetimeIndex: logEntry.Index,
  225. Output: logEntry.Output,
  226. TimedOut: logEntry.TimedOut,
  227. Blocked: logEntry.Blocked,
  228. ExitCode: logEntry.ExitCode,
  229. Tags: logEntry.Tags,
  230. ExecutionTrackingId: logEntry.ExecutionTrackingID,
  231. ExecutionStarted: logEntry.ExecutionStarted,
  232. ExecutionFinished: logEntry.ExecutionFinished,
  233. User: logEntry.Username,
  234. }
  235. if !pble.ExecutionFinished {
  236. pble.CanKill = acl.IsAllowedKill(api.cfg, authenticatedUser, logEntry.Binding.Action)
  237. }
  238. return pble
  239. }
  240. func getExecutionStatusByTrackingID(api *oliveTinAPI, executionTrackingId string) *executor.InternalLogEntry {
  241. logEntry, ok := api.executor.GetLog(executionTrackingId)
  242. if !ok {
  243. return nil
  244. }
  245. return logEntry
  246. }
  247. func getMostRecentExecutionStatusById(api *oliveTinAPI, actionId string) *executor.InternalLogEntry {
  248. var ile *executor.InternalLogEntry
  249. logs := api.executor.GetLogsByActionId(actionId)
  250. if len(logs) == 0 {
  251. return nil
  252. } else {
  253. // Get last log entry
  254. ile = logs[len(logs)-1]
  255. }
  256. return ile
  257. }
  258. func (api *oliveTinAPI) ExecutionStatus(ctx ctx.Context, req *connect.Request[apiv1.ExecutionStatusRequest]) (*connect.Response[apiv1.ExecutionStatusResponse], error) {
  259. res := &apiv1.ExecutionStatusResponse{}
  260. user := auth.UserFromApiCall(ctx, req, api.cfg)
  261. if err := api.checkDashboardAccess(user); err != nil {
  262. return nil, err
  263. }
  264. var ile *executor.InternalLogEntry
  265. if req.Msg.ExecutionTrackingId != "" {
  266. ile = getExecutionStatusByTrackingID(api, req.Msg.ExecutionTrackingId)
  267. } else {
  268. ile = getMostRecentExecutionStatusById(api, req.Msg.ActionId)
  269. }
  270. if ile == nil {
  271. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found for tracking ID %s or action ID %s", req.Msg.ExecutionTrackingId, req.Msg.ActionId))
  272. } else {
  273. res.LogEntry = api.internalLogEntryToPb(ile, user)
  274. }
  275. return connect.NewResponse(res), nil
  276. }
  277. func (api *oliveTinAPI) Logout(ctx ctx.Context, req *connect.Request[apiv1.LogoutRequest]) (*connect.Response[apiv1.LogoutResponse], error) {
  278. user := auth.UserFromApiCall(ctx, req, api.cfg)
  279. log.WithFields(log.Fields{
  280. "username": user.Username,
  281. "provider": user.Provider,
  282. }).Info("Logout: User logged out")
  283. response := connect.NewResponse(&apiv1.LogoutResponse{})
  284. // Clear the authentication cookie by setting it to expire
  285. cookie := &http.Cookie{
  286. Name: "olivetin-sid-local",
  287. Value: "",
  288. MaxAge: -1, // This tells the browser to delete the cookie
  289. HttpOnly: true,
  290. Path: "/",
  291. }
  292. response.Header().Set("Set-Cookie", cookie.String())
  293. return response, nil
  294. }
  295. func (api *oliveTinAPI) GetActionBinding(ctx ctx.Context, req *connect.Request[apiv1.GetActionBindingRequest]) (*connect.Response[apiv1.GetActionBindingResponse], error) {
  296. user := auth.UserFromApiCall(ctx, req, api.cfg)
  297. if err := api.checkDashboardAccess(user); err != nil {
  298. return nil, err
  299. }
  300. binding := api.executor.FindBindingByID(req.Msg.BindingId)
  301. if binding == nil {
  302. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("action with ID %s not found", req.Msg.BindingId))
  303. }
  304. return connect.NewResponse(&apiv1.GetActionBindingResponse{
  305. Action: buildAction(binding, &DashboardRenderRequest{
  306. cfg: api.cfg,
  307. AuthenticatedUser: user,
  308. ex: api.executor,
  309. }),
  310. }), nil
  311. }
  312. func (api *oliveTinAPI) GetDashboard(ctx ctx.Context, req *connect.Request[apiv1.GetDashboardRequest]) (*connect.Response[apiv1.GetDashboardResponse], error) {
  313. user := auth.UserFromApiCall(ctx, req, api.cfg)
  314. if err := api.checkDashboardAccess(user); err != nil {
  315. return nil, err
  316. }
  317. dashboardRenderRequest := api.createDashboardRenderRequest(user)
  318. if api.isDefaultDashboard(req.Msg.Title) {
  319. return api.buildDefaultDashboardResponse(dashboardRenderRequest)
  320. }
  321. return api.buildCustomDashboardResponse(dashboardRenderRequest, req.Msg.Title)
  322. }
  323. func (api *oliveTinAPI) checkDashboardAccess(user *authpublic.AuthenticatedUser) error {
  324. if user.IsGuest() && api.cfg.AuthRequireGuestsToLogin {
  325. return connect.NewError(connect.CodePermissionDenied, fmt.Errorf("guests are not allowed to access the dashboard"))
  326. }
  327. return nil
  328. }
  329. func (api *oliveTinAPI) createDashboardRenderRequest(user *authpublic.AuthenticatedUser) *DashboardRenderRequest {
  330. return &DashboardRenderRequest{
  331. AuthenticatedUser: user,
  332. cfg: api.cfg,
  333. ex: api.executor,
  334. }
  335. }
  336. func (api *oliveTinAPI) isDefaultDashboard(title string) bool {
  337. return title == "default" || title == "" || title == "Actions"
  338. }
  339. func (api *oliveTinAPI) buildDefaultDashboardResponse(rr *DashboardRenderRequest) (*connect.Response[apiv1.GetDashboardResponse], error) {
  340. db := buildDefaultDashboard(rr)
  341. res := &apiv1.GetDashboardResponse{
  342. Dashboard: db,
  343. }
  344. return connect.NewResponse(res), nil
  345. }
  346. func (api *oliveTinAPI) buildCustomDashboardResponse(rr *DashboardRenderRequest, title string) (*connect.Response[apiv1.GetDashboardResponse], error) {
  347. res := &apiv1.GetDashboardResponse{
  348. Dashboard: renderDashboard(rr, title),
  349. }
  350. return connect.NewResponse(res), nil
  351. }
  352. func (api *oliveTinAPI) GetLogs(ctx ctx.Context, req *connect.Request[apiv1.GetLogsRequest]) (*connect.Response[apiv1.GetLogsResponse], error) {
  353. user := auth.UserFromApiCall(ctx, req, api.cfg)
  354. if err := api.checkDashboardAccess(user); err != nil {
  355. return nil, err
  356. }
  357. ret := &apiv1.GetLogsResponse{}
  358. logEntries, paging := api.executor.GetLogTrackingIdsACL(api.cfg, user, req.Msg.StartOffset, api.cfg.LogHistoryPageSize)
  359. for _, le := range logEntries {
  360. ret.Logs = append(ret.Logs, api.internalLogEntryToPb(le, user))
  361. }
  362. ret.CountRemaining = paging.CountRemaining
  363. ret.PageSize = paging.PageSize
  364. ret.TotalCount = paging.TotalCount
  365. ret.StartOffset = paging.StartOffset
  366. return connect.NewResponse(ret), nil
  367. }
  368. // isValidLogEntry checks if a log entry has all required fields populated.
  369. func isValidLogEntry(e *executor.InternalLogEntry) bool {
  370. return e != nil && e.Binding != nil && e.Binding.Action != nil
  371. }
  372. // isLogEntryAllowed checks if a log entry is allowed to be viewed by the user.
  373. func (api *oliveTinAPI) isLogEntryAllowed(e *executor.InternalLogEntry, user *authpublic.AuthenticatedUser) bool {
  374. return acl.IsAllowedLogs(api.cfg, user, e.Binding.Action)
  375. }
  376. // buildEmptyPageResponse creates a response for an empty page.
  377. func buildEmptyPageResponse(page pageInfo) *apiv1.GetActionLogsResponse {
  378. return &apiv1.GetActionLogsResponse{
  379. CountRemaining: 0,
  380. PageSize: page.size,
  381. TotalCount: page.total,
  382. StartOffset: page.start,
  383. }
  384. }
  385. // calculateReversedIndices computes the reversed indices for newest-first pagination.
  386. func calculateReversedIndices(page pageInfo, filteredLen int) (int64, int64) {
  387. startIdx := page.total - page.end
  388. endIdx := page.total - page.start
  389. if startIdx < 0 {
  390. startIdx = 0
  391. }
  392. if endIdx > int64(filteredLen) {
  393. endIdx = int64(filteredLen)
  394. }
  395. return startIdx, endIdx
  396. }
  397. // buildActionLogsResponse builds the response with paginated log entries.
  398. func (api *oliveTinAPI) buildActionLogsResponse(filtered []*executor.InternalLogEntry, page pageInfo, user *authpublic.AuthenticatedUser) *apiv1.GetActionLogsResponse {
  399. startIdx, endIdx := calculateReversedIndices(page, len(filtered))
  400. ret := &apiv1.GetActionLogsResponse{}
  401. for _, le := range filtered[startIdx:endIdx] {
  402. ret.Logs = append(ret.Logs, api.internalLogEntryToPb(le, user))
  403. }
  404. ret.CountRemaining = page.start
  405. ret.PageSize = page.size
  406. ret.TotalCount = page.total
  407. ret.StartOffset = page.start
  408. return ret
  409. }
  410. func (api *oliveTinAPI) GetActionLogs(ctx ctx.Context, req *connect.Request[apiv1.GetActionLogsRequest]) (*connect.Response[apiv1.GetActionLogsResponse], error) {
  411. user := auth.UserFromApiCall(ctx, req, api.cfg)
  412. if err := api.checkDashboardAccess(user); err != nil {
  413. return nil, err
  414. }
  415. filtered := api.filterLogsByACL(api.executor.GetLogsByActionId(req.Msg.ActionId), user)
  416. page := paginate(int64(len(filtered)), api.cfg.LogHistoryPageSize, req.Msg.StartOffset)
  417. if page.empty {
  418. return connect.NewResponse(buildEmptyPageResponse(page)), nil
  419. }
  420. return connect.NewResponse(api.buildActionLogsResponse(filtered, page, user)), nil
  421. }
  422. func (api *oliveTinAPI) filterLogsByACL(entries []*executor.InternalLogEntry, user *authpublic.AuthenticatedUser) []*executor.InternalLogEntry {
  423. filtered := make([]*executor.InternalLogEntry, 0, len(entries))
  424. for _, e := range entries {
  425. if !isValidLogEntry(e) {
  426. continue
  427. }
  428. if api.isLogEntryAllowed(e, user) {
  429. filtered = append(filtered, e)
  430. }
  431. }
  432. return filtered
  433. }
  434. type pageInfo struct {
  435. total int64
  436. size int64
  437. start int64
  438. end int64
  439. empty bool
  440. }
  441. func paginate(total int64, size int64, start int64) pageInfo {
  442. if start < 0 {
  443. start = 0
  444. }
  445. if start >= total {
  446. return pageInfo{total: total, size: size, start: start, end: start, empty: true}
  447. }
  448. end := start + size
  449. if end > total {
  450. end = total
  451. }
  452. return pageInfo{total: total, size: size, start: start, end: end, empty: false}
  453. }
  454. /*
  455. This function is ONLY a helper for the UI - the arguments are validated properly
  456. on the StartAction -> Executor chain. This is here basically to provide helpful
  457. error messages more quickly before starting the action.
  458. */
  459. func (api *oliveTinAPI) ValidateArgumentType(ctx ctx.Context, req *connect.Request[apiv1.ValidateArgumentTypeRequest]) (*connect.Response[apiv1.ValidateArgumentTypeResponse], error) {
  460. err := executor.TypeSafetyCheck("", req.Msg.Value, req.Msg.Type)
  461. desc := ""
  462. if err != nil {
  463. desc = err.Error()
  464. }
  465. return connect.NewResponse(&apiv1.ValidateArgumentTypeResponse{
  466. Valid: err == nil,
  467. Description: desc,
  468. }), nil
  469. }
  470. func (api *oliveTinAPI) WhoAmI(ctx ctx.Context, req *connect.Request[apiv1.WhoAmIRequest]) (*connect.Response[apiv1.WhoAmIResponse], error) {
  471. user := auth.UserFromApiCall(ctx, req, api.cfg)
  472. if err := api.checkDashboardAccess(user); err != nil {
  473. return nil, err
  474. }
  475. res := &apiv1.WhoAmIResponse{
  476. AuthenticatedUser: user.Username,
  477. Usergroup: user.UsergroupLine,
  478. Provider: user.Provider,
  479. Sid: user.SID,
  480. Acls: user.Acls,
  481. }
  482. return connect.NewResponse(res), nil
  483. }
  484. func (api *oliveTinAPI) SosReport(ctx ctx.Context, req *connect.Request[apiv1.SosReportRequest]) (*connect.Response[apiv1.SosReportResponse], error) {
  485. sos := installationinfo.GetSosReport()
  486. if !api.cfg.InsecureAllowDumpSos {
  487. log.Info(sos)
  488. 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."
  489. }
  490. ret := &apiv1.SosReportResponse{
  491. Alert: sos,
  492. }
  493. return connect.NewResponse(ret), nil
  494. }
  495. func (api *oliveTinAPI) DumpVars(ctx ctx.Context, req *connect.Request[apiv1.DumpVarsRequest]) (*connect.Response[apiv1.DumpVarsResponse], error) {
  496. res := &apiv1.DumpVarsResponse{}
  497. if !api.cfg.InsecureAllowDumpVars {
  498. res.Alert = "Dumping variables is not allowed by default because it is insecure."
  499. return connect.NewResponse(res), nil
  500. }
  501. jsonstring, _ := json.MarshalIndent(entities.GetAll(), "", " ")
  502. fmt.Printf("%s", &jsonstring)
  503. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpVars = false again after you don't need it anymore"
  504. return connect.NewResponse(res), nil
  505. }
  506. func (api *oliveTinAPI) DumpPublicIdActionMap(ctx ctx.Context, req *connect.Request[apiv1.DumpPublicIdActionMapRequest]) (*connect.Response[apiv1.DumpPublicIdActionMapResponse], error) {
  507. res := &apiv1.DumpPublicIdActionMapResponse{}
  508. res.Contents = make(map[string]*apiv1.ActionEntityPair)
  509. if !api.cfg.InsecureAllowDumpActionMap {
  510. res.Alert = "Dumping Public IDs is disallowed."
  511. return connect.NewResponse(res), nil
  512. }
  513. api.executor.MapActionIdToBindingLock.RLock()
  514. for k, v := range api.executor.MapActionIdToBinding {
  515. res.Contents[k] = &apiv1.ActionEntityPair{
  516. ActionTitle: v.Action.Title,
  517. EntityPrefix: "?",
  518. }
  519. }
  520. api.executor.MapActionIdToBindingLock.RUnlock()
  521. res.Alert = "Dumping variables has been enabled in the configuration. Please set InsecureAllowDumpActionMap = false again after you don't need it anymore"
  522. return connect.NewResponse(res), nil
  523. }
  524. func (api *oliveTinAPI) GetReadyz(ctx ctx.Context, req *connect.Request[apiv1.GetReadyzRequest]) (*connect.Response[apiv1.GetReadyzResponse], error) {
  525. res := &apiv1.GetReadyzResponse{
  526. Status: "OK",
  527. }
  528. return connect.NewResponse(res), nil
  529. }
  530. func (api *oliveTinAPI) EventStream(ctx ctx.Context, req *connect.Request[apiv1.EventStreamRequest], srv *connect.ServerStream[apiv1.EventStreamResponse]) error {
  531. log.Debugf("EventStream: %v", req.Msg)
  532. user := auth.UserFromApiCall(ctx, req, api.cfg)
  533. if err := api.checkDashboardAccess(user); err != nil {
  534. return err
  535. }
  536. client := &streamingClient{
  537. channel: make(chan *apiv1.EventStreamResponse, 10), // Buffered channel to hold Events
  538. AuthenticatedUser: user,
  539. }
  540. log.WithFields(log.Fields{
  541. "authenticatedUser": user.Username,
  542. }).Debugf("EventStream: client connected")
  543. api.streamingClientsMutex.Lock()
  544. api.streamingClients[client] = struct{}{}
  545. api.streamingClientsMutex.Unlock()
  546. // loop over client channel and send events to connectedClient
  547. for msg := range client.channel {
  548. log.Debugf("Sending event to client: %v", msg)
  549. if err := srv.Send(msg); err != nil {
  550. log.Errorf("Error sending event to client: %v", err)
  551. // Remove disconnected client from the list
  552. api.removeClient(client)
  553. break
  554. }
  555. }
  556. log.Infof("EventStream: client disconnected")
  557. return nil
  558. }
  559. func (api *oliveTinAPI) removeClient(clientToRemove *streamingClient) {
  560. api.streamingClientsMutex.Lock()
  561. delete(api.streamingClients, clientToRemove)
  562. api.streamingClientsMutex.Unlock()
  563. close(clientToRemove.channel)
  564. }
  565. func (api *oliveTinAPI) OnActionMapRebuilt() {
  566. toRemove := []*streamingClient{}
  567. for _, client := range api.copyOfStreamingClients() {
  568. select {
  569. case client.channel <- &apiv1.EventStreamResponse{
  570. Event: &apiv1.EventStreamResponse_ConfigChanged{
  571. ConfigChanged: &apiv1.EventConfigChanged{},
  572. },
  573. }:
  574. default:
  575. log.Warnf("EventStream: client channel is full, removing client")
  576. toRemove = append(toRemove, client)
  577. }
  578. }
  579. for _, client := range toRemove {
  580. api.removeClient(client)
  581. }
  582. }
  583. func (api *oliveTinAPI) OnExecutionStarted(ex *executor.InternalLogEntry) {
  584. toRemove := []*streamingClient{}
  585. for _, client := range api.copyOfStreamingClients() {
  586. select {
  587. case client.channel <- &apiv1.EventStreamResponse{
  588. Event: &apiv1.EventStreamResponse_ExecutionStarted{
  589. ExecutionStarted: &apiv1.EventExecutionStarted{
  590. LogEntry: api.internalLogEntryToPb(ex, client.AuthenticatedUser),
  591. },
  592. },
  593. }:
  594. default:
  595. log.Warnf("EventStream: client channel is full, removing client")
  596. toRemove = append(toRemove, client)
  597. }
  598. }
  599. for _, client := range toRemove {
  600. api.removeClient(client)
  601. }
  602. }
  603. func (api *oliveTinAPI) OnExecutionFinished(ex *executor.InternalLogEntry) {
  604. toRemove := []*streamingClient{}
  605. for _, client := range api.copyOfStreamingClients() {
  606. select {
  607. case client.channel <- &apiv1.EventStreamResponse{
  608. Event: &apiv1.EventStreamResponse_ExecutionFinished{
  609. ExecutionFinished: &apiv1.EventExecutionFinished{
  610. LogEntry: api.internalLogEntryToPb(ex, client.AuthenticatedUser),
  611. },
  612. },
  613. }:
  614. default:
  615. log.Warnf("EventStream: client channel is full, removing client")
  616. toRemove = append(toRemove, client)
  617. }
  618. }
  619. for _, client := range toRemove {
  620. api.removeClient(client)
  621. }
  622. }
  623. func (api *oliveTinAPI) GetDiagnostics(ctx ctx.Context, req *connect.Request[apiv1.GetDiagnosticsRequest]) (*connect.Response[apiv1.GetDiagnosticsResponse], error) {
  624. res := &apiv1.GetDiagnosticsResponse{
  625. SshFoundKey: installationinfo.Runtime.SshFoundKey,
  626. SshFoundConfig: installationinfo.Runtime.SshFoundConfig,
  627. }
  628. return connect.NewResponse(res), nil
  629. }
  630. func (api *oliveTinAPI) Init(ctx ctx.Context, req *connect.Request[apiv1.InitRequest]) (*connect.Response[apiv1.InitResponse], error) {
  631. user := auth.UserFromApiCall(ctx, req, api.cfg)
  632. loginRequired := user.IsGuest() && api.cfg.AuthRequireGuestsToLogin
  633. res := &apiv1.InitResponse{
  634. ShowFooter: api.cfg.ShowFooter,
  635. ShowNavigation: api.cfg.ShowNavigation,
  636. ShowNewVersions: api.cfg.ShowNewVersions,
  637. AvailableVersion: installationinfo.Runtime.AvailableVersion,
  638. CurrentVersion: installationinfo.Build.Version,
  639. PageTitle: api.cfg.PageTitle,
  640. SectionNavigationStyle: api.cfg.SectionNavigationStyle,
  641. DefaultIconForBack: api.cfg.DefaultIconForBack,
  642. EnableCustomJs: api.cfg.EnableCustomJs,
  643. AuthLoginUrl: api.cfg.AuthLoginUrl,
  644. AuthLocalLogin: api.cfg.AuthLocalUsers.Enabled,
  645. OAuth2Providers: buildPublicOAuth2ProvidersList(api.cfg),
  646. AdditionalLinks: buildAdditionalLinks(api.cfg.AdditionalNavigationLinks),
  647. StyleMods: api.cfg.StyleMods,
  648. RootDashboards: api.buildRootDashboards(user, api.cfg.Dashboards),
  649. AuthenticatedUser: user.Username,
  650. AuthenticatedUserProvider: user.Provider,
  651. EffectivePolicy: buildEffectivePolicy(user.EffectivePolicy),
  652. BannerMessage: api.cfg.BannerMessage,
  653. BannerCss: api.cfg.BannerCSS,
  654. ShowDiagnostics: user.EffectivePolicy.ShowDiagnostics,
  655. ShowLogList: user.EffectivePolicy.ShowLogList,
  656. LoginRequired: loginRequired,
  657. }
  658. return connect.NewResponse(res), nil
  659. }
  660. func (api *oliveTinAPI) buildRootDashboards(user *authpublic.AuthenticatedUser, dashboards []*config.DashboardComponent) []string {
  661. var rootDashboards []string
  662. dashboardRenderRequest := api.createDashboardRenderRequest(user)
  663. api.addDefaultDashboardIfNeeded(&rootDashboards, dashboardRenderRequest)
  664. api.addCustomDashboards(&rootDashboards, dashboards, dashboardRenderRequest)
  665. return rootDashboards
  666. }
  667. func (api *oliveTinAPI) addDefaultDashboardIfNeeded(rootDashboards *[]string, rr *DashboardRenderRequest) {
  668. defaultDashboard := buildDefaultDashboard(rr)
  669. if defaultDashboard != nil && len(defaultDashboard.Contents) > 0 {
  670. log.Tracef("defaultDashboard: %+v", defaultDashboard.Contents)
  671. *rootDashboards = append(*rootDashboards, "Actions")
  672. }
  673. }
  674. func (api *oliveTinAPI) addCustomDashboards(rootDashboards *[]string, dashboards []*config.DashboardComponent, rr *DashboardRenderRequest) {
  675. for _, dashboard := range dashboards {
  676. // We have to build the dashboard response instead of just looping over config.dashboards,
  677. // because we need to check if the user has access to the dashboard
  678. db := renderDashboard(rr, dashboard.Title)
  679. if db != nil {
  680. *rootDashboards = append(*rootDashboards, dashboard.Title)
  681. }
  682. }
  683. }
  684. func buildPublicOAuth2ProvidersList(cfg *config.Config) []*apiv1.OAuth2Provider {
  685. var publicProviders []*apiv1.OAuth2Provider
  686. for providerKey, provider := range cfg.AuthOAuth2Providers {
  687. publicProviders = append(publicProviders, &apiv1.OAuth2Provider{
  688. Title: provider.Title,
  689. Icon: provider.Icon,
  690. Key: providerKey,
  691. })
  692. }
  693. sort.Slice(publicProviders, func(i, j int) bool {
  694. return publicProviders[i].Key < publicProviders[j].Key
  695. })
  696. return publicProviders
  697. }
  698. func buildAdditionalLinks(links []*config.NavigationLink) []*apiv1.AdditionalLink {
  699. var additionalLinks []*apiv1.AdditionalLink
  700. for _, link := range links {
  701. additionalLinks = append(additionalLinks, &apiv1.AdditionalLink{
  702. Title: link.Title,
  703. Url: link.Url,
  704. })
  705. }
  706. return additionalLinks
  707. }
  708. func (api *oliveTinAPI) OnOutputChunk(content []byte, executionTrackingId string) {
  709. toRemove := []*streamingClient{}
  710. for _, client := range api.copyOfStreamingClients() {
  711. select {
  712. case client.channel <- &apiv1.EventStreamResponse{
  713. Event: &apiv1.EventStreamResponse_OutputChunk{
  714. OutputChunk: &apiv1.EventOutputChunk{
  715. Output: string(content),
  716. ExecutionTrackingId: executionTrackingId,
  717. },
  718. },
  719. }:
  720. default:
  721. log.Warnf("EventStream: client channel is full, removing client")
  722. toRemove = append(toRemove, client)
  723. }
  724. }
  725. for _, client := range toRemove {
  726. api.removeClient(client)
  727. }
  728. }
  729. func (api *oliveTinAPI) GetEntities(ctx ctx.Context, req *connect.Request[apiv1.GetEntitiesRequest]) (*connect.Response[apiv1.GetEntitiesResponse], error) {
  730. user := auth.UserFromApiCall(ctx, req, api.cfg)
  731. if err := api.checkDashboardAccess(user); err != nil {
  732. return nil, err
  733. }
  734. res := &apiv1.GetEntitiesResponse{
  735. EntityDefinitions: make([]*apiv1.EntityDefinition, 0),
  736. }
  737. for name, entityInstances := range entities.GetEntities() {
  738. def := &apiv1.EntityDefinition{
  739. Title: name,
  740. UsedOnDashboards: findDashboardsForEntity(name, api.cfg.Dashboards),
  741. }
  742. for _, e := range entityInstances {
  743. entity := &apiv1.Entity{
  744. Title: e.Title,
  745. UniqueKey: e.UniqueKey,
  746. Type: name,
  747. }
  748. def.Instances = append(def.Instances, entity)
  749. }
  750. res.EntityDefinitions = append(res.EntityDefinitions, def)
  751. }
  752. return connect.NewResponse(res), nil
  753. }
  754. func findDashboardsForEntity(entityTitle string, dashboards []*config.DashboardComponent) []string {
  755. var foundDashboards []string
  756. findEntityInComponents(entityTitle, "", dashboards, &foundDashboards)
  757. return foundDashboards
  758. }
  759. func findEntityInComponents(entityTitle string, parentTitle string, components []*config.DashboardComponent, foundDashboards *[]string) {
  760. for _, component := range components {
  761. if component.Entity == entityTitle {
  762. *foundDashboards = append(*foundDashboards, parentTitle)
  763. }
  764. if len(component.Contents) > 0 {
  765. findEntityInComponents(entityTitle, component.Title, component.Contents, foundDashboards)
  766. }
  767. }
  768. }
  769. func (api *oliveTinAPI) GetEntity(ctx ctx.Context, req *connect.Request[apiv1.GetEntityRequest]) (*connect.Response[apiv1.Entity], error) {
  770. user := auth.UserFromApiCall(ctx, req, api.cfg)
  771. if err := api.checkDashboardAccess(user); err != nil {
  772. return nil, err
  773. }
  774. res := &apiv1.Entity{}
  775. instances := entities.GetEntityInstances(req.Msg.Type)
  776. log.Infof("msg: %+v", req.Msg)
  777. if len(instances) == 0 {
  778. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity type %s not found", req.Msg.Type))
  779. }
  780. if entity, ok := instances[req.Msg.UniqueKey]; !ok {
  781. return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("entity with unique key %s not found in type %s", req.Msg.UniqueKey, req.Msg.Type))
  782. } else {
  783. res.Title = entity.Title
  784. return connect.NewResponse(res), nil
  785. }
  786. }
  787. func (api *oliveTinAPI) RestartAction(ctx ctx.Context, req *connect.Request[apiv1.RestartActionRequest]) (*connect.Response[apiv1.StartActionResponse], error) {
  788. ret := &apiv1.StartActionResponse{
  789. ExecutionTrackingId: req.Msg.ExecutionTrackingId,
  790. }
  791. var execReqLogEntry *executor.InternalLogEntry
  792. execReqLogEntry, found := api.executor.GetLog(req.Msg.ExecutionTrackingId)
  793. if !found {
  794. log.Warnf("Restarting execution request not possible - not found by tracking ID: %v", req.Msg.ExecutionTrackingId)
  795. return connect.NewResponse(ret), nil
  796. }
  797. log.Warnf("Restarting execution request by tracking ID: %v", req.Msg.ExecutionTrackingId)
  798. action := execReqLogEntry.Binding.Action
  799. if action == nil {
  800. log.Warnf("Restarting execution request not possible - action not found: %v", execReqLogEntry.ActionTitle)
  801. return connect.NewResponse(ret), nil
  802. }
  803. return api.StartAction(ctx, &connect.Request[apiv1.StartActionRequest]{
  804. Msg: &apiv1.StartActionRequest{
  805. // FIXME
  806. UniqueTrackingId: req.Msg.ExecutionTrackingId,
  807. },
  808. })
  809. }
  810. func newServer(ex *executor.Executor) *oliveTinAPI {
  811. server := oliveTinAPI{}
  812. server.cfg = ex.Cfg
  813. server.executor = ex
  814. server.streamingClients = make(map[*streamingClient]struct{})
  815. ex.AddListener(&server)
  816. return &server
  817. }
  818. func GetNewHandler(ex *executor.Executor) (string, http.Handler) {
  819. server := newServer(ex)
  820. jsonOpt := connectproto.WithJSON(
  821. protojson.MarshalOptions{
  822. EmitUnpopulated: true, // https://github.com/OliveTin/OliveTin/issues/674
  823. },
  824. protojson.UnmarshalOptions{
  825. DiscardUnknown: true,
  826. },
  827. )
  828. return apiv1connect.NewOliveTinApiServiceHandler(server, jsonOpt)
  829. }