|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
| 3 | +EXIT_P4RT_VARS_FILE_NOT_FOUND=1 |
| 4 | +readonly P4RT_VARS_FILE=/usr/share/sonic/templates/p4rt_vars.j2 |
| 5 | + |
| 6 | +if [ ! -f "${P4RT_VARS_FILE}" ]; then |
| 7 | + echo "P4rt vars template file not found" |
| 8 | + exit ${EXIT_P4RT_VARS_FILE_NOT_FOUND} |
| 9 | +fi |
| 10 | + |
| 11 | +# Try to read p4rt and certs config from ConfigDB. |
| 12 | +# Use default value if no valid config exists |
| 13 | +P4RT_VARS=$(sonic-cfggen -d -t ${P4RT_VARS_FILE}) |
| 14 | +readonly P4RT_VARS=${P4RT_VARS//[\']/\"} |
| 15 | +readonly X509=$(echo ${P4RT_VARS} | jq -r '.x509') |
| 16 | +readonly P4RT=$(echo ${P4RT_VARS} | jq -r '.p4rt') |
| 17 | +readonly CERTS=$(echo ${P4RT_VARS} | jq -r '.certs') |
| 18 | + |
3 | 19 | P4RT_ARGS=" --alsologtostderr --logbuflevel=-1"
|
4 | 20 |
|
| 21 | +if [ -n "${CERTS}" ]; then |
| 22 | + readonly SERVER_CRT=$(echo ${CERTS} | jq -r '.server_crt // empty') |
| 23 | + readonly SERVER_KEY=$(echo ${CERTS} | jq -r '.server_key // empty') |
| 24 | + if [ -z "${SERVER_CRT}" ] || [ -z "${SERVER_KEY}" ]; then |
| 25 | + P4RT_ARGS+=" --use_insecure_server_credentials" |
| 26 | + else |
| 27 | + P4RT_ARGS+=" --server_certificate_file=${SERVER_CRT} --server_key_file=${SERVER_KEY}" |
| 28 | + fi |
| 29 | + |
| 30 | + readonly CA_CRT=$(echo ${CERTS} | jq -r '.ca_crt // empty') |
| 31 | + if [ ! -z "${CA_CRT}" ]; then |
| 32 | + P4RT_ARGS+=" --ca_certificate_file=${CA_CRT}" |
| 33 | + readonly CRL=$(echo ${CERTS} | jq -r '.cert_crl_dir // empty') |
| 34 | + if [ ! -z "$CRL" ]; then |
| 35 | + P4RT_ARGS+=" --cert_crl_dir=${CRL}" |
| 36 | + fi |
| 37 | + fi |
| 38 | +elif [ -n "${X509}" ]; then |
| 39 | + readonly SERVER_CRT=$(echo ${X509} | jq -r '.server_crt // empty') |
| 40 | + readonly SERVER_KEY=$(echo ${X509} | jq -r '.server_key // empty') |
| 41 | + if [ -z "${SERVER_CRT}" ] || [ -z "${SERVER_KEY}" ]; then |
| 42 | + P4RT_ARGS+=" --use_insecure_server_credentials" |
| 43 | + else |
| 44 | + P4RT_ARGS+=" --server_certificate_file=${SERVER_CRT} --server_key_file=${SERVER_KEY}" |
| 45 | + fi |
| 46 | + |
| 47 | + readonly CA_CRT=$(echo ${X509} | jq -r '.ca_crt // empty') |
| 48 | + if [ ! -z "${CA_CRT}" ]; then |
| 49 | + P4RT_ARGS+=" --ca_certificate_file=${CA_CRT}" |
| 50 | + readonly CRL=$(echo ${X509} | jq -r '.cert_crl_dir // empty') |
| 51 | + if [ ! -z "$CRL" ]; then |
| 52 | + P4RT_ARGS+=" --cert_crl_dir=${CRL}" |
| 53 | + fi |
| 54 | + fi |
| 55 | +else |
| 56 | + P4RT_ARGS+=" --use_insecure_server_credentials" |
| 57 | +fi |
| 58 | + |
| 59 | +# Try to read P4RT authorization config from ConfigDB. |
| 60 | +readonly AUTHZ_FILE=$(echo ${P4RT} | jq -r '.authz_policy // empty') |
| 61 | +if [ ! -z "${AUTHZ_FILE}" ]; then |
| 62 | + P4RT_ARGS+=" --authz_policy_enabled --authorization_policy_file=${AUTHZ_FILE}" |
| 63 | +fi |
| 64 | + |
| 65 | +# Try to read P4RT port config from ConfigDB. |
| 66 | +readonly PORT=$(echo ${P4RT} | jq -r '.port // empty') |
| 67 | +if [ ! -z "${PORT}" ]; then |
| 68 | + P4RT_ARGS+=" --p4rt_grpc_port=${PORT}" |
| 69 | +fi |
| 70 | + |
| 71 | +# Try to read P4RT genetlink config from ConfigDB. |
| 72 | +readonly GENETLINK=$(echo ${P4RT} | jq -r '.use_genetlink // empty') |
| 73 | +if [ ! -z "${GENETLINK}" ]; then |
| 74 | + P4RT_ARGS+=" --use_genetlink=${GENETLINK}" |
| 75 | +fi |
| 76 | + |
| 77 | +# Try to read P4RT port ID config from ConfigDB. |
| 78 | +readonly PORT_ID=$(echo ${P4RT} | jq -r '.use_port_ids // empty') |
| 79 | +if [ ! -z "${PORT_ID}" ]; then |
| 80 | + P4RT_ARGS+=" --use_port_ids=${PORT_ID}" |
| 81 | +fi |
| 82 | + |
| 83 | +# Try to read P4RT save forwarding config from ConfigDB. |
| 84 | +readonly SAVE_FORWARDING_CONFIG=$(echo ${P4RT} | jq -r '.save_forwarding_config_file // empty') |
| 85 | +if [ ! -z "${SAVE_FORWARDING_CONFIG}" ]; then |
| 86 | + P4RT_ARGS+=" --save_forwarding_config_file=${SAVE_FORWARDING_CONFIG}" |
| 87 | +fi |
| 88 | + |
| 89 | +# Try to read P4RT unix socket config from ConfigDB. |
| 90 | +readonly UNIX_SOCKET=$(echo ${P4RT} | jq -r '.p4rt_unix_socket // empty') |
| 91 | +if [ ! -z "${UNIX_SOCKET}" ]; then |
| 92 | + P4RT_ARGS+=" --p4rt_unix_socket=${UNIX_SOCKET}" |
| 93 | +fi |
| 94 | + |
5 | 95 | exec /usr/local/bin/p4rt ${P4RT_ARGS}
|
0 commit comments