oauth2-proxy icon indicating copy to clipboard operation
oauth2-proxy copied to clipboard

structured config #1: introduce mapstructure decoder for yaml parsing

Open tuunit opened this issue 1 month ago • 0 comments

Description

This PR introduces mapstructure for decoding and encoding the yaml files. Mapstructure is a defacto standard library used by many libraries like spf13/viper for more dynamic data loading between different interfaces.

Just using the golang json / yaml encoding / decoding would lead to overwriting the default set before loading the config file. Trying to load the default configs afterwards would be rather hard or impossible.

Motivation and Context

Cases to consider:

  1. When directly loading into a struct you cannot identify if the user explicitly set a boolean to false in the config file or if it was set to the boolean default of false. This is problematic as we have boolean like Cookie.Secure that are supposed to be set to true by default.
  2. For better readability we might want to squash elements in the yaml like HeaderValues either being a SecretSource or ClaimSource. Which isn't supported by the default json / yaml parser
# non-squashed
headers:
- name: X-Forwarded-User
  values:
  - claimSource:
      claim: user

# squashed
headers:
- name: X-Forwarded-User
  values:
  - claim: user
  1. Parsing of time primitives like Duration or Time through strings. If you want to be able to configure Duration types with values like 2h. You need to implement a custom wrapper type and Marshal and Unmarshal method. Mapstructure allows for decoding hooks through the usage of relection.

How Has This Been Tested?

  1. Test cases have been extend / adapted.
  2. Starting the application with toml config file
  3. Using the conversion function from toml to yaml
  4. Starting the application with yaml config file

Checklist:

  • [ ] My change requires a change to the documentation or CHANGELOG.
  • [x] I have updated the documentation/CHANGELOG accordingly.
  • [x] I have created a feature (non-master) branch for my PR.
  • [x] I have written tests for my code changes.

tuunit avatar May 04 '24 14:05 tuunit