Skip to content

Commit c9615c4

Browse files
committed
[#93] AgentId and ApplicationName Length validator
1 parent a056b14 commit c9615c4

File tree

6 files changed

+166
-4
lines changed

6 files changed

+166
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to Pinpoint Node.js agent will be documented in this file.
33

4+
## [0.8.3] - 2021-11-19
5+
### Fixed
6+
- #93 Fix Agent ID length validator
7+
48
## [0.8.2] - 2021-05-10
59
### Fixed
610
- #73 fix require.main undefined error by webpack and node -r pinpoint-node-agent

demo/express/kube.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
apiVersion: v1
2+
items:
3+
- apiVersion: v1
4+
kind: Service
5+
metadata:
6+
creationTimestamp: null
7+
labels:
8+
io.kompose.service: redis
9+
name: redis
10+
spec:
11+
ports:
12+
- name: "6379"
13+
port: 6379
14+
targetPort: 6379
15+
selector:
16+
io.kompose.service: redis
17+
status:
18+
loadBalancer: {}
19+
- apiVersion: apps/v1
20+
kind: Deployment
21+
metadata:
22+
creationTimestamp: null
23+
labels:
24+
io.kompose.service: redis
25+
name: redis
26+
spec:
27+
replicas: 1
28+
selector:
29+
matchLabels:
30+
io.kompose.service: redis
31+
strategy: {}
32+
template:
33+
metadata:
34+
creationTimestamp: null
35+
labels:
36+
io.kompose.service: redis
37+
spec:
38+
containers:
39+
- env:
40+
- name: ALLOW_EMPTY_PASSWORD
41+
value: "yes"
42+
image: redis
43+
name: redis
44+
ports:
45+
- containerPort: 6379
46+
resources: {}
47+
restartPolicy: Always
48+
status: {}
49+
kind: List
50+
metadata: {}
51+

grpc/grpc-idl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 146713e5554f8be8d39e8bea80a2dc6499afae9b

lib/config.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ const configurationValueValidations = {
106106
agentConfig.traceExclusionUrlCacheSize = 100
107107
}
108108
}
109+
},
110+
validateIds: () => {
111+
[{ id: agentConfig.agentId, name: 'Agent ID' }, { id: agentConfig.applicationName, name: 'Application Name' }].filter(id => id.id)
112+
.filter(id => {
113+
const ID_MAX_LEN = 24
114+
const idRegex = /[a-zA-Z0-9\\._\\-]+/
115+
116+
if (id.id.length < 1) {
117+
agentConfig.enable = false
118+
log.error(`You have to set ${id.name}`)
119+
return false
120+
}
121+
122+
if (id.id.length > ID_MAX_LEN) {
123+
agentConfig.enable = false
124+
log.error(`You have to set ${id.name} to less ${ID_MAX_LEN} characters.`)
125+
return false
126+
}
127+
128+
if (!idRegex.test(id.id)) {
129+
agentConfig.enable = false
130+
log.error(`invalidate ${id.name} name with /[a-zA-Z0-9\\._\\-]+/ RegExp`)
131+
return false
132+
}
133+
})
109134
}
110135
}
111136

@@ -121,7 +146,7 @@ const init = (initOptions = {}) => {
121146
Object.entries(REQUIRE_CONFIG).forEach(([propertyName, description]) => {
122147
if (agentConfig.enable && !agentConfig[propertyName]) {
123148
agentConfig.enable = false
124-
log.error(`You must set ${description}. The Pinpoint Node JS Agent has been shutdown.`)
149+
log.error(`You must set ${description}.The Pinpoint Node JS Agent has been shutdown.`)
125150
}
126151
})
127152

@@ -167,7 +192,7 @@ const readRootConfigFile = () => {
167192
const fileName = 'pinpoint-config.json'
168193
const mainModulePath = getMainModulePath(require)
169194
if (!mainModulePath) {
170-
log.warn('If executed with `node -r`, pinpoint-config.json cannot be read because require.main value is undefined.')
195+
log.warn('If executed with `node - r`, pinpoint-config.json cannot be read because require.main value is undefined.')
171196
return {}
172197
}
173198

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pinpoint-node-agent",
3-
"version": "0.8.2",
3+
"version": "0.8.3",
44
"main": "index.js",
55
"scripts": {
66
"test": "./node_modules/.bin/tape ./test/**/*.test.js",

test/config.test.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test('deadline config', (t) => {
5757

5858
const json = require('../lib/pinpoint-config-default')
5959
const result = config.readConfigJson(json)
60-
t.equal(result.streamDeadlineMinutesClientSide, 5)
60+
t.equal(result.streamDeadlineMinutesClientSide, 10)
6161
})
6262

6363
test('main moudle path', (t) => {
@@ -86,5 +86,86 @@ test('main moudle path', (t) => {
8686
actual = config.getMainModulePath({ main: { filename: '/test/test1' } })
8787
t.equal(actual, '/test', 'config.getMainModulePath({ main: { filename: \' / test\' } }) return value is /')
8888

89+
t.end()
90+
})
91+
92+
// https://github.com/pinpoint-apm/pinpoint/blob/master/commons/src/main/java/com/navercorp/pinpoint/common/util/IdValidateUtils.java
93+
// public static final String ID_PATTERN_VALUE = "[a-zA-Z0-9\\._\\-]+";
94+
// https://github.com/pinpoint-apm/pinpoint/blob/master/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/IdValidator.java
95+
// https://github.com/pinpoint-apm/pinpoint/blob/master/commons/src/main/java/com/navercorp/pinpoint/common/PinpointConstants.java
96+
// public final class PinpointConstants {
97+
// public static final int APPLICATION_NAME_MAX_LEN = 24;
98+
// public static final int AGENT_ID_MAX_LEN = 24;
99+
// }
100+
test('Agent ID length check', (t) => {
101+
config.clear()
102+
process.env['PINPOINT_AGENT_ID'] = "agentId"
103+
process.env['PINPOINT_APPLICATION_NAME'] = "appication name"
104+
105+
let given = config.getConfig()
106+
t.true(given.enable, 'configuration agentId, Name, ApplicationName enable agent id')
107+
108+
delete process.env.PINPOINT_AGENT_ID
109+
delete process.env.PINPOINT_APPLICATION_NAME
110+
111+
config.clear()
112+
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
113+
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"
114+
115+
given = config.getConfig()
116+
t.true(given.enable, 'maxlength agentID and application Name')
117+
118+
delete process.env.PINPOINT_AGENT_ID
119+
delete process.env.PINPOINT_APPLICATION_NAME
120+
121+
config.clear()
122+
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdageE"
123+
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"
124+
125+
given = config.getConfig()
126+
t.false(given.enable, 'maxlength agentID error')
127+
128+
delete process.env.PINPOINT_AGENT_ID
129+
delete process.env.PINPOINT_APPLICATION_NAME
130+
131+
config.clear()
132+
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
133+
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappicationE"
134+
135+
given = config.getConfig()
136+
t.false(given.enable, 'maxlength application Name error')
137+
138+
delete process.env.PINPOINT_AGENT_ID
139+
delete process.env.PINPOINT_APPLICATION_NAME
140+
141+
config.clear()
142+
process.env['PINPOINT_AGENT_ID'] = "~"
143+
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"
144+
145+
given = config.getConfig()
146+
t.false(given.enable, 'invalide agent ID')
147+
148+
delete process.env.PINPOINT_AGENT_ID
149+
delete process.env.PINPOINT_APPLICATION_NAME
150+
151+
config.clear()
152+
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
153+
process.env['PINPOINT_APPLICATION_NAME'] = "~"
154+
155+
given = config.getConfig()
156+
t.false(given.enable, 'invalide application name')
157+
158+
delete process.env.PINPOINT_AGENT_ID
159+
delete process.env.PINPOINT_APPLICATION_NAME
160+
161+
config.clear()
162+
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"
163+
164+
given = config.getConfig()
165+
t.false(given.enable, 'agent ID nullable test')
166+
167+
delete process.env.PINPOINT_AGENT_ID
168+
delete process.env.PINPOINT_APPLICATION_NAME
169+
89170
t.end()
90171
})

0 commit comments

Comments
 (0)