Skip to content

Commit eda7efb

Browse files
authored
Merge pull request #31 from dotindustries/feature/bre-70-fix-unkey-loading-before-env-vars-are-read
fix: load unkey client on demand
2 parents 66c31f7 + 6a7c31e commit eda7efb

File tree

9 files changed

+145
-63
lines changed

9 files changed

+145
-63
lines changed

.github/workflows/azure-pr-deploy.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,29 @@ jobs:
5353
uses: actions/cache@v4
5454
with:
5555
path: go-build-cache
56-
key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }}
56+
key: ${{ runner.os }}-go-build-cache-${{ hashFiles('apps/api/go.sum') }}
57+
restore-keys: |
58+
${{ runner.os }}-go-build-cache-
59+
60+
- name: Go Module Cache for Docker
61+
uses: actions/cache@v4
62+
with:
63+
path: go-mod-cache
64+
key: ${{ runner.os }}-go-mod-cache-${{ hashFiles('apps/api/go.sum') }}
65+
restore-keys: |
66+
${{ runner.os }}-go-mod-cache-
5767
5868
- name: Inject go-build-cache
5969
uses: reproducible-containers/buildkit-cache-dance@4b2444fec0c0fb9dbf175a96c094720a692ef810 # v2.1.4
6070
with:
6171
cache-source: go-build-cache
72+
cache-target: /root/.cache/go-build
73+
74+
- name: Inject go-mod-cache
75+
uses: reproducible-containers/buildkit-cache-dance@4b2444fec0c0fb9dbf175a96c094720a692ef810 # v2.1.4
76+
with:
77+
cache-source: go-mod-cache
78+
cache-target: /go/pkg/mod
6279

6380
- name: Build and push API image
6481
uses: docker/build-push-action@v6
@@ -67,9 +84,13 @@ jobs:
6784
push: true
6885
file: ./apps/api/Dockerfile
6986
tags: ${{ env.AZURE_CONTAINER_REGISTRY }}/brease-api:pr-${{ github.event.number }}
70-
platforms: linux/amd64,linux/arm64
71-
cache-from: type=gha
72-
cache-to: type=gha,mode=max
87+
platforms: linux/amd64
88+
cache-from: |
89+
type=gha,scope=pr-${{ github.event.number }}
90+
type=gha,scope=main
91+
cache-to: type=gha,mode=max,scope=pr-${{ github.event.number }}
92+
build-args: |
93+
GO_VERSION=1.24
7394
7495
- name: Create resource group
7596
run: |

.github/workflows/release.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@ jobs:
2323
- name: Install pnpm
2424
uses: pnpm/action-setup@v4
2525

26+
- name: Get pnpm store directory
27+
shell: bash
28+
run: |
29+
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
30+
31+
- name: Setup pnpm cache
32+
uses: actions/cache@v4
33+
with:
34+
path: ${{ env.STORE_PATH }}
35+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
36+
restore-keys: |
37+
${{ runner.os }}-pnpm-store-
38+
2639
- name: Install Dependencies
27-
run: pnpm i
40+
run: pnpm i --frozen-lockfile
2841

2942
- name: Create Release Pull Request or Publish to npm
3043
id: changesets

README.md

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ brease is an open-source monorepo that provides a robust and extensible business
1010
## Features
1111

1212
- **brease Server**: The `api` folder contains a powerful and efficient backend server implemented in Go. It serves as the backbone for the entire business rules engine, offering high performance and scalability.
13-
1413
- **@brease/core**: This TypeScript library, located in the `packages/core` directory, provides framework-agnostic basic functionality. It includes essential utilities and helpers to support the implementation of business rules within your application.
15-
1614
- **@brease/react**: The `packages/react` directory contains the `@brease/react` TypeScript library, which offers a React provider and a collection of simple React hooks. These components enable seamless integration of the brease business rules engine with your React applications.
17-
1815
- **@brease/vue**: Located in the `packages/vue` directory, the `@brease/vue` package provides Vue 3 composables for easy integration of the brease business rules engine into your Vue applications. It simplifies the process of incorporating dynamic business rules and logic into your Vue components.
1916

2017
## Getting Started
@@ -23,47 +20,51 @@ To use brease in your project, follow these steps:
2320

2421
1. Clone the repository:
2522

26-
```bash
27-
git clone https://github.com/dotindustries/brease.git
28-
```
23+
```bash
24+
git clone https://github.com/dotindustries/brease.git
25+
```
2926

3027
2. Install dependencies:
3128

32-
```bash
33-
cd brease
34-
pnpm install
35-
```
29+
```lua
30+
print("asdf")
31+
```
32+
33+
```bash
34+
cd brease
35+
pnpm install
36+
```
3637

3738
3. Build the Go API backend server:
3839

39-
```bash
40-
cd apps/api
41-
go build
42-
```
40+
```bash
41+
cd apps/api
42+
go build
43+
```
4344

4445
4. Start the API server:
4546

46-
```bash
47-
./api
48-
```
47+
```bash
48+
./api
49+
```
4950

5051
5. Install the desired TypeScript libraries (e.g., @brease/core, @brease/react, or @brease/vue) in your project:
5152

52-
```bash
53-
npm install @brease/core
54-
```
53+
```bash
54+
npm install @brease/core
55+
```
5556

56-
or
57+
or
5758

58-
```bash
59-
npm install @brease/react
60-
```
59+
```bash
60+
npm install @brease/react
61+
```
6162

62-
or
63+
or
6364

64-
```bash
65-
npm install @brease/vue
66-
```
65+
```bash
66+
npm install @brease/vue
67+
```
6768

6869
For more detailed instructions, please refer to the individual package directories.
6970

@@ -100,9 +101,7 @@ We are excited to announce that we are actively developing a cloud-hosted versio
100101
### Key Features
101102

102103
- **Scalability**: Seamlessly scale your business rules engine based on your application's needs. Handle large volumes of rules and requests with ease.
103-
104104
- **Reliability**: Benefit from a robust infrastructure that ensures high availability and minimal downtime for your business rules engine.
105-
106105
- **Ease of Use**: Enjoy a user-friendly interface and simplified management process, allowing you to focus on building and managing your rules without worrying about infrastructure or updates.
107106

108107
Stay tuned for updates as we finalize the development and launch of the cloud-hosted version. We are committed to delivering a powerful and efficient solution for businesses of all sizes.

apps/api/Dockerfile

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
1-
ARG GO_VERSION=1
2-
FROM golang:${GO_VERSION}-bookworm as builder
3-
# Update package lists and install ca-certificates
1+
ARG GO_VERSION=1.24
2+
FROM golang:${GO_VERSION}-bookworm AS builder
43

54
WORKDIR /usr/src/app
6-
RUN go env -w GOMODCACHE=/root/.cache/go-build
5+
6+
# Copy dependency files first for better layer caching
77
COPY go.mod go.sum ./
8-
RUN --mount=type=cache,target=/root/.cache/go-build go mod download && go mod verify
8+
9+
# Download dependencies with proper cache mounts
10+
# Separate cache directories for modules and build cache
11+
RUN --mount=type=cache,target=/go/pkg/mod \
12+
--mount=type=cache,target=/root/.cache/go-build \
13+
go mod download && go mod verify
14+
15+
# Copy source code
916
COPY . .
10-
RUN --mount=type=cache,target=/root/.cache/go-build go build -v -o /brease .
1117

12-
FROM debian:bookworm
13-
# Update package lists and install ca-certificates and Infisical CLI
14-
RUN apt-get clean && \
15-
rm -rf /var/lib/apt/lists/* && \
16-
apt-get update --fix-missing && apt-get install -y \
18+
# Build with cache mounts and optimizations
19+
RUN --mount=type=cache,target=/go/pkg/mod \
20+
--mount=type=cache,target=/root/.cache/go-build \
21+
CGO_ENABLED=0 GOOS=linux go build -v -ldflags="-w -s" -o /brease .
22+
23+
# Runtime stage - using distroless for minimal size and better security
24+
FROM gcr.io/distroless/static-debian12:nonroot AS runtime-distroless
25+
26+
COPY --from=builder /brease /usr/local/bin/brease
27+
28+
# Distroless doesn't support shell scripts, so we need a different approach
29+
# If start.sh is essential, use the debian variant below instead
30+
CMD ["/usr/local/bin/brease"]
31+
32+
# Alternative runtime with Infisical support (use this if you need start.sh)
33+
FROM debian:bookworm-slim AS runtime-debian
34+
35+
# Install dependencies in a single layer with cleanup
36+
RUN apt-get update && apt-get install -y --no-install-recommends \
1737
ca-certificates \
1838
curl \
1939
bash \
2040
&& curl -1sLf 'https://artifacts-cli.infisical.com/setup.deb.sh' | bash \
21-
&& apt-get update && apt-get install -y infisical \
41+
&& apt-get update && apt-get install -y --no-install-recommends infisical \
42+
&& apt-get clean \
2243
&& rm -rf /var/lib/apt/lists/*
2344

24-
COPY --from=builder /brease /usr/local/bin/
45+
# Create non-root user
46+
RUN useradd -r -u 65532 -g root nonroot
47+
48+
COPY --from=builder /brease /usr/local/bin/brease
2549
COPY start.sh /usr/local/bin/start.sh
26-
RUN chmod +x /usr/local/bin/start.sh
50+
RUN chmod +x /usr/local/bin/start.sh /usr/local/bin/brease
51+
52+
USER nonroot
2753

2854
CMD ["/usr/local/bin/start.sh"]
55+
56+
# Final stage selector - uncomment the one you want to use
57+
FROM runtime-debian AS final

apps/api/auth/middleware.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
"connectrpc.com/connect"
1313
"github.com/gin-gonic/gin"
14+
"github.com/golang-jwt/jwt/v5"
1415
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1516
components2 "github.com/unkeyed/sdks/api/go/v2/models/components"
1617
"github.com/unkeyed/unkey/sdks/golang/models/sdkerrors"
1718
"go.dot.industries/brease/trace"
1819
"go.dot.industries/brease/worker"
1920
"google.golang.org/grpc/metadata"
2021

21-
"github.com/golang-jwt/jwt/v5"
2222
errors2 "github.com/juju/errors"
2323
"go.dot.industries/brease/env"
2424
"go.uber.org/zap"
@@ -306,7 +306,7 @@ func validateUnkey(ctx context.Context, args interface{}) (interface{}, error) {
306306
},
307307
}, nil
308308
}
309-
resp, err := unkeyClient.Keys.VerifyKey(ctx, components2.V2KeysVerifyKeyRequestBody{
309+
resp, err := Unkey().Keys.VerifyKey(ctx, components2.V2KeysVerifyKeyRequestBody{
310310
Key: key,
311311
})
312312
if err != nil {
@@ -355,6 +355,13 @@ func validateUnkey(ctx context.Context, args interface{}) (interface{}, error) {
355355
Error: errors2.NewUnauthorized(err, "internal error"),
356356
},
357357
}, nil
358+
default:
359+
return validateAuthTokenResult{
360+
error: &validationErr{
361+
Status: http.StatusInternalServerError,
362+
Error: errors2.NewNotValid(err, "Unknown unkey error"),
363+
},
364+
}, nil
358365
}
359366
}
360367

@@ -440,7 +447,7 @@ func validateRootAPIKey(ctx context.Context, args interface{}) (interface{}, err
440447
a := args.(validateAuthTokenArgs)
441448

442449
key := a.token
443-
if a.rootAPIKey == "" || strings.HasPrefix(key, jwtAuthPrefix) || strings.HasPrefix(key, bearerAuthPrefix) {
450+
if a.rootAPIKey == "" || strings.HasPrefix(key, jwtAuthPrefix) {
444451
// not configured to authenticate, but no errors
445452
return validateAuthTokenResult{}, nil
446453
}

apps/api/auth/unkey.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package auth
22

33
import (
4+
"fmt"
5+
46
unkey "github.com/unkeyed/sdks/api/go/v2"
57
"go.dot.industries/brease/env"
68
)
79

8-
var unkeyClient = unkey.New(
9-
unkey.WithSecurity(env.Getenv("UNKEY_TOKEN", "")),
10-
)
10+
var unkeyClient *unkey.Unkey
11+
12+
func Unkey() *unkey.Unkey {
13+
if unkeyClient == nil {
14+
unkeyToken := env.Getenv("UNKEY_TOKEN", "")
15+
if unkeyToken == "" {
16+
panic(fmt.Errorf("UNKEY_TOKEN is not set"))
17+
}
18+
unkeyClient = unkey.New(
19+
unkey.WithSecurity(unkeyToken),
20+
)
21+
}
22+
return unkeyClient
23+
}

apps/api/go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
module go.dot.industries/brease
22

3-
go 1.23.1
4-
5-
toolchain go1.23.4
3+
go 1.24.0
64

75
require (
86
buf.build/gen/go/dot/brease/connectrpc/go v1.18.1-20250813170839-3ed1f1797b5e.1
97
buf.build/gen/go/dot/brease/grpc/go v1.5.1-20250813170839-3ed1f1797b5e.2
108
buf.build/gen/go/dot/brease/protocolbuffers/go v1.36.7-20250813170839-3ed1f1797b5e.1
11-
connectrpc.com/connect v1.18.1
9+
connectrpc.com/connect v1.19.1
1210
connectrpc.com/grpchealth v1.4.0
1311
connectrpc.com/grpcreflect v1.3.0
1412
connectrpc.com/vanguard v0.3.0
@@ -61,7 +59,7 @@ require (
6159
go.uber.org/zap v1.27.0
6260
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792
6361
google.golang.org/grpc v1.73.0
64-
google.golang.org/protobuf v1.36.7
62+
google.golang.org/protobuf v1.36.9
6563
gotest.tools/v3 v3.5.2
6664
)
6765

apps/api/go.sum

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.work

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
go 1.23.1
2-
3-
toolchain go1.23.4
1+
go 1.24.0
42

53
use ./apps/api/

0 commit comments

Comments
 (0)