Metadata-Version: 2.5
Name: inferyx-codegate
Version: 2.0.7
Summary: Multi-user browser VS Code (code-server) on one server: password and AWS IAM Identity Center (SSO) login, per-user Docker workspaces and an admin portal.
Author: Inferyx
License: Proprietary
Requires-Python: >=3.10
Requires-Dist: docker>=7.0
Requires-Dist: fastapi>=0.110
Requires-Dist: httpx>=0.27
Requires-Dist: packaging>=23.0
Requires-Dist: psutil>=5.9
Requires-Dist: pydantic-settings>=2.2
Requires-Dist: pydantic>=2.6
Requires-Dist: python-multipart>=0.0.9
Requires-Dist: rich>=13.7
Requires-Dist: sqlalchemy>=2.0
Requires-Dist: typer>=0.12
Requires-Dist: uvicorn[standard]>=0.29
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: python3-saml>=1.16; extra == 'dev'
Provides-Extra: saml
Requires-Dist: python3-saml>=1.16; extra == 'saml'
Description-Content-Type: text/markdown

# Inferyx CodeGate

Browser VS Code (code-server) for many users on one Ubuntu server. Each user gets their own
Docker workspace; admins manage everything from a web portal.

- **Login:** username/password, and optionally AWS IAM Identity Center (SSO).
- **One container per user**, with CPU/RAM/disk limits and its own home folder.
- **Admin portal** for users, approvals, resources, sessions, audit log and settings.

```
https://code.example.com/codeapp/                    → sign in
https://code.example.com/codeapp/editor/<username>/  → that user's VS Code
https://code.example.com/codeapp/admin/              → admin portal
```

## Requirements

- Ubuntu 24.04 server with root (sudo) access
- Python 3.10+ (`sudo apt install -y python3-pip python3-venv`)
- A DNS name pointing to the server, ports 80 and 443 open
- Optional: AWS IAM Identity Center, if you want SSO login

## Install

```bash
# 1. install into its own virtual environment
sudo python3 -m venv /opt/inferyx-codegate
sudo /opt/inferyx-codegate/bin/pip install "inferyx-codegate[saml]"
sudo ln -sf /opt/inferyx-codegate/bin/inferyx-codegate /usr/local/bin/inferyx-codegate

# 2. set everything up (asks for the domain and the default admin's email + password)
sudo inferyx-codegate setup

# 3. check
sudo inferyx-codegate doctor
```

If installing `[saml]` fails to build `xmlsec`, install `inferyx-codegate` without `[saml]`;
`inferyx-codegate setup` then installs the system libraries and the SAML library for you.

Unattended install:

```bash
sudo inferyx-codegate setup -y --domain code.example.com --admin-email admin@example.com \
  --port 8443 \
  --admin-password-file /root/admin-pw.txt   # omit to generate a password (shown once)
```

### What setup does

Setup is **safe to run again**: it checks each part and skips what's already in place.

| Part | What happens |
|---|---|
| Packages | Installs only what's missing: nginx, docker.io, quota, certbot, xmlsec libraries |
| Docker | Reuses an existing Docker install; builds the workspace image (code-server, Python, AWS CLI) |
| Default admin | Creates the first admin with password login. Must change a generated password at first login |
| nginx | Writes **only its own files** (`/etc/nginx/inferyx-codegate/`, `conf.d/inferyx-codegate-map.conf`, and with a separate port `sites-available/inferyx-codegate`). Never edits your existing nginx files (see below) |
| HTTPS | Runs certbot only for a site Inferyx CodeGate created itself (`--skip-ssl` to skip) |
| Host SSH | Adds `/etc/ssh/sshd_config.d/10-inferyx-codegate.conf` so workspace users can't SSH into the server |
| Service | systemd service `inferyx-codegate` plus a daily backup timer |
| Existing code-server | Detected; you choose keep, stop or disable |

### nginx: choose how it is served

Setup asks for the **HTTPS port**. Your existing nginx sites (AWX, Tomcat, …) are never edited.

**Option A: separate port (e.g. `8443`). Nothing else to do on the server.**
Setup creates its own site file `/etc/nginx/sites-available/inferyx-codegate` that listens on
that port for your domain and reuses the domain's existing certificate (read-only). The login
URL becomes `https://<your-domain>:8443/codeapp/`. **Open that port** in the AWS security group
(and ufw, if used).

```bash
sudo inferyx-codegate setup --port 8443
```

**Option B: port 443 (same URL as your other sites). Default.**
nginx serves one `server { }` block per domain and port, so that block has to load the portal's
routes. Setup does this for you: it creates the folder `/etc/nginx/<your-domain>.d/`, links the
portal's config into it, and (after asking once; `-y` accepts) adds **one** line to the existing
443 block, right after `server_name`:

```nginx
include /etc/nginx/<your-domain>.d/*.conf;
```

Nothing else in that file changes. A backup is kept in `/var/lib/inferyx-codegate/backups/`,
`nginx -t` checks the result, and the original is restored automatically on any error. From
then on, all portal routes live in their own files. `--no-nginx-edit` only prints the line.

If the domain has no server block yet, setup creates its own site (port 80 + certbot).

## First sign-in

1. Open `https://<your-domain>/codeapp/` and sign in as the default admin.
2. Change the password when asked.
3. **Users → Add user**: enter the email, set a temporary password (or **Generate**), choose a
   plan. The user's Linux account, home folder, container and route are created automatically.
4. Give the user their temporary password over a secure channel. They must change it at first
   sign-in, then VS Code opens.

## AWS SSO login (optional)

1. AWS console → IAM Identity Center → **Applications** → **Add application** →
   *I have an application I want to set up* → **SAML 2.0**.
2. In Inferyx CodeGate: **Settings → Login**. Copy the **ACS URL** and **SAML audience** shown there into
   the AWS application ("Manually type your metadata values").
3. AWS **Attribute mappings**:

   | User attribute in the application | Maps to | Format |
   |---|---|---|
   | Subject | `${user:email}` | emailAddress |
   | email | `${user:email}` | unspecified |
   | name *(optional)* | `${user:name}` | unspecified |

4. Copy the **IAM Identity Center SAML metadata file** URL from AWS into **SAML metadata URL**,
   click **Test metadata**, switch on **AWS SSO login**, and **Save**.
5. In AWS, **assign users or groups** to the application. New SSO users wait as *pending* until
   an admin approves them, unless their email domain is on the auto-approve list.

Password login and SSO can each be switched off in **Settings → Login**; at least one stays on.

## The VS Code workspace

Each user's VS Code runs in its own container and is set up for company use:

- Browser tab shows **"<user> — Inferyx CodeGate"** with the Inferyx icon.
- VS Code's own **Accounts** menu, **Chat/Copilot** panel, welcome page and **Restricted Mode**
  banner are turned off. Sign-in is CodeGate's; the status bar shows **"<user> · Sign out"**,
  which signs out in the same tab (the editor closes).
- The ⚙ menu keeps what users need (Settings, Extensions, Keyboard Shortcuts, Themes, Snippets,
  Tasks); Profiles and Layout Density are hidden.
- While the editor downloads (about 5 MB), users see "Loading your workspace…" instead of VS Code's
  early "The editor failed to load" message. Tip: with a valid HTTPS certificate the browser caches
  the editor and later visits load in seconds; with a certificate warning it re-downloads every time.
- System files in the home folder (`.bashrc`, `.cache`, `.config`, …) are hidden in the Explorer
  (still on disk). A project's own dotfiles such as `.git` or `.env` stay visible.
- Telemetry and update checks are off.

These settings are re-applied every time a workspace starts. Users can still change their own
editor settings (theme, font, keybindings).

## Login security

- Passwords are stored only as salted scrypt hashes. Rules: at least 12 characters, 3 of
  lowercase / uppercase / digit / symbol, not containing the username or email.
- After 5 failed attempts an account is locked for 15 minutes (adjustable); admins can unlock it
  from the user page. Attempts are also rate-limited per IP address.
- Sessions use a random token in an `HttpOnly`, `Secure` cookie; only its hash is stored. A new
  token is issued at every sign-in and password change. Session length and idle timeout are in
  **Settings**.
- Changing or resetting a password ends the user's other sessions. Turning a login method off
  ends the sessions that used it.
- Sign-in, sign-out and password forms are protected against cross-site requests.

### Locked out of the web portal?

On the server:

```bash
sudo inferyx-codegate user passwd <admin-username-or-email>   # set a new password (--generate for random)
sudo inferyx-codegate user unlock <user>
sudo inferyx-codegate auth show
sudo inferyx-codegate auth enable password
```

## Day-to-day commands

```bash
sudo inferyx-codegate status | start | stop | restart | logs -f
sudo inferyx-codegate doctor                                 # checks everything, prints fix hints
sudo inferyx-codegate user add dev@example.com --plan medium --password
sudo inferyx-codegate user set <user> --plan large           # CPU/RAM apply live
sudo inferyx-codegate user delete <user> --remove-home       # home is archived first
sudo inferyx-codegate admin add second-admin@example.com
sudo inferyx-codegate backup | restore <file>
```

Full list: `inferyx-codegate --help`, and `inferyx-codegate <command> --help`.

## Upgrade

```bash
sudo inferyx-codegate upgrade --dry-run    # show the plan
sudo inferyx-codegate upgrade              # latest version from PyPI
```

Before upgrading, it backs up the database, config and nginx files, plus the installed package.
After installing, it runs migrations, refreshes nginx routes and restarts the service. If any
step fails, the previous version is restored automatically.

## Disk limits

Without Linux disk quotas, limits are monitored every 5 minutes and a workspace over its limit
is stopped. For hard limits, enable quotas on the `/home` filesystem (maintenance window):

```bash
sudo apt install -y quota linux-image-extra-virtual
# add usrquota to the /home (or /) line in /etc/fstab, then:
sudo mount -o remount /
sudo quotacheck -cum /
sudo quotaon -v /
sudo inferyx-codegate doctor               # "Disk quotas on /home" should show ✓
```

## Files

| Path | Contents |
|---|---|
| `/etc/inferyx-codegate/inferyx-codegate.env` | configuration (`inferyx-codegate config show`) |
| `/var/lib/inferyx-codegate/inferyx-codegate.db` | database: users, sessions, audit log, settings |
| `/var/lib/inferyx-codegate/backups/` | daily and pre-upgrade backups |
| `/var/log/inferyx-codegate/inferyx-codegate.log` | service log |
| `/etc/nginx/inferyx-codegate/` | generated nginx config |
| `/home/<user>` | user files (mounted into their container) |

## Good to know

- Admins can't open other users' VS Code.
- Workspace users can't sign in to the server over SSH; they only get VS Code.
- **sudo inside a workspace** makes the user root in their container. Only give it to people you
  would trust with the server. Mounting `/home` with `nosuid` adds protection
  (`inferyx-codegate doctor` checks it).
- The service runs as root (it manages Linux users, Docker and nginx) but listens only on
  127.0.0.1; nginx is the only public entry point.
- Signing out of Inferyx CodeGate doesn't sign the user out of the AWS access portal.
