Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ jobs:
echo "::error::DOKPLOY_API_KEY is not set; cannot trigger the deployment."
exit 1
fi
curl -fsS -X POST "https://dokploy.hacksubset.com/api/compose.deploy" \
status=$(curl -sS -o /tmp/dokploy-response.json -w '%{http_code}' \
-X POST "https://dokploy.hacksubset.com/api/compose.deploy" \
-H "x-api-key: $DOKPLOY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"composeId":"qFd9GlvcRtYr8iYIYNwhl","title":"GitHub tag ${{ github.ref_name }}"}'
-d '{"composeId":"qFd9GlvcRtYr8iYIYNwhl","title":"GitHub tag ${{ github.ref_name }}"}')
echo "Dokploy answered HTTP $status"
head -c 500 /tmp/dokploy-response.json; echo
test "$status" -ge 200 && test "$status" -lt 300
17 changes: 17 additions & 0 deletions changelog/v0.2.16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# v0.2.16

IP access control for the console and the public API.

## Added

- IP allow/deny rules enforced before authentication. Rules are CIDR-based,
scoped to `admin`, `api` or `all`, can expire, and deny wins. An allow rule
turns its scope into an allowlist; prefix `/0` is rejected so a rule cannot
lock every address out.
- Address observations for `/admin` and `/v1`: IP, country from `CF-IPCountry`,
user agent, first/last seen and request count, recorded at most once a minute
per address.
- `/admin/ip-rules` (list, create, enable/disable, delete) and
`/admin/ip-observations`.
- The console gains an **IP access** page with CIDR rules, expiry, observed
addresses and a one-click **Block IP** action.
3 changes: 2 additions & 1 deletion docs/ADMIN-UI.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The admin console lives in its own repository, [an1ndra/path-admin](https://gith

Read and write against this API: dashboard (spend, cache, latency, provider circuits, top
models, recent failures), usage (filter by key/end user/window, page through rows, export
CSV), audit (every admin write, filterable by action), providers (create/edit with a
CSV), audit (every admin write, filterable by action), IP access (allow/deny CIDR rules and
the addresses seen, with country and user agent), providers (create/edit with a
provider API key, probe, delete), routes (add a model, enable/disable, delete), client keys
(create with budgets and allowlists, top up, revoke), **configuration** (the gateway
switches, the default system prompt, alert email and the daily digest) and **settings**
Expand Down
5 changes: 5 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ ids land at `0.00` and are flagged in `description`.
| GET | `/admin/stats?window=24h` | totals, percentiles, cache, providers/models |
| GET | `/admin/stats?window=24h&bucket=1h` | the same, plus a bucketed time series |
| GET | `/admin/usage?window=24h&limit=100&page=0&keyId=&endUser=` | raw usage rows, paged; the response carries `total` and `has_more` |
| GET | `/admin/ip-rules` | IP allow/deny rules |
| POST | `/admin/ip-rules` | add a rule (`cidr`, `scope`, `action`, `note`, `expires_at`) |
| PATCH | `/admin/ip-rules/{id}` | enable or disable a rule |
| DELETE | `/admin/ip-rules/{id}` | delete a rule |
| GET | `/admin/ip-observations?limit=100` | addresses seen at `/admin` and `/v1`, with country and user agent |
| GET | `/admin/audit?limit=100&page=0&action=` | admin writes, newest first |
| GET | `/admin/prompt-logs?limit=50&page=0&keyId=` | stored prompts when logging is on |
| POST | `/admin/digest/test?date=2026-11-19` | send yesterday's summary now and return its text |
Expand Down
4 changes: 4 additions & 0 deletions docs/OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ curl -s 'localhost:8080/admin/stats?window=24h' -H "$A"
curl -s 'localhost:8080/admin/usage?window=7d&limit=50&page=0' -H "$A"
curl -s 'localhost:8080/admin/usage?window=7d&keyId=3&endUser=alice' -H "$A"
curl -s 'localhost:8080/admin/usage/export?window=30d&limit=5000' -H "$A" -o usage.csv
curl -s 'localhost:8080/admin/ip-rules' -H "$A"
curl -s -X POST 'localhost:8080/admin/ip-rules' -H "$A" -H 'Content-Type: application/json' \
-d '{"cidr":"203.0.113.0/24","scope":"all","action":"deny","note":"abuse report"}'
curl -s 'localhost:8080/admin/ip-observations?limit=50' -H "$A"
curl -s 'localhost:8080/admin/audit?action=route.update&limit=50&page=0' -H "$A"
curl -s 'localhost:8080/admin/prompt-logs?limit=20&page=0' -H "$A"
```
Expand Down
2 changes: 1 addition & 1 deletion path-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.an1ndra</groupId>
<artifactId>path-parent</artifactId>
<version>0.2.15</version>
<version>0.2.16</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.anindra.path.admin;

import com.anindra.path.api.ApiException;
import com.anindra.path.config.AuthFilter;
import com.anindra.path.gateway.IpAccessService;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
* IP allow/deny rules and the last-seen addresses that reached the gateway. Rules are
* enforced in {@link AuthFilter} before authentication, so a blocked address cannot even
* use a valid key.
*/
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class IpAccessController {

private final IpAccessService ipAccess;

public record RuleInput(String cidr, String scope, String action, String note,
@JsonAlias("expiresAt") String expires_at) {
}

public record EnabledInput(Boolean enabled) {
}

@GetMapping("/ip-rules")
public List<Map<String, Object>> rules() {
return ipAccess.listRules();
}

@PostMapping("/ip-rules")
public Map<String, Object> create(@RequestBody RuleInput input, HttpServletRequest request) {
return ipAccess.createRule(input.cidr(), input.scope(), input.action(), input.note(),
instant(input.expires_at()), actor(request));
}

@PatchMapping("/ip-rules/{id}")
public Map<String, Object> enable(@PathVariable Long id, @RequestBody EnabledInput input) {
if (input.enabled() == null) {
throw ApiException.badRequest("'enabled' is required.", "enabled");
}
return ipAccess.setEnabled(id, input.enabled());
}

@DeleteMapping("/ip-rules/{id}")
public Map<String, Object> delete(@PathVariable Long id) {
ipAccess.deleteRule(id);
return Map.of("deleted", true);
}

@GetMapping("/ip-observations")
public List<Map<String, Object>> observations(
@RequestParam(defaultValue = "100") int limit) {
return ipAccess.listObservations(limit);
}

private static Instant instant(String value) {
if (value == null || value.isBlank()) {
return null;
}
try {
return Instant.parse(value.trim());
} catch (java.time.format.DateTimeParseException e) {
throw ApiException.badRequest(
"'expires_at' must be an ISO-8601 instant.", "expires_at");
}
}

private static String actor(HttpServletRequest request) {
Object principal = request.getAttribute(AuthFilter.ADMIN_PRINCIPAL_ATTR);
if (principal instanceof TenancyService.Principal p && p.name() != null) {
return p.name();
}
return "admin";
}
}
16 changes: 16 additions & 0 deletions path-gateway/src/main/java/com/anindra/path/config/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class AuthFilter extends OncePerRequestFilter {

private final ApiKeyService apiKeys;
private final RateLimitStore rateLimiter;
private final com.anindra.path.gateway.IpAccessService ipAccess;
private final com.anindra.path.admin.TenancyService tenancy;
private final AdminAuthLimiter adminAuthLimiter;
private final ClientAuthLimiter clientAuthLimiter;
Expand All @@ -64,6 +65,21 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

try {
// IP rules are enforced before authentication, so a blocked address cannot
// spend valid credentials. The actuator stays out of this: blocking it would
// break the orchestrator's health probe.
String ruleScope = path.startsWith("/admin") ? "admin"
: path.startsWith("/v1") ? "api" : null;
if (ruleScope != null) {
String address = clientAddress(request);
com.anindra.path.gateway.IpAccessService.Decision decision = ipAccess.check(address, ruleScope);
if (!decision.allowed()) {
throw new ApiException(403, "permission_error", decision.code(), decision.message());
}
ipAccess.observe(address, ruleScope, request.getHeader("CF-IPCountry"),
request.getHeader("User-Agent"));
}

if (path.startsWith("/admin")) {
requireAdmin(request);
} else if (path.startsWith("/actuator")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.anindra.path.domain;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.Instant;

/** Last-seen metadata for an address that reached the console or the API. */
@Entity
@Table(name = "ip_observations")
@Getter
@Setter
@NoArgsConstructor
public class IpObservation {

@Id
@Column(length = 64)
private String ip;

@Column(nullable = false, length = 16)
private String scope;

@Column(length = 16)
private String country;

@Column(length = 255)
private String userAgent;

@Column(nullable = false)
private Instant firstSeen = Instant.now();

@Column(nullable = false)
private Instant lastSeen = Instant.now();

@Column(nullable = false)
private long requests;
}
47 changes: 47 additions & 0 deletions path-gateway/src/main/java/com/anindra/path/domain/IpRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.anindra.path.domain;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.Instant;

/** An allow or deny rule for a CIDR, scoped to the console, the API, or both. */
@Entity
@Table(name = "ip_rules")
@Getter
@Setter
@NoArgsConstructor
public class IpRule {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

/** IPv4/IPv6 address or CIDR. */
@Column(nullable = false, length = 64)
private String cidr;

/** admin, api or all. */
@Column(nullable = false, length = 16)
private String scope;

/** allow or deny. Deny wins when both match. */
@Column(nullable = false, length = 8)
private String action;

@Column(length = 255)
private String note;

@Column(nullable = false)
private boolean enabled = true;

private Instant expiresAt;

@Column(length = 128)
private String createdBy;

@Column(nullable = false)
private Instant createdAt = Instant.now();
}
Loading