Logs Async Consumers

The web console terminal’s WebSocket: a bash shell inside the server’s own container.

TerminalConsumer runs a shell.TerminalShell for each connection, in the server process that accepted it, and so in whichever pod, or container, the load balancer chose. It passes the browser’s keystrokes to the shell, and the shell’s output to the browser, where xterm.js draws it. See messages for the protocol.

The shell runs as the server’s operating system user, with the server’s environment, and so with every credential that the server has. Only a superuser can connect, while smarter_settings.app_dashboard_web_console_terminal_enable is set, with a single use ticket from tickets.TerminalTicketApiView. A connection that fails a check is refused before the WebSocket is accepted, and the refusal is logged.

A session ends when the shell exits, when the WebSocket closes (the page is closed or reloaded, the connection drops, or the server restarts), after const.TERMINAL_IDLE_TIMEOUT seconds without input, or after const.TERMINAL_MAX_DURATION seconds. Its shell, and every process of the shell’s session, is then hung up and killed. Each session’s start and end are logged, with its user, host and shell, but not what was typed, which can include credentials.

class smarter.apps.dashboard.views.terminal_emulator.api.consumers.TerminalConsumer(*args, **kwargs)[source]

Bases: AsyncWebsocketConsumer, SmarterHelperMixin

A bash shell for a superuser, over a WebSocket.

See the module’s docstring.

__init__(*args, **kwargs)[source]
async connect()[source]

Check the user and the ticket, then accept the connection and start the shell.

async disconnect(code)[source]

The WebSocket closed: end the shell.

async receive(text_data=None, bytes_data=None)[source]

Input or a resize from the browser.

A binary message is input. A message that is not valid is ignored.

async refuse(reason)[source]

Refuse the connection, before accepting it: the browser sees the handshake fail.

Parameters:

reason (str) – Why, for the log.

Return type:

None

async send_output(data)[source]

Send the shell’s output to the browser.

Parameters:

data (bytes) – The output.

Return type:

None

sessions: ClassVar[set[TerminalConsumer]] = {}

The sessions that this server process runs.

async shell_exited(code)[source]

The shell exited: tell the browser why, and close the WebSocket.

Parameters:

code (int | None) – The shell’s exit status.

Return type:

None

property username: str

The connected user’s name, for the log.

async watch()[source]

End the session once it has been idle, or has lasted, too long.

Return type:

None