-
Notifications
You must be signed in to change notification settings - Fork 976
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement log shipping to Graylog via GELF
- Loading branch information
Showing
6 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
50 changes: 50 additions & 0 deletions
50
opencti-platform/opencti-graphql/src/config/log-shipping.js
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,50 @@ | ||
import { format } from 'winston'; | ||
import GelfTransport from 'winston-gelf'; | ||
|
||
/** | ||
* Create a new log shipping transport. | ||
* @param {Object} conf The transport configuration | ||
* @param {string} conf.logs_shipping_level The minimum log level of messages to send to ship | ||
* @param {string} conf.logs_shipping_env_var_prefix The prefix used to match environment variables. Matching | ||
* variables will be added as meta info to the log data. The value of this property will be stripped from the name | ||
* of the environment variable. | ||
* @param {string} conf.logs_graylog_host The Graylog host to connect to | ||
* @param {number} conf.logs_graylog_port The port to use when connecting to the Graylog host | ||
* @param {'tcp'|'udp'} conf.logs_graylog_adapter The adapter (udp/tcp) to use when connecting to the Graylog host | ||
* @returns {import('winston-gelf')} The newly created log shipping transport | ||
*/ | ||
export function createLogShippingTransport(conf) { | ||
return new GelfTransport({ | ||
level: conf.logs_shipping_level, | ||
format: format.combine( | ||
envVarsFormat(conf.logs_shipping_env_var_prefix)(), | ||
format.json(), | ||
), | ||
gelfPro: { | ||
adapterName: `${conf.logs_graylog_adapter}.js`, // append '.js', as a workaround for https://github.com/evanw/esbuild/issues/3328 | ||
adapterOptions: { | ||
host: conf.logs_graylog_host, | ||
port: conf.logs_graylog_port, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
function envVarsFormat(prefix) { | ||
const envVars = findPrefixedEnvVars(prefix); | ||
|
||
return format( | ||
(info) => ({ ...info, ...envVars }) | ||
); | ||
} | ||
|
||
function findPrefixedEnvVars(prefix) { | ||
return Object.fromEntries( | ||
Object.entries(process.env) | ||
.flatMap(([key, value]) => { | ||
return key.startsWith(prefix) | ||
? [[key.substring(prefix.length), value]] | ||
: []; | ||
}) | ||
); | ||
} |
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