Skip to main content

ServeOptions

Struct ServeOptions 

Source
#[non_exhaustive]
pub struct ServeOptions {
Show 15 fields pub listen: Vec<SocketAddr>, pub tls: Option<ServerTls>, pub client_ca_pem: Option<Vec<u8>>, pub trusted_proxies: Vec<IpNet>, pub trusted_proxy_header: Option<ForwardedHeader>, pub trusted_proxy_client_cert: Option<String>, pub proxy_protocol: bool, pub body_timeout: Duration, pub receive: Option<Arc<ReceivePolicies>>, pub allow_cleartext_credentials: bool, pub parallel_uploads: u32, pub session_idle_timeout: Duration, pub max_sessions: usize, pub on_report: Option<Arc<dyn Fn(ReceiveReport) + Send + Sync>>, pub on_event: Option<Arc<dyn Fn(&ServerEvent) + Send + Sync>>,
}
Expand description

The options of bind.

The struct is #[non_exhaustive], so a caller starts from ServeOptions::default and sets the fields.

bind checks the fields that go together. Its errors list the refusals.

The fields parallel_uploads, session_idle_timeout, max_sessions, and on_report go to the EndpointOptions of the receive endpoint. Their defaults are the defaults of EndpointOptions::default. If receive is None, bind does not read these four fields or allow_cleartext_credentials. It then refuses no value of them.

§Authentication

The server identifies the client of each request by the auth items of the client groups of receive: the requests of the receive endpoint, and the reads of the archive view and of the ref listing. Identification gives one auth item. The server takes the first step that applies:

  1. A request with more than one Authorization header gets 401.
  2. If the connection has no TLS, a bearer or Basic credential gets 403, unless allow_cleartext_credentials is set, or a trusted proxy states the scheme https (see Client address).
  3. A request with an Authorization header gets the item of its credential. A bearer token, Authorization: Bearer TOKEN, gets the token-sha256:HEX item of the SHA-256 digest of the token. A Basic credential, Authorization: Basic with the base64 of USER:PASSWORD, gets the basic-sha256:USER:HEX item of the user name and of the digest of the password. The user name runs to the first :, so the password can hold :. A credential that matches no item gets 401, also when the request has a client certificate. An empty token, a Basic credential with no :, a user name that is not UTF-8, an empty password, and another scheme match no item.
  4. A request with a client certificate gets the first item that matches, in this order: tls-sha256:HEX by the SHA-256 digest of the DER bytes, tls:NAME by the subject CN, tls:*. A certificate that matches no item gets 403. A subject matches tls:NAME only if it holds exactly one CN, of type UTF8String or PrintableString. From a trusted proxy with a header of a forwarded certificate, the certificate comes from that header alone, and never from the TLS handshake (see Forwarded certificate). With no client_ca_pem, a request has no client certificate, so the tls-sha256:HEX, tls:NAME, and tls:* items match no request. The other items of their groups still match.
  5. A request whose client address is in the network of a net: item gets that item. When two items match, the item with the longer prefix wins.
  6. A request gets the anonymous item, if one exists.
  7. Else the request gets 401 when a token-sha256 or a basic-sha256 item exists, and 403 when none exists.

The server compares the digest of a credential with every token-sha256 or basic-sha256 item in constant time. It does not stop at a match. The TLS handshake verifies a client certificate against client_ca_pem. The server reads the certificate of the handshake one time for each connection.

Each 401 carries two headers: WWW-Authenticate: Bearer realm="ostrya" and WWW-Authenticate: Basic realm="ostrya".

A read gets the ReadAccess of the item, with the claims of its certificate. A refused read gets the status of the refusal and an empty body. Server states the rules of a read.

The owner key of a request is the text of its item, as AuthItem writes it. All clients with one item share one owner key, for example all anonymous clients. A client certificate that gives a tls:*, tls:NAME, or tls-sha256:HEX item of a group with tls-accept-claim=true has the key tls-sha256:HEX, the SHA-256 digest of its DER bytes. So two certificates with different claims never share a session. This key is the key of no other client: the certificate sets the item that it matches, and only the certificate of that digest matches the item tls-sha256:HEX.

A session request selects the client group that carries the item in the namespace of its Hello, with ReceivePolicies::select. The collection id of Hello names the namespace, and no collection or the collection id of the repository names the local refs. The session gets the policy of that group and no hooks. With no such group, and with a group that opens no push session, the request gets 403.

§Claims

A client group with tls-accept-claim=true reads the claims of the client certificate that gives the item of a session request or of a read: the URI SANs with the scheme ostrya, as Claims reads them. The claims of the certificate of the TLS handshake are read one time for each connection. The claims of a forwarded certificate are read one time for each cache entry.

  • When the certificate holds one or more claims for the namespace of the session or of the ref that a read names, as ClientAccess::claim_rules gives them, they replace the allow items and the deny items of the group. The other values of the policy come from the group.
  • When it holds no claim for the namespace, the rules of the group apply.
  • When the claims do not parse, the request gets 403, and the server reports a ServerEvent::BadClaims to on_event. The claims do not parse when a URI with the scheme ostrya, in any case, is no claim form, when the certificate holds more than MAX_CLAIMS such URIs, when the certificate does not decode, when it holds more than one subjectAltName extension, and when its subjectAltName extension does not decode. Each case has its own reason.
  • When the rules of the claims open no push session, the session request gets 403.
  • A read gets 403 when the claims do not parse and a group with tls-accept-claim=true carries the item of the certificate, also a read of an object or of config. The server reports a ServerEvent::BadClaims for each such read.

A group with tls-accept-claim=false, a credential, a net: item, and the anonymous item read no claim.

§Client address

The client address is the TCP peer address of the connection, with these changes:

  • With proxy_protocol, the source address of the PROXY header replaces the peer address. A header with no address keeps the peer address.
  • When the peer address is in trusted_proxies, the server reads the client address from the one header of trusted_proxy_header. It walks the hops of the header from the right, and skips each hop whose address is in a trusted network. The first other hop is the client. When all hops are trusted, the client is the leftmost hop. A hop with no address, for example unknown, gives a client with no address, which matches no net: item. A header that is absent, or that has one line of white space alone, keeps the peer address. A header that is not UTF-8, that does not parse, or that has text and no hop, for example X-Forwarded-For: , ,, gives no address.
  • From a trusted peer, the request counts as TLS for step 2 when the proxy states the scheme https, in any case. With ForwardedHeader::Forwarded, the scheme is the proto of the selected hop. With the other headers, it is the rightmost value of X-Forwarded-Proto.
  • From a peer that is not trusted, the server reads no forwarding header, no X-Forwarded-Proto, and no forwarded certificate.

An IPv4-mapped IPv6 address counts as its IPv4 address.

With proxy_protocol, the server compares the source address of the PROXY header with trusted_proxies, so it does not read the forwarding headers and the forwarded certificate of a proxy that states the address of its client in the PROXY header.

A trusted proxy must set or remove the forwarding header, the X-Forwarded-Proto header, and the header of a forwarded certificate of each request. Else a client can send these headers through the proxy and take another address or another certificate.

§Forwarded certificate

trusted_proxy_client_cert names the request header that carries the client certificate from a trusted proxy. The server reads the header for each request, and never keeps the result for a connection, because a proxy sends the requests of many clients over one connection. The value is one of these forms, after the server percent-decodes it, with + kept as +:

  • a PEM certificate that starts with -----BEGIN, as nginx $ssl_client_escaped_cert gives it URL-escaped;
  • the base64 of the DER bytes, as HAProxy ssl_c_der,base64 gives it, or URL-escaped, as Traefik passTLSClientCert gives it.

From a trusted proxy, this header alone gives the certificate. The server does not use the certificate of the TLS handshake, which is the certificate of the proxy. A request with no such header gives no certificate. From a peer that is not trusted, or with no trusted_proxy_client_cert, the certificate of the TLS handshake applies.

An empty value, or white space alone, is no certificate. These cases get 403: a value with a comma, as the chain of Traefik holds; more than one line of the header; PEM with more than one CERTIFICATE section; a value that does not decode; and a certificate that client_ca_pem does not verify. The server uses no intermediate certificate from the proxy, so the client CA must hold each intermediate CA. The verification checks the validity period and the clientAuth extended key usage.

The server keeps each verified certificate in a cache of 1024 entries, keyed by the SHA-256 digest of its DER bytes, until the end of its validity period. A full cache removes its oldest entry. The cache keeps no refusal.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§listen: Vec<SocketAddr>

The addresses to listen on.

Port 0 lets the kernel choose a port, which Server::local_addrs reports. bind refuses an empty list. The default is 127.0.0.1:8080.

§tls: Option<ServerTls>

The TLS files, or None for plain HTTP. The default is None.

§client_ca_pem: Option<Vec<u8>>

The PEM CA certificates that verify the client certificate of the TLS handshake and a forwarded certificate, or None.

bind builds one verifier from the certificates, and the handshake and each forwarded certificate use it. A client that presents no certificate also completes the handshake. A client that presents a certificate that the CA did not sign fails the handshake. The field needs tls or trusted_proxy_client_cert. The default is None.

§trusted_proxies: Vec<IpNet>

The networks of the peers whose forwarding data the server reads.

Client address states the rules. A non-empty list needs trusted_proxy_header. The default is an empty list, which trusts no peer.

§trusted_proxy_header: Option<ForwardedHeader>

The request header from which the server reads the client address of a request from a trusted proxy, or None.

The server reads this one header, and no other header of a client address. The field needs a non-empty trusted_proxies. The default is None.

§trusted_proxy_client_cert: Option<String>

The name of the request header that carries the client certificate from a trusted proxy, or None.

Forwarded certificate states the rules. The name passes ostrya::access::is_header_name. The field needs client_ca_pem and a non-empty trusted_proxies. The default is None.

§proxy_protocol: bool

The switch that reads a PROXY protocol header, version 1 or 2, at the start of each connection.

With the switch, each listener reads the header before the TLS handshake and before the first request. Its source address replaces the peer address. A header with no address, for example UNKNOWN or LOCAL, keeps the peer address. The server closes a connection with no valid header, or with no header within 30 seconds, and writes no byte to it. The default is false.

Each peer that can connect states its own client address in the header. Only the proxy must be able to reach the listener.

§body_timeout: Duration

The longest time that a response body waits for the client to take its next bytes.

If a body waits longer, its connection ends. The bodies of the connection then drop and release the files and the compressors that they hold. An HTTP/2 connection sends a ping after half this time with no frame from the peer. It ends if the peer does not answer within this time.

A body that waits for its own reader, for example for a compressor of the archive view, does not wait for the client. The time does not count for such a body until it gives its next frame.

The default is 60 seconds. bind refuses zero.

§receive: Option<Arc<ReceivePolicies>>

The client groups of the server and their policies, or None for a read-only server.

With None, the server has no receive endpoint and identifies no client. Each read gets the rights of the anonymous item under the implicit group of a config with no client group. The default is None. ReceivePolicies::from_config builds the value from the repository config. Authentication states how a request gets its group. The server does not read the groups or a repository setting of the endpoint again while it runs. A change takes effect at the next start.

§allow_cleartext_credentials: bool

The switch that takes a bearer or Basic credential on a connection without TLS.

The default is false. If the switch is false, the server refuses such a credential with 403, unless a trusted proxy states the scheme https. A server on a loopback address, or behind a proxy that terminates TLS and is not in trusted_proxies, can set it. A read-only server does not read the field.

§parallel_uploads: u32

The number of object streams of one session, as in EndpointOptions::parallel_uploads.

The default is 4.

§session_idle_timeout: Duration

The idle timeout of a session, as in EndpointOptions::session_idle_timeout.

The default is 300 seconds.

§max_sessions: usize

The most sessions open at the same time, as in EndpointOptions::max_sessions.

The default is 16.

§on_report: Option<Arc<dyn Fn(ReceiveReport) + Send + Sync>>

The callback for the report of a committed session, as in EndpointOptions::on_report.

§on_event: Option<Arc<dyn Fn(&ServerEvent) + Send + Sync>>

The callback for each ServerEvent of the server, or None.

The server calls it in the task of the request, so the callback must return soon. A host writes the event to its log, for example the Display text of the event on standard error. ServerEvent::is_warning tells a warning from an error. The server calls it also with no receive policies. The default is None, which drops each event.

Trait Implementations§

Source§

impl Clone for ServeOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ServeOptions

The debug text shows a callback that is set as <callback>, a client CA that is set as <N bytes>, the trusted proxies as networks, and the receive policies as the names of their client groups. It holds no auth item, so no digest and no user name.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ServeOptions

Source§

fn default() -> ServeOptions

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for ServeOptions

§

impl !UnwindSafe for ServeOptions

§

impl Forget for ServeOptions

§

impl Freeze for ServeOptions
where *const u8 is !null: Freeze, PhantomData<SocketAddr>: Freeze, PhantomData<u8>: Freeze, PhantomData<IpNet>: Freeze, *const ArcInner<ReceivePolicies> is !null: Freeze, PhantomData<ArcInner<ReceivePolicies>>: Freeze, *const ArcInner<dyn Fn(ReceiveReport) + Sync + Send> is !null: Freeze, PhantomData<ArcInner<dyn Fn(ReceiveReport) + Sync + Send>>: Freeze, *const ArcInner<dyn Fn(&ServerEvent) + Sync + Send> is !null: Freeze, PhantomData<ArcInner<dyn Fn(&ServerEvent) + Sync + Send>>: Freeze,

§

impl Move for ServeOptions

§

impl Send for ServeOptions
where RawVecInner: Send, Arc<ReceivePolicies>: Send, Arc<dyn Fn(ReceiveReport) + Sync + Send>: Send, Arc<dyn Fn(&ServerEvent) + Sync + Send>: Send,

§

impl Sync for ServeOptions
where RawVecInner: Sync, Arc<ReceivePolicies>: Sync, Arc<dyn Fn(ReceiveReport) + Sync + Send>: Sync, Arc<dyn Fn(&ServerEvent) + Sync + Send>: Sync,

§

impl Unpin for ServeOptions

§

impl UnsafeUnpin for ServeOptions

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more