-
Notifications
You must be signed in to change notification settings - Fork 89
New notification agent: Zentik notifier agent #2490
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
Open
apocaliss92
wants to merge
3
commits into
unraid:master
Choose a base branch
from
apocaliss92:zentik-notifier-agent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,137 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Agent> | ||
| <Name>ZentikNotifier</Name> | ||
| <Variables> | ||
| <Variable Help="Server base URL (Can be your selfhosted instance). Example: https://notifier-api.zentik.app. See docs [a href='https://notifier-docs.zentik.app/docs/intro' target='_blank'][u]here[/u].[/a]" Desc="Server URL" Default="https://notifier-api.zentik.app">SERVER_URL</Variable> | ||
| <Variable Help="Skip TLS certificate validation (insecure). Use 'yes' only if you understand the risks." Desc="Allow Insecure" Default="no">ALLOW_INSECURE</Variable> | ||
| <Variable Help="Bucket ID." Desc="Bucket ID" Default="">BUCKET_ID</Variable> | ||
| <Variable Help="Access Token used as Bearer token in Authorization header." Desc="Access Token" Default="">ACCESS_TOKEN</Variable> | ||
| <Variable Help="Optional comma-separated list of Zentik user IDs to target. Use 'none' to omit." Desc="User IDs" Default="none">USER_IDS</Variable> | ||
| <Variable Help="Specify the fields which are included in the title of the notification." Desc="Notification Title" Default="$SUBJECT">TITLE</Variable> | ||
| <Variable Help="Specify the fields which are included in the subtitle of the notification." Desc="Notification Subtitle" Default="$EVENT">SUBTITLE</Variable> | ||
| <Variable Help="Specify the fields which are included in the message body of the notification." Desc="Notification Message" Default="$DESCRIPTION,$CONTENT,$LINK">MESSAGE</Variable> | ||
| </Variables> | ||
| <Script> | ||
| <![CDATA[ | ||
| #!/bin/bash | ||
| ############ | ||
| {0} | ||
| ############ | ||
|
|
||
| ############ | ||
| # Zentik Notifier agent for Unraid (Dynamix) | ||
| # | ||
| # Available fields from notification system: | ||
| # HOSTNAME, EVENT, IMPORTANCE, SUBJECT, DESCRIPTION, CONTENT, LINK, TIMESTAMP | ||
| ############ | ||
|
|
||
| for bin in curl jq; do | ||
| command -v "$bin" >/dev/null 2>&1 || { echo "Missing dependency: $bin"; exit 10; } | ||
| done | ||
|
|
||
| SCRIPTNAME=$(basename "$0") | ||
| LOG="/var/log/notify_${SCRIPTNAME%.*}" | ||
|
|
||
| # for quick test, setup environment to mimic notify script | ||
| EVENT="${EVENT:-ZentikNotifier test}" | ||
| SUBJECT="${SUBJECT:-Notification}" | ||
| DESCRIPTION="${DESCRIPTION:-No description}" | ||
| IMPORTANCE="${IMPORTANCE:-normal}" | ||
| CONTENT="${CONTENT:-}" | ||
| LINK="${LINK:-}" | ||
| HOSTNAME="${HOSTNAME:-$(hostname)}" | ||
| TIMESTAMP="${TIMESTAMP:-$(date +%s)}" | ||
|
|
||
| # If TITLE/SUBTITLE/MESSAGE were expanded before defaults (e.g. direct test), fallback now | ||
| TITLE="${TITLE:-$SUBJECT}" | ||
| SUBTITLE="${SUBTITLE:-$EVENT}" | ||
| MESSAGE="${MESSAGE:-$DESCRIPTION}" | ||
|
|
||
| # Expand multi-select values into strings. | ||
| # Note: Dynamix multi-select stores separators as literal "\n" sequences. | ||
| # We intentionally interpret escape sequences here to support formatted input. | ||
| TITLE=$(printf '%b' "$TITLE" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g' | sed 's/^ //; s/ $//') | ||
| SUBTITLE=$(printf '%b' "$SUBTITLE" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g' | sed 's/^ //; s/ $//') | ||
| MESSAGE=$(printf '%b' "$MESSAGE") | ||
|
|
||
| # Map Unraid importance -> Zentik deliveryType | ||
| case "$IMPORTANCE" in | ||
| alert|warning|critical) | ||
| DELIVERY_TYPE="CRITICAL" | ||
| ;; | ||
| normal|info) | ||
| DELIVERY_TYPE="NORMAL" | ||
| ;; | ||
| *) | ||
| DELIVERY_TYPE="SILENT" | ||
| ;; | ||
| esac | ||
|
|
||
| if [[ -z "$SERVER_URL" ]]; then | ||
| echo "Missing SERVER_URL" >>"$LOG" | ||
| exit 1 | ||
| fi | ||
| if [[ -z "$BUCKET_ID" || -z "$ACCESS_TOKEN" ]]; then | ||
| echo "Missing BUCKET_ID and/or ACCESS_TOKEN" >>"$LOG" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Build endpoint from base server URL | ||
| SERVER_URL=${SERVER_URL%/} | ||
| ENDPOINT="${SERVER_URL}/message" | ||
|
|
||
| # userIds: optional comma-separated list | ||
| USER_IDS_JSON="null" | ||
| if [[ -n "$USER_IDS" && "$USER_IDS" != "none" ]]; then | ||
| # Parse comma-separated list into JSON array: | ||
| # 1. Split by comma, 2. Trim whitespace, 3. Drop empties, 4. Quote as JSON strings, 5. Collect into array | ||
| USER_IDS_JSON=$(printf '%s\n' "$USER_IDS" | tr ',' '\n' | sed 's/^ *//; s/ *$//' | awk 'NF' | jq -R . | jq -s .) | ||
| [[ "$USER_IDS_JSON" == "[]" ]] && USER_IDS_JSON="null" | ||
| fi | ||
|
|
||
| # Build payload | ||
| PAYLOAD=$(jq -n \ | ||
| --arg bucketId "$BUCKET_ID" \ | ||
| --arg title "$TITLE" \ | ||
| --arg subtitle "$SUBTITLE" \ | ||
| --arg body "$MESSAGE" \ | ||
| --arg deliveryType "$DELIVERY_TYPE" \ | ||
| --argjson userIds "$USER_IDS_JSON" \ | ||
| '{bucketId:$bucketId,title:$title,subtitle:$subtitle,body:$body,deliveryType:$deliveryType,userIds:$userIds}') | ||
|
|
||
| jq_status=$? | ||
| if [[ "$jq_status" -ne 0 ]]; then | ||
| echo "jq error building payload" >>"$LOG" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # POST | ||
| args=( | ||
| -s -X POST | ||
| -m 10 | ||
| -w "\n%{http_code}" | ||
| -H "Authorization: Bearer ${ACCESS_TOKEN}" | ||
| -H 'Content-Type: application/json' | ||
| --data-binary "$PAYLOAD" | ||
| "$ENDPOINT" | ||
| ) | ||
| [[ "${ALLOW_INSECURE}" == "yes" ]] && args+=( -k ) | ||
|
|
||
| RESPONSE=$(curl "${args[@]}" 2>&1) | ||
| CURL_EXIT=$? | ||
|
|
||
| if [[ $CURL_EXIT -ne 0 ]]; then | ||
| echo "curl failed with exit code $CURL_EXIT" >>"$LOG" | ||
| exit 1 | ||
| fi | ||
|
|
||
| HTTP_CODE=$(echo "$RESPONSE" | tail -n1) | ||
|
|
||
| # Check if HTTP status indicates success (2xx) | ||
| if [[ ! "$HTTP_CODE" =~ ^2[0-9]{2}$ ]]; then | ||
| echo "HTTP request failed with status $HTTP_CODE" >>"$LOG" | ||
| exit 1 | ||
| fi | ||
| ]]> | ||
| </Script> | ||
| </Agent> | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add curl exit code validation before parsing HTTP response.
The script captures the curl response but doesn't check curl's exit code. If curl fails due to network errors, DNS resolution failure, or timeout, the script won't properly detect it before attempting to parse the HTTP status code.
🔎 Proposed fix
[[ "${ALLOW_INSECURE}" == "yes" ]] && args+=( -k ) RESPONSE=$(curl "${args[@]}" 2>&1) +CURL_EXIT=$? + +if [[ $CURL_EXIT -ne 0 ]]; then + echo "curl failed with exit code $CURL_EXIT" >>"$LOG" + exit 1 +fi + HTTP_CODE=$(echo "$RESPONSE" | tail -n1) # Check if HTTP status indicates success (2xx)🤖 Prompt for AI Agents