mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
REST API v1: tokens, read endpoints, squawks, webhooks
- Personal tokens (Profile → API tokens) act as their owner; integration tokens (Admin → Integrations) reach chosen aircraft and scopes, and their writes name the person they're for (SAM-On-Behalf-Of). Tokens are stored hashed, shown once, revocable, with last use recorded. - GET aircraft, components, due list, inspections, ADs, logbook entries and squawks; POST/PATCH squawks with external_ref links to the other system. - Idempotency-Key for safe retries, paging, JSON errors, rate limits. - Record history keeps the token with each change (in the hashed payload). - Webhooks (squawk created/updated/resolved, entry signed), HMAC-signed, retried, never sent to the server itself; deliveries listed for admins. - The outbound address check is shared by AirTrail and webhooks; reporting a squawk is announced the same way from the form and the API. - docs/guide/api.md: the API reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec0f43019f
commit
bffdea937c
45 files changed
+1577
-51
No files matched your search
@@ -30,9 +30,6 @@ class AirTrailClient
|
||||
@allow_private = allow_private || self.class.allowed_hosts.include?(@base.host.to_s.downcase)
|
||||
end
|
||||
|
||||
# Host name → IP addresses (replaceable in tests, which have no DNS).
|
||||
class_attribute :resolver, default: ->(host) { Addrinfo.getaddrinfo(host, nil, nil, :STREAM).map(&:ip_address) }
|
||||
|
||||
def self.allowed_hosts = ENV.fetch("AIRTRAIL_ALLOWED_HOSTS", "").split(",").map { _1.strip.downcase }.compact_blank
|
||||
|
||||
def me = request(:get, "/api/v1/me").fetch("data")
|
||||
@@ -85,24 +82,13 @@ class AirTrailClient
|
||||
raise NetworkError, I18n.t("services.air_trail.can_t_reach_airtrail_at", host: @base.host, message: e.message)
|
||||
end
|
||||
|
||||
# The address to connect to: the host's first, once all of its addresses are
|
||||
# ones this connection may reach.
|
||||
# The address to connect to (see OutboundAddress).
|
||||
def address(host)
|
||||
ips = resolver.call(host).map { IPAddr.new(_1.sub(/%.*/, "")) }.uniq
|
||||
raise SocketError, "no address" if ips.empty?
|
||||
ips.each do |ip|
|
||||
ip = ip.native # ::ffff:10.0.0.1 → 10.0.0.1
|
||||
reason = if ip.loopback? || ip.link_local? || UNREACHABLE.any? { _1.include?(ip) } then :never
|
||||
elsif (ip.private? || CGNAT.include?(ip)) && !@allow_private then :private
|
||||
end
|
||||
raise BlockedAddress, I18n.t("services.air_trail.blocked_#{reason}", host: host) if reason
|
||||
end
|
||||
ips.first.to_s
|
||||
OutboundAddress.resolve!(host, allow_private: @allow_private)
|
||||
rescue OutboundAddress::Blocked => e
|
||||
raise BlockedAddress, I18n.t("services.air_trail.blocked_#{e.reason}", host: host)
|
||||
end
|
||||
|
||||
UNREACHABLE = [ IPAddr.new("0.0.0.0/8"), IPAddr.new("::/128"), IPAddr.new("224.0.0.0/4"), IPAddr.new("ff00::/8") ].freeze
|
||||
CGNAT = IPAddr.new("100.64.0.0/10") # shared address space, e.g. Tailscale
|
||||
|
||||
def handle(response)
|
||||
status = response.code.to_i
|
||||
json = begin
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Checks where the server is about to send a request (AirTrail, webhooks):
|
||||
# resolves the host and returns the address to connect to, so the name can't
|
||||
# be pointed elsewhere in between. Never the server itself, link-local
|
||||
# addresses (cloud metadata) or multicast; the private network only when
|
||||
# allowed (admins, or hosts the admin listed).
|
||||
module OutboundAddress
|
||||
class Blocked < StandardError
|
||||
attr_reader :reason # :never | :private
|
||||
|
||||
def initialize(reason, host)
|
||||
@reason = reason
|
||||
super("#{host}: #{reason}")
|
||||
end
|
||||
end
|
||||
|
||||
UNREACHABLE = [ IPAddr.new("0.0.0.0/8"), IPAddr.new("::/128"), IPAddr.new("224.0.0.0/4"), IPAddr.new("ff00::/8") ].freeze
|
||||
CGNAT = IPAddr.new("100.64.0.0/10") # shared address space, e.g. Tailscale
|
||||
|
||||
# Host name → IP addresses (replaceable in tests, which have no DNS).
|
||||
mattr_accessor :resolver, default: ->(host) { Addrinfo.getaddrinfo(host, nil, nil, :STREAM).map(&:ip_address) }
|
||||
|
||||
module_function
|
||||
|
||||
def resolve!(host, allow_private:)
|
||||
ips = resolver.call(host).map { IPAddr.new(_1.sub(/%.*/, "")) }.uniq
|
||||
raise SocketError, "no address for #{host}" if ips.empty?
|
||||
ips.each do |ip|
|
||||
ip = ip.native # ::ffff:10.0.0.1 → 10.0.0.1
|
||||
raise Blocked.new(:never, host) if ip.loopback? || ip.link_local? || UNREACHABLE.any? { _1.include?(ip) }
|
||||
raise Blocked.new(:private, host) if (ip.private? || CGNAT.include?(ip)) && !allow_private
|
||||
end
|
||||
ips.first.to_s
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# What happens when a squawk is reported, from the form or the API: the
|
||||
# aircraft's activity log, and a push notification to its other people when
|
||||
# it grounds the aircraft.
|
||||
module SquawkReported
|
||||
module_function
|
||||
|
||||
def call(squawk, by:)
|
||||
aircraft = squawk.aircraft
|
||||
aircraft.log_event!("squawk", I18n.t("flash.squawks.squawk_reported_2", priority_label: squawk.priority_label),
|
||||
user: by, notes: squawk.issue_reported.truncate(200))
|
||||
return unless squawk.grounding?
|
||||
PushNotifier.notify(aircraft.users.merge(User.active).where.not(id: by&.id), :grounding_squawk,
|
||||
path: Rails.application.routes.url_helpers.aircraft_squawks_path(aircraft),
|
||||
tag: "squawk-#{squawk.id}", tail: aircraft.tail_number, issue: squawk.issue_reported.truncate(120))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
# Tells integrations about changes (docs/api.md, "Webhooks"): each enabled
|
||||
# endpoint subscribed to the event, whose token is active and reaches the
|
||||
# aircraft, gets a delivery, sent by WebhookDeliveryJob after the change is
|
||||
# saved.
|
||||
module Webhooks
|
||||
module_function
|
||||
|
||||
def publish(event, aircraft:, data:)
|
||||
endpoints = WebhookEndpoint.enabled.joins(:api_token).merge(ApiToken.active).includes(:api_token)
|
||||
.select { _1.subscribed?(event) && Array(_1.api_token.aircraft_ids).include?(aircraft.id) }
|
||||
return if endpoints.empty?
|
||||
body = { event: event, id: SecureRandom.uuid, created_at: Time.current.utc.iso8601, aircraft_id: aircraft.id, data: data }.to_json
|
||||
endpoints.each do |endpoint|
|
||||
delivery = endpoint.deliveries.create!(event: event, payload: body)
|
||||
WebhookDeliveryJob.perform_later(delivery.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user