Skip to content

Commit 9a10d6b

Browse files
committed
Add python chaining support, rename chaining functional tests to all match
1 parent f7e082a commit 9a10d6b

File tree

12 files changed

+187
-35
lines changed

12 files changed

+187
-35
lines changed

manifests/examples/testing/scenarios/namespaced/injection-dotnetchaining.yaml renamed to manifests/examples/testing/scenarios/namespaced/chaining-dotnet.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apiVersion: agents.contrastsecurity.com/v1beta1
22
kind: AgentInjector
33
metadata:
4-
name: injection-dotnetchaining
4+
name: chaining-dotnet
55
spec:
66
enabled: true
77
type: dotnet-core
88
selector:
99
labels:
1010
- name: app
11-
value: injection-dotnetchaining
11+
value: chaining-dotnet
1212
image:
1313
pullPolicy: Never
1414
connection:
@@ -19,20 +19,20 @@ spec:
1919
apiVersion: apps/v1
2020
kind: Deployment
2121
metadata:
22-
name: injection-dotnetchaining
22+
name: chaining-dotnet
2323
labels:
24-
app: injection-dotnetchaining
24+
app: chaining-dotnet
2525
spec:
2626
replicas: 1
2727
selector:
2828
matchLabels:
29-
app: injection-dotnetchaining
29+
app: chaining-dotnet
3030
strategy:
3131
type: Recreate
3232
template:
3333
metadata:
3434
labels:
35-
app: injection-dotnetchaining
35+
app: chaining-dotnet
3636
spec:
3737
containers:
3838
- image: k8s.gcr.io/pause:3.3

manifests/examples/testing/scenarios/namespaced/injection-flexchaining.yaml renamed to manifests/examples/testing/scenarios/namespaced/chaining-flex.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apiVersion: agents.contrastsecurity.com/v1beta1
22
kind: AgentInjector
33
metadata:
4-
name: injection-flexchaining
4+
name: chaining-flex
55
spec:
66
enabled: true
77
type: flex
88
selector:
99
labels:
1010
- name: app
11-
value: injection-flexchaining
11+
value: chaining-flex
1212
image:
1313
pullPolicy: Never
1414
connection:
@@ -19,20 +19,20 @@ spec:
1919
apiVersion: apps/v1
2020
kind: Deployment
2121
metadata:
22-
name: injection-flexchaining
22+
name: chaining-flex
2323
labels:
24-
app: injection-flexchaining
24+
app: chaining-flex
2525
spec:
2626
replicas: 1
2727
selector:
2828
matchLabels:
29-
app: injection-flexchaining
29+
app: chaining-flex
3030
strategy:
3131
type: Recreate
3232
template:
3333
metadata:
3434
labels:
35-
app: injection-flexchaining
35+
app: chaining-flex
3636
spec:
3737
containers:
3838
- image: k8s.gcr.io/pause:3.3
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apiVersion: agents.contrastsecurity.com/v1beta1
22
kind: AgentInjector
33
metadata:
4-
name: injection-javatooloptions
4+
name: chaining-java
55
spec:
66
enabled: true
77
type: java
88
selector:
99
labels:
1010
- name: app
11-
value: injection-javatooloptions
11+
value: chaining-java
1212
image:
1313
pullPolicy: Never
1414
connection:
@@ -19,20 +19,20 @@ spec:
1919
apiVersion: apps/v1
2020
kind: Deployment
2121
metadata:
22-
name: injection-javatooloptions
22+
name: chaining-java
2323
labels:
24-
app: injection-javatooloptions
24+
app: chaining-java
2525
spec:
2626
replicas: 1
2727
selector:
2828
matchLabels:
29-
app: injection-javatooloptions
29+
app: chaining-java
3030
strategy:
3131
type: Recreate
3232
template:
3333
metadata:
3434
labels:
35-
app: injection-javatooloptions
35+
app: chaining-java
3636
spec:
3737
containers:
3838
- image: k8s.gcr.io/pause:3.3
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: agents.contrastsecurity.com/v1beta1
2+
kind: AgentInjector
3+
metadata:
4+
name: chaining-python
5+
spec:
6+
enabled: true
7+
type: python
8+
selector:
9+
labels:
10+
- name: app
11+
value: chaining-python
12+
image:
13+
pullPolicy: Never
14+
connection:
15+
name: testing-agent-connection
16+
configuration:
17+
name: testing-agent-configuration
18+
---
19+
apiVersion: apps/v1
20+
kind: Deployment
21+
metadata:
22+
name: chaining-python
23+
labels:
24+
app: chaining-python
25+
spec:
26+
replicas: 1
27+
selector:
28+
matchLabels:
29+
app: chaining-python
30+
strategy:
31+
type: Recreate
32+
template:
33+
metadata:
34+
labels:
35+
app: chaining-python
36+
spec:
37+
containers:
38+
- image: k8s.gcr.io/pause:3.3
39+
name: pause
40+
env:
41+
- name: PYTHONPATH
42+
value: something

manifests/examples/testing/scenarios/namespaced/kustomization.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ resources:
1111
- ./glob-selecting.yaml
1212
- ./init-container-overrides.yaml
1313
- ./injection-dotnet.yaml
14-
- ./injection-dotnetchaining.yaml
1514
- ./injection-dummy.yaml
1615
- ./injection-java.yaml
17-
- ./injection-javatooloptions.yaml
1816
- ./injection-nodejs-require.yaml
1917
- ./injection-nodejs-import.yaml
2018
- ./injection-php.yaml
2119
- ./injection-python.yaml
2220
- ./injection-flex.yaml
23-
- ./injection-flexchaining.yaml
2421
- ./missing-deps.yaml
2522
- ./multiple-images.yaml
2623
- ./namespace.yaml
2724
- ./type-daemonset.yaml
2825
- ./type-deployment.yaml
2926
- ./type-statefulset.yaml
3027
- ./unmatched.yaml
28+
- ./chaining-dotnet.yaml
29+
- ./chaining-flex.yaml
30+
- ./chaining-java.yaml
31+
- ./chaining-python.yaml

src/Contrast.K8s.AgentOperator/Core/Reactions/Injecting/Patching/Agents/PythonAgentPatcher.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
22
// See the LICENSE file in the project root for more information.
33

4-
using System.Collections.Generic;
4+
using Contrast.K8s.AgentOperator.Core.Reactions.Injecting.Patching.Utility;
55
using Contrast.K8s.AgentOperator.Core.State.Resources.Primitives;
66
using Contrast.K8s.AgentOperator.Options;
77
using k8s.Models;
8+
using System;
9+
using System.Collections.Generic;
810

911
namespace Contrast.K8s.AgentOperator.Core.Reactions.Injecting.Patching.Agents;
1012

@@ -20,7 +22,7 @@ public PythonAgentPatcher(InjectorOptions injectorOptions)
2022

2123
public IEnumerable<V1EnvVar> GenerateEnvVars(PatchingContext context)
2224
{
23-
yield return new V1EnvVar("PYTHONPATH", $"{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader");
25+
yield return new V1EnvVar("PYTHONPATH", GetAgentPythonPath(context));
2426
if (_injectorOptions.EnablePythonRewriter)
2527
{
2628
yield return new V1EnvVar("CONTRAST__AGENT__PYTHON__REWRITE", "true");
@@ -29,4 +31,21 @@ public IEnumerable<V1EnvVar> GenerateEnvVars(PatchingContext context)
2931
yield return new V1EnvVar("CONTRAST__AGENT__LOGGER__PATH", $"{context.WritableMountPath}/logs/contrast_agent.log");
3032
yield return new V1EnvVar("CONTRAST_INSTALLATION_TOOL", "KUBERNETES_OPERATOR");
3133
}
34+
35+
public void PatchContainer(V1Container container, PatchingContext context)
36+
{
37+
// Only modify this if CONTRAST_EXISTING_PYTHONPATH isn't already set. This is to prevent infinite loops.
38+
if (container.Env.FirstOrDefault("PYTHONPATH") is { Value: { } currentPath }
39+
&& !string.IsNullOrWhiteSpace(currentPath)
40+
&& !currentPath.Contains("contrast/loader", StringComparison.OrdinalIgnoreCase)
41+
&& container.Env.FirstOrDefault("CONTRAST_EXISTING_PYTHONPATH") is null)
42+
{
43+
container.Env.AddOrUpdate(new V1EnvVar("CONTRAST_EXISTING_PYTHONPATH", currentPath));
44+
container.Env.AddOrUpdate(new V1EnvVar("PYTHONPATH",
45+
$"{GetAgentPythonPath(context)}:{currentPath}"));
46+
}
47+
}
48+
49+
private static string GetAgentPythonPath(PatchingContext context) => $"{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader";
50+
3251
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using Xunit;
99
using Xunit.Abstractions;
1010

11-
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Agents;
11+
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Chaining;
1212

1313
public class DotnetChainingInjectionTests : IClassFixture<TestingContext>
1414
{
15-
private const string ScenarioName = "injection-dotnetchaining";
15+
private const string ScenarioName = "chaining-dotnet";
1616

1717
private readonly TestingContext _context;
1818

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using Xunit;
99
using Xunit.Abstractions;
1010

11-
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Agents;
11+
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Chaining;
1212

1313
public class FlexChainingInjectionTests : IClassFixture<TestingContext>
1414
{
15-
private const string ScenarioName = "injection-flexchaining";
15+
private const string ScenarioName = "chaining-flex";
1616

1717
private readonly TestingContext _context;
1818

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
using Xunit;
99
using Xunit.Abstractions;
1010

11-
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Agents;
11+
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Chaining;
1212

13-
public class JavaToolOptionsInjectionTests : IClassFixture<TestingContext>
13+
public class JavaChainingInjectionTests : IClassFixture<TestingContext>
1414
{
15-
private const string ScenarioName = "injection-javatooloptions";
15+
private const string ScenarioName = "chaining-java";
1616

1717
private readonly TestingContext _context;
1818

19-
public JavaToolOptionsInjectionTests(TestingContext context, ITestOutputHelper outputHelper)
19+
public JavaChainingInjectionTests(TestingContext context, ITestOutputHelper outputHelper)
2020
{
2121
_context = context;
2222
_context.RegisterOutput(outputHelper);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
2+
// See the LICENSE file in the project root for more information.
3+
4+
using System.Threading.Tasks;
5+
using Contrast.K8s.AgentOperator.FunctionalTests.Fixtures;
6+
using FluentAssertions;
7+
using FluentAssertions.Execution;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace Contrast.K8s.AgentOperator.FunctionalTests.Scenarios.Injection.Chaining;
12+
13+
public class PythonChainingInjectionTests : IClassFixture<TestingContext>
14+
{
15+
private const string ScenarioName = "chaining-python";
16+
17+
private readonly TestingContext _context;
18+
19+
public PythonChainingInjectionTests(TestingContext context, ITestOutputHelper outputHelper)
20+
{
21+
_context = context;
22+
_context.RegisterOutput(outputHelper);
23+
}
24+
25+
[Fact]
26+
public async Task When_injected_then_pod_should_have_agent_injection_chaining_environment_variables()
27+
{
28+
var client = await _context.GetClient();
29+
30+
// Act
31+
var result = await client.GetInjectedPodByPrefix(ScenarioName);
32+
33+
// Assert
34+
using (new AssertionScope())
35+
{
36+
var container = result.Spec.Containers.Should().ContainSingle().Subject;
37+
38+
container.Env.Should().Contain(x => x.Name == "PYTHONPATH")
39+
.Which.Value.Should().Be("/contrast/agent:/contrast/agent/contrast/loader:something");
40+
container.Env.Should().Contain(x => x.Name == "CONTRAST_EXISTING_PYTHONPATH")
41+
.Which.Value.Should().Be("something");
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)