Repository navigation
fix(initializer): prevent legacy sed and argument injection #1257
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81b9ee7
fix(initializer): treat legacy template values as literal data
csuzhangxc 5e40921
fix(initializer): JSON-escape legacy dashboard titles
csuzhangxc ad4be4a
fix(initializer): encode cluster names in rule YAML and descriptions
csuzhangxc 2b4f8c8
fix(initializer): narrow legacy injection fix to validated inputs
csuzhangxc 97c25d1
fix(initializer): validate only enabled optional inputs
csuzhangxc 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
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 |
|---|---|---|
| @@ -1,92 +1,131 @@ | ||
| #!/bin/sh | ||
| if [ ! -d $GF_PROVISIONING_PATH/dashboards ];then | ||
| mkdir -p $GF_PROVISIONING_PATH/dashboards | ||
| else | ||
| rm -rf $GF_PROVISIONING_PATH/dashboards/* | ||
|
|
||
| # Values are sed replacement data, never pieces of a sed program. | ||
| replace_template() { | ||
| replacement=$(printf '%s' "$2" | sed 's/[\\&|]/\\&/g') || return 1 | ||
| sed -i "s|$1|$replacement|g" -- "$3" | ||
| } | ||
|
|
||
| # Keep replacement values safe in the existing JSON, YAML and alert templates. | ||
| LC_ALL=C | ||
| export LC_ALL | ||
| namespace_to_validate= | ||
| if [ -n "$GF_DATASOURCE_PATH" ] && [ -n "$GF_PROVISIONING_PATH" ]; then | ||
| namespace_to_validate=$TIDB_CLUSTER_NAMESPACE | ||
| fi | ||
| for value in "$TIDB_CLUSTER_NAME" "$namespace_to_validate"; do | ||
| case "$value" in | ||
| -|*[!a-zA-Z0-9_.-]*) echo "Cluster name and namespace must contain only letters, digits, _, . or -" >&2; exit 1;; | ||
| esac | ||
| done | ||
| if [ -n "$GF_DATASOURCE_PATH" ]; then | ||
| for value in "$GF_K8S_PROMETHEUS_URL" "$GF_TIDB_PROMETHEUS_URL"; do | ||
| case "$value" in | ||
| *[![:graph:]]*|*:) echo "Datasource URLs must be printable ASCII without whitespace or a trailing colon" >&2; exit 1;; | ||
| esac | ||
| case "$value" in | ||
| ''|http://?*|https://?*) ;; | ||
| *) echo "Datasource URLs must use HTTP or HTTPS" >&2; exit 1;; | ||
| esac | ||
| done | ||
| fi | ||
| if [ -z "$PROM_CONFIG_PATH" ]; then | ||
| echo "Prometheus output path is required" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # TiDB dashboard | ||
| cp /tmp/tidb.json $GF_PROVISIONING_PATH/dashboards | ||
| sed -i 's/Test-Cluster-TiDB/'$TIDB_CLUSTER_NAME'-TiDB/g' $GF_PROVISIONING_PATH/dashboards/tidb.json | ||
| if [ -n "$GF_PROVISIONING_PATH" ]; then | ||
| if [ ! -d "$GF_PROVISIONING_PATH/dashboards" ];then | ||
| mkdir -p -- "$GF_PROVISIONING_PATH/dashboards" | ||
| else | ||
| rm -rf -- "$GF_PROVISIONING_PATH/dashboards"/* | ||
| fi | ||
|
|
||
| # Overview dashboard | ||
| cp /tmp/overview.json $GF_PROVISIONING_PATH/dashboards | ||
| sed -i 's/Test-Cluster-Overview/'$TIDB_CLUSTER_NAME'-Overview/g' $GF_PROVISIONING_PATH/dashboards/overview.json | ||
| # TiDB dashboard | ||
| cp -- /tmp/tidb.json "$GF_PROVISIONING_PATH/dashboards" | ||
| replace_template 'Test-Cluster-TiDB' "${TIDB_CLUSTER_NAME}-TiDB" "$GF_PROVISIONING_PATH/dashboards"/tidb.json | ||
|
|
||
| # PD dashboard | ||
| cp /tmp/pd.json $GF_PROVISIONING_PATH/dashboards | ||
| sed -i 's/Test-Cluster-PD/'$TIDB_CLUSTER_NAME'-PD/g' $GF_PROVISIONING_PATH/dashboards/pd.json | ||
| # Overview dashboard | ||
| cp -- /tmp/overview.json "$GF_PROVISIONING_PATH/dashboards" | ||
| replace_template 'Test-Cluster-Overview' "${TIDB_CLUSTER_NAME}-Overview" "$GF_PROVISIONING_PATH/dashboards"/overview.json | ||
|
|
||
| # TiKV dashboard | ||
| cp /tmp/tikv*.json $GF_PROVISIONING_PATH/dashboards | ||
| if [ ! -f /tmp/tikv_pull.json ];then | ||
| sed -i 's/Test-Cluster-TiKV-Details/'$TIDB_CLUSTER_NAME'-TiKV-Details/g' $GF_PROVISIONING_PATH/dashboards/tikv_details.json | ||
| sed -i 's/Test-Cluster-TiKV-Summary/'$TIDB_CLUSTER_NAME'-TiKV-Summary/g' $GF_PROVISIONING_PATH/dashboards/tikv_summary.json | ||
| sed -i 's/Test-Cluster-TiKV-Trouble-Shooting/'$TIDB_CLUSTER_NAME'-TiKV-Trouble-Shooting/g' $GF_PROVISIONING_PATH/dashboards/tikv_trouble_shooting.json | ||
| else | ||
| sed -i 's/Test-Cluster-TiKV/'$TIDB_CLUSTER_NAME'-TiKV/g' $GF_PROVISIONING_PATH/dashboards/tikv_pull.json | ||
| fi | ||
| # PD dashboard | ||
| cp -- /tmp/pd.json "$GF_PROVISIONING_PATH/dashboards" | ||
| replace_template 'Test-Cluster-PD' "${TIDB_CLUSTER_NAME}-PD" "$GF_PROVISIONING_PATH/dashboards"/pd.json | ||
|
|
||
| # Binlog dashboard | ||
| cp /tmp/binlog.json $GF_PROVISIONING_PATH/dashboards | ||
| sed -i 's/Test-Cluster-Binlog/'$TIDB_CLUSTER_NAME'-Binlog/g' $GF_PROVISIONING_PATH/dashboards/binlog.json | ||
| # TiKV dashboard | ||
| cp -- /tmp/tikv*.json "$GF_PROVISIONING_PATH/dashboards" | ||
| if [ ! -f /tmp/tikv_pull.json ];then | ||
| replace_template 'Test-Cluster-TiKV-Details' "${TIDB_CLUSTER_NAME}-TiKV-Details" "$GF_PROVISIONING_PATH/dashboards"/tikv_details.json | ||
| replace_template 'Test-Cluster-TiKV-Summary' "${TIDB_CLUSTER_NAME}-TiKV-Summary" "$GF_PROVISIONING_PATH/dashboards"/tikv_summary.json | ||
| replace_template 'Test-Cluster-TiKV-Trouble-Shooting' "${TIDB_CLUSTER_NAME}-TiKV-Trouble-Shooting" "$GF_PROVISIONING_PATH/dashboards"/tikv_trouble_shooting.json | ||
| else | ||
| replace_template 'Test-Cluster-TiKV' "${TIDB_CLUSTER_NAME}-TiKV" "$GF_PROVISIONING_PATH/dashboards"/tikv_pull.json | ||
| fi | ||
|
|
||
| # Lighting | ||
| cp /tmp/lightning.json $GF_PROVISIONING_PATH/dashboards | ||
| sed -i 's/Test-Cluster-Lightning/'$TIDB_CLUSTER_NAME'-Lightning/g' $GF_PROVISIONING_PATH/dashboards/lightning.json | ||
| # Binlog dashboard | ||
| cp -- /tmp/binlog.json "$GF_PROVISIONING_PATH/dashboards" | ||
| replace_template 'Test-Cluster-Binlog' "${TIDB_CLUSTER_NAME}-Binlog" "$GF_PROVISIONING_PATH/dashboards"/binlog.json | ||
|
|
||
| # Lighting | ||
| cp -- /tmp/lightning.json "$GF_PROVISIONING_PATH/dashboards" | ||
| replace_template 'Test-Cluster-Lightning' "${TIDB_CLUSTER_NAME}-Lightning" "$GF_PROVISIONING_PATH/dashboards"/lightning.json | ||
|
|
||
| fi | ||
|
|
||
| # Rules | ||
| if [ ! -d $PROM_CONFIG_PATH/rules ];then | ||
| mkdir -p $PROM_CONFIG_PATH/rules | ||
| if [ ! -d "$PROM_CONFIG_PATH/rules" ];then | ||
| mkdir -p -- "$PROM_CONFIG_PATH/rules" | ||
| else | ||
| rm -rf $PROM_CONFIG_PATH/rules/* | ||
| rm -rf -- "$PROM_CONFIG_PATH/rules"/* | ||
| fi | ||
| echo $META_TYPE | ||
| echo $META_INSTANCE | ||
| echo $META_VALUE | ||
| cp /tmp/*.rules.yml $PROM_CONFIG_PATH/rules | ||
| for file in $PROM_CONFIG_PATH/rules/* | ||
| echo "$META_TYPE" | ||
| echo "$META_INSTANCE" | ||
| echo "$META_VALUE" | ||
| cp -- /tmp/*.rules.yml "$PROM_CONFIG_PATH/rules" | ||
| for file in "$PROM_CONFIG_PATH/rules"/* | ||
| do | ||
| sed -i 's/ENV_LABELS_ENV/'$TIDB_CLUSTER_NAME'/g' $file | ||
| replace_template 'ENV_LABELS_ENV' "$TIDB_CLUSTER_NAME" "$file" | ||
| done | ||
|
|
||
| # Copy Persistent rules to override raw files | ||
| if [ ! -z $PROM_PERSISTENT_DIR ]; | ||
| if [ -n "$PROM_PERSISTENT_DIR" ]; | ||
| then | ||
| if [ -d $PROM_PERSISTENT_DIR/latest-rules/${TIDB_VERSION##*/} ];then | ||
| cp -f $PROM_PERSISTENT_DIR/latest-rules/${TIDB_VERSION##*/}/*.rules.yml $PROM_CONFIG_PATH/rules | ||
| if [ -d "$PROM_PERSISTENT_DIR/latest-rules/${TIDB_VERSION##*/}" ];then | ||
| cp -f -- "$PROM_PERSISTENT_DIR/latest-rules/${TIDB_VERSION##*/}"/*.rules.yml "$PROM_CONFIG_PATH/rules" | ||
| fi | ||
| fi | ||
|
|
||
|
|
||
| # Datasources | ||
| if [ ! -z $GF_DATASOURCE_PATH ]; | ||
| if [ -n "$GF_DATASOURCE_PATH" ]; | ||
| then | ||
| if [ ! -z $GF_K8S_PROMETHEUS_URL ]; | ||
| if [ -n "$GF_K8S_PROMETHEUS_URL" ]; | ||
| then | ||
| sed -i 's,http://prometheus-k8s.monitoring.svc:9090,'$GF_K8S_PROMETHEUS_URL',g' /tmp/k8s-datasource.yaml | ||
| replace_template 'http://prometheus-k8s.monitoring.svc:9090' "${GF_K8S_PROMETHEUS_URL}" /tmp/k8s-datasource.yaml | ||
| fi | ||
|
|
||
| if [ ! -z $GF_TIDB_PROMETHEUS_URL ]; | ||
| if [ -n "$GF_TIDB_PROMETHEUS_URL" ]; | ||
| then | ||
| sed -i 's,http://127.0.0.1:9090,'$GF_TIDB_PROMETHEUS_URL',g' /tmp/tidb-cluster-datasource.yaml | ||
| replace_template 'http://127.0.0.1:9090' "${GF_TIDB_PROMETHEUS_URL}" /tmp/tidb-cluster-datasource.yaml | ||
| fi | ||
|
|
||
| cp /tmp/k8s-datasource.yaml $GF_DATASOURCE_PATH/ | ||
| cp /tmp/tidb-cluster-datasource.yaml $GF_DATASOURCE_PATH/ | ||
|
|
||
| # pods | ||
| if [ ! -z $TIDB_CLUSTER_NAMESPACE ]; | ||
| then | ||
| sed -i 's/$namespace/'$TIDB_CLUSTER_NAMESPACE'/g' /tmp/pods.json | ||
| else | ||
| sed -i 's/$namespace/default/g' /tmp/pods.json | ||
| cp -- /tmp/k8s-datasource.yaml "$GF_DATASOURCE_PATH/" | ||
| cp -- /tmp/tidb-cluster-datasource.yaml "$GF_DATASOURCE_PATH/" | ||
|
|
||
| if [ -n "$GF_PROVISIONING_PATH" ]; then | ||
| # pods | ||
| if [ -n "$TIDB_CLUSTER_NAMESPACE" ]; | ||
| then | ||
| replace_template '$namespace' "${TIDB_CLUSTER_NAMESPACE}" /tmp/pods.json | ||
| else | ||
| sed -i 's/$namespace/default/g' /tmp/pods.json | ||
| fi | ||
| replace_template 'Test-Cluster-Pods-Info' "${TIDB_CLUSTER_NAME}-Pods-Info" /tmp/pods.json | ||
| cp -- /tmp/pods.json "$GF_PROVISIONING_PATH/dashboards" | ||
|
|
||
| # nodes | ||
| cp -- /tmp/nodes.json "$GF_PROVISIONING_PATH/dashboards" | ||
| fi | ||
| sed -i 's/Test-Cluster-Pods-Info/'$TIDB_CLUSTER_NAME'-Pods-Info/g' /tmp/pods.json | ||
| cp /tmp/pods.json $GF_PROVISIONING_PATH/dashboards | ||
|
|
||
| # nodes | ||
| cp /tmp/nodes.json $GF_PROVISIONING_PATH/dashboards | ||
| fi | ||
|
|
||
Oops, something went wrong.
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.
Script executed:
Repository: pingcap/monitoring
Length of output: 5609
Script executed:
Repository: pingcap/monitoring
Length of output: 25782
Web query:
POSIX shell if conditional command exit status zero no condition true BusyBox ashResult:
Web query:
site:pubs.opengroup.org shell if command exit status zero no condition true POSIXResult:
Web query:
POSIX "exit status" "if" "no condition" "zero" shell command languageResult:
Propagate datasource copy failures.
If
GF_PROVISIONING_PATHis empty and the datasource directory is read-only, both copies can fail but the initializer exits zero. Check each copy so the initializer reports the failure.Suggested fix