Installing the Enforcer

Communicate with the HUMAN Backend

For the enforcer to communicate with HUMAN Collector server, a specific “backend” server must be added and configured in the Fastly UI (or using the contrib/pxbackend.sh script).
Backend parameters (replace ${APP_ID} with your HUMAN Application ID):

  • Name: PX_BACKEND_${APP_ID}
  • Hostname: sapi-${APP_ID}.perimeterx.net
  • Override host: sapi-${APP_ID}.perimeterx.net
  • Use SSL/TLS: Yes

Module installation

Include perimeterx-fastly-enforcer dependency to Cargo.toml:

$cargo add perimeterx-fastly-enforcer

Module integration

To integrate HUMAN Rust module into an existing Rust code, the following base snippet could be used:

1let mut px: PXEnforcer = PXEnforcer::new(perimeterx_fastly_enforcer::DEFAULT_CONFIGSTORE_NAME);
2let px_result = px.enforce(&mut req)?;
3if let Some(r) = px_result {
4 return Ok(r);
5};
6
7//... communicate with Origin server / process request and response
8
9px.post_enforce(&response);

PXEnforcer Setup

Initialize PXEnforcer structure, it takes a name of Fastly “ConfigStore” (it’s possible to use the default name: perimeterx_fastly_enforcer::DEFAULT_CONFIGSTORE_NAME)

1pub fn new(config_store_name: &str) -> Self

This function takes a request and returns a result which basically contains a response (for “blocked” or “first party” requests):

1pub fn enforce(&mut self, req: &mut Request) -> Result<Option<Response>, Error>

At the end of request processing, the following function must be called to finalize HUMAN enforcer code:

1pub fn post_enforce(&self, res: &Response)

It is possible to access HUMANContext structure with various Enforcer variables via px.ctx member:

1// send "score" value to the Origin
2req.set_header("x-px-score", px.ctx.score.to_string());

To set “custom_parameters” variables, the following callback function could be used:

1pub type PXEnrichCustomParamsFn = fn (req: &Request, conf: &PXConfig, params: &mut PXCustomParams);

where:
req: fastly::Request
conf: HUMANConfig
params: modifiable structure with custom_param1 .. custom_param10 fields

To set custom parameters callback function, use the following setter:

1pub fn set_enrich_custom_params_fn(&mut self, f: PXEnrichCustomParamsFn)

Sample code

This is the simplest example on how to use HUMAN Rust module:

1use fastly::{Error, Request, Response};
2use perimeterx_fastly_enforcer::pxenforce::PXEnforcer;
3
4const ORIGIN_BACKEND: &str = "origin_backend";
5
6fn send_to_origin(req: Request) -> Result<Response, Error> {
7 println!("sending to Origin...");
8 match req.send(ORIGIN_BACKEND) {
9 Ok(r) => return Ok(r),
10 Err(e) => return Err(e.into()),
11 }
12}
13
14#[fastly::main]
15fn main(mut req: Request) -> Result<Response, Error> {
16
17 let mut px: PXEnforcer = PXEnforcer::new(perimeterx_fastly_enforcer::DEFAULT_CONFIGSTORE_NAME);
18
19 // execute PX Enforcer for Request
20 let px_result = px.enforce(&mut req)?;
21
22 // return, if it's a "blocked" or "first party" response
23 if let Some(r) = px_result {
24 return Ok(r);
25 };
26
27 // ... process Client request ...
28
29 // we can access "PXContext" structure.
30 // as an example: send "score" value to the Origin
31 req.set_header("x-px-score", px.ctx.score.to_string());
32
33 // a client function to communicate with the Origin
34 let response = send_to_origin(req)?;
35
36 // ... process Origin response ...
37
38 // must be called at the end
39 px.post_enforce(&response);
40
41 // we are ok to send response back to client
42 return Ok(response);
43}