-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into unkey-rails-template
- Loading branch information
Showing
158 changed files
with
4,007 additions
and
1,263 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: Campsite PR Alerts | ||
|
||
on: | ||
pull_request: | ||
types: [opened, closed, merged, ready_for_review, reopened] | ||
|
||
jobs: | ||
post_to_campsite: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '20' | ||
|
||
- name: Post to Campsite | ||
id: post_to_campsite | ||
run: | | ||
if [[ "${{ github.event.pull_request.draft }}" == "true" ]]; then | ||
STATUS_EMOJI="⚪" | ||
elif [[ "${{ github.event.action }}" == "opened" || "${{ github.event.action }}" == "reopened" || "${{ github.event.action }}" == "ready_for_review" ]]; then | ||
STATUS_EMOJI="🟢" | ||
elif [[ "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true" ]]; then | ||
STATUS_EMOJI="🟣" | ||
elif [[ "${{ github.event.action }}" == "closed" ]]; then | ||
STATUS_EMOJI="🔴" | ||
fi | ||
ACTION=${{ github.event.action }} | ||
if [[ "${ACTION}" == "ready_for_review" ]]; then | ||
ACTION="ready for review" | ||
elif [[ "${ACTION}" == "reopened" ]]; then | ||
ACTION="reopened" | ||
elif [[ "${ACTION}" == "closed" && "${{ github.event.pull_request.merged }}" == "true" ]]; then | ||
ACTION="merged" | ||
fi | ||
ESCAPED_TITLE=$(echo "${{ github.event.pull_request.title }}" | jq -Rr @json) | ||
CONTENT="${STATUS_EMOJI} Pull request ${ACTION} by ${{ github.event.pull_request.user.login }}: [#${{ github.event.pull_request.number }} ${ESCAPED_TITLE}](${{ github.event.pull_request.html_url }})" | ||
echo "content=${CONTENT}" >> $GITHUB_OUTPUT | ||
- name: Create Campsite message | ||
uses: campsite/campsite-github-action@v1 | ||
with: | ||
api_key: ${{ secrets.CAMPSITE_API_KEY }} | ||
action_type: create_message | ||
thread_id: ${{ secrets.CAMPSITE_PR_ALERTS_THREAD_ID }} | ||
content: ${{ steps.post_to_campsite.outputs.content }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,7 @@ dist | |
.dev.vars | ||
.wrangler | ||
.vitest | ||
.react-email | ||
.react-email | ||
|
||
.secrets.json | ||
secrets.ts |
146 changes: 146 additions & 0 deletions
146
apps/agent/integration/identities/ratelimits_with_cost_load_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package identities_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
unkey "github.com/unkeyed/unkey-go" | ||
"github.com/unkeyed/unkey-go/models/components" | ||
"github.com/unkeyed/unkey-go/models/operations" | ||
attack "github.com/unkeyed/unkey/apps/agent/pkg/testutil" | ||
"github.com/unkeyed/unkey/apps/agent/pkg/uid" | ||
"github.com/unkeyed/unkey/apps/agent/pkg/util" | ||
) | ||
|
||
func TestIdentityRatelimitsWithCost0Accuracy(t *testing.T) { | ||
// Step 1 -------------------------------------------------------------------- | ||
// Setup the sdk, create an API | ||
// --------------------------------------------------------------------------- | ||
|
||
ctx := context.Background() | ||
rootKey := os.Getenv("INTEGRATION_TEST_ROOT_KEY") | ||
require.NotEmpty(t, rootKey, "INTEGRATION_TEST_ROOT_KEY must be set") | ||
baseURL := os.Getenv("UNKEY_BASE_URL") | ||
require.NotEmpty(t, baseURL, "UNKEY_BASE_URL must be set") | ||
|
||
options := []unkey.SDKOption{ | ||
unkey.WithSecurity(rootKey), | ||
} | ||
|
||
if baseURL != "" { | ||
options = append(options, unkey.WithServerURL(baseURL)) | ||
} | ||
sdk := unkey.New(options...) | ||
|
||
for _, tc := range []struct { | ||
rate attack.Rate | ||
testDuration time.Duration | ||
}{ | ||
{ | ||
rate: attack.Rate{Freq: 100, Per: time.Second}, | ||
testDuration: 1 * time.Minute, | ||
}, | ||
{ | ||
rate: attack.Rate{Freq: 100, Per: time.Second}, | ||
testDuration: 5 * time.Minute, | ||
}, | ||
{ | ||
rate: attack.Rate{Freq: 100, Per: time.Second}, | ||
testDuration: 30 * time.Minute, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("[%s] over %s", tc.rate.String(), tc.testDuration), func(t *testing.T) { | ||
api, err := sdk.Apis.CreateAPI(ctx, operations.CreateAPIRequestBody{ | ||
Name: uid.New("testapi"), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Step 2 -------------------------------------------------------------------- | ||
// Create the identity with ratelimits | ||
// --------------------------------------------------------------------------- | ||
|
||
ratelimit := operations.Ratelimits{ | ||
Name: "ratelimit-a", | ||
Limit: 600, | ||
Duration: time.Minute.Milliseconds(), | ||
} | ||
|
||
externalID := uuid.NewString() | ||
_, err = sdk.Identities.CreateIdentity(ctx, operations.CreateIdentityRequestBody{ | ||
ExternalID: externalID, | ||
Ratelimits: []operations.Ratelimits{ | ||
ratelimit, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Step 3 -------------------------------------------------------------------- | ||
// Create key for this identity | ||
// --------------------------------------------------------------------------- | ||
|
||
key, err := sdk.Keys.CreateKey(ctx, operations.CreateKeyRequestBody{ | ||
APIID: api.Object.APIID, | ||
ExternalID: util.Pointer(externalID), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Step 5 -------------------------------------------------------------------- | ||
// Test ratelimits | ||
// --------------------------------------------------------------------------- | ||
|
||
total := 0 | ||
passed := 0 | ||
withCost := 0 | ||
errors := 0 | ||
|
||
results := attack.Attack(t, tc.rate, tc.testDuration, func() bool { | ||
|
||
cost := int64(0) | ||
if rand.Intn(100) == 0 { | ||
withCost++ | ||
cost = 1 | ||
} | ||
|
||
res, err := sdk.Keys.VerifyKey(context.Background(), components.V1KeysVerifyKeyRequest{ | ||
APIID: unkey.String(api.Object.APIID), | ||
Key: key.Object.Key, | ||
Ratelimits: []components.Ratelimits{ | ||
{Name: ratelimit.Name, | ||
Cost: util.Pointer(cost), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
errors++ | ||
return false | ||
} | ||
|
||
return res.V1KeysVerifyKeyResponse.Valid | ||
|
||
}) | ||
|
||
for valid := range results { | ||
total++ | ||
if valid { | ||
passed++ | ||
} | ||
|
||
} | ||
|
||
// Step 6 -------------------------------------------------------------------- | ||
// Assert ratelimits worked | ||
// --------------------------------------------------------------------------- | ||
|
||
t.Logf("Total: %d, Passed: %d, withCost=1: %d", total, passed, withCost) | ||
|
||
// check requests::api is not exceeded | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# api | ||
|
||
## 0.1.0 | ||
|
||
### Minor Changes | ||
|
||
- 09d36ad: add /v1/keys.whoami route | ||
|
||
## 0.0.14 | ||
|
||
### Patch Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.