Skip to content

Commit

Permalink
feat: Add configuration file + modifiers
Browse files Browse the repository at this point in the history
Co-Authored-By: Sebastián Vargas <[email protected]>
  • Loading branch information
achetronic and sebastocorp committed Oct 9, 2024
1 parent b887b70 commit 5a33383
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 72 deletions.
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![YouTube Channel Subscribers](https://img.shields.io/youtube/channel/subscribers/UCeSb3yfsPNNVr13YsYNvCAw?label=achetronic&link=http%3A%2F%2Fyoutube.com%2Fachetronic)
![X (formerly Twitter) Follow](https://img.shields.io/twitter/follow/achetronic?style=flat&logo=twitter&link=https%3A%2F%2Ftwitter.com%2Fachetronic)

A tiny HTTP server to be used as external authentication service for Envoy
A tiny HTTP server to be used as external authentication service for Envoy

## Motivation

Expand All @@ -16,34 +16,26 @@ Life is hard, but beautiful
As almost every configuration parameter can be defined in environment vars, there are only few flags that can be defined.
They are described in the following table:

| Name | Description | Default | Example |
|:------------------|:-------------------------------|:-------------:|:-------------------------|
| `--log-level` | Verbosity level for logs | `info` | `--log-level info` |
| `--disable-trace` | Disable showing traces in logs | `info` | `--log-level info` |
| Name | Description | Default | Example |
|:------------------|:-----------------------------------------------------|:-----------------:|:-------------------------------|
| `--log-level` | Verbosity level for logs | `info` | `--log-level info` |
| `--disable-trace` | Disable showing traces in logs | `info` | `--log-level info` |
| `--config` | Path to the configuration file <br> [Config Example] | `doorkeeper.yaml` | `--doorkeeper doorkeeper.yaml` |


> Output is thrown always in JSON as it is more suitable for automations
```console
doorkeeper run \
--log-level=info
```
## Configuration

## Environment vars

| Name | Values | Description |
|:---------------------------------------|:----------------------------|:------------|
| `DOORKEEPER_AUTHORIZATION_PARAM_TYPE` | `header\|query` | |
| `DOORKEEPER_AUTHORIZATION_PARAM_NAME` | `*` | |
| `DOORKEEPER_AUTHORIZATION_TYPE` | `hmac\|{}` | |
| `DOORKEEPER_HMAC_TYPE` | `url\|{}` | |
| `DOORKEEPER_HMAC_ENCRYPTION_KEY` | `*` | |
| `DOORKEEPER_HMAC_ENCRYPTION_ALGORITHM` | `md5\|sha1\|sha256\|sha512` | |


A complete example of the config params can be found in [docs/samples/doorkeeper.yaml](./docs/samples/doorkeeper.yaml)

## How to deploy

This project can be deployed in Kubernetes, but also provides binary files
This project can be deployed in Kubernetes, but also provides binary files
and Docker images to make it easy to be deployed however wanted


Expand All @@ -69,10 +61,10 @@ helm upgrade --install --wait doorkeeper \

### Docker

Docker images can be found in GitHub's [packages](https://github.com/freepik-company/doorkeeper/pkgs/container/doorkeeper)
Docker images can be found in GitHub's [packages](https://github.com/freepik-company/doorkeeper/pkgs/container/doorkeeper)
related to this repository

> Do you need it in a different container registry? I think this is not needed, but if I'm wrong, please, let's discuss
> Do you need it in a different container registry? I think this is not needed, but if I'm wrong, please, let's discuss
> it in the best place for that: an issue
## How to contribute
Expand Down Expand Up @@ -105,3 +97,10 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.




[//]: #

[Config Example]: <./README.md#configuration>
54 changes: 54 additions & 0 deletions api/config_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import "regexp"

type DoorkeeperConfigT struct {
Auth AuthorizationConfigT `yaml:"authorization"`
Hmac HmacConfigT `yaml:"hmac"`
Modifiers []ModifierConfigT `yaml:"modifiers"`
}

type AuthorizationConfigT struct {
Type string `yaml:"type"`
Param AuthParamConfigT `yaml:"param"`
}

type AuthParamConfigT struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
}

type HmacConfigT struct {
Type string `yaml:"type"`
EncryptionKey string `yaml:"encryptionKey"`
EncryptionAlgorithm string `yaml:"encryptionAlgorithm"`
}

type ModifierConfigT struct {
Type string `yaml:"type"`
Path ModifierPathConfigT `yaml:"path"`
}

type ModifierPathConfigT struct {
Pattern string `yaml:"pattern"`
Replace string `yaml:"replace"`

// Carry stuff
CompiledRegex *regexp.Regexp
}
19 changes: 14 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"log"

"doorkeeper/internal/httpserver"
"doorkeeper/internal/config"
"doorkeeper/internal/globals"
"doorkeeper/internal/httpserver"
)

var (
httpPortFlag = flag.String("port", "8000", "HTTP server port")
logLevelFlag = flag.String("log-level", "info", "Verbosity level for logs")
httpPortFlag = flag.String("port", "8000", "HTTP server port")
logLevelFlag = flag.String("log-level", "info", "Verbosity level for logs")
disableTraceFlag = flag.Bool("disable-trace", true, "Disable showing traces in logs")
configFlag = flag.String("config", "doorkeeper.yaml", "Path to the config file")
)

func main() {
Expand All @@ -43,6 +45,14 @@ func main() {
log.Fatal(err)
}

// Parse and store the config
configContent, err := config.ReadFile(*configFlag)
if err != nil {
globals.Application.Logger.Fatalf(fmt.Sprintf("failed parsing configuration: %s", err.Error()))
}

globals.Application.Config = configContent

/////////////////////////////
// EXECUTION FLOW RELATED
/////////////////////////////
Expand All @@ -56,4 +66,3 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
}

20 changes: 20 additions & 0 deletions docs/samples/doorkeeper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
authorization:
type: hmac # hmac
param:
type: query # header|query
name: token

hmac:
type: url
encryptionKey: "${SECRETITO}"
encryptionAlgorithm: "sha256"

modifiers:
#- type: header # headers|host|path
# header:
# # TODO

- type: path
path:
pattern: ^(/[a-zA-Z0-9\-_]/)
replace: ""
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ toolchain go1.22.4
require (
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35 changes: 35 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"doorkeeper/api"
"os"

"gopkg.in/yaml.v3"
)

// Marshal TODO
func Marshal(config api.DoorkeeperConfigT) (bytes []byte, err error) {
bytes, err = yaml.Marshal(config)
return bytes, err
}

// Unmarshal TODO
func Unmarshal(bytes []byte) (config api.DoorkeeperConfigT, err error) {
err = yaml.Unmarshal(bytes, &config)
return config, err
}

// ReadFile TODO
func ReadFile(filepath string) (config api.DoorkeeperConfigT, err error) {
var fileBytes []byte
fileBytes, err = os.ReadFile(filepath)
if err != nil {
return config, err
}

fileBytes = []byte(os.ExpandEnv(string(fileBytes)))

config, err = Unmarshal(fileBytes)

return config, err
}
3 changes: 3 additions & 0 deletions internal/globals/globals.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package globals

import (
"doorkeeper/api"
"time"

"go.uber.org/zap"
Expand All @@ -15,6 +16,8 @@ var (
type ApplicationT struct {
Logger zap.SugaredLogger
LogLevel string

Config api.DoorkeeperConfigT
}

// SetLogger TODO
Expand Down
Loading

0 comments on commit 5a33383

Please sign in to comment.