From faea97cd41a09038cc4701dbe946f78bdd3ea18e Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Thu, 28 Oct 2021 16:42:57 +0200 Subject: [PATCH 1/2] Implement write-file command --- main.go | 2 ++ write.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 write.go diff --git a/main.go b/main.go index 7de1f53..c4028e0 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,8 @@ func main() { }) case "wait-signal": waitSignal(os.Stdout, args[2:], os.Exit) + case "write-file": + writeFile(args[2:], os.Stdin, os.Stderr, os.Exit) case "license": license(args[2:]) default: diff --git a/write.go b/write.go new file mode 100644 index 0000000..706e781 --- /dev/null +++ b/write.go @@ -0,0 +1,43 @@ +package main + +import ( + "io" + "os" + "log" +) + +func writeFile(args []string, stdin io.Reader, stderr io.Writer, exit exitFunc) { + l := log.New(stderr, "", 0) + if len(args) < 1 { + exit(1) + } + + file, err := os.OpenFile(args[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + l.Printf("Error opening file: %+v", err) + exit(1) + } + + err = file.Chmod(0600) + if err != nil { + l.Printf("Error chmod'ing file: %+v", err) + exit(1) + } + + buf := make([]byte, 8192) + for { + nBytes, err := stdin.Read(buf) + if err != nil { + exit(0) + } + + wBytes, err := file.Write(buf[0:nBytes]) + if err != nil { + exit(1) + } + if wBytes != nBytes { + // Write should never fail without error + exit(1) + } + } +} From 8c041f4206eaaadb0d1248de93beafc038bfa6e1 Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Thu, 25 Nov 2021 14:00:22 +0100 Subject: [PATCH 2/2] Document write-file --- USAGE.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/USAGE.md b/USAGE.md index ec8ac9d..e99ac7f 100644 --- a/USAGE.md +++ b/USAGE.md @@ -19,6 +19,9 @@ Currently, the agent has the following modes: `wait-signal` : Waits for a specific signal or signals before it exits. +`write-file` +: Write standard input into the given file + `license` : This mode will print the license of the agent and exit. @@ -58,6 +61,13 @@ the value of `\0` or a newline (`\n`) is received on `stdin`. This byte will not be passed to the program and if any other byte is received the agent will exit to prevent data stream corruption. +## Writing files + +The backend APIs do not support adding custom files on the fly in containers. +So the `write-file` mode was created. The mode takes just one argument, the +name and path of the file to write. The contents of the file will be read from +standard input + ## Reporting PID Since most container implementations don't report the PID of the process