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.
Header names the gateway may pass to an HTTP authorizer.
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.
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 23 def initialize(mode:, allow: [], inspection: :connect, gateway: nil, authorizer: nil, forward_headers: [], mutation_headers: []) mode = mode.to_sym inspection = inspection.to_sym 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?) raise PolicyError, "network allow, gateway, authorizer, and header 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? raise PolicyError, "network mode :allowlist requires at least one exact destination" end @mode = mode @allow = Array(allow).map { |endpoint| normalize_endpoint(endpoint) }.uniq.freeze @inspection = inspection @gateway = gateway @authorizer = authorizer @forward_headers = Array(forward_headers).map { |name| normalize_header_name(name) }.uniq.freeze @mutation_headers = Array(mutation_headers).map { |name| normalize_header_name(name) }.uniq.freeze freeze end
Builds an outbound policy. Enforcement remains the configured gateway’s responsibility.
Public Instance Methods
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 76 def ==(other) other.is_a?(self.class) && [mode, allow, inspection, gateway, authorizer, forward_headers, mutation_headers] == [other.mode, other.allow, other.inspection, other.gateway, other.authorizer, other.forward_headers, other.mutation_headers] end
Policies compare by their normalized enforcement declaration.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 73 def allowlist? = mode == :allowlist
Indicates that outbound traffic must pass an allowlist gateway.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 86 def hash = [mode, allow, inspection, gateway, authorizer, forward_headers, mutation_headers].hash
Hashes the normalized enforcement declaration.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 69 def inherit? = mode == :inherit
Indicates unrestricted backend-provided connectivity.
Source
# File lib/little_ghost/sandbox/network_policy.rb, line 71 def none? = mode == :none
Indicates that outbound connectivity must be disabled.