3
0

Add webconsole

This commit is contained in:
Denis Arh
2022-02-15 19:24:09 +01:00
parent 32c013f089
commit 22c47d6ba7
34 changed files with 3060 additions and 15 deletions

View File

@@ -1,3 +1,5 @@
webconsole/dist
webconsole/node_modules
.idea
.dev
build

View File

@@ -170,6 +170,26 @@
# Default: <no value>
# HTTP_SSL_TERMINATED=<no value>
###############################################################################
# Enable web console. When running in dev environment, web console is enabled by default.
# Type: bool
# Default: <no value>
# HTTP_SERVER_WEB_CONSOLE_ENABLED=<no value>
###############################################################################
# Username for the web console endpoint.
# Type: string
# Default: admin
# HTTP_SERVER_WEB_CONSOLE_USERNAME=admin
###############################################################################
# Password for the web console endpoint. When running in dev environment, password is not required.
#
# Corteza intentionally sets default password to random chars to prevent security incidents.
# Type: string
# Default: <no value>
# HTTP_SERVER_WEB_CONSOLE_PASSWORD=<no value>
###############################################################################
###############################################################################
# RBAC options

View File

@@ -25,10 +25,33 @@ jobs:
restore-keys: ${{ runner.os }}-go-
- run: make test.all
release-linux:
build-web-console:
runs-on: ubuntu-latest
needs:
- test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
- uses: actions/cache@v2
if: ${{ !env.ACT }}
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.OS }}-node-
- name: Install dependencies
working-directory: ./webconsole
run: yarn install
- name: Build Package
working-directory: ./webconsole
run: yarn build
release-linux:
runs-on: ubuntu-latest
needs:
- build-web-console
env:
BUILD_OS: linux
BUILD_ARCH: amd64
@@ -47,7 +70,7 @@ jobs:
release-darwin:
runs-on: macos-latest
needs:
- test
- build-web-console
env:
BUILD_OS: darwin
BUILD_ARCH: amd64
@@ -73,7 +96,7 @@ jobs:
release-docker:
runs-on: ubuntu-latest
needs:
- test
- build-web-console
- release-linux
env:
BUILD_OS: linux

View File

@@ -1,3 +1,12 @@
# bundle web-console
FROM node:16.14-alpine as webconsole-build-stage
WORKDIR /webconsole
COPY ./webconsole ./
# Snapshot is built in development mode and with source map
RUN yarn install && yarn build --mode dev --sourcemap
# build server
FROM golang:1.17-buster as server-build-stage
@@ -9,6 +18,7 @@ WORKDIR /corteza
COPY . ./
COPY --from=webconsole-build-stage /webconsole/dist ./webconsole/dist
RUN make release-clean release

View File

@@ -119,5 +119,23 @@ HTTPServer: schema.#optionsGroup & {
"""
env: "HTTP_SSL_TERMINATED"
}
web_console_enabled: {
type: "bool"
defaultGoExpr: "false"
description: "Enable web console. When running in dev environment, web console is enabled by default."
}
web_console_username: {
defaultValue: "admin"
description: "Username for the web console endpoint."
}
web_console_password: {
defaultGoExpr: "string(rand.Bytes(32))"
description: """
Password for the web console endpoint. When running in dev environment, password is not required.
Corteza intentionally sets default password to random chars to prevent security incidents.
"""
}
}
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/cortezaproject/corteza-server/pkg/logger"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/cortezaproject/corteza-server/pkg/version"
"github.com/cortezaproject/corteza-server/webconsole"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
@@ -22,7 +23,9 @@ import (
// routes used when server is in waiting mode
func waitingRoutes(log *zap.Logger, httpOpt options.HttpServerOpt) (r chi.Router) {
r = chi.NewRouter()
mountServiceHandlers(r, log, httpOpt)
r.Use(handleCORS)
mountServiceHandlers(r, log, httpOpt, waiting)
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
// For non GET requests, return 503 (service unavailable)
@@ -114,14 +117,40 @@ func activeRoutes(log *zap.Logger, mountable []func(r chi.Router), envOpt option
if httpOpt.BaseUrl != "/" {
r.Handle("/", http.RedirectHandler(httpOpt.BaseUrl, http.StatusTemporaryRedirect))
}
mountServiceHandlers(r, log, httpOpt)
mountServiceHandlers(r, log, httpOpt, active)
return
}
func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerOpt) {
func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerOpt, state uint32) {
if opt.WebConsoleEnabled {
path := "/console"
log.Info("web console enabled (HTTP_SERVER_WEB_CONSOLE_ENABLED=true): " + path)
r.Route(path, func(r chi.Router) {
if len(opt.WebConsolePassword) > 0 {
credentials := map[string]string{
opt.WebConsoleUsername: opt.WebConsolePassword,
}
r.Use(middleware.BasicAuth("web-console", credentials))
} else {
// warn only in waiting state to avoid repeated log messages
if state == waiting {
// warn the user regardless of what environment Corteza is running in.
log.Warn("SECURITY RISK: web console is enabled and unprotected, set " +
"HTTP_SERVER_WEB_CONSOLE_USERNAME, HTTP_SERVER_WEB_CONSOLE_PASSWORD " +
"if not running in development environment!")
}
}
webconsole.Mount(r)
mountDebugLogViewer(r, log)
// redirect from /console to /console/ui/
r.Mount("/", http.RedirectHandler(path+"/ui", http.StatusTemporaryRedirect))
})
}
if opt.EnableDebugRoute {
mountDebugHandler(r, log)
}
@@ -134,9 +163,10 @@ func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerO
mountHealthCheckHandler(r, log, opt.BaseUrl)
}
mountDebugLogViewer(r, log)
}
// @todo move all these routes under /console and
// output JSON instead of plain raw text
func mountDebugHandler(r chi.Router, log *zap.Logger) {
log.Debug("route debugger enabled: /__routes")
r.Get("/__routes", debugRoutes(r))
@@ -192,7 +222,7 @@ func mountHealthCheckHandler(r chi.Router, log *zap.Logger, basePath string) {
func mountDebugLogViewer(r chi.Router, log *zap.Logger) {
var (
path = "/view-log"
path = "/server-log-feed"
)
r.Get(path+".json", func(w http.ResponseWriter, r *http.Request) {
@@ -206,7 +236,7 @@ func mountDebugLogViewer(r chi.Router, log *zap.Logger) {
if aux := q.Get("after"); len(aux) > 0 {
after, err = strconv.Atoi(aux)
if err != nil {
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for after: %w", err), false)
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for after: %v", err), false)
return
}
}
@@ -214,13 +244,11 @@ func mountDebugLogViewer(r chi.Router, log *zap.Logger) {
if aux := q.Get("limit"); len(aux) > 0 {
limit, err = strconv.Atoi(aux)
if err != nil {
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for limit: %w", err), false)
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for limit: %v", err), false)
return
}
}
_, _ = logger.WriteLogBuffer(w, after, limit)
})
log.Debug("log viewer route enabled: " + path)
}

View File

@@ -48,7 +48,7 @@ func New(log *zap.Logger, envOpt options.EnvironmentOpt, httpOpt options.HttpSer
waitForOpt: waitForOpt,
}
s.demux = Demux(waiting, waitingRoutes(s.log, s.httpOpt))
s.demux = Demux(waiting, waitingRoutes(s.log.Named("waiting"), s.httpOpt))
s.demux.Router(shutdown, shutdownRoutes())
return s

View File

@@ -38,7 +38,14 @@ type (
// and mounts it to chi Router
func MountSPA(r chi.Router, path string, root fs.FS, cc ...configurator) error {
path = "/" + strings.Trim(strings.TrimRight(path, "*"), "/") + "/"
handler, err := FileServer(root, append(cc, UrlPrefix(path), Fallbacks("index.html"))...)
cc = append(
[]configurator{UrlPrefix(path), Fallbacks("index.html")},
// appnd all configurators at the end and allow override of prefix & fallbacks
cc...
)
handler, err := FileServer(root, cc...)
if err != nil {
return err
}

View File

@@ -1,5 +1,15 @@
package options
func (o *HttpServerOpt) Defaults() {
if Environment().IsDevelopment() {
// enable web console and remove username, password defaults
// if this is explicitly via ENV, it will override these defaults
o.WebConsoleEnabled = true
o.WebConsoleUsername = ""
o.WebConsolePassword = ""
}
}
func (o *HttpServerOpt) Cleanup() {
o.BaseUrl = CleanBase(o.BaseUrl)
o.ApiBaseUrl = CleanBase(o.ApiBaseUrl)

View File

@@ -43,6 +43,9 @@ type (
WebappBaseDir string `env:"HTTP_WEBAPP_BASE_DIR"`
WebappList string `env:"HTTP_WEBAPP_LIST"`
SslTerminated bool `env:"HTTP_SSL_TERMINATED"`
WebConsoleEnabled bool `env:"HTTP_SERVER_WEB_CONSOLE_ENABLED"`
WebConsoleUsername string `env:"HTTP_SERVER_WEB_CONSOLE_USERNAME"`
WebConsolePassword string `env:"HTTP_SERVER_WEB_CONSOLE_PASSWORD"`
}
RbacOpt struct {
@@ -325,6 +328,9 @@ func HttpServer() (o *HttpServerOpt) {
WebappBaseDir: "./webapp/public",
WebappList: "admin,compose,workflow,reporter",
SslTerminated: isSecure(),
WebConsoleEnabled: false,
WebConsoleUsername: "admin",
WebConsolePassword: string(rand.Bytes(32)),
}
// Custom defaults

14
webconsole/.eslintrc.cjs Normal file
View File

@@ -0,0 +1,14 @@
/* eslint-env node */
require("@rushstack/eslint-patch/modern-module-resolution");
module.exports = {
"root": true,
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/eslint-config-typescript/recommended",
],
"env": {
"vue/setup-compiler-macros": true
}
}

30
webconsole/.gitignore vendored Normal file
View File

@@ -0,0 +1,30 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
.DS_Store
dist
dist-ssr
!dist/.placeholder
coverage
*.local
public/config.js
/cypress/videos/
/cypress/screenshots/
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

46
webconsole/DEVELOPMENT.md Normal file
View File

@@ -0,0 +1,46 @@
# Corteza Server Web Console
## Type Support for `.vue` Imports in TS
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
1. Disable the built-in TypeScript Extension
1) Run `Extensions: Show Built-in Extensions` from VSCode's command palette
2) Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
## Customize configuration
See [Vite Configuration Reference](https://vitejs.dev/config/).
## Project Setup
```sh
npm install
```
### Compile and Hot-Reload for Development
```sh
npm run dev
```
### Type-Check, Compile and Minify for Production
```sh
npm run build
```
### Run Unit Tests with [Vitest](https://vitest.dev/)
```sh
npm run test:unit
```
### Lint with [ESLint](https://eslint.org/)
```sh
npm run lint
```

11
webconsole/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Corteza Server Web Console
When enabled (`HTTP_WEB_CONSOLE_ENABLED=true`), it allows insight and management of corteza internals.
## Web Console development
When developing web-console backend (API) base URL must be set to the actual server. That can be achieved by setting a URL as value local store item with `console-api-base-url` as key:
```javascript
localStorage.setItem('console-api-base-url', '//localhost:3000/console')
```

2
webconsole/dist/.placeholder vendored Normal file
View File

@@ -0,0 +1,2 @@
Placeholder file to avoid broken build when dist is empty (go error: contains no embeddable files)
File is copied from the public to dist when web console app is built.

1
webconsole/env.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
/// <reference types="vite/client" />

13
webconsole/index.html Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon32x32.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Corteza Server Web Console</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

41
webconsole/package.json Normal file
View File

@@ -0,0 +1,41 @@
{
"name": "corteza-server-web-console",
"license": "Apache-2.0",
"version": "0.0.0",
"contributors": [
"Denis Arh <denis.arh@crust.tech>"
],
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview --port 5050",
"test:unit": "vitest --environment jsdom",
"typecheck": "vue-tsc --noEmit && vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
},
"dependencies": {
"axios": "^0.26.0",
"pinia": "^2.0.11",
"sass": "^1.49.7",
"vue": "^3.2.29",
"vue-router": "^4.0.12"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.1.0",
"@types/jsdom": "^16.2.14",
"@types/node": "^16.11.22",
"@vitejs/plugin-vue": "^2.1.0",
"@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^10.0.0",
"@vue/test-utils": "^2.0.0-rc.18",
"@vue/tsconfig": "^0.1.3",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^8.2.0",
"jsdom": "^19.0.0",
"prettier": "^2.5.1",
"typescript": "~4.5.5",
"vite": "^2.7.13",
"vitest": "^0.2.5",
"vue-tsc": "^0.31.1"
}
}

View File

@@ -0,0 +1,2 @@
Placeholder file to avoid broken build when dist is empty (go error: contains no embeddable files)
File is copied from the public to dist when web console app is built.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

27
webconsole/server.go Normal file
View File

@@ -0,0 +1,27 @@
package webconsole
import (
"embed"
"io/fs"
"github.com/cortezaproject/corteza-server/pkg/http"
"github.com/go-chi/chi/v5"
)
var (
// we need combination of go:embed dist/* and .placeholder
// file inside. If only dist (w/o) wildcard is used,
// dot-file (.placeholder) will be ignored
//go:embed dist/*
assets embed.FS
)
func Mount(r chi.Router) error {
assets, err := fs.Sub(assets, "dist")
if err != nil {
panic(err)
}
return http.MountSPA(r, "/ui", assets, http.UrlPrefix("/console/ui"))
}

View File

@@ -0,0 +1,56 @@
<template>
<header>
<div class="wrapper">
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/log-viewer">Logs</RouterLink>
</nav>
</div>
</header>
<div
v-if="capturedError"
class="error"
>
<h2>Error</h2>
<p v-html="capturedError"></p>
</div>
<main
v-else
>
<router-view />
</main>
</template>
<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import { onBeforeMount, ref } from 'vue'
const capturedError = ref<string | undefined>(undefined)
onBeforeMount(() => {
//
})
</script>
<style lang="scss">
@import '@/assets/base.css';
nav {
background: black;
padding: 15px;
font-size: 140%;
a {
margin-right: 20px;
color: white;
text-decoration: none;
}
}
.error {
background: #ffa9a9;
padding: 40px;
height: 100vh;
}
</style>

View File

@@ -0,0 +1,5 @@
html, body {
margin: 0;
padding: 0;
font-family: monospace;
}

View File

@@ -0,0 +1,53 @@
import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
export interface LogEntry {
ts: Date
level: string
logger: string
msg: string
index: number
extra?: Record<string, unknown>
}
interface LogEntryResponse extends Omit<LogEntry, 'ts'> {
ts: string
[_:string]: unknown
}
interface LogFetchParams {
after?: number
limit?: number
}
let baseURL = '/console'
const storedBaseURL = window.localStorage.getItem('console-api-base-url')
if (storedBaseURL !== null) {
baseURL = storedBaseURL
console.warn('using base URL from local store (key: console-api-base-url)', { baseURL })
}
export async function fetchLoggedEvents (params: LogFetchParams = {}): Promise<Array<LogEntry>> {
const config: AxiosRequestConfig = {
params
}
return api()
.get<Array<LogEntryResponse>>('/server-log-feed.json', config)
.then(({ data }) => {
return data.map(({ ts, index, level, logger, msg, ...extra }) => ({
ts: new Date(Date.parse(ts)),
index,
level,
logger,
msg,
extra: Object.getOwnPropertyNames(extra).length > 0 ? extra : undefined
}))
})
}
function api(): AxiosInstance {
return axios.create({ baseURL })
}

12
webconsole/src/main.ts Normal file
View File

@@ -0,0 +1,12 @@
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './WebConsole.vue'
import router from './views'
const app = createApp(App)
app.use(createPinia())
app.use(router)
app.mount('#app')

View File

@@ -0,0 +1,16 @@
import { defineStore } from 'pinia'
export const useCounterStore = defineStore({
id: 'counter',
state: () => ({
counter: 0
}),
getters: {
doubleCount: (state) => state.counter * 2
},
actions: {
increment() {
this.counter++
}
}
})

View File

@@ -0,0 +1,10 @@
<template>
<section>
<h1>Welcome to Corteza Server Web Console.</h1>
</section>
</template>
<style lang="scss">
section {
padding: 20px;
}
</style>

View File

@@ -0,0 +1,129 @@
<template>
<section>
<h1>Log viewer</h1>
<table>
<thead>
<tr>
<td>one</td>
<td>level</td>
<td>logger</td>
<td>message</td>
</tr>
</thead>
<tfoot v-if="lastRefresh">
<tr>
<td
colspan="4"
class="last-refresh"
>
{{ lastRefresh.toISOString().substring(11) }}
</td>
</tr>
</tfoot>
<tbody>
<tr
v-for="e in events"
:key="e.index"
:class="[`level-${e.level}`]"
>
<td class="ts">{{ e.ts.toISOString().substring(11) }}</td>
<td class="level">{{ e.level }}</td>
<td class="logger">{{ e.logger }}</td>
<td class="msg">
{{ e.msg }}
<pre v-if="e.extra">{{ e.extra }}</pre>
</td>
</tr>
</tbody>
</table>
</section>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { fetchLoggedEvents } from '@/libs/logs'
import type { LogEntry } from '@/libs/logs'
let intervalHandler: number | undefined
const events = ref<Array<LogEntry>>([])
const lastRefresh = ref<Date | undefined>()
onMounted(() => {
events.value = []
fetch().then((interval: boolean) => {
if (!interval) {
return
}
setInterval(async () => {
let after: number | undefined
if (events.value.length > 0) {
after = events.value[events.value.length - 1].index
}
await fetch(after)
}, 2000)
}).catch((err ) => {
alert(err)
})
})
async function fetch (after?: number): Promise<boolean> {
return fetchLoggedEvents({ after, limit: -1 })
.then(ee => {
events.value.push(...ee)
lastRefresh.value = new Date()
return true
})
.catch((err) => {
alert(err)
return false
})
}
onUnmounted(() => {
if (intervalHandler) {
clearInterval(intervalHandler)
}
})
</script>
<style lang="scss" scoped>
table {
width: 100vw;
font-family: monospace;
tbody {
tr {
&.level-warn {
background: gold;
}
&.level-debug .msg {
color: gray;
}
td {
padding: 3px;
margin: 0;
border-top: 1px dotted silver;
vertical-align: top;
&.msg pre {
color: gray;
}
}
}
}
tfoot {
.last-refresh {
padding: 20px;
text-align: center;
}
}
}
</style>

View File

@@ -0,0 +1,23 @@
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './HomeView.vue'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/log-viewer',
name: 'log-viewer',
// route level code-splitting
// this generates a separate chunk (About.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import('./LogViewer.vue')
}
]
})
export default router

20
webconsole/tsconfig.json Normal file
View File

@@ -0,0 +1,20 @@
{
"extends": "@vue/tsconfig/tsconfig.web.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"exclude": ["src/**/__tests__/*"],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"references": [
{
"path": "./tsconfig.vite-config.json"
},
{
"path": "./tsconfig.vitest.json"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"extends": "@vue/tsconfig/tsconfig.node.json",
"include": ["vite.config.*"],
"compilerOptions": {
"composite": true,
"types": ["node", "vitest"]
}
}

View File

@@ -0,0 +1,12 @@
{
"extends": "@vue/tsconfig/tsconfig.node.json",
"include": ["src/**/__tests__/*"],
"compilerOptions": {
"composite": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
},
"types": ["node", "jsdom"]
}
}

17
webconsole/vite.config.ts Normal file
View File

@@ -0,0 +1,17 @@
import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
base: '/console/ui/',
plugins: [vue()],
resolve: {
alias: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
});

2373
webconsole/yarn.lock Normal file

File diff suppressed because it is too large Load Diff