class LittleGhost::Sandbox::NetworkPolicy
Declares outbound connectivity for sandbox-launched processes. A network policy does not apply to providers or arbitrary Ruby tools in the host.
Attributes
Normalized endpoints accepted by an allowlist gateway.
Network::Facade listeners that forward to one upstream each.
Header names the gateway may pass to an HTTP authorizer, or :all.
Explicit gateway declaration, when supplied.
Inspection level requested from the gateway.
Connectivity mode: :inherit, :none, or :allowlist.
Header names an HTTP authorizer may set on an upstream request.
Per-host request timeouts in seconds for inspected requests.
Public Class Methods
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 12 def self.coerce(value) return nil if value.nil? return value if value.is_a?(self) return new(mode: value) if value.is_a?(Symbol) || value.is_a?(String) raise PolicyError, "network policy must be a mode, Hash, or Sandbox::NetworkPolicy" unless value.is_a?(Hash) new(**value.transform_keys(&:to_sym)) end
Returns value unchanged or converts a mode or Hash to a policy.
# File lib/little_ghost/sandbox/network_policy.rb, line 30 def initialize(mode:, allow: [], inspection: :connect, gateway: nil, authorizer: nil, forward_headers: [], mutation_headers: [], timeouts: {}, facades: []) mode = mode.to_sym inspection = inspection.to_sym facades = Array(facades).map { |facade| Network::Facade.coerce(facade) } raise PolicyError, "network mode must be :inherit, :none, or :allowlist" unless MODES.include?(mode) unless INSPECTION_MODES.include?(inspection) raise PolicyError, "network inspection must be :connect or :http" end if mode != :allowlist && (!Array(allow).empty? || gateway || authorizer || !Array(forward_headers).empty? || !Array(mutation_headers).empty? || !timeouts.empty? || !facades.empty?) raise PolicyError, "network allow, gateway, authorizer, header, timeout, and facade policy require mode :allowlist" end if inspection == :http && mode != :allowlist raise PolicyError, "HTTP inspection requires mode :allowlist" end if inspection == :http && !authorizer raise PolicyError, "HTTP inspection requires an authorizer" end if mode == :allowlist && Array(allow).empty? && facades.empty? raise PolicyError, "network mode :allowlist requires at least one exact destination or facade" end raise PolicyError, "network facades require an authorizer" if !facades.empty? && !authorizer if facades.map(&:name).uniq.length != facades.length raise PolicyError, "network facade names must be unique" end if facades.map { |facade| facade.port || facade.socket }.uniq.length != facades.length raise PolicyError, "network facade listeners must be unique" end @mode = mode @allow = Array(allow).map { |endpoint| normalize_endpoint(endpoint) }.uniq.freeze @inspection = inspection @gateway = gateway @authorizer = authorizer @forward_headers = if forward_headers == :all :all else Array(forward_headers).map { |name| normalize_header_name(name) }.uniq.freeze end @mutation_headers = Array(mutation_headers).map { |name| normalize_header_name(name) }.uniq.freeze @timeouts = timeouts.to_h do |host, seconds| [String(host).downcase.freeze, Network.timeout_seconds(seconds)] end.freeze @facades = facades.freeze freeze end
Builds an outbound policy. Enforcement remains the configured gateway’s responsibility.
forward_headers names the client headers an HTTP authorizer receives, or :all to send every header except credentials. timeouts maps exact host names to per-request timeouts in seconds for inspected requests. facades lists Network::Facade declarations or Hashes; every facade request is authorized by authorizer. Network::HostPolicy#network_policy derives these values from one host policy.
Public Instance Methods
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 105 def ==(other) other.is_a?(self.class) && [mode, allow, inspection, gateway, authorizer, forward_headers, mutation_headers, timeouts, facades] == [other.mode, other.allow, other.inspection, other.gateway, other.authorizer, other.forward_headers, other.mutation_headers, other.timeouts, other.facades] end
Policies compare by their normalized enforcement declaration.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 102 def allowlist? = mode == :allowlist
Indicates that outbound traffic must pass an allowlist gateway.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 115 def hash = [mode, allow, inspection, gateway, authorizer, forward_headers, mutation_headers, timeouts, facades].hash
Hashes the normalized enforcement declaration.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 98 def inherit? = mode == :inherit
Indicates unrestricted backend-provided connectivity.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 100 def none? = mode == :none
Indicates that outbound connectivity must be disabled.