<@U04UQ68083H>, I have a question in Ory Oathkeepe...
# ory-copilot
c
@chilly-king-10285, I have a question in Ory Oathkeeper, I'm trying to create a mutator that will inject the logged in user's public metadata into the request header. I can do this with this example
Copy code
mutators:
  noop:
    enabled: true
  header:
    enabled: true
    config:
      headers:
        X-User-IAM-ID: "{{ print .Subject }}"
        X-User-IAM-Public-Metadata: "{{ print .Extra.identity.metadata_public }}"
but this example doesn't handle the case if the user has no "metadata_public" and it's value is "nil", how can I handle this case?
c
A message from kapa.ai
Based on the information provided in the extracts, there isn't a direct way to handle a
nil
value for
metadata_public
in the configuration of the
header
mutator. The configuration provided in the extracts doesn't include conditional logic or error handling within the template itself. However, you could potentially handle this at the application level where the headers are being used. If the
X-User-IAM-Public-Metadata
header is
nil
or not present, you could have your application logic handle this case appropriately. For more advanced handling, you might want to consider using the
hydrator
mutator mentioned in the extracts. This mutator allows for fetching additional data from external APIs, which can be then used by other mutators. You could potentially use this to fetch and handle user metadata in a more sophisticated way. For more detailed information, you might want to refer to the Ory Oathkeeper documentation: - [Mutators](https://www.ory.sh/docs/oathkeeper/pipeline/mutator) - [Hydrator Mutator](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#hydrator) - [Header Mutator](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#header)
c
@chilly-king-10285 can you give an example for using a hydrator mutator to retrieve information from kratos and inject that into the request header?
c
A message from kapa.ai
I'm sorry, but the provided extracts do not contain enough information to provide a detailed example of using a hydrator mutator to retrieve information from Kratos and inject that into the request header. However, I can provide a general idea based on the information available. The
hydrator
mutator works by making an upstream HTTP call to an API specified in the Per-Rule Configuration. The request is a POST request and it contains a JSON representation of the AuthenticationSession struct in the body. The response from the API should be a similar JSON object, but with
extra
or
header
fields modified. Here is a general example of how the
hydrator
mutator could be used:
Copy code
yaml mutators:   hydrator:     enabled: true     config:       api:         url: <http://my-api/my-endpoint>
In this example, the
hydrator
mutator would make a POST request to
<http://my-api/my-endpoint>
with the AuthenticationSession as the request body. The API at
<http://my-api/my-endpoint>
would need to be set up to handle this request, fetch the necessary information from Kratos, and return a response with the
extra
or
header
fields modified as necessary. For more detailed information and examples, you would need to refer to the [Ory Oathkeeper documentation](https://www.ory.sh/docs/oathkeeper/pipeline/mutator#hydrator).
c
@chilly-king-10285 you are mistaken, there is a way to check for a value in a mutator, this what I have used in my code to achieve that
Copy code
mutators:
  noop:
    enabled: true
  header:
    enabled: true
    config:
      headers:
        X-User-IAM-ID: "{{ print .Subject }}"
        X-User-IAM-Branch-Id: "{{ if .Extra.identity.metadata_public }} {{ if .Extra.identity.metadata_public.branch_id }} {{ print .Extra.identity.metadata_public.branch_id }} {{ end }} {{ end }}"
c
A message from kapa.ai
Thank you for sharing your solution. It appears you're using conditional logic within the Go text/template syntax to check if
metadata_public
and
branch_id
exist before printing the value. This is a great way to handle potential
nil
values and avoid errors. Here's the code you shared for reference:
Copy code
yaml mutators:   noop:     enabled: true   header:     enabled: true     config:       headers:         X-User-IAM-ID: "{{ print .Subject }}"         X-User-IAM-Branch-Id: "{{ if .Extra.identity.metadata_public }} {{ if .Extra.identity.metadata_public.branch_id }} {{ print .Extra.identity.metadata_public.branch_id }} {{ end }} {{ end }}"
In this configuration, the
X-User-IAM-Branch-Id
header will only be set if
.Extra.identity.metadata_public
and
.Extra.identity.metadata_public.branch_id
exist, preventing potential issues with
nil
values. This is a great example of how flexible the Ory Oathkeeper mutators can be when combined with the power of Go's text/template package.