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
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "time"
|
|
3
|
+
|
|
4
|
+
module Argos
|
|
5
|
+
module Domain
|
|
6
|
+
module Events
|
|
7
|
+
# Constructs the `{payload, metadata}` packet shipped by every
|
|
8
|
+
# framework middleware to `/api/v1/ingest`. Pure — no I/O.
|
|
9
|
+
class PayloadBuilder
|
|
10
|
+
def initialize(include_body: true, include_headers: true, identity_header: "X-User-ID")
|
|
11
|
+
@include_body = include_body
|
|
12
|
+
@include_headers = include_headers
|
|
13
|
+
@identity_header = identity_header
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def build_packet(method:, path:, query:, user_id:, client_ip:, user_agent:,
|
|
17
|
+
source:, environment_id: nil, body: nil, headers: {})
|
|
18
|
+
is_ajax = headers["X-Requested-With"] == "XMLHttpRequest" ||
|
|
19
|
+
headers.fetch("Accept", "").include?("application/json")
|
|
20
|
+
content_length = headers.fetch("Content-Length", 0).to_i
|
|
21
|
+
|
|
22
|
+
event = {
|
|
23
|
+
"type" => "http_request",
|
|
24
|
+
"method" => method.to_s.upcase,
|
|
25
|
+
"path" => path,
|
|
26
|
+
"timestamp" => Time.now.utc.iso8601,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
actor = {
|
|
30
|
+
"user_id" => user_id || "anonymous",
|
|
31
|
+
"ip" => client_ip,
|
|
32
|
+
"ua" => user_agent,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
data = {
|
|
36
|
+
"headers" => @include_headers ? headers : {},
|
|
37
|
+
"query" => query || {},
|
|
38
|
+
"content_length" => content_length,
|
|
39
|
+
"is_ajax" => is_ajax,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if body && @include_body && !body.empty?
|
|
43
|
+
data["body"] = body
|
|
44
|
+
content_type = headers.fetch("Content-Type", "")
|
|
45
|
+
if content_type.include?("application/json")
|
|
46
|
+
begin
|
|
47
|
+
data["json"] = JSON.parse(body)
|
|
48
|
+
rescue JSON::ParserError
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
payload = { "schema_version" => 2, "event" => event, "actor" => actor, "data" => data }
|
|
54
|
+
metadata = {
|
|
55
|
+
"source" => source,
|
|
56
|
+
"client_ip" => client_ip,
|
|
57
|
+
"schema_version" => "2",
|
|
58
|
+
"userAgent" => user_agent,
|
|
59
|
+
}
|
|
60
|
+
metadata["env_id"] = environment_id if environment_id
|
|
61
|
+
|
|
62
|
+
{ payload: payload, metadata: metadata }
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
# Normalized verdict constants.
|
|
3
|
+
module Verdict
|
|
4
|
+
ALLOW = "ALLOW".freeze
|
|
5
|
+
BLOCK = "BLOCK".freeze
|
|
6
|
+
PENDING = "PENDING".freeze
|
|
7
|
+
FLAG = "FLAG".freeze
|
|
8
|
+
|
|
9
|
+
# Returns the upper-cased verdict from an event hash. Checks (in order):
|
|
10
|
+
# signal → verdict → Verdict → decision.verdict.
|
|
11
|
+
def self.normalize(event)
|
|
12
|
+
return "" unless event.is_a?(Hash)
|
|
13
|
+
raw = event["signal"] ||
|
|
14
|
+
event["verdict"] ||
|
|
15
|
+
event["Verdict"] ||
|
|
16
|
+
(event["decision"].is_a?(Hash) ? event["decision"]["verdict"] : nil)
|
|
17
|
+
raw.to_s.upcase
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.block?(raw)
|
|
21
|
+
raw.to_s.upcase == BLOCK
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Domain
|
|
3
|
+
module Resilience
|
|
4
|
+
# Pure state machine: counts consecutive failures, opens after
|
|
5
|
+
# `threshold` hits, auto-resets after `timeout`.
|
|
6
|
+
class CircuitBreaker
|
|
7
|
+
attr_reader :failures, :state, :last_failure_time, :threshold, :timeout
|
|
8
|
+
|
|
9
|
+
def initialize(threshold, timeout)
|
|
10
|
+
@threshold = threshold
|
|
11
|
+
@timeout = timeout
|
|
12
|
+
@failures = 0
|
|
13
|
+
@state = :closed
|
|
14
|
+
@last_failure_time = nil
|
|
15
|
+
@mutex = Mutex.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def open?
|
|
19
|
+
@mutex.synchronize do
|
|
20
|
+
if @state == :open
|
|
21
|
+
if Time.now.to_f - @last_failure_time > @timeout
|
|
22
|
+
@state = :half_open
|
|
23
|
+
return false
|
|
24
|
+
end
|
|
25
|
+
return true
|
|
26
|
+
end
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def record_success
|
|
32
|
+
@mutex.synchronize do
|
|
33
|
+
@failures = 0
|
|
34
|
+
@state = :closed
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def record_failure
|
|
39
|
+
@mutex.synchronize do
|
|
40
|
+
@failures += 1
|
|
41
|
+
if @failures >= @threshold
|
|
42
|
+
@state = :open
|
|
43
|
+
@last_failure_time = Time.now.to_f
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Domain
|
|
3
|
+
module Resilience
|
|
4
|
+
# Pure retry-delay calculator. No I/O, no sleeping.
|
|
5
|
+
class RetryCalculator
|
|
6
|
+
def initialize(base_delay: 0.5, max_delay: 30.0, multiplier: 2.0)
|
|
7
|
+
@base_delay = base_delay
|
|
8
|
+
@max_delay = max_delay
|
|
9
|
+
@multiplier = multiplier
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def calculate_delay(attempt)
|
|
13
|
+
raw = @base_delay * (@multiplier**(attempt - 1))
|
|
14
|
+
capped = [raw, @max_delay].min
|
|
15
|
+
jitter = rand(0.0..(capped * 0.1))
|
|
16
|
+
capped + jitter
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require "ipaddr"
|
|
2
|
+
require "json"
|
|
3
|
+
|
|
4
|
+
module Argos
|
|
5
|
+
module Domain
|
|
6
|
+
module Validation
|
|
7
|
+
# Validates outbound metadata before it hits `/api/v1/ingest`.
|
|
8
|
+
module MetadataValidator
|
|
9
|
+
UUID_PATTERN = /\A[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\z/.freeze
|
|
10
|
+
RFC3339_PATTERN = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})\z/.freeze
|
|
11
|
+
|
|
12
|
+
def self.validate!(metadata)
|
|
13
|
+
errors = []
|
|
14
|
+
|
|
15
|
+
ext_id = metadata["externalID"] || metadata["external_id"]
|
|
16
|
+
if ext_id.nil? || ext_id.to_s.empty?
|
|
17
|
+
errors << { field: "externalID", message: "cannot be empty" }
|
|
18
|
+
elsif ext_id.to_s.length > 512
|
|
19
|
+
errors << { field: "externalID", message: "exceeds maximum length of 512" }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
env_id = metadata["env_id"] || metadata["environment_id"]
|
|
23
|
+
if env_id && !env_id.to_s.empty? && !env_id.to_s.match?(UUID_PATTERN)
|
|
24
|
+
errors << { field: "env_id", message: "must be a valid UUID" }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
client_ip = metadata["client_ip"] || metadata["ipAddress"]
|
|
28
|
+
if client_ip && !client_ip.to_s.empty?
|
|
29
|
+
begin
|
|
30
|
+
IPAddr.new(client_ip.to_s)
|
|
31
|
+
rescue IPAddr::InvalidAddressError, IPAddr::AddressFamilyError
|
|
32
|
+
errors << { field: "client_ip", message: "invalid IP address format" }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
timestamp = metadata["timestamp"] || metadata["time"]
|
|
37
|
+
if timestamp && !timestamp.to_s.empty? && !timestamp.to_s.match?(RFC3339_PATTERN)
|
|
38
|
+
errors << { field: "timestamp", message: "must be a valid RFC3339 datetime" }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
ua = metadata["userAgent"] || metadata["user_agent"]
|
|
42
|
+
if ua && ua.to_s.length > 1024
|
|
43
|
+
errors << { field: "userAgent", message: "exceeds maximum length of 1024" }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
sid = metadata["sessionID"] || metadata["session_id"]
|
|
47
|
+
if sid && sid.to_s.length > 256
|
|
48
|
+
errors << { field: "sessionID", message: "exceeds maximum length of 256" }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
raise ValidationError, "Metadata validation failed: #{errors.to_json}" unless errors.empty?
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Argos
|
|
4
|
+
module Domain
|
|
5
|
+
module Validation
|
|
6
|
+
# Parsed, ready-to-use OpenAPI document. Built once via [[parse]]
|
|
7
|
+
# from the raw envelope returned by the gateway.
|
|
8
|
+
class OpenApiSpec
|
|
9
|
+
# @!attribute paths
|
|
10
|
+
# @return [Array<Hash>] each entry is `{template:, regex:, operations:}`
|
|
11
|
+
attr_reader :paths, :raw
|
|
12
|
+
|
|
13
|
+
def initialize(raw, paths)
|
|
14
|
+
@raw = raw
|
|
15
|
+
@paths = paths
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.parse(raw_string)
|
|
19
|
+
return nil if raw_string.nil? || raw_string.to_s.strip.empty?
|
|
20
|
+
|
|
21
|
+
begin
|
|
22
|
+
doc = JSON.parse(raw_string)
|
|
23
|
+
rescue JSON::ParserError, TypeError
|
|
24
|
+
return nil
|
|
25
|
+
end
|
|
26
|
+
return nil unless doc.is_a?(Hash)
|
|
27
|
+
|
|
28
|
+
paths_hash = doc["paths"] || {}
|
|
29
|
+
paths = paths_hash.map do |template, item|
|
|
30
|
+
{
|
|
31
|
+
template: template,
|
|
32
|
+
regex: template_to_regex(template),
|
|
33
|
+
operations: item.is_a?(Hash) ? item.transform_keys(&:downcase) : {},
|
|
34
|
+
}
|
|
35
|
+
end
|
|
36
|
+
new(doc, paths)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.template_to_regex(template)
|
|
40
|
+
Regexp.new("^#{template.gsub(/\{[^}]+\}/, '([^/]+)')}$")
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Argos
|
|
4
|
+
module Domain
|
|
5
|
+
module Validation
|
|
6
|
+
# Pure validator: given a parsed [[OpenApiSpec]] and a request
|
|
7
|
+
# (method/path/body), returns a result hash. No I/O, no caching.
|
|
8
|
+
module OpenApiValidator
|
|
9
|
+
def self.validate(spec, method:, path:, headers: {}, query: {}, body: nil)
|
|
10
|
+
normalized_method = method.to_s.downcase
|
|
11
|
+
|
|
12
|
+
matched = spec.paths.find { |p| p[:regex].match?(path) }
|
|
13
|
+
unless matched
|
|
14
|
+
return {
|
|
15
|
+
"valid" => false,
|
|
16
|
+
"route_matched" => false,
|
|
17
|
+
"violations" => [{
|
|
18
|
+
"type" => "UNKNOWN_ROUTE",
|
|
19
|
+
"message" => "No route matched in spec",
|
|
20
|
+
"severity" => 80,
|
|
21
|
+
}],
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
operation = matched[:operations][normalized_method]
|
|
26
|
+
unless operation
|
|
27
|
+
return {
|
|
28
|
+
"valid" => false,
|
|
29
|
+
"route_matched" => true,
|
|
30
|
+
"matched_path" => matched[:template],
|
|
31
|
+
"violations" => [{
|
|
32
|
+
"type" => "UNKNOWN_ROUTE",
|
|
33
|
+
"message" => "Method #{method.to_s.upcase} not allowed for #{matched[:template]}",
|
|
34
|
+
"severity" => 80,
|
|
35
|
+
}],
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
violations = []
|
|
40
|
+
request_body = operation["requestBody"]
|
|
41
|
+
has_json_body = request_body.is_a?(Hash) &&
|
|
42
|
+
request_body["content"].is_a?(Hash) &&
|
|
43
|
+
request_body["content"]["application/json"]
|
|
44
|
+
|
|
45
|
+
if has_json_body && body && !body.to_s.empty?
|
|
46
|
+
begin
|
|
47
|
+
JSON.parse(body)
|
|
48
|
+
rescue JSON::ParserError => e
|
|
49
|
+
violations << {
|
|
50
|
+
"type" => "INVALID_BODY",
|
|
51
|
+
"message" => "Body is not valid JSON: #{e.message}",
|
|
52
|
+
"severity" => 50,
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
{
|
|
58
|
+
"valid" => violations.empty?,
|
|
59
|
+
"route_matched" => true,
|
|
60
|
+
"matched_path" => matched[:template],
|
|
61
|
+
"violations" => violations,
|
|
62
|
+
}
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
require "rack"
|
|
2
|
+
require "stringio"
|
|
3
|
+
|
|
4
|
+
module Argos
|
|
5
|
+
class Middleware
|
|
6
|
+
def initialize(app, client:, mode: :async, check_blocklist: true, exclude_paths: ["/health", "/metrics", "/_health", "/favicon.ico"], include_body: true, include_headers: true, identity_header: "X-User-ID")
|
|
7
|
+
@app = app
|
|
8
|
+
@client = client
|
|
9
|
+
@mode = mode
|
|
10
|
+
@check_blocklist = check_blocklist
|
|
11
|
+
@exclude_paths = exclude_paths
|
|
12
|
+
@include_body = include_body
|
|
13
|
+
@include_headers = include_headers
|
|
14
|
+
@identity_header = identity_header
|
|
15
|
+
@payload_builder = Utils::PayloadBuilder.new(
|
|
16
|
+
include_body: include_body,
|
|
17
|
+
include_headers: include_headers,
|
|
18
|
+
identity_header: identity_header
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(env)
|
|
23
|
+
request = Rack::Request.new(env)
|
|
24
|
+
|
|
25
|
+
# Skip excluded paths
|
|
26
|
+
if @exclude_paths.any? { |path| request.path.start_with?(path) }
|
|
27
|
+
return @app.call(env)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
client_ip = extract_ip(request)
|
|
31
|
+
user_id = extract_user_id(request)
|
|
32
|
+
|
|
33
|
+
if @check_blocklist
|
|
34
|
+
status = @client.check_access(ip: client_ip, user_id: user_id)
|
|
35
|
+
if status[:blocked]
|
|
36
|
+
reason = status[:block_reason] || "blocked by Argos"
|
|
37
|
+
return blocked_response(reason)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Extract request details
|
|
42
|
+
method = request.request_method
|
|
43
|
+
path = request.fullpath # includes query string
|
|
44
|
+
query_hash = request.GET || {}
|
|
45
|
+
user_agent = request.user_agent || ""
|
|
46
|
+
|
|
47
|
+
headers = extract_headers(env)
|
|
48
|
+
|
|
49
|
+
body_str = ""
|
|
50
|
+
if @include_body
|
|
51
|
+
body_io = request.body
|
|
52
|
+
if body_io
|
|
53
|
+
begin
|
|
54
|
+
body_io.rewind if body_io.respond_to?(:rewind)
|
|
55
|
+
body_str = body_io.read || ""
|
|
56
|
+
# Replace rack.input with a fresh StringIO so the downstream app
|
|
57
|
+
# can read the body again (Rack::Lint input is not rewindable).
|
|
58
|
+
env["rack.input"] = StringIO.new(body_str)
|
|
59
|
+
env["rack.input"].rewind
|
|
60
|
+
rescue StandardError
|
|
61
|
+
# Ignore non-rewindable stream errors — body capture is best-effort
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
packet = @payload_builder.build_packet(
|
|
67
|
+
method: method,
|
|
68
|
+
path: path,
|
|
69
|
+
query: query_hash,
|
|
70
|
+
user_id: user_id,
|
|
71
|
+
client_ip: client_ip,
|
|
72
|
+
user_agent: user_agent,
|
|
73
|
+
source: "middleware-rack-#{@mode}",
|
|
74
|
+
environment_id: @client.environment_id,
|
|
75
|
+
body: body_str,
|
|
76
|
+
headers: headers
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if @mode == :sync
|
|
80
|
+
begin
|
|
81
|
+
event = @client.ingest(packet[:payload], packet[:metadata])
|
|
82
|
+
|
|
83
|
+
if @client.is_blocked_verdict?(event)
|
|
84
|
+
reason = event.dig("decision", "reason") ||
|
|
85
|
+
event["reason"] ||
|
|
86
|
+
"Request blocked by Argos security policy"
|
|
87
|
+
return blocked_response(reason)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
env["argos_event"] = event
|
|
91
|
+
env["argos_verdict"] = @client.normalize_verdict(event)
|
|
92
|
+
rescue StandardError
|
|
93
|
+
# Fail open — never bring the host app down because of Argos
|
|
94
|
+
end
|
|
95
|
+
else
|
|
96
|
+
@client.queue_event(packet[:payload], packet[:metadata])
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
status, headers, body = @app.call(env)
|
|
100
|
+
|
|
101
|
+
if (verdict = env["argos_verdict"]) && !verdict.empty?
|
|
102
|
+
headers = headers.dup
|
|
103
|
+
headers["x-argos-verdict"] = verdict
|
|
104
|
+
reason = env.dig("argos_event", "decision", "reason") || env.dig("argos_event", "reason")
|
|
105
|
+
headers["x-argos-reason"] = reason if reason
|
|
106
|
+
elsif @mode == :async
|
|
107
|
+
headers = headers.dup
|
|
108
|
+
headers["x-argos-verdict"] = "allow"
|
|
109
|
+
headers["x-argos-reason"] = "async_pending"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
[status, headers, body]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def extract_ip(request)
|
|
118
|
+
# Rack has a good default ip extraction considering X-Forwarded-For
|
|
119
|
+
request.ip || "127.0.0.1"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def extract_user_id(request)
|
|
123
|
+
# Fallback checks
|
|
124
|
+
env = request.env
|
|
125
|
+
env["HTTP_#{@identity_header.upcase.tr('-', '_')}"] ||
|
|
126
|
+
env["HTTP_X_USER_ID"] ||
|
|
127
|
+
env["HTTP_X_AUTH_USER_ID"] ||
|
|
128
|
+
"anonymous"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def extract_headers(env)
|
|
132
|
+
headers = {}
|
|
133
|
+
env.each do |k, v|
|
|
134
|
+
if k.start_with?("HTTP_")
|
|
135
|
+
# Convert HTTP_USER_AGENT -> User-Agent
|
|
136
|
+
header_name = k[5..-1].split('_').map(&:capitalize).join('-')
|
|
137
|
+
headers[header_name] = v
|
|
138
|
+
elsif %w[CONTENT_TYPE CONTENT_LENGTH].include?(k)
|
|
139
|
+
header_name = k.split('_').map(&:capitalize).join('-')
|
|
140
|
+
headers[header_name] = v
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
headers
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def blocked_response(reason)
|
|
147
|
+
body = { error: "blocked", reason: reason }.to_json
|
|
148
|
+
[
|
|
149
|
+
403,
|
|
150
|
+
{
|
|
151
|
+
"content-type" => "application/json",
|
|
152
|
+
"x-argos-verdict" => "BLOCK",
|
|
153
|
+
"x-argos-reason" => reason
|
|
154
|
+
},
|
|
155
|
+
[body]
|
|
156
|
+
]
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Ports
|
|
3
|
+
module Outbound
|
|
4
|
+
# Outbound port for diagnostics. Host apps may swap any logger.
|
|
5
|
+
class Diagnostics
|
|
6
|
+
def info(message); end
|
|
7
|
+
def warn(message); end
|
|
8
|
+
def error(message); end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Default — `warn`/`Kernel#warn` to stderr, matching the SDK's
|
|
12
|
+
# historical behaviour.
|
|
13
|
+
class StderrDiagnostics < Diagnostics
|
|
14
|
+
def info(m); Kernel.warn("[Argos-SDK] #{m}"); end
|
|
15
|
+
def warn(m); Kernel.warn("[Argos-SDK] #{m}"); end
|
|
16
|
+
def error(m); Kernel.warn("[Argos-SDK] #{m}"); end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# No-op — useful in tests.
|
|
20
|
+
class NullDiagnostics < Diagnostics
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Ports
|
|
3
|
+
module Outbound
|
|
4
|
+
# Outbound port: where the SDK obtains the OpenAPI document.
|
|
5
|
+
# Returns the raw JSON string, or `nil` if unavailable.
|
|
6
|
+
class SpecSource
|
|
7
|
+
def fetch
|
|
8
|
+
raise NotImplementedError
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
module Ports
|
|
3
|
+
module Outbound
|
|
4
|
+
TransportRequest = Struct.new(:method, :endpoint, :body, :query, :extra_headers, keyword_init: true) do
|
|
5
|
+
def initialize(method:, endpoint:, body: nil, query: nil, extra_headers: {})
|
|
6
|
+
super
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
TransportResponse = Struct.new(:status, :body, :retry_after_secs, keyword_init: true) do
|
|
11
|
+
def initialize(status:, body:, retry_after_secs: nil)
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def success?
|
|
16
|
+
status >= 200 && status < 300
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Outbound transport port: how the application layer talks to the
|
|
21
|
+
# Argos gateway. Default adapter is `Argos::Adapters::Http::NetHttpTransport`.
|
|
22
|
+
class Transport
|
|
23
|
+
def send(request)
|
|
24
|
+
raise NotImplementedError
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Argos
|
|
2
|
+
class Railtie < ::Rails::Railtie
|
|
3
|
+
config.argos = ActiveSupport::OrderedOptions.new
|
|
4
|
+
|
|
5
|
+
initializer "argos.middleware" do |app|
|
|
6
|
+
# Only add middleware if config is present
|
|
7
|
+
if config.argos.api_key
|
|
8
|
+
argos_config = Argos::Config.new
|
|
9
|
+
argos_config.api_key = config.argos.api_key
|
|
10
|
+
|
|
11
|
+
# Override defaults with Rails config if present
|
|
12
|
+
argos_config.base_url = config.argos.base_url if config.argos.key?(:base_url)
|
|
13
|
+
argos_config.environment_id = config.argos.environment_id if config.argos.key?(:environment_id)
|
|
14
|
+
argos_config.timeout = config.argos.timeout if config.argos.key?(:timeout)
|
|
15
|
+
argos_config.max_retries = config.argos.max_retries if config.argos.key?(:max_retries)
|
|
16
|
+
argos_config.auto_block_on_block = config.argos.auto_block_on_block if config.argos.key?(:auto_block_on_block)
|
|
17
|
+
argos_config.blocklist_cache_ttl = config.argos.blocklist_cache_ttl if config.argos.key?(:blocklist_cache_ttl)
|
|
18
|
+
|
|
19
|
+
client = Argos::Client.new(argos_config)
|
|
20
|
+
|
|
21
|
+
mode = config.argos.mode || :async
|
|
22
|
+
|
|
23
|
+
# Insert Argos middleware high in the stack, typically after ActionDispatch::RequestId
|
|
24
|
+
app.middleware.insert_after ActionDispatch::RequestId, Argos::Middleware, client: client, mode: mode
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/argos-sdk.rb
ADDED