config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package config
  2. import (
  3. "fmt"
  4. "slices"
  5. )
  6. // ReservedArgumentNamePrefix is reserved for OliveTin-injected system arguments.
  7. const ReservedArgumentNamePrefix = "ot_"
  8. // JustificationRequiredNoTemplate requires a manual justification with no prefilled template.
  9. const JustificationRequiredNoTemplate = " "
  10. // Action represents the core functionality of OliveTin - commands that show up
  11. // as buttons in the UI.
  12. type Action struct {
  13. SaveLogs SaveLogsConfig `koanf:"saveLogs"`
  14. Shell string `koanf:"shell"`
  15. Icon string `koanf:"icon"`
  16. ExecOnCalendarFile string `koanf:"execOnCalendarFile"`
  17. SourceFile string `koanf:"-"`
  18. ShellAfterCompleted string `koanf:"shellAfterCompleted"`
  19. Justification string `koanf:"justification"`
  20. EnabledExpression string `koanf:"enabledExpression"`
  21. Entity string `koanf:"entity"`
  22. Title string `koanf:"title"`
  23. PopupOnStart string `koanf:"popupOnStart"`
  24. OnClick string `koanf:"onclick"`
  25. ID string `koanf:"id"`
  26. MaxRate []RateSpec `koanf:"maxRate"`
  27. Acls []string `koanf:"acls"`
  28. ExecOnWebhook []WebhookConfig `koanf:"execOnWebhook"`
  29. Triggers []string `koanf:"triggers"`
  30. Exec []string `koanf:"exec"`
  31. ExecOnFileCreatedInDir []string `koanf:"execOnFileCreatedInDir"`
  32. Arguments []ActionArgument `koanf:"arguments"`
  33. ExecOnCron []string `koanf:"execOnCron"`
  34. Groups []string `koanf:"groups"`
  35. ExecOnFileChangedInDir []string `koanf:"execOnFileChangedInDir"`
  36. Timeout int `koanf:"timeout"`
  37. MaxConcurrent int `koanf:"maxConcurrent"`
  38. Hidden bool `koanf:"hidden"`
  39. ExecOnStartup bool `koanf:"execOnStartup"`
  40. }
  41. func (action *Action) RequiresJustification() bool {
  42. return action != nil && action.Justification != ""
  43. }
  44. func (action *Action) JustificationTemplateText() string {
  45. if !action.RequiresJustification() {
  46. return ""
  47. }
  48. if action.Justification == JustificationRequiredNoTemplate {
  49. return ""
  50. }
  51. return action.Justification
  52. }
  53. // ActionGroup defines shared limits and metadata for a set of actions.
  54. type ActionGroup struct {
  55. Icon string `koanf:"icon"`
  56. MaxConcurrent int `koanf:"maxConcurrent"`
  57. QueueSize int `koanf:"queueSize"`
  58. }
  59. // ActionArgument objects appear on Actions.
  60. type ActionArgument struct {
  61. Suggestions map[string]string `koanf:"suggestions"`
  62. Name string `koanf:"name"`
  63. Title string `koanf:"title"`
  64. Description string `koanf:"description"`
  65. Type string `koanf:"type"`
  66. Default string `koanf:"default"`
  67. Entity string `koanf:"entity"`
  68. SuggestionsBrowserKey string `koanf:"suggestionsBrowserKey"`
  69. Choices []ActionArgumentChoice `koanf:"choices"`
  70. RejectNull bool `koanf:"rejectNull"`
  71. }
  72. // ActionArgumentChoice represents a predefined choice for an argument.
  73. type ActionArgumentChoice struct {
  74. Value string `koanf:"value"`
  75. Title string `koanf:"title"`
  76. }
  77. // RateSpec allows you to set a max frequency for an action.
  78. type RateSpec struct {
  79. Duration string `koanf:"duration"`
  80. Limit int `koanf:"limit"`
  81. }
  82. // WebhookConfig defines configuration for generic webhook triggers.
  83. // WebhookAuthTypeNone must be set explicitly for a webhook that accepts
  84. // unauthenticated requests.
  85. const WebhookAuthTypeNone = "none"
  86. // WebhookTemplateNames are the webhook templates OliveTin knows. Each one sets
  87. // its own authType, so a webhook that uses one does not need an authType in
  88. // the config.
  89. var WebhookTemplateNames = []string{
  90. "github-push",
  91. "github-pr",
  92. "github-pull-request",
  93. "github-release",
  94. "github-workflow",
  95. }
  96. func IsKnownWebhookTemplate(name string) bool {
  97. return slices.Contains(WebhookTemplateNames, name)
  98. }
  99. // HmacWebhookAuthTypes sign only the request body. The older of the two hash
  100. // functions is only accepted so that OliveTin can check signatures from
  101. // senders that still use it, such as older GitHub webhooks; OliveTin does not
  102. // use it for anything else.
  103. var HmacWebhookAuthTypes = []string{"hmac-sha256", "hmac-sha1"} // DevSkim: ignore DS126858
  104. func IsHmacWebhookAuthType(authType string) bool {
  105. return slices.Contains(HmacWebhookAuthTypes, authType)
  106. }
  107. type WebhookConfig struct {
  108. Secret string `koanf:"secret"` // Optional: secret for signature verification
  109. AuthType string `koanf:"authType"` // Required unless set by a template: "hmac-sha256", "hmac-sha1", "bearer", "basic", "none"
  110. AuthHeader string `koanf:"authHeader"` // Optional: custom header name for auth (default: "X-Webhook-Signature")
  111. MatchHeaders map[string]string `koanf:"matchHeaders"` // Match HTTP headers
  112. MatchPath string `koanf:"matchPath"` // JSONPath expression to match in request body (format: "jsonpath=value" or just "jsonpath")
  113. MatchQuery map[string]string `koanf:"matchQuery"` // Match URL query parameters
  114. Extract map[string]string `koanf:"extract"` // Map action argument names to JSONPath expressions
  115. Template string `koanf:"template"` // Optional: template name (e.g., "github-push", "github-pr")
  116. Justification string `koanf:"justification"` // Optional JSONPath to extract justification from webhook body
  117. }
  118. // Entity represents a "thing" that can have multiple actions associated with it.
  119. // for example, a media player with a start and stop action.
  120. type EntityFile struct {
  121. File string `koanf:"file"`
  122. Name string `koanf:"name"`
  123. Icon string `koanf:"icon"`
  124. SourceFile string `koanf:"-"`
  125. Properties []EntityProperty `koanf:"properties"`
  126. Acls []string `koanf:"acls"`
  127. }
  128. // EntityProperty defines a column shown when listing entity instances in the UI.
  129. type EntityProperty struct {
  130. Name string `koanf:"name"`
  131. Title string `koanf:"title"`
  132. }
  133. // PermissionsList defines what users can do with an action.
  134. type PermissionsList struct {
  135. View bool `koanf:"view"`
  136. Exec bool `koanf:"exec"`
  137. Logs bool `koanf:"logs"`
  138. Kill bool `koanf:"kill"`
  139. }
  140. // AccessControlList defines what permissions apply to a user or user group.
  141. type AccessControlList struct {
  142. Name string `koanf:"name"`
  143. MatchUsergroups []string `koanf:"matchUsergroups"`
  144. MatchUsernames []string `koanf:"matchUsernames"`
  145. Permissions PermissionsList `koanf:"permissions"`
  146. Policy ConfigurationPolicy `koanf:"policy"`
  147. AddToEveryAction bool `koanf:"addToEveryAction"`
  148. }
  149. // ConfigurationPolicy defines global settings which are overridden with an ACL.
  150. type ConfigurationPolicy struct {
  151. ShowDiagnostics bool `koanf:"showDiagnostics"`
  152. ShowLogList bool `koanf:"showLogList"`
  153. ShowVersionNumber bool `koanf:"showVersionNumber"`
  154. }
  155. // FeaturesConfig holds global opt-in feature flags. New flags default to false.
  156. type FeaturesConfig struct {
  157. HeaderSearch bool `koanf:"headerSearch"`
  158. }
  159. type PrometheusConfig struct {
  160. Enabled bool `koanf:"enabled"`
  161. DefaultGoMetrics bool `koanf:"defaultGoMetrics"`
  162. }
  163. // SecurityConfig allows users to fine tune the security related HTTP headers and cookie options.
  164. type SecurityConfig struct {
  165. ContentSecurityPolicy string `koanf:"contentSecurityPolicy"`
  166. XFrameOptions string `koanf:"xFrameOptions"`
  167. HeaderContentSecurityPolicy bool `koanf:"headerContentSecurityPolicy"`
  168. HeaderXContentTypeOptions bool `koanf:"headerXContentTypeOptions"`
  169. HeaderXFrameOptions bool `koanf:"headerXFrameOptions"`
  170. ForceSecureCookies bool `koanf:"forceSecureCookies"`
  171. }
  172. // Config is the global config used through the whole app.
  173. type Config struct {
  174. ActionGroups map[string]*ActionGroup `koanf:"actionGroups"`
  175. AuthOAuth2Providers map[string]*OAuth2Provider `koanf:"authOAuth2Providers"`
  176. SaveLogs SaveLogsConfig `koanf:"saveLogs"`
  177. DefaultIconForBack string `koanf:"defaultIconForBack"`
  178. AuthOAuth2RedirectURL string `koanf:"authOAuth2RedirectUrl"`
  179. ListenAddressRestActions string `koanf:"listenAddressRestActions"`
  180. ListenAddressPrometheus string `koanf:"listenAddressPrometheus"`
  181. ExternalRestAddress string `koanf:"externalRestAddress"`
  182. LogLevel string `koanf:"logLevel"`
  183. ThemeName string `koanf:"themeName"`
  184. ServiceLogs ServiceLogsConfig `koanf:"serviceLogs"`
  185. ListenAddressSingleHTTPFrontend string `koanf:"listenAddressSingleHTTPFrontend"`
  186. AuthJwtHmacSecret string `koanf:"authJwtHmacSecret"`
  187. AuthJwtCertsURL string `koanf:"authJwtCertsUrl"`
  188. DefaultIconForActions string `koanf:"defaultIconForActions"`
  189. Include string `koanf:"include"`
  190. PageTitle string `koanf:"pageTitle"`
  191. BannerCSS string `koanf:"bannerCss"`
  192. BannerMessage string `koanf:"bannerMessage"`
  193. DefaultPopupOnStart string `koanf:"defaultPopupOnStart"`
  194. ServiceHostMode string `koanf:"serviceHostMode"`
  195. DefaultOnClick string `koanf:"defaultOnClick"`
  196. AuthJwtCookieName string `koanf:"authJwtCookieName"`
  197. AuthJwtHeader string `koanf:"authJwtHeader"`
  198. AuthJwtAud string `koanf:"authJwtAud"`
  199. ListenAddressWebUI string `koanf:"listenAddressWebUI"`
  200. SectionNavigationStyle string `koanf:"sectionNavigationStyle"`
  201. DefaultIconForDirectories string `koanf:"defaultIconForDirectories"`
  202. AuthJwtClaimUsername string `koanf:"authJwtClaimUsername"`
  203. AuthJwtClaimUserGroup string `koanf:"authJwtClaimUserGroup"`
  204. AuthJwtPubKeyPath string `koanf:"authJwtPubKeyPath"`
  205. AuthHttpHeaderUsername string `koanf:"authHttpHeaderUsername"`
  206. AuthHttpHeaderUserGroup string `koanf:"authHttpHeaderUserGroup"`
  207. AuthHttpHeaderUserGroupSep string `koanf:"authHttpHeaderUserGroupSep"`
  208. WebUIDir string `koanf:"webUIDir"`
  209. AuthLoginUrl string `koanf:"authLoginUrl"`
  210. AuthJwtDomain string `koanf:"authJwtDomain"`
  211. Security SecurityConfig `koanf:"security"`
  212. Actions []*Action `koanf:"actions"`
  213. AccessControlLists []*AccessControlList `koanf:"accessControlLists"`
  214. StyleMods []string `koanf:"styleMods"`
  215. AdditionalNavigationLinks []*NavigationLink `koanf:"additionalNavigationLinks"`
  216. Entities []*EntityFile `koanf:"entities"`
  217. Dashboards []*DashboardComponent `koanf:"dashboards"`
  218. sourceFiles []string
  219. AuthLocalUsers AuthLocalUsersConfig `koanf:"authLocalUsers"`
  220. LogHistoryPageSize int64 `koanf:"logHistoryPageSize"`
  221. LogDebugOptions LogDebugOptions `koanf:"logDebugOptions"`
  222. DefaultPermissions PermissionsList `koanf:"defaultPermissions"`
  223. DefaultPolicy ConfigurationPolicy `koanf:"defaultPolicy"`
  224. Prometheus PrometheusConfig `koanf:"prometheus"`
  225. CheckForUpdates bool `koanf:"checkForUpdates"`
  226. InsecureAllowDumpJwtClaims bool `koanf:"insecureAllowDumpJwtClaims"`
  227. InsecureAllowDumpActionMap bool `koanf:"insecureAllowDumpActionMap"`
  228. InsecureAllowDumpServerDiagnostics bool `koanf:"insecureAllowDumpServerDiagnostics"`
  229. InsecureAllowDumpVars bool `koanf:"insecureAllowDumpVars"`
  230. InsecureAllowDumpOAuth2UserData bool `koanf:"insecureAllowDumpOAuth2UserData"`
  231. CronSupportForSeconds bool `koanf:"cronSupportForSeconds"`
  232. AuthRequireGuestsToLogin bool `koanf:"authRequireGuestsToLogin"`
  233. EnableCustomJs bool `koanf:"enableCustomJs"`
  234. ShowNavigateOnStartIcons bool `koanf:"showNavigateOnStartIcons"`
  235. ShowNewVersions bool `koanf:"showNewVersions"`
  236. ShowNavigation bool `koanf:"showNavigation"`
  237. ShowFooter bool `koanf:"showFooter"`
  238. UseSingleHTTPFrontend bool `koanf:"useSingleHTTPFrontend"`
  239. ThemeCacheDisabled bool `koanf:"themeCacheDisabled"`
  240. Features FeaturesConfig `koanf:"features"`
  241. }
  242. type AuthLocalUsersConfig struct {
  243. Users []*LocalUser `koanf:"users"`
  244. Enabled bool `koanf:"enabled"`
  245. }
  246. type LocalUser struct {
  247. Username string `koanf:"username"`
  248. Usergroup string `koanf:"usergroup"`
  249. Password string `koanf:"password"`
  250. ApiKey string `koanf:"apiKey"`
  251. }
  252. type OAuth2Provider struct {
  253. AuthUrl string `koanf:"authUrl"`
  254. UserGroupField string `koanf:"userGroupField"`
  255. ClientID string `koanf:"clientId"`
  256. ClientSecret string `koanf:"clientSecret"`
  257. Icon string `koanf:"icon"`
  258. AddToUsergroup string `koanf:"addToUsergroup"`
  259. Title string `koanf:"title"`
  260. WhoamiUrl string `koanf:"whoamiUrl"`
  261. Name string `koanf:"name"`
  262. UsernameField string `koanf:"usernameField"`
  263. TokenUrl string `koanf:"tokenUrl"`
  264. CertBundlePath string `koanf:"certBundlePath"`
  265. Scopes []string `koanf:"scopes"`
  266. CallbackTimeout int `koanf:"callbackTimeout"`
  267. InsecureSkipVerify bool `koanf:"insecureSkipVerify"`
  268. }
  269. type NavigationLink struct {
  270. Title string `koanf:"title"`
  271. Url string `koanf:"url"`
  272. Target string `koanf:"target"`
  273. }
  274. type SaveLogsConfig struct {
  275. ResultsDirectory string `koanf:"resultsDirectory"`
  276. OutputDirectory string `koanf:"outputDirectory"`
  277. }
  278. type ServiceLogsConfig struct {
  279. Directory string `koanf:"directory"`
  280. }
  281. type LogDebugOptions struct {
  282. SingleFrontendRequests bool `koanf:"singleFrontendRequests"`
  283. SingleFrontendRequestHeaders bool `koanf:"singleFrontendRequestHeaders"`
  284. AclCheckStarted bool `koanf:"aclCheckStarted"`
  285. AclMatched bool `koanf:"aclMatched"`
  286. AclNotMatched bool `koanf:"aclNotMatched"`
  287. AclNoneMatched bool `koanf:"aclNoneMatched"`
  288. }
  289. type DashboardComponent struct {
  290. Title string `koanf:"title"`
  291. Category string `koanf:"category"`
  292. Type string `koanf:"type"`
  293. Entity string `koanf:"entity"`
  294. Icon string `koanf:"icon"`
  295. CssClass string `koanf:"cssClass"`
  296. Acls []string `koanf:"acls"`
  297. InlineAction *Action `koanf:"inlineAction"`
  298. Contents []*DashboardComponent `koanf:"contents"`
  299. }
  300. func DefaultConfig() *Config {
  301. return DefaultConfigWithBasePort(1337)
  302. }
  303. // DefaultConfig gets a new Config structure with sensible default values.
  304. func DefaultConfigWithBasePort(basePort int) *Config {
  305. config := Config{}
  306. config.UseSingleHTTPFrontend = true
  307. config.PageTitle = "OliveTin"
  308. config.ShowFooter = true
  309. config.ShowNavigation = true
  310. config.ShowNewVersions = true
  311. config.ShowNavigateOnStartIcons = true
  312. config.EnableCustomJs = false
  313. config.ExternalRestAddress = "."
  314. config.LogLevel = "INFO"
  315. config.LogHistoryPageSize = 10
  316. config.CheckForUpdates = false
  317. config.DefaultPermissions.Exec = true
  318. config.DefaultPermissions.View = true
  319. config.DefaultPermissions.Logs = true
  320. config.DefaultPermissions.Kill = true
  321. config.AuthJwtClaimUsername = "name"
  322. config.AuthJwtClaimUserGroup = "group"
  323. config.AuthRequireGuestsToLogin = false
  324. config.WebUIDir = "./webui"
  325. config.CronSupportForSeconds = false
  326. config.SectionNavigationStyle = "sidebar"
  327. config.DefaultOnClick = "nothing"
  328. config.DefaultPopupOnStart = "nothing"
  329. config.InsecureAllowDumpVars = false
  330. config.InsecureAllowDumpServerDiagnostics = false
  331. config.InsecureAllowDumpActionMap = false
  332. config.InsecureAllowDumpJwtClaims = false
  333. config.Prometheus.Enabled = false
  334. config.Prometheus.DefaultGoMetrics = false
  335. config.Security.HeaderContentSecurityPolicy = true
  336. config.Security.ContentSecurityPolicy = ContentSecurityPolicyDefault
  337. config.Security.HeaderXContentTypeOptions = true
  338. config.Security.HeaderXFrameOptions = true
  339. config.Security.XFrameOptions = "DENY"
  340. config.DefaultIconForActions = "hugeicons:CommandLineIcon"
  341. config.DefaultIconForDirectories = "&#128193"
  342. config.DefaultIconForBack = "«"
  343. config.ThemeCacheDisabled = false
  344. config.ServiceHostMode = ""
  345. config.ListenAddressSingleHTTPFrontend = fmt.Sprintf("0.0.0.0:%d", basePort)
  346. config.ListenAddressRestActions = fmt.Sprintf("localhost:%d", basePort+1)
  347. config.ListenAddressWebUI = fmt.Sprintf("localhost:%d", basePort+3)
  348. config.ListenAddressPrometheus = fmt.Sprintf("localhost:%d", basePort+4)
  349. config.DefaultPolicy.ShowDiagnostics = true
  350. config.DefaultPolicy.ShowLogList = true
  351. config.DefaultPolicy.ShowVersionNumber = true
  352. return &config
  353. }