Skip to content

Commit

Permalink
Use Winston to log extra information (Dashlane#13)
Browse files Browse the repository at this point in the history
* Use Winston to log extra information

* Rebase with pre-commit hook and apply linter
  • Loading branch information
jboillot authored Apr 13, 2022
1 parent 3df31e1 commit 7df11f6
Show file tree
Hide file tree
Showing 9 changed files with 744 additions and 293 deletions.
994 changes: 712 additions & 282 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/inquirer": "^8.1.3",
"@types/inquirer-autocomplete-prompt": "^1.3.3",
"@types/node": "^17.0.5",
"@types/winston": "^2.4.4",
"@types/xml2json": "^0.11.4",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
Expand All @@ -43,6 +44,7 @@
"inquirer-autocomplete-prompt": "^2.0.0",
"keytar": "^7.9.0",
"otplib": "^12.0.1",
"winston": "^3.7.2",
"xml2json": "^0.12.0",
"zlib": "^1.0.5"
}
Expand Down
2 changes: 1 addition & 1 deletion src/api-connect/signRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ export const assertNever = (x: never): never => {
if (isTestEnvironment) {
throw new Error('Should have never been here');
}
console.log('Should have never been here', x);
console.error('Should have never been here', x);
return x;
};
3 changes: 2 additions & 1 deletion src/crypto/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as crypto from 'crypto';
import winston from 'winston';
import { hmacSha256, sha512 } from './hash.js';

export interface Argon2d {
Expand Down Expand Up @@ -45,7 +46,7 @@ export const argonDecrypt = (dataBuffer: Buffer, originalKey: Buffer, iv: Buffer

export const getCipheringMethod = (cipheredData: string): CipheringMethod => {
if (!cipheredData || cipheredData.length === 0) {
console.log(cipheredData);
winston.debug(cipheredData);
throw new Error('invalid ciphered data');
}
const newMarkerDelimiter = '$';
Expand Down
3 changes: 2 additions & 1 deletion src/database/connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import Database from 'better-sqlite3';
import winston from 'winston';

// The most appropriate folder to store the user's data, by OS
const USER_DATA_PATH =
Expand All @@ -16,7 +17,7 @@ export const connect = () => {
}

const db = new Database(DB_PATH + '/userdata.db');
console.log('Connected to database.');
winston.debug('Connected to database.');

return db;
};
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#!/usr/bin/env node
import { program } from 'commander';
import winston from 'winston';
import { sync } from './middleware/sync.js';
import { getPassword } from './middleware/get.js';
import { connectAndPrepare } from './database/index.js';

const debugLevel = process.argv.indexOf('--debug') !== -1 ? 'debug' : 'info';

winston.configure({
level: debugLevel,
format: winston.format.combine(winston.format.splat(), winston.format.cli()),
transports: [new winston.transports.Console()],
});

program.name('dcli').description('[Non Official] Dashlane CLI').version('0.1.0');

program.option('--debug', 'Print debug messages');

program
.command('sync')
.description('Manually synchronize the local vault with Dashlane')
Expand Down
12 changes: 8 additions & 4 deletions src/middleware/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Database from 'better-sqlite3';
import inquirer from 'inquirer';
import inquirerAutocomplete from 'inquirer-autocomplete-prompt';
import { authenticator } from 'otplib';
import winston from 'winston';

import { getCipheringMethod, argonDecrypt } from '../crypto/decrypt.js';
import { AuthentifiantTransactionContent, BackupEditTransaction, VaultCredential } from '../types';
Expand Down Expand Up @@ -87,12 +88,15 @@ const decryptTransactions = async (
return decryptTransactions(transactions, masterPassword, login);
}

const passwordsDecrypted = transactions
.filter((transaction) => transaction.type === 'AUTHENTIFIANT')
const authentifiantTransactions = transactions.filter((transaction) => transaction.type === 'AUTHENTIFIANT');

const passwordsDecrypted = authentifiantTransactions
.map((transaction: BackupEditTransaction) => decryptTransaction(transaction, derivate))
.filter(notEmpty);

console.log('Encountered decryption errors:', transactions.length - passwordsDecrypted.length);
if (authentifiantTransactions.length !== passwordsDecrypted.length) {
console.error('Encountered decryption errors:', authentifiantTransactions.length - passwordsDecrypted.length);
}
return passwordsDecrypted;
};

Expand All @@ -104,7 +108,7 @@ export const getPassword = async (params: GetPassword): Promise<void> => {
throw new Error("Couldn't retrieve master pasword in OS keychain.");
}

console.log('Retrieving:', titleFilter || '');
winston.debug('Retrieving:', titleFilter || '');
const transactions = db
.prepare(`SELECT * FROM transactions WHERE action = 'BACKUP_EDIT'`)
.all() as BackupEditTransaction[];
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/registerDevice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inquirer from 'inquirer';
import Database from 'better-sqlite3';
import winston from 'winston';
import {
completeDeviceRegistration,
performDuoPushVerification,
Expand All @@ -16,7 +17,7 @@ interface RegisterDevice {

export const registerDevice = async (params: RegisterDevice): Promise<DeviceKeysWithLogin> => {
const { db } = params;
console.log('Registering the device...');
winston.debug('Registering the device...');

const { login } = await inquirer.prompt<{ login: string }>([
{
Expand Down
7 changes: 4 additions & 3 deletions src/middleware/sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Database from 'better-sqlite3';
import winston from 'winston';
import { getLatestContent } from '../steps/index.js';
import type { DeviceKeysWithLogin } from '../types.js';

Expand All @@ -9,7 +10,7 @@ interface Sync {

export const sync = async (params: Sync) => {
const { db, deviceKeys } = params;
console.log('Start syncing...');
winston.debug('Start syncing...');

const formerSyncTimestamp =
(
Expand Down Expand Up @@ -46,11 +47,11 @@ export const sync = async (params: Sync) => {
// save the new transaction timestamp in the db
db.prepare('REPLACE INTO syncUpdates(timestamp) VALUES(?)').bind(Number(latestContent.timestamp)).run();

console.log('Requested timestamp ', formerSyncTimestamp, ', new timestamp', latestContent.timestamp);
winston.debug('Requested timestamp ', formerSyncTimestamp, ', new timestamp', latestContent.timestamp);

const summaryCounted: Record<string, number> = {};
Object.keys(latestContent.summary).forEach((key) => {
summaryCounted[key] = Object.keys(latestContent.summary[key]).length;
});
console.log(summaryCounted);
winston.debug(summaryCounted);
};

0 comments on commit 7df11f6

Please sign in to comment.