-
Notifications
You must be signed in to change notification settings - Fork 4
Feat/env auth migration #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| .git | ||
| .gitignore | ||
|
|
||
| # Local secrets | ||
| .env | ||
| .env.local | ||
| .env.* | ||
| !.env.example | ||
| credentials.json | ||
| *-creds.json | ||
|
|
||
| # Build/test artifacts | ||
| bauer | ||
| bauer-api | ||
| bauer-output/ | ||
| dist/ | ||
| *.log | ||
| bauer-doc-suggestions.json | ||
| bauer-log.json | ||
|
|
||
| # Local tooling files | ||
| .vscode/ | ||
| .DS_Store |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Google service account credentials | ||
| GOOGLE_TYPE=service_account | ||
| GOOGLE_PROJECT_ID=your-project-id | ||
| GOOGLE_PRIVATE_KEY_ID=your-private-key-id | ||
| GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nREPLACE_ME\n-----END PRIVATE KEY-----\n" | ||
| GOOGLE_CLIENT_EMAIL=service-account@your-project-id.iam.gserviceaccount.com | ||
| GOOGLE_CLIENT_ID=your-client-id | ||
| GOOGLE_AUTH_URI=https://accounts.google.com/o/oauth2/auth | ||
| GOOGLE_TOKEN_URI=https://oauth2.googleapis.com/token | ||
| GOOGLE_AUTH_PROVIDER_X509_CERT_URL=https://www.googleapis.com/oauth2/v1/certs | ||
| GOOGLE_CLIENT_X509_CERT_URL=https://www.googleapis.com/robot/v1/metadata/x509/service-account%40your-project-id.iam.gserviceaccount.com | ||
| GOOGLE_UNIVERSE_DOMAIN=googleapis.com | ||
|
|
||
| # API basic auth shared secret (used as password) | ||
| API_SECRET=replace-with-a-long-random-secret |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| FROM golang:1.24-alpine AS builder | ||
|
|
||
| WORKDIR /src | ||
|
|
||
| RUN apk add --no-cache ca-certificates git | ||
|
|
||
| COPY go.mod go.sum ./ | ||
| RUN go mod download | ||
|
|
||
| COPY cmd ./cmd | ||
| COPY internal ./internal | ||
|
|
||
| RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/bauer-api ./cmd/app | ||
|
|
||
| FROM alpine:3.22 | ||
|
|
||
| RUN apk add --no-cache ca-certificates | ||
|
|
||
| WORKDIR /app | ||
| COPY --from=builder /out/bauer-api /app/bauer-api | ||
|
|
||
| EXPOSE 8090 | ||
|
|
||
| ENTRYPOINT ["/app/bauer-api"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "crypto/subtle" | ||
| "net/http" | ||
| ) | ||
|
|
||
| const apiUser = "bauer" | ||
|
|
||
| func APIBasicAuth(next http.Handler, secret string) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be easier to add middlewares to handlers that need it than using an if statement? |
||
| if r.URL.Path == "/api/v1/health" { | ||
| next.ServeHTTP(w, r) | ||
| return | ||
| } | ||
|
|
||
| username, password, ok := r.BasicAuth() | ||
| if !ok || !secureEquals(username, apiUser) || !secureEquals(password, secret) { | ||
| w.Header().Set("WWW-Authenticate", `Basic realm="bauer-api"`) | ||
| http.Error(w, "unauthorized", http.StatusUnauthorized) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for the same of consistency, we can use |
||
| return | ||
| } | ||
|
|
||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
|
|
||
| func secureEquals(a, b string) bool { | ||
| if len(a) != len(b) { | ||
| return false | ||
| } | ||
| return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add docker instruction to the README?