Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logg milliseconds, add method and path to output #3

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

The Guard is a HTTP Server that responds to requests on http://localhost:8000/auth and authenticates the header `Authorization: Bearer JWT` against the configured ISSUER, AUDIENCE and authorizes the request agains a comma separated list of subjects.


## How to use

This application is designed to use with Forward Auth, specifically for ingress-nginx, enable with this annotation:
```yaml
metadata:
annotations:
nginx.ingress.kubernetes.io/auth-url: "http://oauth-guard.monitor.svc.cluster.local:8000/auth"
```

## Configuration

- `ISSUER` - Required. A issuer to verify JWT against. Must support the `${ISSUER}.well-known/openid-configuration` endpoint.
Expand Down
10 changes: 6 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ func AuthHandler(subjects []string, verifier Verifier) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t := time.Now()

event := log.Info().Str("method", r.Method).Str("path", r.URL.Path)

auth := r.Header.Get("Authorization")
jwt, err := parseAuthHeader(auth)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("Forbidden"))
log.Info().Err(err).Dur("latency", time.Since(t)).Int("status", http.StatusUnauthorized).Msg("Unauthorized")
event.Err(err).Dur("elappsed_ms", time.Since(t)).Int("status", http.StatusUnauthorized).Msg("Unauthorized")
return
}

Expand All @@ -39,7 +41,7 @@ func AuthHandler(subjects []string, verifier Verifier) http.Handler {
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("Forbidden"))
log.Info().Err(err).Dur("latency", time.Since(t)).Int("status", http.StatusUnauthorized).Msg("Unauthorized")
event.Err(err).Dur("elappsed_ms", time.Since(t)).Int("status", http.StatusUnauthorized).Msg("Unauthorized")
return
}

Expand All @@ -48,13 +50,13 @@ func AuthHandler(subjects []string, verifier Verifier) http.Handler {
if !found {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte("Forbidden"))
log.Info().Err(err).Dur("latency", time.Since(t)).Int("status", http.StatusForbidden).Str("sub", subject).Msg("Forbidden")
event.Err(err).Dur("elappsed_ms", time.Since(t)).Int("status", http.StatusForbidden).Str("sub", subject).Msg("Forbidden")
return
}

w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
log.Info().Dur("latency", time.Since(t)).Int("status", http.StatusOK).Str("sub", subject).Msg("Authorized")
event.Dur("elappsed_ms", time.Since(t)).Int("status", http.StatusOK).Str("sub", subject).Msg("Authorized")
})
}

Expand Down
50 changes: 50 additions & 0 deletions deploymet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: oauth-guard
namespace: monitor
spec:
selector:
matchLabels:
app: radix-oauth-guard
template:
metadata:
labels:
app: radix-oauth-guard
spec:
containers:
- name: guard
image: ghcr.io/equinor/radix-oauth-guard:v0.2.1
imagePullPolicy: Always
ports:
- containerPort: 8000
name: http
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
capabilities:
drop:
- ALL
env:
- name: LOG_PRETTY
value: "True"
- name: LOG_LEVEL
value: debug
- name: ISSUER
value: https://northeurope.oic.prod-aks.azure.com/3aa4a235-b6e2-48d5-9195-7fcf05b459b0/a2d93ba1-cbde-4408-8979-c100cce7b448/
- name: AUDIENCE
value: extmonprom
- name: SUBJECTS
value: system:serviceaccount:monitor:prometheus-operator-prometheus
---
apiVersion: v1
kind: Service
metadata:
name: oauth-guard
namespace: monitor
spec:
selector:
app: radix-oauth-guard
ports:
- name: http
port: 8000
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func initLogger(opts Options) {
logWriter = &zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}
}

zerolog.DurationFieldUnit = time.Millisecond
logger := zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger()

log.Logger = logger
Expand Down
Loading