id: rule:foo:bar
match:
url: http://my-service.local/<**>
strategy: glob
upstream: http://backend-a:8080
methods:
- GET
- POST
execute:
- authenticator: foo
- authorizer: bar
- contextualizer: foo
- unifier: zab
on_error:
- error_handler: foobarRule
Rules are the heart of heimdall. These allow execution of arbitrary logic, required by your upstream service. This section describes everything related to the configuration of a particular rule and how can these be combined to rule sets, which can then be loaded by a rule provider.
Rule Configuration
A single rule consists of the following properties:
id: string (mandatory)The unique identifier of a rule. It must be unique across all rules loaded by the same Rule Provider. To ensure this, it is recommended to let the
idinclude the name of your upstream service, as well as its purpose. E.g.rule:my-service:public-api.match: RuleMatcher (mandatory)Defines how to match a rule and supports the following properties:
url: string (mandatory)Glob or Regex pattern of the endpoints of your upstream service, which this rule should apply to. Query parameters are ignored.
strategy: Matching Strategy (optional)Which strategy to use for matching of the value, provided in the
urlproperty. Can begloborregex. Defaults toglob.
methods: string array (optional)Which HTTP methods (
GET,POST,PATCH, etc) are allowed for the matched URL. If not specified, every request to that URL will result in405 Method Not Allowedresponse from heimdall.upstream: string (mandatory in Proxy operation mode)Defines where to forward the proxied request to. Used only when heimdall is operated in the Proxy operation mode. Only the URL schema and the host parts are used if this property is specified.
execute: Regular Pipeline (mandatory)Which mechanisms to use to authenticate, authorize, hydrate (enrich) and mutate the subject of the request.
on_error: Error Handler Pipeline (optional)Which error handler mechanisms to use if any of the mechanisms, defined in the
executeproperty, fails. This property is optional only, if a default rule has been configured and contains anon_errordefinition.
Matching Strategy
Matching strategies are used to match the url patterns in rules and explicitly set by making use of matching_strategy rule property. Following strategies are available:
regex- to matchurlexpressions by making use of regular expressions.glob- to matchurlexpressions by making use of glob expressions.
Heimdall uses dlclark/regexp2 and gobwas/glob to match regex expressions, respectively glob expressions. Head over to linked resources to get more insights about possible options for expression definitions.
https://mydomain.com/matcheshttps://mydomain.com/and doesn’t matchhttps://mydomain.com/fooorhttps://mydomain.com.<https|http>://mydomain.com/<.*>matcheshttps://mydomain.com/andhttp://mydomain.com/foo. Doesn’t matchhttps://other-domain.com/orhttps://mydomain.com.http://mydomain.com/<+>matcheshttp://mydomain.com/123, but doesn’t matchhttp://mydomain/abc.http://mydomain.com/<(?!protected).*>matcheshttp://mydomain.com/resource, but doesn’t matchhttp://mydomain.com/protected.
https://mydomain.com/<m?n>matcheshttps://mydomain.com/manand does not matchhttp://mydomain.com/foo.https://mydomain.com/<{foo*,bar*}>matcheshttps://mydomain.com/fooorhttps://mydomain.com/barand doesn’t matchhttps://mydomain.com/any.
Regular Pipeline
As described in the Concepts section, heimdall’s decision pipeline consists of multiple mechanisms - at least consisting of authenticators and unifiers. The definition of such a pipeline happens as a list of required mechanisms (previously configured) with the corresponding ids in the following order:
List of authenticators using
authenticatoras key, followed by the required authenticatorid. Authenticators following the first defined in the list are used by heimdall as fallback. That is, if first authenticator fails due to missing authentication data, second is executed, etc. By default, fallback is not used if an authenticator fails due to validation errors of the given authentication data. E.g. if an authenticator fails to validate the signature of a JWT token, the next authenticator in the list will not be executed. Instead, the entire pipeline will fail and lead to the execution of the error handler pipeline. This list is mandatory if no default rule is configured.Some authenticators use the same sources to get subject authentication object from. E.g. the jwtand theoauth2_introspectionauthenticators can retrieve tokens from the same places in the request. If such authenticators are used in the same pipeline, you should configure the more specific ones before the more general ones to have working default fallbacks. To stay with the above example, thejwtauthenticator is more specific compared tooauth2_introspection, as it will be only executed, if the token is in a JWT format. In contrast to this, theoauth2_introspectionauthenticator is more general and does not care about the token format, thus will feel responsible for the request as soon as it finds a bearer token. You can however also make use of theallow_fallback_on_errorconfiguration property and set it totrue. This will allow a fallback even if the verification of the credentials fail.List of contextualizers and authorizers in any order (optional). Can also be mixed. As with authenticators, the list definition happens using either
contextualizerorauthorizeras key, followed by the requiredid. All mechanisms in this list are executed in the order, they are defined. If any of these fails, the entire pipeline fails, which leads to the execution of the error handler pipeline. This list is optional.List of unifiers using
unifiersas key, followed by the required unifierid. All unifiers in this list are executed in the order, they are defined. If any of these fails, the entire pipeline fails, which leads to the execution of the error handler pipeline. This list is mandatory if no default rule is configured.
In all cases, the used mechanism can be partially reconfigured if supported by the corresponding type. Configuration goes into the config properties. These reconfigurations are always local to the given rule. With other words, you can adjust your rule specific pipeline as you want without any side effects.
Execution of an contextualizer, authorizer, or unifier mechanisms can optionally happen conditionally by making use of a CEL expression in an if clause, which has access to the Subject and the Request objects. If the if clause is not present, the corresponding mechanism is always executed.
# list of authenticators
- authenticator: foo
- authenticator: bar
config:
subject: anon
# ... any further required authenticator
# list of authorizers and contextualizers in any order
- contextualizer: baz
config:
cache_ttl: 0s
- authorizer: zab
- contextualizer: foo
if: Subject.ID != "anonymous"
- contextualizer: bar
- authorizer: foo
if: Request.Method == "POST"
config:
expressions:
- expression: |
// some expression logic deviating from the
// definition in the pipeline configuration.
# ... any further required authorizer or contextualizer
# list of unifiers
- unifier: foo
- unifier: bar
config:
headers:
- X-User-ID: {{ quote .ID }}
# ... any further required unifiersThis example uses
two authenticators, with authenticator named
barbeing the fallback for the authenticator namedfoo. This fallback authenticator is obviously of type anonymous as it reconfigures the referenced prototype to useanonfor subject id.multiple contextualizers and authorizers, with first contextualizer having its cache disabled (
cache_ttlset to 0s) and the last authorizer being of type cel as it reconfigures the referenced prototype to use a different authorization script.two unifiers, with the second one being obviously of type header, as it defines a
X-User-IDheader set to the value of the subject id to be forwarded to the upstream service.contextualizer
foois only executed if the authenticated subject is not anonymous.authorizer
foois only executed if the request method is HTTP POST.
Error Handler Pipeline
Compared to the Regular Pipeline, the error handler pipeline is pretty simple. It is also a list of mechanisms, but all referenced types are error handler types. Thus, each entry in this list must have error_handler as key, followed by the ìd of the required error handler, previously defined in Heimdall’s Pipeline Mechanisms configuration. Error handlers are always executed as fallbacks. So, if the condition of the first error handler does not match, second is selected, if its condition matches, it is executed, otherwise the next one is selected, etc. If none of the conditions of the defined error handlers match, the default error handler is executed.
As with the regular pipeline, partial reconfiguration of the used mechanisms is possible if supported by the corresponding type. The overrides are always local to the given rule as well.
- error_handler: foo
- error_handler: bar
config:
when:
# rule specific conditionsThis example uses two error handlers, named foo and bar. bar will only be selected by heimdall if foo 's error condition (defined in Heimdall’s Pipeline Mechanisms configuration) does not match. bar does also override the error condition as required by the given rule.
Rule Set
In principle, a rule set is just a list of rules with some additional meta information. Each RuleSet definition has the following attributes if not stated otherwise by a particular provider:
version: string (mandatory)The version schema of the
RuleSet. The current version of heimdall supports only the version1.name: string (optional)The name of a rule set. Used only for logging purposes.
rules: Rule Configuration array (mandatory)List of the actual rules.
version: "1"
name: my-rule-set
rules:
- id: rule:1
match:
url: https://my-service1.local/<**>
methods: [ "GET" ]
execute:
- authorizer: foobar
- id: rule:2
match:
url: https://my-service2.local/<**>
methods: [ "GET" ]
execute:
- authorizer: barfooLast updated on Jun 20, 2023