Skip to content

feat: cipher secret configurable #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: preparation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9a46c3d
chore: update midway version
guangwong Oct 12, 2018
cdaf389
chore: change config
guangwong Oct 12, 2018
bfc3fdb
chore: update built-in assets version
guangwong Oct 12, 2018
b9ecc25
chore: use old version ws
guangwong Oct 12, 2018
6c2a63a
chore: update configs
guangwong Oct 12, 2018
d262173
chore: update configs
guangwong Oct 12, 2018
d4b8420
fix: fix wrong mode config
guangwong Oct 13, 2018
7650866
v0.0.2
guangwong Oct 15, 2018
ef4f781
test: add run cov
guangwong Oct 16, 2018
3fa8c0d
test: use blank password
guangwong Oct 16, 2018
5e5f1d2
test: init db
guangwong Oct 16, 2018
2dec4d4
test: fix test issue
guangwong Oct 16, 2018
bae7b8e
chore: add code cov
guangwong Oct 16, 2018
981ba7e
chore: add more test cov reporter
guangwong Oct 16, 2018
a9675f9
chore: mark hook executable
guangwong Oct 16, 2018
c9b54fc
chore: update cov.sh
guangwong Oct 16, 2018
bcbbfc3
test: add test cases
Oct 22, 2018
e505cfa
test: add test cases
Chunlin-Li Oct 22, 2018
805ec2e
chore: travis timezone
Chunlin-Li Oct 22, 2018
f2ade86
test: add test cases
Chunlin-Li Oct 22, 2018
2f1fce0
Merge pull request #1 from Chunlin-Li/test
guangwong Oct 23, 2018
32b1a84
test: add test for metricsManager
guangwong Oct 23, 2018
234a316
test: add test for metricsService
guangwong Oct 23, 2018
5977974
Merge pull request #2 from midwayjs/metrics_test
Chunlin-Li Oct 23, 2018
9f2c61b
Merge branch 'preparation' of github.com:midwayjs/sandbox into prepar…
Chunlin-Li Oct 23, 2018
c4f2e24
test: debugServer test case
Chunlin-Li Oct 23, 2018
46299e8
style: tslint
Chunlin-Li Oct 23, 2018
bde43d2
feat: cipher secret configurable
Chunlin-Li Oct 24, 2018
ce3a417
fix: disable debugServer Autowire
Chunlin-Li Oct 24, 2018
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
Empty file modified .githooks/pre-commit/filter.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ assembly/
mocks_data/proxy/**/__*
tms
coverage
.nyc_output

############################
# visual studio code settings
Expand Down
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
language: node_js
services:
- mysql
node_js:
- "8"
- "9"
- "10"
services:
- mysql
before_install:
- sudo mysql < ./packages/sandbox-core/test/fixtures/sql/initDB.sql 2>&1
- export TZ=Asia/Shanghai
install:
- npm i
- npm i codecov
before_script:
- lerna bootstrap --no-ci
- lerna run build
- npm run bootstrap
- npm run build
script:
- npm run cov
after_script:
- codecov
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "0.0.1"
"version": "0.0.2"
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
"name": "sandbox",
"private": true,
"devDependencies": {
"nyc": "^11.2.1",
"git-hooks": "^1.1.10",
"lerna": "^3.4.0"
},
"nyc": {
"reporter": [
"text",
"json",
"html",
"lcovonly"
]
},
"scripts": {
"build": "lerna run build"
"bootstrap": "lerna bootstrap --no-ci",
"build": "lerna run build",
"cov": "sh scripts/cov.sh"
}
}
9 changes: 6 additions & 3 deletions packages/sandbox-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sandbox-core",
"version": "0.0.1",
"version": "0.0.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"dependencies": {
Expand All @@ -13,7 +13,7 @@
"mysql2": "^1.5.3",
"sequelize": "^4.39.0",
"urllib": "^2.29.0",
"ws": "^6.0.0"
"ws": "^1.1.5"
},
"devDependencies": {
"@types/chai": "^4.1.4",
Expand All @@ -28,6 +28,8 @@
"@types/urllib": "^2.28.0",
"chai": "^3.5.0",
"midway-bin": "^0.3.8",
"midway-mock": "^0.4.2",
"mm": "^2.4.1",
"mocha": "^3.5.0",
"nyc": "^11.2.1",
"ts-node": "^4.1.0",
Expand All @@ -38,7 +40,8 @@
"prepublishOnly": "npm run build",
"lint": "tslint --fix -p .",
"build": "npm run lint && rm -rf ./dist && tsc",
"test": "midway-bin test --ts"
"test": "midway-bin test --ts",
"cov": "midway-bin cov --ts"
},
"files": [
"dist",
Expand Down
34 changes: 23 additions & 11 deletions packages/sandbox-core/src/core/debugServer/cipher.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import crypto = require('crypto');
const secret = 'midway_sandbox';
import { provide, async, scope, ScopeEnum, config } from 'midway-mirror';

@scope(ScopeEnum.Singleton)
@async()
@provide()
export class Cipher {

static encrypt(value: string) {
if ('string' !== typeof value) {
throw new Error('value required');
}
const cipher = crypto.createCipher('des', secret);
@config('cipher')
config;

encrypt(value: string) {
this.paramCheck(value);
const cipher = crypto.createCipher('des', this.config.secret);
return cipher.update(value, 'utf8', 'hex') + cipher.final('hex');
}

static decrypt(value: string) {
if ('string' !== typeof value) {
throw new Error('value required');
}
decrypt(value: string) {
this.paramCheck(value);
try {
const decipher = crypto.createDecipher('des', secret);
const decipher = crypto.createDecipher('des', this.config.secret);
return decipher.update(value, 'hex', 'utf8') + decipher.final('utf8');
} catch (e) {
return '';
}
}

private paramCheck(value: string) {
if ('string' !== typeof value) {
throw new Error('value required');
}
// 必须 在 config 中配置 cipher.secret
if (!this.config.secret) {
throw new Error('cipher secret config required');
}
}

}
15 changes: 10 additions & 5 deletions packages/sandbox-core/src/core/debugServer/debugServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import WebSocket = require('ws');
import {Server as WebSocketServer} from 'ws';
import urllib = require('urllib');
import {Server as HTTPServer} from 'http';
import { provide, inject, autowire } from 'midway-mirror';
import {Cipher} from './cipher';

@provide()
@autowire(false)
export class DebugServer extends EventEmitter {

@inject()
cipher: Cipher;

private httpServer: HTTPServer;
private wsServer: WebSocketServer;
private logger = console;

public constructor (server) {
super();
public setServer(server) {
this.httpServer = server;
}

Expand All @@ -36,16 +41,16 @@ export class DebugServer extends EventEmitter {

try {

const url = request.url;
const upgradeReq = socket.upgradeReq;
const url = upgradeReq.url;
if (!url.startsWith('/remoteDebug')) {
socket.terminate();
return;
}

socket.pause();
const query = QueryString.parse(Url.parse(url).query);
const tokenRaw: string = query.token as any;
const info = JSON.parse(Cipher.decrypt(tokenRaw));
const info = JSON.parse(this.cipher.decrypt(tokenRaw));
info.host = info.ip;

this.logger.log('ws', url);
Expand Down
8 changes: 6 additions & 2 deletions packages/sandbox-core/src/core/manager/errorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class ErrorManager {
attributes: [['log_path', 'path']],
where: { [Sequelize.Op.and]: required },
group: ['path'],
raw: true,
};
return this.errorModel.findAll(conditions);
}
Expand All @@ -133,20 +134,23 @@ export class ErrorManager {
],
where: { [Sequelize.Op.and]: required },
group: ['errType'],
raw: true,
};
return this.errorModel.findAll(conditions);
}

private findErrorTypeDist(required: object[]) {
const downSampling = Sequelize.literal('(unix_timestamp - unix_timestamp % 60)');
const conditions: SearchCondition = {
attributes: [
['error_type', 'errType'],
[Sequelize.fn('COUNT', Sequelize.literal('*')), 'cnt'],
[Sequelize.literal('(unix_timestamp - unix_timestamp % 60)'), 'timestamp'],
[downSampling, 'timestamp'],
],
where: { [Sequelize.Op.and]: required },
group: ['timestamp', 'errType'],
group: [downSampling, 'errType'],
order: [['timestamp', 'DESC']],
raw: true,
};
return this.errorModel.findAll(conditions);
}
Expand Down
62 changes: 18 additions & 44 deletions packages/sandbox-core/src/core/model/dw/sandboxGalaxySlsTraces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,65 +41,39 @@ export async function factory(context: IApplicationContext) {
type: Sequelize.TEXT,
field: 'trace_name',
},
traceSpans: {
type: Sequelize.TEXT,
field: 'trace_spans',
},
traceId: {
type: Sequelize.STRING(256),
field: 'trace_id',
},
uuid: {
type: Sequelize.STRING(256),
allowNull: false,
},
spanName: {
type: Sequelize.STRING(512),
field: 'span_name',
},
spanTimestamp: {
type: Sequelize.STRING(128),
field: 'span_timestamp',
},
spanDuration: {
traceDuration: {
type: Sequelize.INTEGER,
field: 'span_duration',
},
spanError: {
type: Sequelize.INTEGER(4),
allowNull: true,
defaultValue: 0,
field: 'span_error',
field: 'trace_duration',
},
spanType: {
traceStatus: {
type: Sequelize.INTEGER,
field: 'span_type',
},
spanMethod: {
type: Sequelize.STRING(128),
field: 'span_method',
},
spanTarget: {
type: Sequelize.TEXT,
field: 'span_target',
field: 'trace_status',
defaultValue: 1,
},
spanCode: {
type: Sequelize.STRING(128),
field: 'span_code',
},
spanTags: {
type: Sequelize.TEXT,
field: 'span_tags',
uuid: {
type: Sequelize.STRING(256),
allowNull: false,
},
spanId: {
type: Sequelize.STRING(128),
field: 'span_id',
unixTimestamp: {
type: Sequelize.BIGINT,
field: 'unix_timestamp',
},
spanRpcId: {
type: Sequelize.STRING(128),
field: 'span_rpcid',
version: {
type: Sequelize.INTEGER,
},
}, {
timestamps: false,
underscored: false,
freezeTableName: true,
tableName: 'sandbox_galaxy_sls_trace_nodes',
tableName: 'sandbox_galaxy_sls_traces',
});

SLSTraceModel.removeAttribute('id');
Expand Down
5 changes: 4 additions & 1 deletion packages/sandbox-core/src/core/service/remoteDebugService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class RemoteDebugService implements IRemoteDebugService {
@inject('privilegeAdapter')
privilegeAdapter: IPrivilegeAdapter;

@inject()
cipher: Cipher;

async getDebuggableHost(options: HostSelector & AppSelector & UserSelector): Promise<DebuggableHost> {
const {scope, scopeName, uid} = options;
const hasPermission = await this.privilegeAdapter.isAppOps(scope, scopeName, uid);
Expand All @@ -22,7 +25,7 @@ export class RemoteDebugService implements IRemoteDebugService {
}
const debuggableProcesses = await this.pandoraAdapter.getDebuggableProcesses(options);
for (const process of debuggableProcesses) {
process.token = Cipher.encrypt(JSON.stringify({
process.token = this.cipher.encrypt(JSON.stringify({
debugPort: process.debugPort,
ip: options.ip,
webSocketDebuggerUrl: process.webSocketDebuggerUrl,
Expand Down
Loading