Skip to content
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

ROX-8830: Respect local overwrites of variables in BASH_ENV #99

Merged
merged 27 commits into from
Jan 6, 2022

Conversation

vikin91
Copy link
Contributor

@vikin91 vikin91 commented Dec 22, 2021

This PR:

  • Fixes variable shadowing problem (see description of the problem below)
  • Adds regression tests for variable shadowing problem
  • Changes CI step "Create commit in stackrox that updates the base image" to update the existing branch instead of overwriting it with force.

This is (a part of) the proper fix for the workaround introduced in stackrox/stackrox#180

Variable shadowing problem

This PR fixes the problem defined as follows:

$ cat /etc/bash.env
export FOO="bar"
export MAIN="3"
$ cat ~/test.sh
#!/usr/bin/env bash

echo "FOO: $FOO"
echo "MAIN: $MAIN"
$ export BASH_ENV=/etc/bash.env
$ ~/test.sh
FOO: bar
MAIN: 3

$ FOO=x ~/test.sh
FOO: bar  # <<< error, should be FOO=x
MAIN: 3

This faulty behavior results in broken master as described in ROX-8821

Fix

The fix is short and its intention should be clear when looking at the diff.

We modify cci-export in such a way, so that it respects the existing value of env variable. This changes the priority hierarchy from:

  1. cci-export FOO bar (i.e., contents of $BASH_ENV)
  2. export FOO=bar
  3. FOO=x ./script.sh

Into:

  1. FOO=x ./script.sh
  2. export FOO=bar
  3. cci-export FOO bar (i.e., contents of $BASH_ENV)

We obtain this by changing the contents of $BASH_ENV produced by

export CIRCLECI=true 
cci-export FOO bar

from:

export FOO=bar

into

export FOO="${FOO:-bar}"

however, there can be maximally one export VAR line for each VAR, because othwerwise, subsequent cci-export calls would not work as expected.

How tested

@vikin91 vikin91 changed the title ROX-8821: Revert parts of #96 related to BASH_ENV ROX-8830: Revert parts of #96 related to BASH_ENV Dec 22, 2021
@vikin91 vikin91 changed the title ROX-8830: Revert parts of #96 related to BASH_ENV ROX-8830: Respect local overwrites of variables in BASH_ENV Dec 22, 2021
@vikin91 vikin91 force-pushed the pr/fix-bash-env-shadowing branch from c2743b8 to e8c3d68 Compare December 27, 2021 15:43
@vikin91 vikin91 marked this pull request as ready for review December 27, 2021 15:49
echo >&2 "Env var BASH_ENV not properly set"
return 1
fi
echo "export ${key}=$(printf '%q' "$value")" >> "$BASH_ENV"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my limited understanding of the issue, the problem is that BASH_ENV contents may override whatever is set in the environment before bash process is launched.

  1. Parent process exports environment variables.
  2. Parent process starts real bash (through our wrappers but that doesn't matter).
  3. bash sees BASH_ENV and sources that file.
  4. BASH_ENV overwrites environment variables if their names match the ones set by the parent process.

If we assume, we don't want overwriting only for values that we put into $BASH_ENV with our cci-export function, could we simply modify this specific line from

echo "export ${key}=$(printf '%q' "$value")" >> "$BASH_ENV"

to

echo "export ${key}=\${${key}:-$(printf '%q' "$value")}" >> "$BASH_ENV"

Warning: this needs testing if escaping works as it should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct, I added this in line 39.

This solution requires handling one edge-case: cci-exporting the same key twice with different values. In order to make this work properly, I needed to add line 35: remove_lines_starting_with "$BASH_ENV" "$(printf 'export %q="${%q:-' "$key" "$key")". There are unit tests in this PR to cover this edge-case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I did not even think about it. Let me take another look at the current approach.

Copy link
Contributor

@msugakov msugakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into tests after we converge on the main change.

images/Dockerfile.test.cci-export Outdated Show resolved Hide resolved
images/static-contents/bin/bash-wrapper Outdated Show resolved Hide resolved
images/static-contents/bin/bash-wrapper Outdated Show resolved Hide resolved
images/static-contents/bin/bash-wrapper Outdated Show resolved Hide resolved
ec25484 Revert parts of #96 related to BASH_ENV
8ca7ad5 Revert "Revert parts of #96 related to BASH_ENV"
213f065 Propose candidate no.1 for a proper fix
7f80506 Why bash-wrapper is not being updated?
afe22b3 Undo debug and TODO statements
e2a78e5 Allov cci-export to handle values containing whitespaces
6df0296 Add unit-tests for cci-export
c950ade Do not force-push updated branch
1ee91e0 Fix bug in cci-export
519cc00 Add circleci job running tests
22252c6 Provide separate slim image for testing cci-export
9e9a93a Fix path of Dockerfile
d481aac Change machine type for running the tests in CI
b3435ed Fix missing code checkout
ce7f03b Fix: Add missing auth
8ad7325 Fix: Add missing context
da0e58e Fix permissions for bash.env
1ec50d4 Fix permissions of test folder
fe774ac Try without setup_file
3ab6bba Add bash.env to Dockerfile
cc1a485 Provide temp file to sed
32b55d6 Provide BASH_ENV dynamically
7ddf038 Add testcase
d83548d Do not assert_success of printer
d653b83 Try updating created PRs instead ov overwriting with force
@vikin91 vikin91 force-pushed the pr/fix-bash-env-shadowing branch from 72f01f1 to 2439030 Compare January 4, 2022 08:32
@vikin91
Copy link
Contributor Author

vikin91 commented Jan 5, 2022

Thanks for the comments, I addressed some of them so far, but I will ping you when I am ready with the rest.
I want to move now to RS-382 for an hour or two (as long as I have the topic fresh in my head).

.circleci/config.yml Outdated Show resolved Hide resolved
images/test/bats/FILE Outdated Show resolved Hide resolved
@vikin91
Copy link
Contributor Author

vikin91 commented Jan 5, 2022

Okay, I think that #101 may take a bit longer and it is not crucial at all at the moment.
Okay to merge this now and move the priority back to the helm topic, @msugakov ?

I would then continue on #101 when we have less time pressure.

Copy link
Contributor

@viswajithiii viswajithiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@msugakov
Copy link
Contributor

msugakov commented Jan 5, 2022

I'm certainly ok to merge this without #101. I'm a bit 😿 about postponing #101 because I thought it would not be that difficult.

@vikin91
Copy link
Contributor Author

vikin91 commented Jan 5, 2022

Let's see how much we postpone :) Maybe only several hours :)

@vikin91 vikin91 merged commit 22a63f8 into master Jan 6, 2022
@vikin91 vikin91 deleted the pr/fix-bash-env-shadowing branch January 6, 2022 10:51
vikin91 added a commit that referenced this pull request Jan 12, 2022
* X-Smart-Branch-Parent: master
* Fix issue ROX-8821

Co-authored-by: msugakov <[email protected]>
vikin91 added a commit that referenced this pull request Jan 13, 2022
f27bebd WIP: Separate common base
1ec38ef Try using base image for cci-export tester and rox
1fb1e7b Fix: Add missing build-and-push-base, add missing --build-args
1e14b10 Fix docker build --build-arg syntax
a779455 Fix computing BASE_TAG
2b8f8f0 Do not use image-suffix
56cc4e1 Fix: add required context to test-cci-export
30daa3d Remove -base suffix
bb11ab8 Fix: rename tag: base-snapshot to snapshot-base
3720b28 Fix: Login to quay before building cci-export tester
f2ea426 Remove redundant requirement for step 'build-and-push-rox'
41d7ea2 Make base a dependency of env-check. Move common parts to base
6145aef Fix missing docker login
af38330 Shuffle shared parts between base and rox Dockerfile
b3fdf0b Fix docker login issue
6e0a41c Move ARG BASE_TAG to the top
5b9c829 Try to separate roksdb as well
2ebd77d Make roksdb a dependency to rox
60e9a53 Fix typo
4e6f7c4 Fix other typos and mistakes (concentration decreases)
6013df6 Update ca certs
2dde39d Try to build rocksdb image only once per version
8438734 Assign ROCKSDB_VERSION from ARG to ENV
755fd6c Fix confusion with tag_prefix and base_tag
159f4a8 Fix pushing built image
a98e13d Fix variable TAG
2af3cd5 Use newer remote_docker that supports 'docker manifest inspect'
917f82e Do not remove wget
b8e52c4 No need to COPY --from=base
2e19b09 Undo adding '--no-install-recommends' to one apt-get install command
607f422 Promote rocksdb_version to a pipeline parameter
96243f0 Try using different pushed_image file to avoid conflicts
be93611 Try moving attach_workspace to pre_steps
d4e1521 Revert "Try moving attach_workspace to pre_steps"
065e1ca Ensure pushed-image-file is unique to prevent 'Concurrent upstream...' error
0889271 ROX-8830: Respect local overwrites of variables in BASH_ENV (#99)
4376a2d Move yarn to base. Cleanup installs in the first and second RUN
edbc52c Remove duplicate installs in rox
2fb53d6 Document reasons for separating rocksdb.Dockerfile and resp. build task
22c1318 Hadolint-warning: use WORKDIR in env-check.Dockerfile
4d8fa15 Hadolint: Pin versions for apt-get in base.Dockerfile
1d04e6c Hadolint: Fix warning SC2067 in base.Dockerfile
ed9138d Hadolint: Fix DL3042
5508757 Hadolint: Fix DL3004 and DL4001
6c1cf1b Add hadolint ignore with reasons
6483d14 Fix vrong version pin (copy-paste error)
a99b269 Fix inline-comments and quotes
7675b6e Fix find -exec part
c831fc1 Add gosu alongside with sudo to base
ca321a0 Minor: Remove unhelpful comment
30428c7 Fix gosu usage
8fa071b Do not use gosu
8e4f651 Revert pinning in base.Dockerfile
f2b8594 Add .hadolint.yaml to configure ingore rules for this repo
30cc746 Rewrite build-and-push-image to avoid too many --build-args
173965b Add missing use-base params to build-and-push-image
98ee1e1 Remove foo-printer from old location (rebase leftover)
0b61702 Remove duplicate job (clean rebase leftover)
31128a2 ROX-8834: Automatically open test-PRs also against collector and scanner (#97)
e55c856 Merge branch 'master' into pr/ROX-8830-common-container-base
vikin91 added a commit that referenced this pull request Jan 14, 2022
f27bebd WIP: Separate common base
1ec38ef Try using base image for cci-export tester and rox
1fb1e7b Fix: Add missing build-and-push-base, add missing --build-args
1e14b10 Fix docker build --build-arg syntax
a779455 Fix computing BASE_TAG
2b8f8f0 Do not use image-suffix
56cc4e1 Fix: add required context to test-cci-export
30daa3d Remove -base suffix
bb11ab8 Fix: rename tag: base-snapshot to snapshot-base
3720b28 Fix: Login to quay before building cci-export tester
f2ea426 Remove redundant requirement for step 'build-and-push-rox'
41d7ea2 Make base a dependency of env-check. Move common parts to base
6145aef Fix missing docker login
af38330 Shuffle shared parts between base and rox Dockerfile
b3fdf0b Fix docker login issue
6e0a41c Move ARG BASE_TAG to the top
5b9c829 Try to separate roksdb as well
2ebd77d Make roksdb a dependency to rox
60e9a53 Fix typo
4e6f7c4 Fix other typos and mistakes (concentration decreases)
6013df6 Update ca certs
2dde39d Try to build rocksdb image only once per version
8438734 Assign ROCKSDB_VERSION from ARG to ENV
755fd6c Fix confusion with tag_prefix and base_tag
159f4a8 Fix pushing built image
a98e13d Fix variable TAG
2af3cd5 Use newer remote_docker that supports 'docker manifest inspect'
917f82e Do not remove wget
b8e52c4 No need to COPY --from=base
2e19b09 Undo adding '--no-install-recommends' to one apt-get install command
607f422 Promote rocksdb_version to a pipeline parameter
96243f0 Try using different pushed_image file to avoid conflicts
be93611 Try moving attach_workspace to pre_steps
d4e1521 Revert "Try moving attach_workspace to pre_steps"
065e1ca Ensure pushed-image-file is unique to prevent 'Concurrent upstream...' error
0889271 ROX-8830: Respect local overwrites of variables in BASH_ENV (#99)
4376a2d Move yarn to base. Cleanup installs in the first and second RUN
edbc52c Remove duplicate installs in rox
2fb53d6 Document reasons for separating rocksdb.Dockerfile and resp. build task
22c1318 Hadolint-warning: use WORKDIR in env-check.Dockerfile
4d8fa15 Hadolint: Pin versions for apt-get in base.Dockerfile
1d04e6c Hadolint: Fix warning SC2067 in base.Dockerfile
ed9138d Hadolint: Fix DL3042
5508757 Hadolint: Fix DL3004 and DL4001
6c1cf1b Add hadolint ignore with reasons
6483d14 Fix vrong version pin (copy-paste error)
a99b269 Fix inline-comments and quotes
7675b6e Fix find -exec part
c831fc1 Add gosu alongside with sudo to base
ca321a0 Minor: Remove unhelpful comment
30428c7 Fix gosu usage
8fa071b Do not use gosu
8e4f651 Revert pinning in base.Dockerfile
f2b8594 Add .hadolint.yaml to configure ingore rules for this repo
30cc746 Rewrite build-and-push-image to avoid too many --build-args
173965b Add missing use-base params to build-and-push-image
98ee1e1 Remove foo-printer from old location (rebase leftover)
0b61702 Remove duplicate job (clean rebase leftover)
31128a2 ROX-8834: Automatically open test-PRs also against collector and scanner (#97)
e55c856 Merge branch 'master' into pr/ROX-8830-common-container-base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants