Skip to content
Open
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
13 changes: 12 additions & 1 deletion example-wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ crons = ["*/5 * * * *"] # * * * * * = run every 5 minutes
API_HEADER = "x-kelpie-key"
MAX_FAILURES_PER_WORKER = "5"
ADMIN_ID = "00000000-0000-0000-0000-000000000000"

TOKEN_CACHE_TTL = "60"
JWKS_CACHE_TTL = "600"
MAX_SALAD_API_RETRIES = "3"
AUTH_URL=""
JWKS_URL=""
MAX_CONCURRENT_SCALING_RULES="4"
MAX_FAILURES_PER_WORKER="3"
SALAD_USERNAME=""

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
Expand All @@ -32,6 +39,10 @@ id = "330b9fef-d0b3-4061-962c-9f1a038f488c"
binding = "salad_cache"
id = "fea342ad-1262-4264-8fe9-57a9984204d7"

[[kv_namespaces]]
binding = "token_cache"
id = "fea342ad-1262-4264-8fe9-57a9984204d7"

[[d1_databases]]
binding = "DB"
database_name = "kelpie"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kelpie-api",
"version": "0.7.0",
"version": "0.7.1",
Comment thread
mgorkii-nlplogix marked this conversation as resolved.
"private": true,
"scripts": {
"deploy": "wrangler deploy",
Expand Down
15 changes: 9 additions & 6 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ export async function validateAuth(req: AuthedRequest, env: Env) {
* If we don't have a cached user ID, we need to validate the API Key.
*/
try {
const payload = await validateSaladApiKey(env, saladApiKey, saladOrg || '');
req.saladOrg = payload.organization_name;
payload.organization_id;
try {
await listContainerGroups(env, req.saladOrg, saladProject, true);
const payload = await validateSaladApiKey(env, saladApiKey, saladOrg || '');
req.saladOrg = payload.organization_name;
} catch {
req.saladOrg = saladOrg;
}
try {
await listContainerGroups(env, req.saladOrg, saladProject, true, saladApiKey ?? undefined);
req.saladProject = saladProject;
Comment thread
mgorkii-nlplogix marked this conversation as resolved.
/**
* If everything is valid, we check to see if we have a user provisioned for this org already.
Expand Down Expand Up @@ -102,7 +105,7 @@ export async function validateAuth(req: AuthedRequest, env: Env) {
* If we check to make sure the project exists and is valid.
*/
try {
await listContainerGroups(env, req.saladOrg, saladProject, true);
await listContainerGroups(env, req.saladOrg, saladProject, true, saladApiKey ?? undefined);
req.saladProject = saladProject;

/**
Expand Down Expand Up @@ -182,7 +185,7 @@ export async function validateSaladApiKey(env: Env, apiKey: string, orgName: str
}

if (!body.is_entitled) {
throw new Error('This organization is not entitled to use the Kelpie API');
console.log(`Organization ${orgName} is not entitled to use the Kelpie API`);
Comment thread
mgorkii-nlplogix marked this conversation as resolved.
}

await env.token_cache.put(cacheKey, JSON.stringify(body), {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/salad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function fetchWithRetries(url: string, options: RequestInit, retrie
throw new Error(`Did not receive response from API after ${retries} attempts`);
}

export async function listContainerGroups(env: Env, orgName: string, projectName: string, noCache = false): Promise<SaladContainerGroup[]> {
export async function listContainerGroups(env: Env, orgName: string, projectName: string, noCache = false, apiKey?: string): Promise<SaladContainerGroup[]> {
// Check to see if we cached the value already
if (!noCache) {
const cachedValue = await env.salad_cache.get(`${orgName}/${projectName}`);
Expand All @@ -35,7 +35,7 @@ export async function listContainerGroups(env: Env, orgName: string, projectName

// Fetch the container groups from Salad
const url = `${saladBaseUrl}/organizations/${orgName}/projects/${projectName}/containers`;
const response = await fetchWithRetries(url, { headers: { 'Salad-Api-Key': env.SALAD_API_KEY } }, maxRetries);
const response = await fetchWithRetries(url, { headers: { 'Salad-Api-Key': apiKey || env.SALAD_API_KEY } }, maxRetries);
if (!response.ok) {
console.log(`Failed to fetch container groups in project ${orgName}/${projectName}: ${response.status}`);
console.log(await response.text());
Expand Down