-
Notifications
You must be signed in to change notification settings - Fork 4
Escape \ in environment variable
#168
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
Martin-Zeithaml
wants to merge
22
commits into
v3.x/staging
Choose a base branch
from
v3.x/bugfix/envs
base: v3.x/staging
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 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f9c9277
Escape backslash too
Martin-Zeithaml b4f640c
Typo
Martin-Zeithaml 5c16b7a
Code change
Martin-Zeithaml aad3151
PR number update in CHLOG
Martin-Zeithaml 426641a
Minor formatting changes
Martin-Zeithaml 925237d
Config for testing
Martin-Zeithaml b8a4612
Grep update
Martin-Zeithaml 8ca3524
More test cases
Martin-Zeithaml 7f13d3d
Variable indentation
Martin-Zeithaml e849474
Yaml comment
Martin-Zeithaml 850b235
More tests
Martin-Zeithaml 9e7921d
Test if launcher exist
Martin-Zeithaml ada8daa
Update readme
Martin-Zeithaml 6704488
Typo in var name
Martin-Zeithaml fb5ae68
Unix variable names bug
Martin-Zeithaml fd9d386
zowe.runtimeDirectory indentation
Martin-Zeithaml 06fd44b
Readme update
Martin-Zeithaml ebb77c2
Add rc to comment
Martin-Zeithaml f08dd70
Test updated
Martin-Zeithaml 8116dc2
ESJ test cases
Martin-Zeithaml d1f10ee
Update based on code review
Martin-Zeithaml 68c770b
Merge branch 'v3.x/staging' into v3.x/bugfix/envs
Martin-Zeithaml 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 |
|---|---|---|
|
|
@@ -436,15 +436,19 @@ static bool arrayListContains(ArrayList *list, char *element) { | |
| static char* escape_string(char *input) { | ||
| int length = strlen(input); | ||
| int quotes = 0; | ||
| int backSlashes = 0; | ||
| for (int i = 0; i < length; i++) { | ||
| if (input[i] == '\"') quotes++; | ||
| if (input[i] == '\\') backSlashes++; | ||
| } | ||
|
|
||
| char *output = malloc(length + quotes + 2 + 1); // add quote on first and the last position and escape quotes inside | ||
| // 2 + 1 = add quote on first and the last position and \0 at the end | ||
| // quotes & backSlashes = add '\' to escape '"' and '\' | ||
| char *output = malloc(length + quotes + backSlashes + 2 + 1); | ||
| output[0] = '\"'; | ||
| int j = 1; | ||
| for (int i = 0; i < length; i++) { | ||
| if (input[i] == '\"') { | ||
| if (input[i] == '\"' || input[i] == '\\') { | ||
| output[j++] = '\\'; | ||
| } | ||
| output[j++] = input[i]; | ||
|
|
@@ -464,7 +468,7 @@ static char* jsonToString(Json *json) { | |
| return jsonAsBoolean(json) ? "true" : "false"; | ||
| case JSON_TYPE_NUMBER: | ||
| case JSON_TYPE_INT64: | ||
| output = malloc(21); // Longest string possible -9223372036854775807 | ||
| output = malloc(21); // Longest string possible -9223372036854775808 (20+\0) | ||
| snprintf(output, 21, "%ld", jsonAsInt64(json)); | ||
| return output; | ||
| case JSON_TYPE_DOUBLE: | ||
|
|
@@ -476,11 +480,19 @@ static char* jsonToString(Json *json) { | |
| } | ||
| } | ||
|
|
||
| static bool is_valid_key(char *key) { | ||
| // Zowe.environments key must follow Unix variable name syntax: | ||
| // alphaNum | underscore & first char is not a digit | ||
| static bool is_key_valid_unix_name(char *key) { | ||
|
||
| int length = strlen(key); | ||
| if (!length) { | ||
| return false; | ||
| } | ||
| if (isdigit(key[0])) { | ||
| return false; | ||
| } | ||
| for (int i = 0; i < length; i++) { | ||
| if (isalnum(key[i])) continue; | ||
| if (strchr("_-", key[i])) continue; | ||
| if (key[i] == '_') continue; | ||
| return false; | ||
| } | ||
| return true; | ||
|
|
@@ -536,8 +548,8 @@ static void set_shared_uss_env(ConfigManager *configmgr) { | |
| // Get all environment variables defined in zowe.yaml and put them in the output as they are | ||
| for (JsonProperty *property = jsonObjectGetFirstProperty(object); property != NULL; property = jsonObjectGetNextProperty(property)) { | ||
| char *key = jsonPropertyGetKey(property); | ||
| if (!is_valid_key(key)) { | ||
| WARN("Key in zowe.yaml `zowe.environments.%s` is invalid and it will be ignored\n", key); | ||
| if (!is_key_valid_unix_name(key)) { | ||
| WARN("Key in configuration `zowe.environments.%s` is invalid and it will be ignored\n", key); | ||
| continue; | ||
| } | ||
|
|
||
|
|
||
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
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,76 @@ | ||
| #!/bin/sh | ||
|
|
||
| # This program and the accompanying materials are | ||
| # made available under the terms of the Eclipse Public License v2.0 which accompanies | ||
| # this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html | ||
| # | ||
| # SPDX-License-Identifier: EPL-2.0 | ||
| # | ||
| # Copyright Contributors to the Zowe Project. | ||
|
|
||
| # Start with any parameter -> prints Launcher's output | ||
| # rc of this = number of errors found | ||
|
|
||
| print= | ||
| errors=0 | ||
|
|
||
| if [ ! -z "${1}" ]; then | ||
| print='1' | ||
| fi | ||
|
|
||
| LAUNCHER='../bin/zowe_launcher' | ||
| if [ ! -f "${LAUNCHER}" ]; then | ||
| echo "Executable \"${LAUNCHER}\" not found." | ||
| exit 1 | ||
| fi | ||
|
|
||
| ZOWE_YAML="./files/zowe.environments.yaml" | ||
|
|
||
| LAUNCHER_OUTPUT= | ||
| LAUNCHER_OUTPUT_TEST_VAR= | ||
| LAUNCHER_OUTPUT_KEYS= | ||
| export ZLDEBUG='ON' | ||
| export CONFIG="FILE(${ZOWE_YAML})" | ||
|
|
||
| LAUNCHER_OUTPUT=$("${LAUNCHER}" "ha1" 2>&1) | ||
|
|
||
| if [ ! -z "${print}" ]; then | ||
| printf "%s" "${LAUNCHER_OUTPUT}" | ||
| fi | ||
|
|
||
| printf "\n---zowe.environments.TEST_VAR_* expected to be used or ignored ---\n\n" | ||
| # awk prints between TEST_VAR_START= and TEST_VAR_END= | ||
| LAUNCHER_OUTPUT_TEST_VAR=$(printf "%s" "${LAUNCHER_OUTPUT}" | awk '/TEST_VAR_START=/{flag=1}/TEST_VAR_END=/{print; flag=0}flag') | ||
| while read -r line; do | ||
| if [ -n "$(echo "${line}" | grep 'TEST_VAR_')" ]; then | ||
| key=$(echo "${line}" | cut -d: -f1) | ||
| if [ -n "$(echo "${line}" | grep '#')" ]; then | ||
| output=$(echo "${LAUNCHER_OUTPUT_TEST_VAR}" | grep "${key}") | ||
| if [ -n "${output}" ]; then | ||
| printf "OK: %s\n" "${output}" | ||
| else | ||
| echo "Key '${key}' not found!" | ||
| errors=`expr $errors + 1` | ||
| fi | ||
|
|
||
| fi | ||
| fi | ||
| done < "${ZOWE_YAML}" | ||
|
|
||
| printf "\n---zowe.environments.* expected to be ignored ---\n\n" | ||
| LAUNCHER_OUTPUT_KEYS=$(printf "%s" "${LAUNCHER_OUTPUT}" | grep -e 'Key in configuration') | ||
| while read -r line; do | ||
| if [ -n "$(echo "${line}" | grep -e '<ignored>')" ]; then | ||
| key="$(echo "${line}" | awk -F: '{ print $1}')" | ||
| key="\`zowe.environments.${key}\`" | ||
| matchKey="$(printf "%s" "${LAUNCHER_OUTPUT_KEYS}" | grep -e "${key}")" | ||
| if [ -n "${matchKey}" ]; then | ||
| printf "OK: Key '%s' found: %s\n" "${key}" "${matchKey}" | ||
| else | ||
| echo "Key '${key}' not found!" | ||
| errors=`expr $errors + 1` | ||
| fi | ||
| fi | ||
| done < "${ZOWE_YAML}" | ||
|
|
||
| exit $errors |
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,14 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "$id": "https://zowe.org/schemas/v2/server-common", | ||
| "title": "Common types", | ||
| "description": "Simplified version for testing", | ||
| "$defs": { | ||
| "semverVersion": { | ||
| "$anchor": "zoweSemverVersion", | ||
| "type": "string", | ||
| "description": "A semantic version, see https://semver.org/", | ||
| "pattern": "^[0-9]*\\.[0-9]*\\.[0-9]*(-*[a-zA-Z][0-9a-zA-Z\\-\\.]*)?(\\+[0-9a-zA-Z\\-\\.]*)?$" | ||
| } | ||
| } | ||
| } |
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,13 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "$id": "https://zowe.org/schemas/v2/server-base", | ||
| "title": "Zowe configuration file", | ||
| "description": "Simplified version for testing", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "zowe": { | ||
| "type": "object" | ||
| } | ||
| } | ||
| } |
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,82 @@ | ||
| zowe: | ||
| environments: | ||
| TEST_VAR_START: \S\T\A\R\T # "\\S\\T\\A\\R\\T" | ||
| # *** Bool *** | ||
| TEST_VAR_BOOL_01: true # true | ||
| TEST_VAR_BOOL_02: false # false | ||
| TEST_VAR_BOOL_03: !!bool true # true | ||
| TEST_VAR_BOOL_04: !!bool false # false | ||
| # *** Int *** | ||
| TEST_VAR_INT_01: 123456 # 123456 | ||
| TEST_VAR_INT_02: !!int 123456 # 123456 | ||
| TEST_VAR_INT_03: 0x7FFFFFFF # 2147483647 | ||
| TEST_VAR_INT_04: 0x00000001 # 1 | ||
| TEST_VAR_INT_05: !!int 0x7FFFFFFF # 2147483647 | ||
| TEST_VAR_INT_06: !!int 0x00000001 # 1 | ||
| TEST_VAR_INT_07: 9223372036854775807 # 9223372036854775807 | ||
| TEST_VAR_INT_08: !!int 9223372036854775807 # 9223372036854775807 | ||
| TEST_VAR_INT_09: -9223372036854775808 # -9223372036854775808 | ||
| TEST_VAR_INT_10: !!int -9223372036854775808 # -9223372036854775808 | ||
| # *** Str *** | ||
| TEST_VAR_STR_01: \\\\\\\\\\ # "\\\\\\\\\\\\\\\\\\\\" | ||
| TEST_VAR_STR_02: !!str \\\\\\\\\\ # "\\\\\\\\\\\\\\\\\\\\" | ||
| TEST_VAR_STR_03: '""""""""""' # "\"\"\"\"\"\"\"\"\"\"" | ||
| TEST_VAR_STR_04: '"' # "\"" | ||
| TEST_VAR_STR_05: \n # "\\n" | ||
| TEST_VAR_STR_06: \0 # "\\0" | ||
| TEST_VAR_STR_07: 'TE\"ST' # "TE\\\"ST" | ||
| TEST_VAR_STR_08: "TE\"ST" # "TE\"ST" | ||
| TEST_VAR_STR_09: TE\"ST # "TE\\\"ST" | ||
| TEST_VAR_STR_10: !!str 123 # "123" | ||
| TEST_VAR_STR_11: !!str true # "true" | ||
| TEST_VAR_STR_12: |- # William Shakespeare: All's Well That Ends Well | ||
| Love all, trust a few, | ||
| Do wrong to none: be able for thine enemy | ||
| Rather in power than use; and keep thy friend | ||
| Under thy own life's key: be check'd for silence, | ||
| But never tax'd for speech. | ||
| # *** Null *** | ||
| TEST_VAR_NULL_01: | ||
| TEST_VAR_NULL_02: ~ | ||
| TEST_VAR_NULL_03: null | ||
| # *** Array *** | ||
| TEST_VAR_ARRAY_01: | ||
| - kiwi | ||
| - banana | ||
| - orange | ||
| TEST_VAR_ARRAY_02: ['A', 'B', 'C'] | ||
| TEST_VAR_NESTED: | ||
| enabled: true | ||
| disabled: false | ||
| # *** Embedded JS *** | ||
| TEST_VAR_EJS_01: "${{ zowe.environments.TEST_VAR_INT_01 }}" # zowe.environments.TEST_VAR_INT_01 | ||
| TEST_VAR_EJS_02: "${{ 1+1*7 }}" # 8 | ||
| TEST_VAR_EJS_03: "${{ 'EJS' }}" # "EJS" | ||
| TEST_VAR_EJS_04: "${{ `EJS` }}" # "EJS" | ||
| TEST_VAR_EJS_05: "${{ \"EJS\" }}" # "EJS" | ||
| TEST_VAR_EJS_06: "${{ `E` + 'J' + \"S\" }}" # "EJS" | ||
| TEST_VAR_EJS_07: "${{ `\\` }}" # "\\" | ||
| TEST_VAR_EJS_08: "${{ /* # true */ | ||
| () => { if (1 == 1) return true } (); | ||
| }}" | ||
| TEST_VAR_END: \E\N\D # "\\E\\N\\D" | ||
| # *** Ignored keys *** | ||
| -key-: ~ # <ignored> | ||
| ?key: '?' # <ignored> | ||
| 1+1: =2 # <ignored> | ||
| 1-1: "=0" # <ignored> | ||
| _!_: '!!!' # <ignored> | ||
| 1TEST: true # <ignored> | ||
| 3_141592: 6535 # <ignored> | ||
| (): !!int 0 # <ignored> | ||
| ~null: not null # <ignored> | ||
| =: equal sign # <ignored> | ||
| <h1>: heading # <ignored> | ||
| runtimeDirectory: "${{ () => { | ||
| let thisPath = os.realpath('./fakeRuntime'); | ||
| if (thisPath[1] == 0) { | ||
| return thisPath[0]; | ||
| } | ||
| return undefined; | ||
| } () | ||
| }}" |
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.
The precedence of the operators is not very obvious. Could you add parenthesis?
(alphaNum | underscore) & first char is not a digitOr just write it like this: