Skip to content

Inherit environment variables, volumes, and volume mounts from parent in cascading scans#538

Merged
J12934 merged 20 commits intosecureCodeBox:mainfrom
EndPositive:cascading-scan-spec-merge-inherit
Jul 22, 2021
Merged

Inherit environment variables, volumes, and volume mounts from parent in cascading scans#538
J12934 merged 20 commits intosecureCodeBox:mainfrom
EndPositive:cascading-scan-spec-merge-inherit

Conversation

@EndPositive
Copy link
Contributor

@EndPositive EndPositive commented Jul 3, 2021

Description

This PR ensures that child scans inherit environment variables, volumes, and volume mounts (hereafter named as 'spec') from their parent scan (499c077, 0f224fa). The spec from the applied cascading rule is merged with those from the parent scan (cascading rule spec takes precedence 3391d74). Finally, to ensure that the spec from the cascading rule is only applied to the currently matched scan (and not its children), the child scan purges the cascading rule spec from the parent scan when inheriting them (b7b1856).

This behavior is useful in cases where you want to set the http_proxy variable for cascaded scans or when you want to mount CA certificates into cascaded scans.

I also considered retrieving the spec from the root scan, but currently there's no way (to my knowledge) to accurately determine the root scan in the cascading scan hook).

Checklist

  • Test your changes as thoroughly as possible before you commit them. Preferably, automate your test by unit/integration tests.
  • Make sure npm test runs for the whole project.
  • Make codeclimate checks happy

…cading rule

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
…cascading rule

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
@J12934
Copy link
Member

J12934 commented Jul 9, 2021

Hi, sorry for the late feedback, has been a busy week :)

I think this makes sense and this is a valid usecase, but I don't think this should be enabled, but should be opted in by a setting on the scan, just like the inherit Labels and inherit Annotations settings: https://github.com/secureCodeBox/secureCodeBox/blob/main/operator/apis/execution/v1/scan_types.go#L19

apiVersion: "execution.securecodebox.io/v1"
kind: Scan
metadata:
  name: "nmap-scanme.nmap.org"
spec:
  scanType: "nmap"
  parameters:
    - scanme.nmap.org
  env:
    - name: TEST_ENV
      valueFrom:
        secretKeyRef:
          key: secret-name
          name: zap-customer-credentials
  cascades:
    inheritLabels: false
    inheritAnnotations: true
    inheritVolumes: true
    inheritEnv: true
    matchLabels:
      securecodebox.io/intensive: light

The inherit* settings should already be cascaded to the child scans, so this should work for this use case.

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
@EndPositive
Copy link
Contributor Author

Done! I used cascades.inheritVolumes for both the volumes and volumeMounts, is that okay? Or do you prefer them separate?

Copy link
Member

@J12934 J12934 left a comment

Choose a reason for hiding this comment

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

Hi this already looks great 👍
Found some small issues while going over the changes and some things which confused me.

let cascadingScans: Array<ExtendedScanSpec> = [];
const cascadingRuleChain = getScanChain(parentScan);

parentScan = purgeCascadedRuleFromScan(parentScan, cascadedRule);
Copy link
Member

Choose a reason for hiding this comment

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

This took me a minute to understand why this is needed.
I think this should at least have a comment explaining why this is required or be moved closer to the point where the env vars & volumes are merged to make it clear that this is done to avoid duplicates there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added some comments. Feel free to add more or move it where you think it fits better.


const cascadedScan = cascadedScans[0]

expect(cascadedScans).toMatchInlineSnapshot(`
Copy link
Member

Choose a reason for hiding this comment

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

I think we should skip these assertions on the cascadedScans in these new tests, as these are basically a intermediate result for the purpose of this test, as the actual relevant logic on really happens in getCascadingScanDefinition. I was confused for a while looking at this and wondering why there is only on env var in the cascadedScans var.

That in itself might be something we should refactor in the future, unifying getCascadingScans and getCascadingScanDefinition and run all assertions on the actual scan specs. But this doesn't have to happen in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the getCascadingScans function to return the actual scan definition. Also the tests have been updated, so please take another look.

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
…date the tests accordingly.

Also replaces intermediate checks on the scan definition by only the test's specific assertion (e.g. random name test).

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
…method

Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
@EndPositive EndPositive requested a review from J12934 July 21, 2021 12:08
Copy link
Member

@J12934 J12934 left a comment

Choose a reason for hiding this comment

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

Awesome work 🚀

Signed-off-by: Jannik Hollenbach <jannik.hollenbach@iteratec.com>
@J12934 J12934 merged commit a15b044 into secureCodeBox:main Jul 22, 2021
@EndPositive EndPositive deleted the cascading-scan-spec-merge-inherit branch July 22, 2021 22:08
@rfelber rfelber changed the title Inherit environment variables, volumes, and volume mounts from parent scan Inherit environment variables, volumes, and volume mounts from parent in cascading scans Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cascading-scans enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants