Skip to content

Commit

Permalink
feat: track logins on supabase add infra related details to the balle…
Browse files Browse the repository at this point in the history
…rine
  • Loading branch information
MatanYadaev authored and pratapalakshmi committed Nov 4, 2024
1 parent 901a591 commit d505994
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 4 deletions.
4 changes: 4 additions & 0 deletions deploy/.env
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ WEBSOCKET_SVC_PORT=3500
KYB_APP_PORT=5201
BACKOFFICE_PORT=5137
HEADLESS_SVC_PORT=5173
DOMAIN_NAME=""
TELEMETRY_ENABLED=true
TELEMETRY_SUPABASE_URL=""
TELEMETRY_SUPABASE_API_KEY=""
3 changes: 3 additions & 0 deletions deploy/docker-compose-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ services:
HASHING_KEY_SECRET: ${HASHING_KEY_SECRET}
HASHING_KEY_SECRET_BASE64: ${HASHING_KEY_SECRET_BASE64}
NOTION_API_KEY: ${NOTION_API_KEY}
TELEMETRY_ENABLED: ${TELEMETRY_ENABLED}
TELEMETRY_SUPABASE_URL: ${TELEMETRY_SUPABASE_URL}
TELEMETRY_SUPABASE_API_KEY: ${TELEMETRY_SUPABASE_API_KEY}
depends_on:
ballerine-postgres:
condition: service_healthy
Expand Down
71 changes: 70 additions & 1 deletion pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions services/workflows-service/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ WEB_UI_SDK_URL=http://localhost:5202
#HASHING_KEY_SECRET="$2b$10$FovZTB91/QQ4Yu28nvL8e."
HASHING_KEY_SECRET_BASE64=JDJiJDEwJDNFeWtwWEs4QkdiczlRaWFwLkM4Vk8=
NOTION_API_KEY=secret
TELEMETRY_ENABLED=true
TELEMETRY_SUPABASE_URL=""
TELEMETRY_SUPABASE_API_KEY=""
3 changes: 2 additions & 1 deletion services/workflows-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ COPY --from=dev /app/scripts ./scripts
COPY --from=dev /app/src ./src
COPY --from=dev /app/tsconfig.build.json ./tsconfig.build.json
COPY --from=dev /app/tsconfig.json ./tsconfig.json
COPY --from=dev /app/entrypoint.sh ./entrypoint.sh

EXPOSE 3000

CMD [ "dumb-init", "npm", "run", "prod" ]
ENTRYPOINT ["/app/entrypoint.sh"]

71 changes: 71 additions & 0 deletions services/workflows-service/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

set -e

infra_file="/tmp/infra.json"

## Get cloudProvider details
function get_cloud_provider() {
release_details=$(uname -r)
if [[ $release_details == *"amzn"* ]];then
# Example: 5.10.192-183.736.amzn2.x86_64
cloud_provider="amazon";
elif [[ $release_details == *"azure"* ]];then
# Example: 5.15.0-1059-azure
cloud_provider="azure";
elif [[ $release_details == *"cloud"* ]];then
# Example: 6.1.0-18-cloud-amd64
cloud_provider="gcp";
elif [[ $release_details == *"generic"* ]];then
# Example: 6.8.0-31-generic
cloud_provider="digitalocean"
elif [[ $release_details == *"ecs"* ]];then
cloud_provider="alibaba"
elif [[ -n "${DYNO}" ]];then
cloud_provider="heroku"
else
cloud_provider="others(including local)";
fi
}

## Get deployment tool details
function get_tool() {
if [[ -z "${KUBERNETES_SERVICE_HOST}" ]]; then
dep_tool="likely docker";
else
dep_tool="kubernetes";
fi
}


## Check hostname
function get_hostname() {
hostname="$(cat /etc/hostname)"
}

## Get current Time
function get_current_time(){
currentTime="$(date -u -Iseconds)"
}

## Check if it's a ECS Fargate deployment
function check_for_fargate() {
if [[ $cloud_provider == "amazon" && $dep_tool == "likely docker" ]]; then
dep_tool="ecs-fargate"
fi
}

## Main Block
get_cloud_provider
get_tool
get_hostname
check_for_fargate
get_current_time

infra_json='{"cloudProvider":"'"$cloud_provider"'","tool":"'"$dep_tool"'","hostname":"'"$hostname"'", "currentTime": "'"$currentTime"'"}'
echo "$infra_json"

echo $infra_json > $infra_file

dumb-init npm run prod

1 change: 1 addition & 0 deletions services/workflows-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@sentry/integrations": "^7.52.1",
"@sentry/node": "^7.52.1",
"@sinclair/typebox": "0.32.15",
"@supabase/supabase-js": "^2.43.1",
"@t3-oss/env-core": "^0.6.1",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
Expand Down
25 changes: 23 additions & 2 deletions services/workflows-service/src/auth/local/local-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { AuthGuard } from '@nestjs/passport';
import { ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
import { createClient } from '@supabase/supabase-js';
import { env } from '@/env';

export class LocalAuthGuard extends AuthGuard('local') {
async canActivate(context: ExecutionContext) {
const result = await super.canActivate(context);
const request = context.switchToHttp().getRequest<Request>();

await super.logIn(request);

if (env.TELEMETRY_ENABLED && env.TELEMETRY_SUPABASE_URL && env.TELEMETRY_SUPABASE_API_KEY) {
try {
const SupabaseClient = createClient(
env.TELEMETRY_SUPABASE_URL,
env.TELEMETRY_SUPABASE_API_KEY,
{
db: { schema: 'public' },
},
);
const fullUrl = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;
const { data: result, error } = await SupabaseClient.from('logins').insert([
{ url: fullUrl },
]);
if (error) {
console.error('Error inserting data:', error.message);
return false;
}
} catch (err) {
console.error('Unexpected error:', err);
}
}
return result as boolean;
}
}
7 changes: 7 additions & 0 deletions services/workflows-service/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export const serverEnvSchema = {
IN_MEMORIES_SECRET_ACQUIRER_ID: z.string().optional(),
IN_MEMORIES_SECRET_PRIVATE_KEY: z.string().optional(),
IN_MEMORIES_SECRET_CONSUMER_KEY: z.string().optional(),
TELEMETRY_ENABLED: z
.enum(['true', 'false'])
.default('true')
.transform(value => value === 'true')
.describe('Enable or disable telemetry'),
TELEMETRY_SUPABASE_URL: z.string().url().optional().describe('Supabase URL for telemetry'),
TELEMETRY_SUPABASE_API_KEY: z.string().optional().describe('Supabase API key for telemetry'),
};

if (!process.env['ENVIRONMENT_NAME'] || process.env['ENVIRONMENT_NAME'] === 'local') {
Expand Down
23 changes: 23 additions & 0 deletions services/workflows-service/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AppLoggerService } from './common/app-logger/app-logger.service';
import { exceptionValidationFactory } from './errors';
import swagger from '@/swagger/swagger';
import { applyFormats, patchNestJsSwagger } from 'ballerine-nestjs-typebox';
import { createClient } from '@supabase/supabase-js';

// provide swagger OpenAPI generator support
patchNestJsSwagger();
Expand Down Expand Up @@ -50,6 +51,28 @@ const corsOrigins = [
];

const main = async () => {
// Infra related data
const infradata = require('/tmp/infra.json');
if (env.TELEMETRY_ENABLED && env.TELEMETRY_SUPABASE_URL && env.TELEMETRY_SUPABASE_API_KEY) {
try {
const SupabaseClient = createClient(
env.TELEMETRY_SUPABASE_URL,
env.TELEMETRY_SUPABASE_API_KEY,
{
db: { schema: 'public' },
},
);
const { data, error } = await SupabaseClient.from('infra').insert([infradata]);
if (error) {
console.error('Error inserting data:', error.message);
} else {
console.log('Data inserted successfully:', data);
}
} catch (error: Error | any) {
console.error('Error inserting data:', error.message);
}
}

const app = await NestFactory.create(AppModule, {
bufferLogs: true, //will be buffered until a custom logger is attached
snapshot: true,
Expand Down

0 comments on commit d505994

Please sign in to comment.