diff --git a/cmd/filter-proxy/main.go b/cmd/filter-proxy/main.go index 05b8fcc..d3c645c 100644 --- a/cmd/filter-proxy/main.go +++ b/cmd/filter-proxy/main.go @@ -18,7 +18,6 @@ import ( "github.com/itchyny/gojq" "github.com/delta10/filter-proxy/internal/config" - "github.com/delta10/filter-proxy/internal/logs" "github.com/delta10/filter-proxy/internal/route" "github.com/delta10/filter-proxy/internal/utils" ) @@ -54,7 +53,7 @@ func main() { utils.DelHopHeaders(r.Header) - authorizationStatusCode, authorizationResponse := authorizeRequestWithService(config, path, r) + authorizationStatusCode, _ := authorizeRequestWithService(config, path, r) if authorizationStatusCode != http.StatusOK { writeError(w, authorizationStatusCode, "unauthorized request") return @@ -182,36 +181,6 @@ func main() { return } - if path.LogBackend != "" { - logBackendName, ok := config.LogBackends[path.LogBackend] - if !ok { - writeError(w, http.StatusInternalServerError, "could not find log backend: "+path.LogBackend) - return - } - - logBackend := logs.NewLogBackend(logBackendName) - - labels := map[string]string{ - "system": "filter-proxy", - "backend": path.Backend.Slug, - } - - logLine := map[string]string{ - "method": r.Method, - "path": r.URL.String(), - "status": proxyResp.Status, - "user_id": fmt.Sprint(authorizationResponse.User.Id), - "user_username": authorizationResponse.User.Username, - "ip": utils.ReadUserIP(r), - } - - err := logBackend.WriteLog(labels, logLine) - if err != nil { - writeError(w, http.StatusInternalServerError, "could not write log to backend") - return - } - } - defer proxyResp.Body.Close() if path.ResponseRewrite != "" && proxyResp.StatusCode == http.StatusOK { diff --git a/config.yaml b/config.yaml index 5a66fc2..ff3e2fd 100644 --- a/config.yaml +++ b/config.yaml @@ -8,7 +8,6 @@ authorizationServiceUrl: http://localhost:8000/atlas/api/v1/authorize paths: - path: /api/ows - logBackend: loki backend: slug: geoserver path: /ows @@ -40,7 +39,6 @@ paths: backend: slug: haal-centraal-brk path: /kadastraalonroerendezaken/{kadastraalOnroerendeZaakIdentificatie:[0-9]+} - logBackend: loki responseRewrite: | { aardCultuurBebouwd: .aardCultuurBebouwd, @@ -60,7 +58,6 @@ paths: backend: slug: haal-centraal-brk path: /kadastraalonroerendezaken/{kadastraalOnroerendeZaakIdentificatie:[0-9]+}/zakelijkgerechtigden - logBackend: loki responseRewrite: | { "_embedded": { @@ -102,7 +99,6 @@ paths: backend: slug: haal-centraal-brk path: /publiekrechtelijkebeperkingen - logBackend: loki responseRewrite: | { "_embedded": { @@ -153,7 +149,3 @@ backends: rootCertificates: .vscode/pki-o-g1.crt header: apikey: ${KVK_API_KEY} - -logBackends: - loki: - baseUrl: http://localhost:3100/loki diff --git a/internal/config/config.go b/internal/config/config.go index fa74963..f1d9009 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -31,26 +31,20 @@ type Path struct { Slug string `yaml:"slug"` Path string `yaml:"path"` } `yaml:"backend"` - LogBackend string `yaml:"logBackend"` RequestRewrite string `yaml:"requestRewrite"` ResponseRewrite string `yaml:"responseRewrite"` } -type LogBackend struct { - BaseURL string `yaml:"baseUrl"` -} - type Config struct { ListenAddress string `yaml:"listenAddress"` ListenTLS struct { Certificate string `yaml:"certificate"` Key string `yaml:"key"` } `yaml:"listenTls"` - AuthorizationServiceURL string `yaml:"authorizationServiceUrl"` - JwksURL string `yaml:"jwksUrl"` - Paths []Path `yaml:"paths"` - Backends map[string]Backend `yaml:"backends"` - LogBackends map[string]LogBackend `yaml:"logBackends"` + AuthorizationServiceURL string `yaml:"authorizationServiceUrl"` + JwksURL string `yaml:"jwksUrl"` + Paths []Path `yaml:"paths"` + Backends map[string]Backend `yaml:"backends"` } // NewConfig returns a new decoded Config struct diff --git a/internal/logs/logs.go b/internal/logs/logs.go deleted file mode 100644 index 06a56c8..0000000 --- a/internal/logs/logs.go +++ /dev/null @@ -1,86 +0,0 @@ -package logs - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "net/http" - "net/url" - "time" - - "github.com/delta10/filter-proxy/internal/config" -) - -func NewLogBackend(backend config.LogBackend) *LogBackend { - return &LogBackend{ - Config: backend, - } -} - -type LogBackend struct { - Config config.LogBackend -} - -type Stream struct { - Stream map[string]string `json:"stream"` - Values [][]any `json:"values"` -} - -type Body struct { - Streams []Stream `json:"streams"` -} - -func (l *LogBackend) WriteLog(labels map[string]string, line map[string]string) error { - parsedUrl, err := url.Parse(l.Config.BaseURL) - if err != nil { - return err - } - - parsedUrl = parsedUrl.JoinPath("/api/v1/push") - - marshalledLine, err := json.Marshal(line) - if err != nil { - return err - } - - body := Body{ - Streams: []Stream{ - { - Stream: labels, - Values: [][]any{ - { - fmt.Sprint(time.Now().UnixNano()), - string(marshalledLine), - }, - }, - }, - }, - } - - marshalled, err := json.Marshal(body) - if err != nil { - return err - } - - logRequest, err := http.NewRequest("POST", parsedUrl.String(), bytes.NewReader(marshalled)) - if err != nil { - return err - } - - logRequest.Header.Add("Content-Type", "application/json") - - client := &http.Client{} - logResponse, err := client.Do(logRequest) - if err != nil { - return err - } - - defer logResponse.Body.Close() - - if logResponse.StatusCode != http.StatusNoContent { - return errors.New("could not create log entry") - } - - return nil -}