-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Enable gnmi/telemetry user authorization by config_db #21363
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
Merged
qiluo-msft
merged 19 commits into
sonic-net:master
from
liuh-80:dev/liuh/enable_gnmi_cert_auth
Feb 20, 2025
+96
−51
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9a401e5
Enable gnmi/telemetry user authorization by config_db
liuh-80 05f42c0
Update yang model
liuh-80 fa702a4
Rename `--user_auth` to `--client_auth`
liuh-80 1af6a5f
Add user_auth pattern to YANG models
liuh-80 476a569
Refactor user_auth type definition
liuh-80 47de0d8
Refactor user authentication handling in scripts
liuh-80 24bb572
Remove max-elements constraint from GNMI_CLIENT_CERT_LIST
liuh-80 13b292a
Refactor user authentication handling in scripts
liuh-80 423914b
Fix syntax error in shell scripts
liuh-80 8b4b884
Refactor user authentication handling in telemetry scripts
liuh-80 30f4d64
Add debug echo for telemetry arguments
liuh-80 fe4090b
Fix GNMI variable checks in scripts
liuh-80 5253122
Refactor GNMI client certificate handling
liuh-80 78ed03c
Fix typo in CRL_EXPIRE_DURATION variable check
liuh-80 bfa73a8
Refactor JSON field extraction in scripts
liuh-80 d4b3572
Rename `Extract_json_field` to `extract_field`
liuh-80 22256a3
Validate port value in GNMI scripts
liuh-80 356188f
Remove single quotes around port variable
liuh-80 d64c967
Remove unnecessary quotes in telemetry scripts
liuh-80 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 |
|---|---|---|
|
|
@@ -3,6 +3,11 @@ | |
| EXIT_TELEMETRY_VARS_FILE_NOT_FOUND=1 | ||
| INCORRECT_TELEMETRY_VALUE=2 | ||
| TELEMETRY_VARS_FILE=/usr/share/sonic/templates/telemetry_vars.j2 | ||
| ESCAPE_QUOTE="'\''" | ||
|
|
||
| extract_field() { | ||
| echo $(echo $1 | jq -r $2) | ||
| } | ||
|
|
||
| if [ ! -f "$TELEMETRY_VARS_FILE" ]; then | ||
| echo "Telemetry vars template file not found" | ||
|
|
@@ -25,31 +30,28 @@ export CVL_SCHEMA_PATH=/usr/sbin/schema | |
| export GOTRACEBACK=crash | ||
|
|
||
| if [ -n "$CERTS" ]; then | ||
| SERVER_CRT=$(echo $CERTS | jq -r '.server_crt') | ||
| SERVER_KEY=$(echo $CERTS | jq -r '.server_key') | ||
| SERVER_CRT=$(extract_field "$CERTS" '.server_crt') | ||
| SERVER_KEY=$(extract_field "$CERTS" '.server_key') | ||
| if [ -z $SERVER_CRT ] || [ -z $SERVER_KEY ]; then | ||
| TELEMETRY_ARGS+=" --insecure" | ||
| else | ||
| TELEMETRY_ARGS+=" --server_crt $SERVER_CRT --server_key $SERVER_KEY " | ||
| fi | ||
|
|
||
| CA_CRT=$(echo $CERTS | jq -r '.ca_crt') | ||
| CA_CRT=$(extract_field "$CERTS" '.ca_crt') | ||
| if [ ! -z $CA_CRT ]; then | ||
| TELEMETRY_ARGS+=" --ca_crt $CA_CRT" | ||
| fi | ||
|
|
||
| # Reuse GNMI_CLIENT_CERT for telemetry service | ||
| TELEMETRY_ARGS+=" --config_table_name GNMI_CLIENT_CERT" | ||
| elif [ -n "$X509" ]; then | ||
| SERVER_CRT=$(echo $X509 | jq -r '.server_crt') | ||
| SERVER_KEY=$(echo $X509 | jq -r '.server_key') | ||
| SERVER_CRT=$(extract_field "$X509" '.server_crt') | ||
| SERVER_KEY=$(extract_field "$X509" '.server_key') | ||
| if [ -z $SERVER_CRT ] || [ -z $SERVER_KEY ]; then | ||
| TELEMETRY_ARGS+=" --insecure" | ||
| else | ||
| TELEMETRY_ARGS+=" --server_crt $SERVER_CRT --server_key $SERVER_KEY " | ||
| fi | ||
|
|
||
| CA_CRT=$(echo $X509 | jq -r '.ca_crt') | ||
| CA_CRT=$(extract_field "$X509" '.ca_crt') | ||
| if [ ! -z $CA_CRT ]; then | ||
| TELEMETRY_ARGS+=" --ca_crt $CA_CRT" | ||
| fi | ||
|
|
@@ -61,34 +63,26 @@ fi | |
| if [ -z "$GNMI" ]; then | ||
| PORT=8080 | ||
| else | ||
| PORT=$(echo $GNMI | jq -r '.port') | ||
| PORT=$(extract_field "$GNMI" '.port') | ||
| if ! [[ $PORT =~ ^[0-9]+$ ]]; then | ||
| echo "Incorrect port value ${PORT}, expecting positive integers" >&2 | ||
| exit $INCORRECT_TELEMETRY_VALUE | ||
| fi | ||
| fi | ||
| TELEMETRY_ARGS+=" --port $PORT" | ||
|
|
||
| CLIENT_AUTH=$(echo $GNMI | jq -r '.client_auth') | ||
| CLIENT_AUTH=$(extract_field "$GNMI" '.client_auth') | ||
| if [ -z $CLIENT_AUTH ] || [ $CLIENT_AUTH == "false" ]; then | ||
| TELEMETRY_ARGS+=" --allow_no_client_auth" | ||
| fi | ||
|
|
||
| LOG_LEVEL=$(echo $GNMI | jq -r '.log_level') | ||
| LOG_LEVEL=$(extract_field "$GNMI" '.log_level') | ||
| if [[ $LOG_LEVEL =~ ^[0-9]+$ ]]; then | ||
| TELEMETRY_ARGS+=" -v=$LOG_LEVEL" | ||
| else | ||
| TELEMETRY_ARGS+=" -v=2" | ||
| fi | ||
|
|
||
| if [ -nz "$GNMI" ]; then | ||
| ENABLE_CRL=$(echo $GNMI | jq -r '.enable_crl') | ||
| if [ $ENABLE_CRL == "true" ]; then | ||
| TELEMETRY_ARGS+=" --enable_crl" | ||
| fi | ||
|
|
||
| CRL_EXPIRE_DURATION=$(echo $GNMI | jq -r '.crl_expire_duration') | ||
| if [ -n $CRL_EXPIRE_DURATION ]; then | ||
| TELEMETRY_ARGS+=" --crl_expire_duration $CRL_EXPIRE_DURATION" | ||
| fi | ||
| fi | ||
|
|
||
| # gNMI save-on-set behavior is disabled by default. | ||
| # Save-on-set can be turned on by setting the "TELEMETRY|gnmi|save_on_set" | ||
| # to "true". | ||
|
|
@@ -98,7 +92,7 @@ if [ ! -z "$SAVE_ON_SET" ]; then | |
| fi | ||
|
|
||
| # Server will handle threshold connections consecutively | ||
| THRESHOLD_CONNECTIONS=$(echo $GNMI | jq -r '.threshold') | ||
| THRESHOLD_CONNECTIONS=$(extract_field "$GNMI" '.threshold') | ||
| if [[ $THRESHOLD_CONNECTIONS =~ ^[0-9]+$ ]]; then | ||
| TELEMETRY_ARGS+=" --threshold $THRESHOLD_CONNECTIONS" | ||
| else | ||
|
|
@@ -111,7 +105,7 @@ else | |
| fi | ||
|
|
||
| # Close idle connections after certain duration (in seconds) | ||
| IDLE_CONN_DURATION=$(echo $GNMI | jq -r '.idle_conn_duration') | ||
| IDLE_CONN_DURATION=$(extract_field "$GNMI" '.idle_conn_duration') | ||
| if [[ $IDLE_CONN_DURATION =~ ^[0-9]+$ ]]; then | ||
| TELEMETRY_ARGS+=" --idle_conn_duration $IDLE_CONN_DURATION" | ||
| else | ||
|
|
@@ -124,4 +118,25 @@ else | |
| fi | ||
| TELEMETRY_ARGS+=" -gnmi_native_write=false" | ||
|
|
||
| USER_AUTH=$(extract_field "$GNMI" '.user_auth') | ||
| if [ ! -z "$USER_AUTH" ] && [ $USER_AUTH != "null" ]; then | ||
| TELEMETRY_ARGS+=" --client_auth $USER_AUTH" | ||
|
|
||
| if [ $USER_AUTH == "cert" ]; then | ||
| # Reuse GNMI_CLIENT_CERT for telemetry service | ||
| TELEMETRY_ARGS+=" --config_table_name GNMI_CLIENT_CERT" | ||
|
|
||
| ENABLE_CRL=$(echo $GNMI | jq -r '.enable_crl') | ||
| if [ $ENABLE_CRL == "true" ]; then | ||
| TELEMETRY_ARGS+=" --enable_crl" | ||
| fi | ||
|
|
||
| CRL_EXPIRE_DURATION=$(extract_field "$GNMI" '.crl_expire_duration') | ||
| if [ ! -z "$CRL_EXPIRE_DURATION" ] && [ $CRL_EXPIRE_DURATION != "null" ]; then | ||
| TELEMETRY_ARGS+=" --crl_expire_duration $CRL_EXPIRE_DURATION" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| fi | ||
| fi | ||
| fi | ||
|
|
||
| echo "telemetry args: $TELEMETRY_ARGS" | ||
| exec /usr/sbin/telemetry ${TELEMETRY_ARGS} | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is it possible to command inject by malicious user? #Closed
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.
It's possible but it's out of scope of this PR.
There are lots of SONiC service start with bash script and Redis config.
This is a Redis permission issue.
Uh oh!
There was an error while loading. Please reload this page.
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.
This script start gnmi service with 'exec' command, which does not have injection issue, because it will 'replace' current bash process with telemetry process:
https://linux.die.net/man/1/bash
I verified with:
// change config_db to make /test after gnmi start
sonic-db-cli CONFIG_DB hset 'GNMI|certs' server_crt '/etc/sonic/telemetry/streamingtelemetryserver.cer && mkdir /test #'
The telemetry service will failed with a invalid command, but will not run the 'mkdir /test' part.
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.
suggest && -> || in test.
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.
Test with all these, not found security issue, this is because 'exec' will pass all content behind it as parameter, so && -> || will not break and run another command.
After change config DB, GNMI service can't start and the 'mkdir /test' command not execute:
root@vlab-01:/# sonic-db-cli CONFIG_DB hset 'GNMI|certs' server_crt '/etc/sonic/telemetry/streamingtelemetryserver.cer && mkdir /test #'
0
root@vlab-01:/# /usr/bin/gnmi-native.sh
/usr/bin/gnmi-native.sh: line 36: [: too many arguments
gnmi args: -logtostderr --server_crt /etc/sonic/telemetry/streamingtelemetryserver.cer && mkdir /test # --server_key '/etc/sonic/telemetry/streamingtelemetryserver.key' --ca_crt '/etc/sonic/telemetry/dsmsroot.cer' --port 50052 -v=2 --threshold 100 --idle_conn_duration 5
I0219 01:09:02.072311 878 telemetry.go:194] client_auth not provided, using defaults.
E0219 01:09:02.072380 878 telemetry.go:69] Unable to setup telemetry config due to err: port must be > 0.
root@vlab-01:/#
root@vlab-01:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin sonic srv sys tmp usr var
root@vlab-01:/# sonic-db-cli CONFIG_DB hset 'GNMI|certs' server_crt '/etc/sonic/telemetry/streamingtelemetryserver.cer || mkdir /test #'
0
root@vlab-01:/# /usr/bin/gnmi-native.sh
/usr/bin/gnmi-native.sh: line 36: [: too many arguments
gnmi args: -logtostderr --server_crt /etc/sonic/telemetry/streamingtelemetryserver.cer || mkdir /test # --server_key /etc/sonic/telemetry/streamingtelemetryserver.key --ca_crt /etc/sonic/telemetry/dsmsroot.cer --port 50052 -v=2 --threshold 100 --idle_conn_duration 5
I0219 01:06:40.599396 757 telemetry.go:194] client_auth not provided, using defaults.
E0219 01:06:40.599477 757 telemetry.go:69] Unable to setup telemetry config due to err: port must be > 0.
root@vlab-01:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin sonic srv sys tmp usr var
root@vlab-01:/# sonic-db-cli CONFIG_DB hset 'GNMI|certs' server_crt '/etc/sonic/telemetry/streamingtelemetryserver.cer -> mkdir /test #'
0
root@vlab-01:/# /usr/bin/gnmi-native.sh
/usr/bin/gnmi-native.sh: line 35: [: syntax error: `->' unexpected
gnmi args: -logtostderr --server_crt /etc/sonic/telemetry/streamingtelemetryserver.cer -> mkdir /test # --server_key /etc/sonic/telemetry/streamingtelemetryserver.key --ca_crt /etc/sonic/telemetry/dsmsroot.cer --port 50052 -v=2 --threshold 100 --idle_conn_duration 5
flag provided but not defined: ->
Usage of /usr/sbin/telemetry:
-allow_no_client_auth
root@vlab-01:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin sonic srv sys tmp usr var