diff --git a/backend/package-lock.json b/backend/package-lock.json index dc88bfb..4ff4ce0 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@aws-sdk/client-dynamodb": "^3.693.0", "@aws-sdk/client-s3": "^3.693.0", + "@aws-sdk/client-secrets-manager": "^3.699.0", "@aws-sdk/lib-dynamodb": "^3.693.0", "@types/aws-lambda": "^8.10.145", "@types/jszip": "^3.4.0", @@ -385,6 +386,59 @@ "node": ">=16.0.0" } }, + "node_modules/@aws-sdk/client-secrets-manager": { + "version": "3.699.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-secrets-manager/-/client-secrets-manager-3.699.0.tgz", + "integrity": "sha512-DWBOvozaWG/qZGuzPVs2wdguprkX7FSsjoH0bSPJ0oirGFEBq5d33HSJ3PmN8HzaLknWS+s5EdDLoB6NYzy7og==", + "dependencies": { + "@aws-crypto/sha256-browser": "5.2.0", + "@aws-crypto/sha256-js": "5.2.0", + "@aws-sdk/client-sso-oidc": "3.699.0", + "@aws-sdk/client-sts": "3.699.0", + "@aws-sdk/core": "3.696.0", + "@aws-sdk/credential-provider-node": "3.699.0", + "@aws-sdk/middleware-host-header": "3.696.0", + "@aws-sdk/middleware-logger": "3.696.0", + "@aws-sdk/middleware-recursion-detection": "3.696.0", + "@aws-sdk/middleware-user-agent": "3.696.0", + "@aws-sdk/region-config-resolver": "3.696.0", + "@aws-sdk/types": "3.696.0", + "@aws-sdk/util-endpoints": "3.696.0", + "@aws-sdk/util-user-agent-browser": "3.696.0", + "@aws-sdk/util-user-agent-node": "3.696.0", + "@smithy/config-resolver": "^3.0.12", + "@smithy/core": "^2.5.3", + "@smithy/fetch-http-handler": "^4.1.1", + "@smithy/hash-node": "^3.0.10", + "@smithy/invalid-dependency": "^3.0.10", + "@smithy/middleware-content-length": "^3.0.12", + "@smithy/middleware-endpoint": "^3.2.3", + "@smithy/middleware-retry": "^3.0.27", + "@smithy/middleware-serde": "^3.0.10", + "@smithy/middleware-stack": "^3.0.10", + "@smithy/node-config-provider": "^3.1.11", + "@smithy/node-http-handler": "^3.3.1", + "@smithy/protocol-http": "^4.1.7", + "@smithy/smithy-client": "^3.4.4", + "@smithy/types": "^3.7.1", + "@smithy/url-parser": "^3.0.10", + "@smithy/util-base64": "^3.0.0", + "@smithy/util-body-length-browser": "^3.0.0", + "@smithy/util-body-length-node": "^3.0.0", + "@smithy/util-defaults-mode-browser": "^3.0.27", + "@smithy/util-defaults-mode-node": "^3.0.27", + "@smithy/util-endpoints": "^2.1.6", + "@smithy/util-middleware": "^3.0.10", + "@smithy/util-retry": "^3.0.10", + "@smithy/util-utf8": "^3.0.0", + "@types/uuid": "^9.0.1", + "tslib": "^2.6.2", + "uuid": "^9.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, "node_modules/@aws-sdk/client-sso": { "version": "3.696.0", "resolved": "https://registry.npmjs.org/@aws-sdk/client-sso/-/client-sso-3.696.0.tgz", diff --git a/backend/package.json b/backend/package.json index 8eb9e5c..fbcec45 100644 --- a/backend/package.json +++ b/backend/package.json @@ -28,6 +28,7 @@ "dependencies": { "@aws-sdk/client-dynamodb": "^3.693.0", "@aws-sdk/client-s3": "^3.693.0", + "@aws-sdk/client-secrets-manager": "^3.699.0", "@aws-sdk/lib-dynamodb": "^3.693.0", "@types/aws-lambda": "^8.10.145", "@types/jszip": "^3.4.0", diff --git a/backend/src/handlers/PackageCreate/index.ts b/backend/src/handlers/PackageCreate/index.ts index 26c11db..162932d 100644 --- a/backend/src/handlers/PackageCreate/index.ts +++ b/backend/src/handlers/PackageCreate/index.ts @@ -6,13 +6,13 @@ import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; const utilsPath = process.env.UTILS_PATH || '/opt/nodejs/common/utils'; import dotenv from 'dotenv'; dotenv.config(); - + // eslint-disable-next-line @typescript-eslint/no-require-imports const { createErrorResponse, getPackageByName, updatePackageHistory, uploadPackageMetadata } = require(utilsPath); const interfacesPath = process.env.INTERFACES_PATH || '/opt/nodejs/common/interfaces'; - - + + /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-vars */ const interfaces = require(interfacesPath); type PackageData = typeof interfaces.PackageData; @@ -24,9 +24,14 @@ type PackageMetadata = typeof interfaces.PackageMetadata; const servicesPath = process.env.SERVICES_PATH || '/opt/nodejs/services/rate'; // eslint-disable-next-line @typescript-eslint/no-require-imports -const { npmAnalysis } = require(`${servicesPath}/tools/api`); - +const { runAnalysis } = require(`${servicesPath}/tools/scripts`); +const { getEnvVars } = require(`${servicesPath}/tools/getEnvVars`); +const ratingInterfaces = require(`${servicesPath}/utils/interfaces`); +const { metricCalc } = require(`${servicesPath}/tools/metricCalc`); +type envVars = typeof ratingInterfaces.envVars; +type repoData = typeof ratingInterfaces.repoData; +type metricData = typeof ratingInterfaces.metricData; import { createHash } from 'crypto'; @@ -39,7 +44,7 @@ import http from 'isomorphic-git/http/node'; import yazl from 'yazl'; import axios from 'axios'; - + const s3 = new S3({ region: 'us-east-2', useArnRegion: false, // Ignore ARN regions and stick to 'us-east-2' @@ -52,6 +57,33 @@ type NpmMetadata = { }; }; +// Use this code snippet in your app. +// If you need more information about configurations or implementing the sample code, visit the AWS docs: +// https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started.html + +import { + SecretsManagerClient, + GetSecretValueCommand +} from "@aws-sdk/client-secrets-manager"; + +const client = new SecretsManagerClient({ + region: "us-east-2", +}); + + +export async function getSecret(secret_name: string): Promise { + const response = await client.send( + new GetSecretValueCommand({ + SecretId: secret_name, + VersionStage: "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified + }) + ); + const secret = response.SecretString || ""; + return secret; +} + + +// Your code goes here // Main Lambda handler function export const handler = async (event: APIGatewayProxyEvent): Promise => { @@ -85,6 +117,13 @@ export const handler = async (event: APIGatewayProxyEvent): Promise { zipFile.end(); }); } + +async function getScores(url: string): Promise { + const envVar: envVars = new getEnvVars(); + const runAnalysisClass = new runAnalysis(envVar); + + try { + const repoData: repoData[] = await runAnalysisClass.runAnalysis([url]); + const repo = repoData[0]; + const metricCalcClass = new metricCalc(); + const result: metricData = metricCalcClass.getValue(repo); + + return result; + } catch (error) { + throw new Error(`Could not execute URL analysis of modules: ${error}`); + } +} diff --git a/backend/template.yml b/backend/template.yml index 674bd9f..4754291 100644 --- a/backend/template.yml +++ b/backend/template.yml @@ -220,6 +220,11 @@ Resources: TableName: !Ref PackageMetadataTable - DynamoDBWritePolicy: TableName: !Ref PackageHistoryTable + - Statement: + - Effect: Allow + Action: + - secretsmanager:GetSecretValue + Resource: arn:aws:secretsmanager:us-east-2:122610496633:secret:GitHubToken Environment: Variables: S3_BUCKET_NAME: !Sub "package-files-${Stage}"