argos-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +27 -0
- data/LICENSE +21 -0
- data/README.md +184 -0
- data/docs/architecture.md +55 -0
- data/docs/benchmarks.md +29 -0
- data/docs/limits.md +44 -0
- data/docs/verification.md +52 -0
- data/lib/argos/adapters/http/http_spec_source.rb +30 -0
- data/lib/argos/adapters/http/net_http_transport.rb +106 -0
- data/lib/argos/application/services/response_interpreter.rb +44 -0
- data/lib/argos/application/services/spec_validation_service.rb +76 -0
- data/lib/argos/application/use_cases/block_entity.rb +40 -0
- data/lib/argos/application/use_cases/check_access.rb +65 -0
- data/lib/argos/application/use_cases/detect_environment.rb +31 -0
- data/lib/argos/application/use_cases/event_queue.rb +64 -0
- data/lib/argos/application/use_cases/get_decision.rb +18 -0
- data/lib/argos/application/use_cases/health_check.rb +20 -0
- data/lib/argos/application/use_cases/ingest_batch.rb +38 -0
- data/lib/argos/application/use_cases/ingest_event.rb +90 -0
- data/lib/argos/composition/argos_client.rb +83 -0
- data/lib/argos/domain/caching/blocklist_cache.rb +34 -0
- data/lib/argos/domain/configuration/config.rb +25 -0
- data/lib/argos/domain/errors/errors.rb +27 -0
- data/lib/argos/domain/events/event_normalizer.rb +138 -0
- data/lib/argos/domain/events/payload_builder.rb +67 -0
- data/lib/argos/domain/models/verdict.rb +24 -0
- data/lib/argos/domain/resilience/circuit_breaker.rb +50 -0
- data/lib/argos/domain/resilience/retry_calculator.rb +21 -0
- data/lib/argos/domain/validation/metadata_validator.rb +56 -0
- data/lib/argos/domain/validation/openapi_spec.rb +45 -0
- data/lib/argos/domain/validation/openapi_validator.rb +67 -0
- data/lib/argos/middleware.rb +159 -0
- data/lib/argos/ports/outbound/clock.rb +14 -0
- data/lib/argos/ports/outbound/diagnostics.rb +24 -0
- data/lib/argos/ports/outbound/spec_source.rb +13 -0
- data/lib/argos/ports/outbound/transport.rb +29 -0
- data/lib/argos/railtie.rb +28 -0
- data/lib/argos/version.rb +3 -0
- data/lib/argos-sdk.rb +4 -0
- data/lib/argos.rb +75 -0
- metadata +133 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 03c626ec33d24fd2c125e9f0e0703279b72e827a38a2288f9e6bc3e08914b47b
|
|
4
|
+
data.tar.gz: 989a70839a2c8d069102b71e2b1dba7b935fe73638ea1980ac0cbe16bcaba2a8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e8dd01344a7ac4dcbd912571e3c33875de5fca4b99b09cc9145518f09ffaace745df4d0127e66d9645ce69043a816958eeb10cd0d5baa08dfda3b0c026e9f82b
|
|
7
|
+
data.tar.gz: 6c4acb6125d1f57242451cbc0129255d67944f0e40a1ea612ee4b0c58ae097c5d8475217364fbd9423f292489413043957fc91341e213c84f1ab034335ab5bcd
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `argos_sdk` are documented here. This project follows
|
|
4
|
+
[Semantic Versioning](https://semver.org/).
|
|
5
|
+
|
|
6
|
+
## [1.0.0] — 2026-12-03
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
- Rack middleware for Rails, Sinatra, Hanami, Roda, Cuba and Grape.
|
|
10
|
+
- Real-time ingestion with `ALLOW` / `BLOCK` normalisation and fail-open
|
|
11
|
+
middleware semantics.
|
|
12
|
+
- Blocklist checks with an in-process TTL cache and auto-block on `BLOCK`.
|
|
13
|
+
- Exponential backoff retry and a fail-open circuit breaker.
|
|
14
|
+
- OpenAPI request validation attached to event metadata as `sdk_validation`.
|
|
15
|
+
- Flat-event normalisation to the strict v2 ingestion contract, with
|
|
16
|
+
`ValidationError` naming any missing required field.
|
|
17
|
+
- Batch ingestion with the same normalisation.
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
- The gem now declares `required_ruby_version` and ships LICENSE/CHANGELOG.
|
|
21
|
+
- Ruby 3.4's bundled `erb` is declared as a test dependency so rspec starts.
|
|
22
|
+
- CI integration tests start `tool/mock_argos_server.rb` and point the demo
|
|
23
|
+
apps at it via `ARGOS_BASE_URL`, so they no longer depend on the public
|
|
24
|
+
gateway (whose bootstrap calls could stall the first request past the
|
|
25
|
+
smoke test's timeout).
|
|
26
|
+
|
|
27
|
+
[1.0.0]: https://github.com/Argos-Security/argos-ruby/releases/tag/v1.0.0
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Argos-Security <contact@argossecops.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Argos Security SDK for Ruby
|
|
2
|
+
|
|
3
|
+
Real-time threat detection and blocklist enforcement for Rack-based Ruby apps.
|
|
4
|
+
The SDK ships every request to the Argos ingestion API, turns a `BLOCK` verdict
|
|
5
|
+
into a `403` before your app runs, and can cache/auto-block the offending IP.
|
|
6
|
+
|
|
7
|
+
It is plain Rack middleware, so it works with **Rails, Sinatra, Hanami, Roda,
|
|
8
|
+
Cuba and Grape** (any Rack stack), plus a zero-config Railtie for Rails.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
# Gemfile
|
|
16
|
+
gem "argos-sdk"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
bundle install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Requires Ruby >= 3.1 and Rack >= 2.0.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require "argos"
|
|
31
|
+
|
|
32
|
+
config = Argos::Config.new
|
|
33
|
+
config.api_key = ENV.fetch("ARGOS_API_KEY")
|
|
34
|
+
# Required by the ingestion service (X-Environment-ID). Auto-detection is
|
|
35
|
+
# best-effort, so set it explicitly in production.
|
|
36
|
+
config.environment_id = ENV.fetch("ARGOS_ENVIRONMENT_ID")
|
|
37
|
+
config.auto_block_on_block = true
|
|
38
|
+
|
|
39
|
+
client = Argos::Client.new(config)
|
|
40
|
+
|
|
41
|
+
# The ergonomic flat event is normalised to the strict v2 shape.
|
|
42
|
+
event = client.ingest(
|
|
43
|
+
"event_type" => "login",
|
|
44
|
+
"user_id" => "user123",
|
|
45
|
+
"ip" => "203.0.113.7",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if client.is_blocked_verdict?(event)
|
|
49
|
+
warn "blocked: #{event["reason"] || event.dig("decision", "reason")}"
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Sinatra / Rails / Roda:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
use Argos::Middleware, client: client, mode: :sync
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`mode: :sync` waits for the verdict and blocks inline; `mode: :async` enqueues
|
|
60
|
+
the event and returns immediately.
|
|
61
|
+
|
|
62
|
+
### Verified local run
|
|
63
|
+
|
|
64
|
+
`tool/mock_argos_server.rb` is a small stand-in for the gateway (WEBrick, test
|
|
65
|
+
group dependency). `examples/middleware_demo.rb` drives the real Rack
|
|
66
|
+
middleware over real HTTP to that mock:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
bundle exec ruby -Ilib examples/middleware_demo.rb
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
CI runs the same fixture as a standalone server for the Sinatra/Roda demo
|
|
73
|
+
apps (port and bind are optional):
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
ARGOS_MOCK_PORT=19099 BIND=127.0.0.1 bundle exec ruby tool/mock_argos_server.rb
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Actual output:
|
|
80
|
+
|
|
81
|
+
```text
|
|
82
|
+
== benign POST
|
|
83
|
+
status: 200
|
|
84
|
+
verdict: ALLOW
|
|
85
|
+
body: {"ok":true}
|
|
86
|
+
|
|
87
|
+
== XSS POST (fresh IP)
|
|
88
|
+
status: 403
|
|
89
|
+
verdict: BLOCK
|
|
90
|
+
reason: xss detected (mock)
|
|
91
|
+
body: {"error":"blocked","reason":"xss detected (mock)"}
|
|
92
|
+
|
|
93
|
+
== benign POST from the blocked IP (cache fast path)
|
|
94
|
+
status: 403
|
|
95
|
+
verdict: BLOCK
|
|
96
|
+
reason: Auto-blocked: BLOCK verdict
|
|
97
|
+
body: {"error":"blocked","reason":"Auto-blocked: BLOCK verdict"}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Capabilities and proof
|
|
103
|
+
|
|
104
|
+
`bundle exec rspec` runs 52 examples.
|
|
105
|
+
|
|
106
|
+
| Capability | Implementation | Proving test |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| Real contract headers (`X-API-Key`, `X-Environment-ID`, no `Authorization`) | `adapters/http/net_http_transport.rb` | `spec/contract_spec.rb` → "sends X-API-Key and X-Environment-ID, never Authorization" |
|
|
109
|
+
| Flat event normalised to v2 (`event.type/method/path`, `actor.ip`) | `domain/events/event_normalizer.rb` | `spec/contract_spec.rb` → "normalises a flat event into the v2 shape the parser accepts" |
|
|
110
|
+
| Invalid event rejected client-side with the field name | `event_normalizer.rb` | `spec/contract_spec.rb` → three rejection examples |
|
|
111
|
+
| 403 `verdict: BLOCK` surfaced as a block | `services/response_interpreter.rb`, `models/verdict.rb` | `spec/contract_spec.rb` → "surfaces a 403 verdict:BLOCK as a block" |
|
|
112
|
+
| Batch events normalised | `use_cases/ingest_batch.rb` | `spec/contract_spec.rb` → "normalises flat events inside a batch" |
|
|
113
|
+
| Rack middleware blocks before the app | `middleware.rb` | `spec/middleware_spec.rb` → "blocks request if ingest returns BLOCK" |
|
|
114
|
+
| Sync vs async mode + verdict headers | `middleware.rb` | `spec/middleware_spec.rb` |
|
|
115
|
+
| Excluded paths | `middleware.rb` | `spec/middleware_spec.rb` → "skips excluded paths" |
|
|
116
|
+
| Auto-block on BLOCK | `use_cases/ingest_event.rb` | `spec/client_spec.rb` → "treats 403 on ingest as a BLOCK verdict and auto-blocks IP" |
|
|
117
|
+
| TTL blocklist cache | `domain/caching/blocklist_cache.rb` | `spec/core/blocklist_cache_spec.rb` |
|
|
118
|
+
| Circuit breaker | `domain/resilience/circuit_breaker.rb` | `spec/core/circuit_breaker_spec.rb` |
|
|
119
|
+
| Exponential backoff | `domain/resilience/retry_calculator.rb` | `spec/core/retry_calculator_spec.rb` |
|
|
120
|
+
| OpenAPI request validation | `domain/validation/openapi_validator.rb` | `spec/validation/spec_validator_spec.rb` |
|
|
121
|
+
| Metadata validation | `domain/validation/metadata_validator.rb` | `spec/validation/metadata_validator_spec.rb` |
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Middleware options
|
|
126
|
+
|
|
127
|
+
| Option | Default | Meaning |
|
|
128
|
+
|---|---|---|
|
|
129
|
+
| `client:` | required | `Argos::Client` instance |
|
|
130
|
+
| `mode:` | `:async` | `:sync` blocks inline; `:async` enqueues |
|
|
131
|
+
| `check_blocklist:` | `true` | Check the local cache before the app |
|
|
132
|
+
| `exclude_paths:` | health/metrics/favicon | Prefixes skipped entirely |
|
|
133
|
+
| `include_body:` | `true` | Inspect the request body |
|
|
134
|
+
| `include_headers:` | `true` | Send request headers |
|
|
135
|
+
| `identity_header:` | `X-User-ID` | Header used as the user id |
|
|
136
|
+
|
|
137
|
+
## Configuration
|
|
138
|
+
|
|
139
|
+
| Option | Default |
|
|
140
|
+
|---|---|
|
|
141
|
+
| `api_key` | required |
|
|
142
|
+
| `base_url` | `https://api.argossecops.com` |
|
|
143
|
+
| `environment_id` | auto-detected |
|
|
144
|
+
| `timeout` | `5.0` s |
|
|
145
|
+
| `max_retries` | `3` |
|
|
146
|
+
| `circuit_breaker_threshold` | `5` |
|
|
147
|
+
| `circuit_breaker_timeout` | `60` s |
|
|
148
|
+
| `blocklist_cache_ttl` | `60` s |
|
|
149
|
+
| `auto_block_on_block` | `true` |
|
|
150
|
+
| `queue_size` | `1000` |
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Limits
|
|
155
|
+
|
|
156
|
+
See [docs/limits.md](docs/limits.md). In short:
|
|
157
|
+
|
|
158
|
+
- Detection happens in the Argos service; there is no local ML.
|
|
159
|
+
- Set `environment_id` explicitly; a request issued before auto-detection
|
|
160
|
+
completes omits `X-Environment-ID` and the real service rejects it.
|
|
161
|
+
- `:async` mode does not block the current request.
|
|
162
|
+
- The Rails Railtie is not covered by the unit suite (no Rails in CI); the
|
|
163
|
+
Rack middleware it installs is tested.
|
|
164
|
+
- Sinatra and Roda smoke apps exist under `test-apps/` but are only run in CI
|
|
165
|
+
with a live API key.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Development
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
bundle install
|
|
173
|
+
bundle exec rspec
|
|
174
|
+
bundle exec ruby -Ilib bench/benchmark.rb
|
|
175
|
+
bundle exec ruby -Ilib examples/middleware_demo.rb
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
See [docs/architecture.md](docs/architecture.md),
|
|
179
|
+
[docs/verification.md](docs/verification.md),
|
|
180
|
+
[docs/benchmarks.md](docs/benchmarks.md).
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
MIT. See [LICENSE](LICENSE). Copyright (C) 2026 Argos-Security <contact@argossecops.com>.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
The gem uses a hexagonal (ports and adapters) layout:
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
lib/argos/
|
|
7
|
+
domain/ pure logic — config, verdict, blocklist cache, circuit
|
|
8
|
+
breaker, retry, metadata validation, OpenAPI validator,
|
|
9
|
+
event normaliser, payload builder
|
|
10
|
+
ports/ outbound interfaces — Transport, SpecSource, Clock,
|
|
11
|
+
Diagnostics
|
|
12
|
+
application/ use cases — ingest, batch, block, check access, decision,
|
|
13
|
+
environment detection, health, event queue; services —
|
|
14
|
+
response interpreter, spec validation
|
|
15
|
+
adapters/ Net::HTTP transport and HTTP spec source
|
|
16
|
+
composition/ Argos::Client facade wiring everything
|
|
17
|
+
middleware.rb Rack middleware
|
|
18
|
+
railtie.rb Rails integration
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Sync request flow
|
|
22
|
+
|
|
23
|
+
1. `Argos::Middleware#call` skips `exclude_paths`.
|
|
24
|
+
2. Extracts IP/user id, checks the blocklist cache.
|
|
25
|
+
3. Reads and re-buffers the body so the downstream Rack app can read it.
|
|
26
|
+
4. `PayloadBuilder` builds the v2 payload.
|
|
27
|
+
5. `IngestEvent` normalises metadata and payload, validates, attaches
|
|
28
|
+
`sdk_validation`, POSTs `{payload, metadata}`.
|
|
29
|
+
6. `203`/`BLOCK` → `403` response with `x-argos-verdict` and
|
|
30
|
+
`x-argos-reason`; `ALLOW` continues with the verdict header.
|
|
31
|
+
7. Any exception is swallowed (fail-open).
|
|
32
|
+
|
|
33
|
+
## The ingestion contract
|
|
34
|
+
|
|
35
|
+
The service requires `X-API-Key` and `X-Environment-ID`, and parses
|
|
36
|
+
`payload.event.{type,method,path}` and `payload.actor.ip`. It responds with
|
|
37
|
+
`{verdict, anomaly_score, event_id, reason}` and HTTP 403 for `BLOCK`.
|
|
38
|
+
|
|
39
|
+
`Domain::Events::EventNormalizer.normalize_payload` is the single enforcement
|
|
40
|
+
point. It accepts the strict shape or a flat event (`event_type`, `user_id`,
|
|
41
|
+
`ip`, `body`, ...), defaults method/path/ip, and raises
|
|
42
|
+
`Argos::ValidationError` naming the missing field if it cannot produce a valid
|
|
43
|
+
event.
|
|
44
|
+
|
|
45
|
+
## Middleware and Rails
|
|
46
|
+
|
|
47
|
+
`Argos::Middleware` is a normal Rack middleware, so the same object is used by
|
|
48
|
+
Rails, Sinatra, Hanami, Roda, Cuba and Grape. `Argos::Railtie` only exists to
|
|
49
|
+
read `Rails.application.config.argos` and insert the middleware after
|
|
50
|
+
`ActionDispatch::RequestId`; it does not change the request path.
|
|
51
|
+
|
|
52
|
+
## Async queue
|
|
53
|
+
|
|
54
|
+
`Application::UseCases::EventQueue` is an in-process bounded queue flushed on a
|
|
55
|
+
timer (single `/ingest` or batched `/ingest/batch`). It is not durable.
|
data/docs/benchmarks.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Benchmarks
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
bundle exec ruby -Ilib bench/benchmark.rb
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
In-process only (no network). Each case is warmed up, then measured over
|
|
8
|
+
100,000 iterations with `Process::CLOCK_MONOTONIC`.
|
|
9
|
+
|
|
10
|
+
## Recorded run
|
|
11
|
+
|
|
12
|
+
Environment: Ruby 3.4.13, Linux x86_64.
|
|
13
|
+
|
|
14
|
+
| Case | ns/op | M ops/s |
|
|
15
|
+
|---|---:|---:|
|
|
16
|
+
| Blocklist cache hit | 766 | 1.3 |
|
|
17
|
+
| Payload builder (middleware) | 3 570 | 0.3 |
|
|
18
|
+
| `normalize_payload` (flat→v2) | 4 347 | 0.2 |
|
|
19
|
+
| Retry delay calculation | 331 | 3.0 |
|
|
20
|
+
|
|
21
|
+
## Interpretation
|
|
22
|
+
|
|
23
|
+
- A cache hit is well under a millisecond (the README's "lightning-fast block
|
|
24
|
+
wall" is about avoiding an HTTP round-trip, not a hard latency promise).
|
|
25
|
+
- The payload builder and normaliser allocate hashes and copy strings; they
|
|
26
|
+
are the dominant SDK cost per request. Network latency is not included.
|
|
27
|
+
- Async mode is "zero added latency" only in the sense that it does not wait
|
|
28
|
+
for the API: the enqueue itself still does the packet construction above.
|
|
29
|
+
- These are micro-benchmarks, not a throughput or capacity plan.
|
data/docs/limits.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Limits
|
|
2
|
+
|
|
3
|
+
## Detection
|
|
4
|
+
|
|
5
|
+
- Verdicts come from the Argos service; the gem does not detect threats
|
|
6
|
+
locally.
|
|
7
|
+
- Middleware fails open: timeouts, 5xx, bad JSON and transport errors never
|
|
8
|
+
block the host app. An outage means requests are not inspected.
|
|
9
|
+
- `auto_block_on_block` trusts the verdict. A false positive can block a
|
|
10
|
+
legitimate IP until the cache TTL or dashboard entry expires.
|
|
11
|
+
|
|
12
|
+
## Contract
|
|
13
|
+
|
|
14
|
+
- `X-Environment-ID` is required. Auto-detection via `/api/v1/me` is
|
|
15
|
+
asynchronous; a request issued before it completes omits the header and is
|
|
16
|
+
rejected by the real service. Set `Config#environment_id` explicitly.
|
|
17
|
+
- Payloads must be v2. Flat events are normalised; a flat event without an
|
|
18
|
+
event type, or a structured event missing `event.type`/`method`/`path`/
|
|
19
|
+
`actor.ip`, raises `Argos::ValidationError` before any HTTP call.
|
|
20
|
+
- `verdict` is the authoritative response field; `signal` is accepted for
|
|
21
|
+
backwards compatibility only.
|
|
22
|
+
|
|
23
|
+
## Runtime
|
|
24
|
+
|
|
25
|
+
- `:async` mode does not block the current request; malicious traffic may pass
|
|
26
|
+
once before a cached blocklist entry stops it.
|
|
27
|
+
- The async queue is in-process, bounded and not durable; a crash loses queued
|
|
28
|
+
events.
|
|
29
|
+
- Body inspection buffers the body in memory; there is no streaming.
|
|
30
|
+
- `exclude_paths` is prefix-based, so `/health` also excludes `/healthz`.
|
|
31
|
+
|
|
32
|
+
## Framework coverage
|
|
33
|
+
|
|
34
|
+
- The middleware is plain Rack and is unit-tested with `rack-test`.
|
|
35
|
+
- The Rails Railtie is not exercised in CI; it is a thin configuration wrapper
|
|
36
|
+
over the tested middleware.
|
|
37
|
+
- Sinatra and Roda have runnable fixtures under `test-apps/`; their smoke jobs
|
|
38
|
+
need the live API key secret.
|
|
39
|
+
- Hanami, Cuba and Grape have no dedicated fixture.
|
|
40
|
+
|
|
41
|
+
## Packaging
|
|
42
|
+
|
|
43
|
+
- The gem is private and versioned at 1.0.0. Release is a deliberate tag-only
|
|
44
|
+
action; do not publish from a pull request.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Verification
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
bundle install
|
|
5
|
+
bundle exec rspec # 52 examples
|
|
6
|
+
bundle exec ruby -Ilib examples/middleware_demo.rb
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
CI (`.github/workflows/ci.yml`) runs the suite on Ruby 3.1–3.4, checks every
|
|
10
|
+
library file with `ruby -wc`, builds the gem, and boots the Sinatra/Roda demo
|
|
11
|
+
apps for a smoke + BLOCK test.
|
|
12
|
+
|
|
13
|
+
The integration job is hermetic: it starts `tool/mock_argos_server.rb` and
|
|
14
|
+
points the demos at it with `ARGOS_BASE_URL`. It deliberately does **not** use
|
|
15
|
+
the public gateway. Without an explicit base URL, the SDK's synchronous
|
|
16
|
+
bootstrap (spec fetch, environment detection, ingest retries at 5 s per
|
|
17
|
+
attempt) blocks the first request for far longer than a health probe waits,
|
|
18
|
+
which is what made the first CI run fail with `000` on `/`.
|
|
19
|
+
|
|
20
|
+
## Test inventory
|
|
21
|
+
|
|
22
|
+
| File | Examples cover |
|
|
23
|
+
|---|---|
|
|
24
|
+
| `spec/client_spec.rb` | ingest allow/block, auto-block, verdict normalisation |
|
|
25
|
+
| `spec/contract_spec.rb` | real `NetHttpTransport` + WebMock: headers, flat→v2 normalisation, structured pass-through, `verdict:BLOCK` surfacing, client-side rejection, batch normalisation |
|
|
26
|
+
| `spec/middleware_spec.rb` | excluded paths, blocklist block, sync allow/block, headers, async mode |
|
|
27
|
+
| `spec/core/blocklist_cache_spec.rb` | TTL hit/miss/expiry/invalidate |
|
|
28
|
+
| `spec/core/circuit_breaker_spec.rb` | threshold, reset, half-open |
|
|
29
|
+
| `spec/core/retry_calculator_spec.rb` | exponential growth, cap, jitter |
|
|
30
|
+
| `spec/validation/metadata_validator_spec.rb` | length, IP, UUID, timestamp rules |
|
|
31
|
+
| `spec/validation/spec_validator_spec.rb` | OpenAPI fetch/cache and violation taxonomy |
|
|
32
|
+
|
|
33
|
+
## Strict contract stub
|
|
34
|
+
|
|
35
|
+
`spec/contract_spec.rb` uses the real `NetHttpTransport`, with WebMock
|
|
36
|
+
intercepting the socket. The stub:
|
|
37
|
+
|
|
38
|
+
- checks `X-API-Key` and `X-Environment-ID`, and rejects `Authorization`;
|
|
39
|
+
- parses the body and returns `400` naming the first missing v2 field;
|
|
40
|
+
- returns `403 {"verdict":"BLOCK","reason":"xss detected"}` for a malicious
|
|
41
|
+
body.
|
|
42
|
+
|
|
43
|
+
That stub is what proves the payload is parseable, rather than merely
|
|
44
|
+
accepted by a permissive double.
|
|
45
|
+
|
|
46
|
+
## Not verified
|
|
47
|
+
|
|
48
|
+
- A live Argos service (needs an API key and network).
|
|
49
|
+
- The Rails Railtie against a real Rails app — CI has no Rails dependency.
|
|
50
|
+
The middleware it installs is covered by `middleware_spec.rb`.
|
|
51
|
+
- Hanami/Cuba/Grape have no dedicated fixture; they use the same Rack
|
|
52
|
+
middleware.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Argos
|
|
4
|
+
module Adapters
|
|
5
|
+
module Http
|
|
6
|
+
# Default `SpecSource` — fetches OpenAPI from `GET /api/v1/spec`
|
|
7
|
+
# via the shared transport.
|
|
8
|
+
class HttpSpecSource < Ports::Outbound::SpecSource
|
|
9
|
+
def initialize(transport:)
|
|
10
|
+
@transport = transport
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def fetch
|
|
14
|
+
res = @transport.send(Ports::Outbound::TransportRequest.new(
|
|
15
|
+
method: :get, endpoint: "/api/v1/spec",
|
|
16
|
+
))
|
|
17
|
+
return nil unless res.success?
|
|
18
|
+
|
|
19
|
+
envelope = JSON.parse(res.body) rescue nil
|
|
20
|
+
return nil unless envelope.is_a?(Hash)
|
|
21
|
+
raw = envelope["spec"]
|
|
22
|
+
return nil if raw.nil? || raw.to_s.empty?
|
|
23
|
+
raw.is_a?(String) ? raw : raw.to_json
|
|
24
|
+
rescue
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "uri"
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Argos
|
|
6
|
+
module Adapters
|
|
7
|
+
module Http
|
|
8
|
+
# Default outbound transport — wraps `Net::HTTP`, applies SDK
|
|
9
|
+
# headers, runs the retry loop, feeds success/failure into the breaker.
|
|
10
|
+
class NetHttpTransport < Ports::Outbound::Transport
|
|
11
|
+
def initialize(config:, breaker:, env_id_provider:)
|
|
12
|
+
@config = config
|
|
13
|
+
@breaker = breaker
|
|
14
|
+
@env_id = env_id_provider
|
|
15
|
+
@retry = Domain::Resilience::RetryCalculator.new(
|
|
16
|
+
base_delay: 0.5,
|
|
17
|
+
max_delay: 30.0,
|
|
18
|
+
multiplier: 2.0,
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def send(request)
|
|
23
|
+
raise CircuitOpenError, "Circuit is open" if @breaker.open?
|
|
24
|
+
|
|
25
|
+
url = build_url(request)
|
|
26
|
+
uri = URI.parse(url)
|
|
27
|
+
last_error = nil
|
|
28
|
+
|
|
29
|
+
(0..@config.max_retries).each do |attempt|
|
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
31
|
+
http.use_ssl = (uri.scheme == "https")
|
|
32
|
+
http.open_timeout = @config.timeout
|
|
33
|
+
http.read_timeout = @config.timeout
|
|
34
|
+
|
|
35
|
+
req_class = case request.method
|
|
36
|
+
when :get then Net::HTTP::Get
|
|
37
|
+
when :post then Net::HTTP::Post
|
|
38
|
+
when :put then Net::HTTP::Put
|
|
39
|
+
when :delete then Net::HTTP::Delete
|
|
40
|
+
else raise "Unsupported HTTP method: #{request.method}"
|
|
41
|
+
end
|
|
42
|
+
req = req_class.new(uri.request_uri)
|
|
43
|
+
|
|
44
|
+
apply_headers(req, request.extra_headers)
|
|
45
|
+
if request.body
|
|
46
|
+
req.body = request.body.to_json
|
|
47
|
+
req["Content-Type"] = "application/json"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
begin
|
|
51
|
+
res = http.request(req)
|
|
52
|
+
status = res.code.to_i
|
|
53
|
+
body = res.body.to_s
|
|
54
|
+
if status >= 200 && status < 300
|
|
55
|
+
@breaker.record_success
|
|
56
|
+
return Ports::Outbound::TransportResponse.new(status: status, body: body)
|
|
57
|
+
end
|
|
58
|
+
# 5xx feeds breaker + retry; 4xx flows through.
|
|
59
|
+
if status >= 500 && attempt < @config.max_retries
|
|
60
|
+
sleep @retry.calculate_delay(attempt + 1)
|
|
61
|
+
next
|
|
62
|
+
end
|
|
63
|
+
@breaker.record_failure if status >= 500
|
|
64
|
+
retry_after = status == 429 && res["Retry-After"] ? res["Retry-After"].to_f : nil
|
|
65
|
+
return Ports::Outbound::TransportResponse.new(
|
|
66
|
+
status: status, body: body, retry_after_secs: retry_after,
|
|
67
|
+
)
|
|
68
|
+
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
69
|
+
last_error = TimeoutError.new(e.message)
|
|
70
|
+
rescue StandardError => e
|
|
71
|
+
last_error = e
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
if attempt < @config.max_retries
|
|
75
|
+
sleep @retry.calculate_delay(attempt + 1)
|
|
76
|
+
else
|
|
77
|
+
@breaker.record_failure
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
raise last_error || Error.new("Unknown transport error")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def build_url(req)
|
|
87
|
+
url = "#{@config.base_url.chomp('/')}#{req.endpoint}"
|
|
88
|
+
if req.query && !req.query.empty?
|
|
89
|
+
url += "?" + URI.encode_www_form(req.query)
|
|
90
|
+
end
|
|
91
|
+
url
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def apply_headers(req, extra)
|
|
95
|
+
req["X-API-Key"] = @config.api_key
|
|
96
|
+
req["User-Agent"] = "Argos-Ruby-SDK/1.0.0"
|
|
97
|
+
req["X-Argos-Schema-Version"] = "2"
|
|
98
|
+
if (env_id = @env_id.call)
|
|
99
|
+
req["X-Environment-ID"] = env_id
|
|
100
|
+
end
|
|
101
|
+
(extra || {}).each { |k, v| req[k] = v }
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Argos
|
|
4
|
+
module Application
|
|
5
|
+
module Services
|
|
6
|
+
# Translates raw transport responses into typed results or raises
|
|
7
|
+
# the right Argos::Error subtype.
|
|
8
|
+
module ResponseInterpreter
|
|
9
|
+
# 403 on /ingest is the ML BLOCK verdict — surface its body to the
|
|
10
|
+
# caller so it can be normalised into an event.
|
|
11
|
+
def self.interpret_ingest(res)
|
|
12
|
+
return parse(res.body) if res.success? || res.status == 403
|
|
13
|
+
raise map_error(res)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.interpret(res)
|
|
17
|
+
return parse(res.body) if res.success?
|
|
18
|
+
raise map_error(res)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.parse(body)
|
|
22
|
+
return {} if body.nil? || body.to_s.empty?
|
|
23
|
+
JSON.parse(body)
|
|
24
|
+
rescue JSON::ParserError
|
|
25
|
+
{}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.map_error(res)
|
|
29
|
+
case res.status
|
|
30
|
+
when 401 then AuthenticationError.new("Invalid API key", 401, res.body)
|
|
31
|
+
when 403 then AuthenticationError.new("Access denied", 403, res.body)
|
|
32
|
+
when 429 then RateLimitError.new("Rate limit exceeded", 429, res.body, res.retry_after_secs || 60.0)
|
|
33
|
+
else
|
|
34
|
+
if res.status >= 500
|
|
35
|
+
ServerError.new("Server error: #{res.status}", res.status, res.body)
|
|
36
|
+
else
|
|
37
|
+
HttpError.new("API error: #{res.status}", res.status, res.body)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Application
|
|
3
|
+
module Services
|
|
4
|
+
# Application service: cached OpenAPI document + delegation to the
|
|
5
|
+
# pure `OpenApiValidator`. Fails open.
|
|
6
|
+
class SpecValidationService
|
|
7
|
+
def initialize(source:, ttl: 3600.0, clock: nil, diagnostics: nil)
|
|
8
|
+
@source = source
|
|
9
|
+
@ttl = ttl
|
|
10
|
+
@clock = clock || Ports::Outbound::SystemClock.new
|
|
11
|
+
@diag = diagnostics || Ports::Outbound::StderrDiagnostics.new
|
|
12
|
+
@spec = nil
|
|
13
|
+
@last_fetch = nil
|
|
14
|
+
@mutex = Mutex.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def loaded?; !@spec.nil? end
|
|
18
|
+
def last_fetch; @last_fetch end
|
|
19
|
+
def path_count; @spec ? @spec.paths.size : 0 end
|
|
20
|
+
|
|
21
|
+
# Eagerly fetch the spec in a background thread.
|
|
22
|
+
def initialize_async
|
|
23
|
+
Thread.new do
|
|
24
|
+
begin
|
|
25
|
+
fetch_and_load
|
|
26
|
+
rescue => e
|
|
27
|
+
@diag.warn("Spec validator will retry/initialize on demand: #{e.message}")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Returns the validator result hash, or `nil` when the spec is not
|
|
33
|
+
# loaded (callers treat as "skipped").
|
|
34
|
+
def validate(method:, path:, headers: {}, query: {}, body: nil)
|
|
35
|
+
ensure_fresh
|
|
36
|
+
return nil unless @spec
|
|
37
|
+
Domain::Validation::OpenApiValidator.validate(
|
|
38
|
+
@spec,
|
|
39
|
+
method: method,
|
|
40
|
+
path: path,
|
|
41
|
+
headers: headers,
|
|
42
|
+
query: query,
|
|
43
|
+
body: body,
|
|
44
|
+
)
|
|
45
|
+
rescue => e
|
|
46
|
+
@diag.warn("Spec validation error: #{e.message}")
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def ensure_fresh
|
|
53
|
+
stale = @spec.nil? || @last_fetch.nil? || (@clock.now - @last_fetch) > @ttl
|
|
54
|
+
return unless stale
|
|
55
|
+
begin
|
|
56
|
+
fetch_and_load
|
|
57
|
+
rescue => e
|
|
58
|
+
@diag.warn("Background spec refresh failed: #{e.message}")
|
|
59
|
+
@last_fetch = @clock.now - @ttl + 300 # retry in ~5 min
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def fetch_and_load
|
|
64
|
+
raw = @source.fetch
|
|
65
|
+
parsed = Domain::Validation::OpenApiSpec.parse(raw)
|
|
66
|
+
raise "fetched empty openapi spec" if parsed.nil?
|
|
67
|
+
@mutex.synchronize do
|
|
68
|
+
@spec = parsed
|
|
69
|
+
@last_fetch = @clock.now
|
|
70
|
+
end
|
|
71
|
+
@diag.info("OpenAPI spec loaded (#{parsed.paths.size} paths)")
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|