|
| 1 | +# ActiveAdminVpn |
| 2 | + |
| 3 | +Restrict access to [ActiveAdmin](https://activeadmin.info/) to requests coming from |
| 4 | +configured VPN IP ranges. Optionally auto-login VPN users so they skip the login form. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +Add to your Gemfile: |
| 9 | + |
| 10 | +```ruby |
| 11 | +gem "active_admin_vpn" |
| 12 | +``` |
| 13 | + |
| 14 | +Then run: |
| 15 | + |
| 16 | +```bash |
| 17 | +bundle install |
| 18 | +rails generate active_admin_vpn:install |
| 19 | +``` |
| 20 | + |
| 21 | +This creates `config/initializers/active_admin_vpn.rb`. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +### Option 1 — Environment variable (recommended) |
| 28 | + |
| 29 | +Set `VPN_IP_RANGE` in your environment (`.env`, Heroku config vars, etc.): |
| 30 | + |
| 31 | +```bash |
| 32 | +VPN_IP_RANGE="10.0.0.0/8,192.168.1.0/24" |
| 33 | +``` |
| 34 | + |
| 35 | +The gem reads this automatically — no extra code needed. |
| 36 | + |
| 37 | +### Option 2 — Initializer |
| 38 | + |
| 39 | +```ruby |
| 40 | +# config/initializers/active_admin_vpn.rb |
| 41 | +ActiveAdminVpn.configure do |config| |
| 42 | + config.ip_ranges = "10.0.0.0/8,192.168.1.0/24" |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Auto-login (skip login form on VPN) |
| 49 | + |
| 50 | +```ruby |
| 51 | +ActiveAdminVpn.configure do |config| |
| 52 | + config.ip_ranges = ENV["VPN_IP_RANGE"] |
| 53 | + config.auto_login = true |
| 54 | + config.auto_login_email = "admin@yourcompany.com" |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +When `auto_login` is `true`, any request from a VPN IP is automatically signed in |
| 59 | +as the given `AdminUser` — no username/password required. |
| 60 | + |
| 61 | +> **Security note:** Keep the standard login enabled for non-VPN users. Auto-login |
| 62 | +> only applies when the request IP is inside a configured VPN range. |
| 63 | +
|
| 64 | +--- |
| 65 | + |
| 66 | +## Route constraint (optional) |
| 67 | + |
| 68 | +If you prefer to block at the routing layer instead of the controller layer: |
| 69 | + |
| 70 | +```ruby |
| 71 | +# config/routes.rb |
| 72 | +constraints(ActiveAdminVpn::VpnConstraint) do |
| 73 | + ActiveAdmin.routes(self) |
| 74 | +end |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## Trusted proxies |
| 80 | + |
| 81 | +If your app runs behind a load balancer or reverse proxy, make sure Rails trusts |
| 82 | +the forwarded IP headers so `request.remote_ip` reflects the real client IP: |
| 83 | + |
| 84 | +```ruby |
| 85 | +# config/application.rb |
| 86 | +config.action_dispatch.trusted_proxies = [ |
| 87 | + "127.0.0.1", |
| 88 | + "::1", |
| 89 | + IPAddr.new("10.0.0.0/8"), |
| 90 | + IPAddr.new("172.16.0.0/12"), |
| 91 | + IPAddr.new("192.168.0.0/16") |
| 92 | +] |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## All configuration options |
| 98 | + |
| 99 | +| Option | Default | Description | |
| 100 | +|--------------------|-------------|------------------------------------------------------| |
| 101 | +| `ip_ranges` | `VPN_IP_RANGE` env | Comma-separated CIDR ranges | |
| 102 | +| `auto_login` | `false` | Skip login form for VPN users | |
| 103 | +| `auto_login_email` | `nil` | AdminUser email used for auto-login | |
| 104 | +| `forbidden_status` | `:forbidden`| HTTP status for blocked requests (403) | |
| 105 | +| `forbidden_message`| `"Access denied..."` | Body text for blocked requests | |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## License |
| 110 | + |
| 111 | +MIT |
0 commit comments