test post registration webhook

This commit is contained in:
Dawid Wysokiński 2023-11-25 08:33:39 +01:00
parent 906d448177
commit c6e5d64850
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
6 changed files with 98 additions and 0 deletions

View File

@ -64,6 +64,14 @@ selfservice:
after:
password:
hooks:
- hook: web_hook
config:
url: http://webhook:9234/webhook/registration
method: POST
body: file:///etc/config/kratos/webhook.registration.jsonnet
response:
parse: false
ignore: true
- hook: session
- hook: show_verification_ui

View File

@ -0,0 +1,3 @@
function(ctx) {
ctx: ctx
}

View File

@ -28,6 +28,11 @@ services:
- KRATOS_PUBLIC_URL=http://kratos:4433/
- KRATOS_BROWSER_URL=http://127.0.0.1:4433/
restart: unless-stopped
webhook:
build:
context: ./webhook
dockerfile: Dockerfile
restart: unless-stopped
kratos:
depends_on:
- kratos-migrate

20
webhook/Dockerfile Normal file
View File

@ -0,0 +1,20 @@
FROM golang:1.21.4-alpine3.18 AS builder
WORKDIR /webhook
COPY go.mod ./
RUN go mod download && apk --no-cache add make
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -o webhook ./
######## Start a new stage from scratch #######
FROM alpine:3.18
RUN apk --no-cache add ca-certificates tzdata
COPY --from=builder /webhook/webhook /usr/bin/
EXPOSE 9234/tcp
CMD ["webhook"]

3
webhook/go.mod Normal file
View File

@ -0,0 +1,3 @@
module webhook
go 1.21

59
webhook/main.go Normal file
View File

@ -0,0 +1,59 @@
package main
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
ctx := context.Background()
server := http.Server{
Addr: ":9234",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
body, _ := io.ReadAll(r.Body)
fmt.Println(string(body))
w.WriteHeader(http.StatusOK)
}),
}
idleConnsClosed := make(chan struct{})
go func() {
waitForShutdownSignal(ctx)
slog.Info("received shutdown signal")
ctxTimeout, cancel := context.WithTimeout(ctx, 10)
defer cancel()
if err := server.Shutdown(ctxTimeout); err != nil {
slog.Warn("shutdown failed", slog.Any("error", err))
}
close(idleConnsClosed)
}()
slog.Info("Server is listening on the addr "+server.Addr, slog.String("addr", server.Addr))
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("ListenAndServe failed", slog.Any("err", err))
os.Exit(1)
}
<-idleConnsClosed
}
func waitForShutdownSignal(ctx context.Context) {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
}