Skip to content

Commit

Permalink
fix: replace credential hash with uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Sep 5, 2023
1 parent f0eb774 commit d571b33
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 35 deletions.
5 changes: 2 additions & 3 deletions backend/src/sms/middlewares/sms.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ApiInvalidCredentialsError,
ApiNotFoundError,
} from '@core/errors/rest-api.errors'
import { v4 as uuid } from 'uuid'

export interface SmsMiddleware {
getCredentialsFromBody: Handler
Expand Down Expand Up @@ -241,9 +242,7 @@ export const InitSmsMiddleware = (
try {
// Store credentials in AWS secrets manager
const stringifiedCredential = JSON.stringify(credentials)
const credentialName = await SmsService.getEncodedHash(
stringifiedCredential
)
const credentialName = uuid()

// We only want to tag Twilio credentials with the environment tag in SecretsManager
// when env is development. This allows us to quickly identify and clean up dev secrets.
Expand Down
11 changes: 3 additions & 8 deletions backend/src/sms/routes/tests/sms-campaign.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,7 @@ describe('POST /campaign/{campaignId}/sms/new-credentials', () => {
.spyOn(SmsService, 'sendCampaignMessage')
.mockResolvedValue()

// getEncodedHash is used as the stored name in AWS SecretsManager
const HASHED_CREDS = 'HASHED_CREDS'
const mockGetEncodedHash = jest
.spyOn(SmsService, 'getEncodedHash')
.mockResolvedValue(HASHED_CREDS)
const EXPECTED_CRED_NAME = 'HASHED_CREDS'

const res = await request(app)
.post(`/campaign/${nonDemoCampaign.id}/sms/new-credentials`)
Expand All @@ -246,7 +242,7 @@ describe('POST /campaign/{campaignId}/sms/new-credentials', () => {

expect(mockSecretsManager.createSecret).toHaveBeenCalledWith(
expect.objectContaining({
Name: HASHED_CREDS,
Name: EXPECTED_CRED_NAME,
SecretString: JSON.stringify({
accountSid: 'twilio_account_sid',
apiKey: 'twilio_api_key',
Expand All @@ -259,12 +255,11 @@ describe('POST /campaign/{campaignId}/sms/new-credentials', () => {
// Ensure credential was added into DB
const dbCredential = await Credential.findOne({
where: {
name: HASHED_CREDS,
name: EXPECTED_CRED_NAME,
},
})
expect(dbCredential).not.toBe(null)
mockSendCampaignMessage.mockRestore()
mockGetEncodedHash.mockRestore()
})
})

Expand Down
14 changes: 4 additions & 10 deletions backend/src/sms/routes/tests/sms-settings.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ describe('POST /settings/sms/credentials', () => {
const mockSendValidationMessage = jest
.spyOn(SmsService, 'sendValidationMessage')
.mockResolvedValue()

// getEncodedHash is used as the stored name in AWS SecretsManager
const HASHED_CREDS = 'HASHED_CREDS'
const mockGetEncodedHash = jest
.spyOn(SmsService, 'getEncodedHash')
.mockResolvedValue(HASHED_CREDS)
const EXPECTED_CRED_NAME = 'HASHED_CREDS'

const res = await request(app).post('/settings/sms/credentials').send({
label: CREDENTIAL_LABEL,
Expand All @@ -79,7 +74,7 @@ describe('POST /settings/sms/credentials', () => {

expect(mockSecretsManager.createSecret).toHaveBeenCalledWith(
expect.objectContaining({
Name: HASHED_CREDS,
Name: EXPECTED_CRED_NAME,
SecretString: JSON.stringify({
accountSid: 'twilio_account_sid',
apiKey: 'twilio_api_key',
Expand All @@ -92,7 +87,7 @@ describe('POST /settings/sms/credentials', () => {
// Ensure credential was added into DB
const dbCredential = await Credential.findOne({
where: {
name: HASHED_CREDS,
name: EXPECTED_CRED_NAME,
},
})
expect(dbCredential).not.toBe(null)
Expand All @@ -101,12 +96,11 @@ describe('POST /settings/sms/credentials', () => {
where: {
label: CREDENTIAL_LABEL,
type: ChannelType.SMS,
credName: HASHED_CREDS,
credName: EXPECTED_CRED_NAME,
userId: 1,
},
})
expect(dbUserCredential).not.toBe(null)
mockSendValidationMessage.mockRestore()
mockGetEncodedHash.mockRestore()
})
})
14 changes: 0 additions & 14 deletions backend/src/sms/services/sms.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bcrypt from 'bcrypt'
import { Transaction } from 'sequelize'

import config from '@core/config'
Expand Down Expand Up @@ -181,18 +180,6 @@ const getCampaignDetails = async (
return { ...campaignDetails, cost_per_message: costPerSms }
}

/**
* Returns a base 64 encoded hash for secrets manager
* @param secret
*/
const getEncodedHash = async (secret: string): Promise<string> => {
const secretHash = await bcrypt.hash(
secret,
config.get('aws.secretManagerSalt')
)
return Buffer.from(secretHash).toString('base64')
}

const uploadCompleteOnPreview = ({
transaction,
template,
Expand Down Expand Up @@ -309,7 +296,6 @@ const getTwilioCostPerOutgoingSMSSegmentUSD = async (
}

export const SmsService = {
getEncodedHash,
findCampaign,
getCampaignDetails,
getHydratedMessage,
Expand Down

0 comments on commit d571b33

Please sign in to comment.