Skip to content
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

feat: proxy configuration #45

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "splunk-slack-alerts",
"version": "2.3.0",
"version": "2.3.1",
"description": "Slack alert action for Splunk Enterprise",
"private": true,
"splunk": {
Expand Down
3 changes: 3 additions & 0 deletions src/app/README/alert_actions.conf.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ param.webhook_url = <string>
* The webhook URL to send the Slack message requests to. This can be obtained
* by creating a new "Incoming webhook" integration in Slack.

param.http_proxy = <string>
* your proxy http://proxy:port

param.from_user = <string>
* DEPRECATED - This is only used in the deprecated webhook_url parameter.
* The name of the user sending the Slack message. By default this is "Splunk".
Expand Down
3 changes: 3 additions & 0 deletions src/app/README/savedsearches.conf.spec
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ action.slack.param.slack_app_oauth_token_override = <string>
action.slack.param.webhook_url_override = <string>
* Override the Slack webhook URL for a single alert. This useful when wanting
* to send some alerts to different Slack teams.

action.slack.param.proxy_url_override = <string>
* Override Proxy setting for single alert.
18 changes: 15 additions & 3 deletions src/app/bin/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def send_slack_message(payload):
config = payload.get('configuration')
body = json.dumps(build_slack_message(payload))

http_proxy = config.get('http_proxy', '')

if config.get('proxy_url_override'):
http_proxy = config.get('proxy_url_override', '')
log("DEBUG Using proxy URL from proxy_url_override: %s" % http_proxy)

req = urllib.request
if http_proxy:
proxy_handler = req.ProxyHandler({ 'http': "%s" % http_proxy, 'https': "%s" % http_proxy})
opener = req.build_opener(proxy_handler)
req.install_opener(opener)

# Since Slack webhook URLs are deprecated, we will bias towards using Slack Apps, if they are provided
is_using_slack_app = (("slack_app_oauth_token" in config and config["slack_app_oauth_token"]) or ("slack_app_oauth_token_override" in config and config["slack_app_oauth_token_override"]))
if is_using_slack_app:
Expand All @@ -114,7 +126,7 @@ def send_slack_message(payload):
log("INFO Using configured Slack App OAuth token: %s" % token)

log('DEBUG Calling url="https://slack.com/api/chat.postMessage" with token=%s and body=%s' % (token, body))
req = urllib.request.Request("https://slack.com/api/chat.postMessage", ensure_binary(body), {"Content-Type": "application/json", 'Authorization': "Bearer %s" % token})
msg_req = req.Request("https://slack.com/api/chat.postMessage", ensure_binary(body), {"Content-Type": "application/json", 'Authorization': "Bearer %s" % token})

# To preserve backwards compatibility, we will fallback to the webhook_url configuration, if a Slack App OAuth token is not provided
else:
Expand All @@ -133,10 +145,10 @@ def send_slack_message(payload):
return ERROR_CODE_VALIDATION_FAILED

log('DEBUG Calling url="%s" with body=%s' % (url, body))
req = urllib.request.Request(url, ensure_binary(body), {"Content-Type": "application/json"})
msg_req = req.Request(url, ensure_binary(body), {"Content-Type": "application/json"})

try:
res = urllib.request.urlopen(req)
res = urllib.request.urlopen(msg_req)
res_body = str(res.read())
log("INFO Slack API responded with HTTP status=%d" % res.code)
log("DEBUG Slack API response: %s" % res_body)
Expand Down
3 changes: 2 additions & 1 deletion src/app/default/alert_actions.slap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ param.attachment_alert_title = :bell: {search_name}
param.attachment_adhoc_title = :zap: Ad-hoc search
param.attachment_results_link = <{results_link}|Show results in Splunk> :mag:
param.attachment_footer_text = Splunk Alert
param.attachment_fallback = Alert generated by Splunk: {results_link}
param.attachment_fallback = Alert generated by Splunk: {results_link}
param.http_proxy =
14 changes: 14 additions & 0 deletions src/app/default/data/ui/alerts/slack.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@
</span>
</div>
</div>
<div style="clear: both;" class="control-group">
<label class="control-label" for="proxy_url_override">Proxy URL</label>
<div class="controls">
<input
type="text"
name="action.slack.param.proxy_url_override"
id="slack_fields"
placeholder="Optional"
/>
<span class="help-block">
You can override the default proxy setting. Use <code>http://yourproxy:port </code>
</span>
</div>
</div>
<br clear="both" />
<div style="clear: both" class="control-group">
<label class="control-label" for="slack_url_override">Webhook URL</label>
Expand Down
2 changes: 2 additions & 0 deletions src/ui/pages/slack_alerts_setup/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function loadAlertActionConfig() {
webhook_url: d['param.webhook_url'],
from_user: d['param.from_user'],
from_user_icon: d['param.from_user_icon'],
http_proxy: d['param.http_proxy'],
};
});
}
Expand All @@ -31,6 +32,7 @@ export function updateAlertActionConfig(data) {
`param.webhook_url=${encodeURIComponent(data.webhook_url)}`,
`param.from_user=${encodeURIComponent(data.from_user)}`,
`param.from_user_icon=${encodeURIComponent(data.from_user_icon)}`,
`param.http_proxy=${encodeURIComponent(data.http_proxy)}`,
].join('&'),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Expand Down
16 changes: 16 additions & 0 deletions src/ui/pages/slack_alerts_setup/form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export function SetupForm() {
const updateUrl = useCallback((e, { value }) => update({ ...data, webhook_url: value }));
const updateUser = useCallback((e, { value }) => update({ ...data, from_user: value }));
const updateUserIcon = useCallback((e, { value }) => update({ ...data, from_user_icon: value }));
const updateProxy = useCallback((e, { value }) => update({ ...data, http_proxy: value }));

const slackAppOauthToken = loading ? '' : data.slack_app_oauth_token;
const webhookUrl = loading ? '' : data.webhook_url;
const fromUserName = loading ? '' : data.from_user;
const fromUserIcon = loading ? '' : data.from_user_icon;
const httpProxy = loading ? '' : data.http_proxy;

const oauth_app_manifest = `
display_information:
Expand Down Expand Up @@ -144,6 +146,20 @@ settings:
</TabLayout>

<div style={{ margin: 20 }}>
<Heading level={3}>Proxy Setting</Heading>
<Paragraph>
The following settings will configure the default proxy server for slack alerts.
</Paragraph>
<FormWrapper>
<ControlGroup label="Proxy URL" help="Configure proxy, leave empty if you don't need proxy">
<Text
value={httpProxy}
onChange={updateProxy}
disabled={loading}
placeholder="http://yourproxy:port"
/>
</ControlGroup>
</FormWrapper>
<Heading level={3}>Message Appearance</Heading>
<Paragraph>
The following settings will influence how messages will show up in Slack.{' '}
Expand Down