Skip to content

Commit

Permalink
[#93] AgentId and ApplicationName Length validator
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Nov 22, 2021
1 parent a056b14 commit c9615c4
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to Pinpoint Node.js agent will be documented in this file.

## [0.8.3] - 2021-11-19
### Fixed
- #93 Fix Agent ID length validator

## [0.8.2] - 2021-05-10
### Fixed
- #73 fix require.main undefined error by webpack and node -r pinpoint-node-agent
Expand Down
51 changes: 51 additions & 0 deletions demo/express/kube.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: v1
items:
- apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
ports:
- name: "6379"
port: 6379
targetPort: 6379
selector:
io.kompose.service: redis
status:
loadBalancer: {}
- apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: redis
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
spec:
containers:
- env:
- name: ALLOW_EMPTY_PASSWORD
value: "yes"
image: redis
name: redis
ports:
- containerPort: 6379
resources: {}
restartPolicy: Always
status: {}
kind: List
metadata: {}

1 change: 1 addition & 0 deletions grpc/grpc-idl
Submodule grpc-idl added at 146713
29 changes: 27 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ const configurationValueValidations = {
agentConfig.traceExclusionUrlCacheSize = 100
}
}
},
validateIds: () => {
[{ id: agentConfig.agentId, name: 'Agent ID' }, { id: agentConfig.applicationName, name: 'Application Name' }].filter(id => id.id)
.filter(id => {
const ID_MAX_LEN = 24
const idRegex = /[a-zA-Z0-9\\._\\-]+/

if (id.id.length < 1) {
agentConfig.enable = false
log.error(`You have to set ${id.name}`)
return false
}

if (id.id.length > ID_MAX_LEN) {
agentConfig.enable = false
log.error(`You have to set ${id.name} to less ${ID_MAX_LEN} characters.`)
return false
}

if (!idRegex.test(id.id)) {
agentConfig.enable = false
log.error(`invalidate ${id.name} name with /[a-zA-Z0-9\\._\\-]+/ RegExp`)
return false
}
})
}
}

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

Expand Down Expand Up @@ -167,7 +192,7 @@ const readRootConfigFile = () => {
const fileName = 'pinpoint-config.json'
const mainModulePath = getMainModulePath(require)
if (!mainModulePath) {
log.warn('If executed with `node -r`, pinpoint-config.json cannot be read because require.main value is undefined.')
log.warn('If executed with `node - r`, pinpoint-config.json cannot be read because require.main value is undefined.')
return {}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pinpoint-node-agent",
"version": "0.8.2",
"version": "0.8.3",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/tape ./test/**/*.test.js",
Expand Down
83 changes: 82 additions & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('deadline config', (t) => {

const json = require('../lib/pinpoint-config-default')
const result = config.readConfigJson(json)
t.equal(result.streamDeadlineMinutesClientSide, 5)
t.equal(result.streamDeadlineMinutesClientSide, 10)
})

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

t.end()
})

// https://github.com/pinpoint-apm/pinpoint/blob/master/commons/src/main/java/com/navercorp/pinpoint/common/util/IdValidateUtils.java
// public static final String ID_PATTERN_VALUE = "[a-zA-Z0-9\\._\\-]+";
// https://github.com/pinpoint-apm/pinpoint/blob/master/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/IdValidator.java
// https://github.com/pinpoint-apm/pinpoint/blob/master/commons/src/main/java/com/navercorp/pinpoint/common/PinpointConstants.java
// public final class PinpointConstants {
// public static final int APPLICATION_NAME_MAX_LEN = 24;
// public static final int AGENT_ID_MAX_LEN = 24;
// }
test('Agent ID length check', (t) => {
config.clear()
process.env['PINPOINT_AGENT_ID'] = "agentId"
process.env['PINPOINT_APPLICATION_NAME'] = "appication name"

let given = config.getConfig()
t.true(given.enable, 'configuration agentId, Name, ApplicationName enable agent id')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"

given = config.getConfig()
t.true(given.enable, 'maxlength agentID and application Name')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdageE"
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"

given = config.getConfig()
t.false(given.enable, 'maxlength agentID error')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappicationE"

given = config.getConfig()
t.false(given.enable, 'maxlength application Name error')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_AGENT_ID'] = "~"
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"

given = config.getConfig()
t.false(given.enable, 'invalide agent ID')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_AGENT_ID'] = "agentIdagentIdagentIdage"
process.env['PINPOINT_APPLICATION_NAME'] = "~"

given = config.getConfig()
t.false(given.enable, 'invalide application name')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

config.clear()
process.env['PINPOINT_APPLICATION_NAME'] = "appicationnameappication"

given = config.getConfig()
t.false(given.enable, 'agent ID nullable test')

delete process.env.PINPOINT_AGENT_ID
delete process.env.PINPOINT_APPLICATION_NAME

t.end()
})

0 comments on commit c9615c4

Please sign in to comment.