Skip to content
Merged
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
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

patreon: switcherapi
ko_fi: petruki
custom: ['https://www.paypal.com/donate/?business=A5R6K3JAD8SW4&no_recurring=0&item_name=Thank+you+for+supporting+the+Open+Source+Community&currency_code=CAD']
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fetch-depth: 0

- name: Use Node.js 20.x
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20.x

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/re-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
ref: ${{ github.event.inputs.tag }}

- name: Use Node.js 20.x
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20.x

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 0

- name: Use Node.js 20.x
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20.x

Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,3 @@ See also our SDKs to integrate Switcher API with your application.

- **Auth** - Client API: /criteria/auth [POST]
- **Executing** - Client API: /criteria?key=SWITCHER_KEY [POST]

* * *

## Donations
Donations for coffee, cookies or pizza are extremely welcomed.<br>
Please, find the sponsor button at the top for more options.

[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate/?business=A5R6K3JAD8SW4&no_recurring=0&item_name=Thank+you+for+supporting+the+Open+Source+Community&currency_code=CAD)
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.3.0",
"mongoose": "^8.1.3",
"mongoose": "^8.2.0",
"pino": "^8.19.0",
"pino-pretty": "^10.3.1",
"swagger-ui-express": "^5.0.0",
Expand All @@ -57,11 +57,11 @@
"babel-jest": "^29.7.0",
"babel-polyfill": "^6.26.0",
"env-cmd": "^10.1.0",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"jest-sonar-reporter": "^2.0.0",
"node-notifier": "^10.0.1",
"nodemon": "^3.0.3",
"nodemon": "^3.1.0",
"sinon": "^17.0.1",
"supertest": "^6.3.4"
},
Expand Down
3 changes: 2 additions & 1 deletion src/models/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import crypto from 'crypto';
import jwt from 'jsonwebtoken';
import { Team } from './team';
import { notifyAcCreation, notifyAcDeletion } from '../external/switcher-api-facade';
import { EncryptionSalts } from './common';

const adminSchema = new mongoose.Schema({
name: {
Expand Down Expand Up @@ -196,7 +197,7 @@ adminSchema.pre('save', async function (next) {
const admin = this;

if (admin.isModified('password')) {
admin.password = await bcryptjs.hash(admin.password, 8);
admin.password = await bcryptjs.hash(admin.password, EncryptionSalts.ADMIN);
notifyAcCreation(admin._id);
}

Expand Down
5 changes: 5 additions & 0 deletions src/models/common/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import History from '../history';
import { checkHistory } from '../../external/switcher-api-facade';

export const EncryptionSalts = Object.freeze({
ADMIN: 8,
COMPONENT: 8
});

function checkDifference(diff, documents, defaultIgnoredFields,
keyArr, keys, pos) {

Expand Down
3 changes: 2 additions & 1 deletion src/models/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { randomUUID } from 'crypto';
import jwt from 'jsonwebtoken';
import { Config } from './config';
import Domain from './domain';
import { EncryptionSalts } from './common';

const componentSchema = new mongoose.Schema({
name: {
Expand Down Expand Up @@ -56,7 +57,7 @@ componentSchema.methods.generateApiKey = async function () {
const component = this;

const apiKey = randomUUID();
const hash = await bcryptjs.hash(apiKey, 8);
const hash = await bcryptjs.hash(apiKey, EncryptionSalts.COMPONENT);
component.apihash = hash;
await component.save();

Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/db_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Metric } from '../../src/models/metric';
import { EnvType, Environment } from '../../src/models/environment';
import { ConfigStrategy, StrategiesType, OperationsType } from '../../src/models/config-strategy';
import Slack from '../../src/models/slack';
import { EncryptionSalts } from '../../src/models/common';

process.env.JWT_SECRET = process.env.JWT_SECRET || 'test_secret';

Expand Down Expand Up @@ -274,7 +275,7 @@ export const setupDatabase = async () => {
await new Permission(permissionAll3).save();
await new Permission(permissionAll4).save();

const hash = await bcryptjs.hash(component1Key, 8);
const hash = await bcryptjs.hash(component1Key, EncryptionSalts.COMPONENT);
component1.apihash = hash;
await new Component(component1).save();
};
7 changes: 3 additions & 4 deletions tests/fixtures/db_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ConfigStrategy, StrategiesType, OperationsType } from '../../src/models
import { ActionTypes, RouterTypes, Permission } from '../../src/models/permission';
import { Team } from '../../src/models/team';
import Slack from '../../src/models/slack';
import { EncryptionSalts } from '../../src/models/common';

process.env.JWT_SECRET = process.env.JWT_SECRET || 'test_secret';

Expand All @@ -37,7 +38,7 @@ export const adminAccount = {
active: true
};

export let apiKey;
export const apiKey = randomUUID();
export const domainId = new mongoose.Types.ObjectId();
export const domainDocument = {
_id: domainId,
Expand Down Expand Up @@ -237,9 +238,7 @@ export const setupDatabase = async () => {
await new ConfigStrategy(configStrategyTIME_BETWEENDocument).save();
await new ConfigStrategy(configStrategyTIME_GREATDocument).save();

const newApiKey = randomUUID();
const hash = await bcryptjs.hash(newApiKey, 8);
const hash = await bcryptjs.hash(apiKey, EncryptionSalts.COMPONENT);
component1.apihash = hash;
await new Component(component1).save();
apiKey = newApiKey;
};
9 changes: 4 additions & 5 deletions tests/fixtures/db_client_payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Config } from '../../src/models/config';
import Component from '../../src/models/component';
import { Environment, EnvType } from '../../src/models/environment';
import { ConfigStrategy, StrategiesType, OperationsType } from '../../src/models/config-strategy';
import { EncryptionSalts } from '../../src/models/common';

process.env.JWT_SECRET = process.env.JWT_SECRET || 'test_secret';

Expand All @@ -32,7 +33,7 @@ export const adminAccount = {
active: true
};

export let apiKey = undefined;
export const apiKey = randomUUID();
export const domainId = new mongoose.Types.ObjectId();
export const domainDocument = {
_id: domainId,
Expand Down Expand Up @@ -115,10 +116,8 @@ export const setupDatabase = async () => {
await new GroupConfig(groupConfigDocument).save();
await new Config(configPayloadDocument).save();
await new ConfigStrategy(configStrategyPAYLOAD_HAS_ONEDocument).save();

const newApiKey = randomUUID();
const hash = await bcryptjs.hash(newApiKey, 8);

const hash = await bcryptjs.hash(apiKey, EncryptionSalts.COMPONENT);
component1.apihash = hash;
await new Component(component1).save();
apiKey = newApiKey;
};
5 changes: 3 additions & 2 deletions tests/fixtures/db_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bcryptjs from 'bcryptjs';
import Component from '../../src/models/component';
import Domain from '../../src/models/domain';
import { EnvType } from '../../src/models/environment';
import { EncryptionSalts } from '../../src/models/common';

export async function createDummyDomain(domainName, accountId) {
const domainDocument = {
Expand All @@ -26,8 +27,8 @@ export async function createDummyComponent(componentName, domainId, accountId) {
owner: accountId
};

const apiKey = await bcryptjs.hash(componentDocument._id + componentDocument.name, 8);
const hash = await bcryptjs.hash(apiKey, 8);
const apiKey = await bcryptjs.hash(componentDocument._id + componentDocument.name, EncryptionSalts.COMPONENT);
const hash = await bcryptjs.hash(apiKey, EncryptionSalts.COMPONENT);
componentDocument.apihash = hash;

await new Component(componentDocument).save();
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/db_metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import GroupConfig from '../../src/models/group-config';
import { Config } from '../../src/models/config';
import Domain from '../../src/models/domain';
import Component from '../../src/models/component';
import { EncryptionSalts } from '../../src/models/common';

process.env.JWT_SECRET = process.env.JWT_SECRET || 'test_secret';

Expand Down Expand Up @@ -171,7 +172,7 @@ export const setupDatabase = async () => {
await new Metric(entry4).save();

const newApiKey = randomUUID();
const hash = await bcryptjs.hash(newApiKey, 8);
const hash = await bcryptjs.hash(newApiKey, EncryptionSalts.COMPONENT);
component1.apihash = hash;
await new Component(component1).save();
};
5 changes: 3 additions & 2 deletions tests/model/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
domainDocument
} from '../fixtures/db_api';
import Component from '../../src/models/component';
import { EncryptionSalts } from '../../src/models/common';

afterAll(async () => {
await new Promise(resolve => setTimeout(resolve, 1000));
Expand All @@ -25,13 +26,13 @@ describe('(Deprecated) Testing component authentication', () => {
const generateApiKeyDeprecated = async (component) => {
const buffer = randomBytes(32);
const apiKey = Buffer.from(buffer).toString('base64');
const hash = await bcryptjs.hash(apiKey, 8);
const hash = await bcryptjs.hash(apiKey, EncryptionSalts.COMPONENT);
component.apihash = hash;
await component.save();

const generatedApiKey = Buffer.from(apiKey).toString('base64');
return generatedApiKey;
}
};

test('COMPONENT_MODEL - Should authenticate component using old API key format', async () => {
// Given
Expand Down