-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add redis OpenTelemetry query sanitizers (#665)
Add functions to sanitize the Redis query reported by OpenTelemetry. This converts `set user_password password1` to `set ? ?` so that it doesn't store any sensitive data. Part of appsignal/opentelemetry#1
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...nodejs/.changesets/add-opentelemetry-node-redis-and-ioredis-query-sanitizers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
bump: "patch" | ||
type: "add" | ||
--- | ||
|
||
Add OpenTelemetry node-redis and ioredis query sanitizers. We recommend using these sanitizers to ensure no sensitive data is sent in query statements. Add the sanitizer to the `dbStatementSerializer` config as demonstrated below. | ||
|
||
```js | ||
// tracing.js | ||
// Add the RedisDbStatementSerializer import | ||
const { RedisDbStatementSerializer } = require("@appsignal/nodejs"); | ||
const { RedisInstrumentation } = require("@opentelemetry/instrumentation-redis"); | ||
const sdk = new opentelemetry.NodeSDK({ | ||
instrumentations: [ | ||
new RedisInstrumentation({ | ||
// Configure the AppSignal RedisDbStatementSerializer to sanitize queries | ||
dbStatementSerializer: RedisDbStatementSerializer | ||
}) | ||
] | ||
}); | ||
``` | ||
|
||
The same can be done for the ioredis instrumentation: | ||
|
||
```js | ||
// tracing.js | ||
// Add the IORedisDbStatementSerializer import | ||
const { IORedisDbStatementSerializer } = require('@appsignal/nodejs'); | ||
const { IORedisInstrumentation } = require('@opentelemetry/instrumentation-ioredis'); | ||
const sdk = new opentelemetry.NodeSDK({ | ||
instrumentations: [ | ||
// Add the IORedisInstrumentation | ||
new IORedisInstrumentation({ | ||
// Configure the AppSignal IORedisDbStatementSerializer to sanitize queries | ||
dbStatementSerializer: IORedisDbStatementSerializer | ||
}) | ||
] | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/nodejs/src/instrumentation/redis/__tests__/opentelemetry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RedisDbStatementSerializer } from "../opentelemetry" | ||
|
||
describe("RedisDbStatementSerializer", () => { | ||
it("sanitizes queries without arguments", () => { | ||
const result = RedisDbStatementSerializer("get", []) | ||
|
||
expect(result).toEqual("get") | ||
}) | ||
|
||
it("sanitizes queries with single arguments", () => { | ||
const result = RedisDbStatementSerializer("get", ["my_key"]) | ||
|
||
expect(result).toEqual("get ?") | ||
}) | ||
|
||
it("sanitizes queries with multiple argumentsj", () => { | ||
const result = RedisDbStatementSerializer("set", ["my_key", "my value"]) | ||
|
||
expect(result).toEqual("set ? ?") | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
packages/nodejs/src/instrumentation/redis/opentelemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// dbStatementSerializer for OpenTelemetry node-redis and ioredis packages | ||
// This ensures no sensitive data is sent in Redis queries. | ||
export function RedisDbStatementSerializer(command: string, args: Array<any>) { | ||
const values = [] | ||
if (args.length > 0) { | ||
for (let i = 0; i < args.length; i++) { | ||
values.push("?") | ||
} | ||
return `${command} ${values.join(" ")}` | ||
} else { | ||
return command | ||
} | ||
} |