Skip to content

Commit

Permalink
New Production check: UNknown Slack nango connection ids (#2695)
Browse files Browse the repository at this point in the history
* New Production check: UNknown Slack nango connection ids

* Put Slack config in a set
  • Loading branch information
PopDaph authored Nov 29, 2023
1 parent fcc4009 commit 5f660a1
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
35 changes: 35 additions & 0 deletions front/package-lock.json

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

1 change: 1 addition & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@headlessui/react": "^1.7.7",
"@heroicons/react": "^2.0.11",
"@nangohq/frontend": "^0.16.1",
"@nangohq/node": "^0.36.37",
"@sendgrid/mail": "^7.7.0",
"@slack/web-api": "^6.8.1",
"@tailwindcss/forms": "^0.5.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Nango } from "@nangohq/node";
import { QueryTypes, Sequelize } from "sequelize";

import { CheckFunction } from "@app/production_checks/types/check";

const {
CONNECTORS_DATABASE_READ_REPLICA_URI,
NANGO_SECRET_KEY,
NANGO_SLACK_CONNECTOR_ID,
} = process.env;

export const nangoConnectionIdCleanupSlack: CheckFunction = async (
checkName,
reportSuccess,
reportFailure
) => {
if (!NANGO_SECRET_KEY) {
throw new Error("Env var NANGO_SECRET_KEY is not defined");
}
if (!NANGO_SLACK_CONNECTOR_ID) {
throw new Error("Env var NANGO_SLACK_CONNECTOR_ID is not defined");
}
if (!CONNECTORS_DATABASE_READ_REPLICA_URI) {
throw new Error(
"Env var CONNECTORS_DATABASE_READ_REPLICA_URI is not defined"
);
}

// Get all the Slack configurations in the database
const connectorsSequelize = new Sequelize(
CONNECTORS_DATABASE_READ_REPLICA_URI,
{
logging: false,
}
);
const dbSlackConfigurationsData: { id: number; slackTeamId: string }[] =
await connectorsSequelize.query(
`SELECT id, "slackTeamId" FROM "slack_configurations"`,
{ type: QueryTypes.SELECT }
);
const dbSlackConfigurations = new Set(
dbSlackConfigurationsData.map((sc) => sc.slackTeamId)
);

// Get all the Slack connections in Nango (created more than 1 hour ago)
const oneHourAgo = new Date();
oneHourAgo.setHours(oneHourAgo.getHours() - 1);
const nango = new Nango({ secretKey: NANGO_SECRET_KEY });
const nangoConnections = await nango.listConnections();
const nangoSlackConnections = nangoConnections.connections.filter(
(connection) => {
const createdAt = new Date(connection.created);
return (
connection.provider === NANGO_SLACK_CONNECTOR_ID &&
createdAt < oneHourAgo
);
}
);

// Check that all the Slack connections in Nango have a corresponding Slack configuration in the database
const unknownNangoSlackConnections = [];
for (const conn of nangoSlackConnections) {
const connectionDetail = await nango.getConnection(
conn.provider,
conn.connection_id
);
const slackTeamId = connectionDetail.credentials.raw.team.id;
if (!dbSlackConfigurations.has(slackTeamId)) {
unknownNangoSlackConnections.push({
connectionId: conn.connection_id,
slackTeamId,
});
}
}

if (unknownNangoSlackConnections.length > 0) {
reportFailure(
{ unknownConnections: unknownNangoSlackConnections },
"Unknown Slack Teams in Nango"
);
} else {
reportSuccess({});
}
};
5 changes: 5 additions & 0 deletions front/production_checks/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { v4 as uuidv4 } from "uuid";

import mainLogger from "@app/logger/logger";
import { managedDataSourceGCGdriveCheck } from "@app/production_checks/checks/managed_data_source_gdrive_gc";
import { nangoConnectionIdCleanupSlack } from "@app/production_checks/checks/nango_connection_id_cleanup_slack";
import { Check } from "@app/production_checks/types/check";

export async function runAllChecksActivity() {
Expand All @@ -11,6 +12,10 @@ export async function runAllChecksActivity() {
name: "managed_data_source_gdrive_gc",
check: managedDataSourceGCGdriveCheck,
},
{
name: "nango_connection_id_cleanup_slack",
check: nangoConnectionIdCleanupSlack,
},
];
await runAllChecks(checks);
}
Expand Down

0 comments on commit 5f660a1

Please sign in to comment.