Skip to content
This repository was archived by the owner on Dec 15, 2023. It is now read-only.

Commit 8d206b7

Browse files
committed
feat: Allow setting pidMode=task in the output task definition
Signed-off-by: Grzegorz Nosek <[email protected]>
1 parent d124f22 commit 8d206b7

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed

pkg/hocon/hocon.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ func (k *KiltHocon) Runtime(info *kilt.TargetInfo) (*kilt.Runtime, error) {
7373
}
7474
return extractRuntime(config)
7575
}
76+
77+
func (k *KiltHocon) Task() (*kilt.Task, error) {
78+
config, err := k.prepareFullStringConfig(&kilt.TargetInfo{})
79+
if err != nil {
80+
return nil, fmt.Errorf("could not assemble full config: %w", err)
81+
}
82+
return extractTask(config)
83+
}

pkg/hocon/task.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package hocon
2+
3+
import (
4+
"github.com/falcosecurity/kilt/pkg/kilt"
5+
"github.com/go-akka/configuration"
6+
)
7+
8+
func extractTask(config *configuration.Config) (*kilt.Task, error) {
9+
var task = new(kilt.Task)
10+
11+
if config.HasPath("task.pid_mode") {
12+
task.PidMode = config.GetString("task.pid_mode")
13+
}
14+
15+
return task, nil
16+
}

pkg/kilt/kilt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ func (k *Kilt) Build(info *TargetInfo) (*Build, error) {
1717
func (k *Kilt) Runtime(info *TargetInfo) (*Runtime, error) {
1818
return k.definition.Runtime(info)
1919
}
20+
21+
func (k *Kilt) Task() (*Task, error) {
22+
return k.definition.Task()
23+
}

pkg/kilt/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Build struct {
3434
Resources []BuildResource
3535
}
3636

37+
type Task struct {
38+
PidMode string // the only value is `task` right now
39+
}
40+
3741
type RuntimeUpload struct {
3842
Payload *Payload
3943
Destination string
@@ -70,4 +74,5 @@ type Payload struct {
7074
type LanguageInterface interface {
7175
Build(info *TargetInfo) (*Build, error)
7276
Runtime(info *TargetInfo) (*Runtime, error)
77+
Task() (*Task, error)
7378
}

runtimes/cloudformation/cfnpatcher/cfn_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ var sidecarEnvTests = [...]string{
5858
"sidecar_env/volumes_from",
5959
}
6060

61+
var taskPidModeTests = [...]string{
62+
"task_pid_mode/command",
63+
}
64+
6165
const defaultConfig = `
6266
build {
6367
entry_point: ["/kilt/run", "--", ${?original.metadata.captured_tag}]
@@ -112,6 +116,25 @@ build {
112116
}
113117
`
114118

119+
const taskPidModeConfig = `
120+
build {
121+
entry_point: ["/kilt/run", "--", ${?original.metadata.captured_tag}]
122+
command: [] ${?original.entry_point} ${?original.command}
123+
mount: [
124+
{
125+
name: "KiltImage"
126+
image: "KILT:latest"
127+
volumes: ["/kilt"]
128+
entry_point: ["/kilt/wait"]
129+
}
130+
]
131+
capabilities: ["SYS_PTRACE"]
132+
}
133+
task {
134+
pid_mode: "task"
135+
}
136+
`
137+
115138
func runTest(t *testing.T, name string, context context.Context, config Configuration) {
116139
fragment, err := ioutil.ReadFile("fixtures/" + name + ".json")
117140
if err != nil {
@@ -197,6 +220,22 @@ func TestPatchingSidecarEnv(t *testing.T) {
197220
}
198221
}
199222

223+
func TestPatchingTask(t *testing.T) {
224+
l := log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
225+
226+
for _, testName := range taskPidModeTests {
227+
t.Run(testName, func(t *testing.T) {
228+
runTest(t, testName, l.WithContext(context.Background()),
229+
Configuration{
230+
Kilt: taskPidModeConfig,
231+
OptIn: false,
232+
RecipeConfig: "{}",
233+
UseRepositoryHints: false,
234+
})
235+
})
236+
}
237+
}
238+
200239
func TestPatchingForParameterizingEnvars(t *testing.T) {
201240
l := log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
202241

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"Resources": {
3+
"taskdef": {
4+
"Type": "AWS::ECS::TaskDefinition",
5+
"Properties": {
6+
"RequiresCompatibilities": [
7+
"FARGATE"
8+
],
9+
"Tags": [
10+
{
11+
"Key": "antani",
12+
"Value": "sbiribuda"
13+
},
14+
{
15+
"Key": "kiltinclude",
16+
"Value": "itisignored"
17+
}
18+
],
19+
"ContainerDefinitions": [
20+
{
21+
"Name": "app",
22+
"Image": "busybox",
23+
"Command": ["/bin/sh"]
24+
}
25+
]
26+
}
27+
}
28+
}
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"Resources": {
3+
"taskdef": {
4+
"Properties": {
5+
"ContainerDefinitions": [
6+
{
7+
"Command": [
8+
"/bin/sh"
9+
],
10+
"EntryPoint": [
11+
"/kilt/run",
12+
"--",
13+
""
14+
],
15+
"Image": "busybox",
16+
"LinuxParameters": {
17+
"Capabilities": {
18+
"Add": [
19+
"SYS_PTRACE"
20+
]
21+
}
22+
},
23+
"Name": "app",
24+
"VolumesFrom": [
25+
{
26+
"ReadOnly": true,
27+
"SourceContainer": "KiltImage"
28+
}
29+
]
30+
},
31+
{
32+
"EntryPoint": [
33+
"/kilt/wait"
34+
],
35+
"Image": "KILT:latest",
36+
"Name": "KiltImage"
37+
}
38+
],
39+
"RequiresCompatibilities": [
40+
"FARGATE"
41+
],
42+
"Tags": [
43+
{
44+
"Key": "antani",
45+
"Value": "sbiribuda"
46+
},
47+
{
48+
"Key": "kiltinclude",
49+
"Value": "itisignored"
50+
}
51+
],
52+
"PidMode": "task"
53+
},
54+
"Type": "AWS::ECS::TaskDefinition"
55+
}
56+
}
57+
}

runtimes/cloudformation/cfnpatcher/patcher.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ func applyTaskDefinitionPatch(ctx context.Context, name string, resource *gabs.C
4444
successes := 0
4545
containers := make(map[string]kilt.BuildResource)
4646
k := kiltapi.NewKiltFromHoconWithConfig(configuration.Kilt, configuration.RecipeConfig)
47+
48+
taskPatch, err := k.Task()
49+
if err != nil {
50+
return nil, fmt.Errorf("could not get task definition patch: %w", err)
51+
}
52+
53+
if taskPatch.PidMode != "" {
54+
if !resource.Exists("Properties") {
55+
_, err := resource.Set(map[string]interface{}{}, "Properties")
56+
if err != nil {
57+
return nil, fmt.Errorf("could not add properties to task definition: %w", err)
58+
}
59+
}
60+
61+
_, err = resource.Set(taskPatch.PidMode, "Properties", "PidMode")
62+
if err != nil {
63+
return nil, fmt.Errorf("could not set PidMode: %w", err)
64+
}
65+
}
66+
4767
if resource.Exists("Properties", "ContainerDefinitions") {
4868
for _, container := range resource.S("Properties", "ContainerDefinitions").Children() {
4969
info := extractContainerInfo(ctx, resource, name, container, configuration)

0 commit comments

Comments
 (0)