|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "golang.org/x/sys/unix" |
| 13 | +) |
| 14 | + |
| 15 | +// Linux pipe buffer capacity; writes up to this size complete without blocking. |
| 16 | +const maxPipeSecretSize = 65536 |
| 17 | + |
| 18 | +const ( |
| 19 | + SecretTypePipeVar = "pipeVar" |
| 20 | + SecretTypePathVar = "pathVar" |
| 21 | + SecretTypeValueVar = "valueVar" |
| 22 | +) |
| 23 | + |
| 24 | +type Secret struct { |
| 25 | + Type string `json:"type"` |
| 26 | + Value string `json:"value"` |
| 27 | + Target string `json:"target"` |
| 28 | +} |
| 29 | + |
| 30 | +func getSecrets() ([]Secret, error) { |
| 31 | + pipePath := "/tmp/secrets" |
| 32 | + if err := unix.Mkfifo(pipePath, 0600); err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + defer os.Remove(pipePath) |
| 36 | + |
| 37 | + fmt.Printf("Waiting for secrets to be written to named pipe at %s...\n", pipePath) |
| 38 | + file, err := os.OpenFile(pipePath, os.O_RDONLY, os.ModeNamedPipe) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + defer file.Close() |
| 43 | + |
| 44 | + var secrets []Secret |
| 45 | + decoder := json.NewDecoder(file) |
| 46 | + if err := decoder.Decode(&secrets); err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return secrets, nil |
| 51 | +} |
| 52 | + |
| 53 | +func writeSecretToPipe(secret Secret) (string, error) { |
| 54 | + if len(secret.Value) > maxPipeSecretSize { |
| 55 | + return "", fmt.Errorf("secret for %s exceeds pipe buffer size (%d > %d)", secret.Target, len(secret.Value), maxPipeSecretSize) |
| 56 | + } |
| 57 | + |
| 58 | + fds := make([]int, 2) |
| 59 | + if err := unix.Pipe(fds); err != nil { |
| 60 | + return "", err |
| 61 | + } |
| 62 | + readFd, writeFd := fds[0], fds[1] |
| 63 | + |
| 64 | + if _, err := unix.Write(writeFd, []byte(secret.Value)); err != nil { |
| 65 | + unix.Close(readFd) |
| 66 | + unix.Close(writeFd) |
| 67 | + return "", err |
| 68 | + } |
| 69 | + unix.Close(writeFd) |
| 70 | + |
| 71 | + return fmt.Sprintf("/dev/fd/%d", readFd), nil |
| 72 | +} |
| 73 | + |
| 74 | +func writeSecretToMemfd(secret Secret) (path string, err error) { |
| 75 | + fd, err := unix.MemfdCreate(secret.Target, 0) |
| 76 | + if err != nil { |
| 77 | + return "", err |
| 78 | + } |
| 79 | + defer func() { |
| 80 | + if err != nil { |
| 81 | + unix.Close(fd) |
| 82 | + } |
| 83 | + }() |
| 84 | + |
| 85 | + if _, err = unix.Write(fd, []byte(secret.Value)); err != nil { |
| 86 | + return "", err |
| 87 | + } |
| 88 | + |
| 89 | + if _, err = unix.Seek(fd, 0, unix.SEEK_SET); err != nil { |
| 90 | + return "", err |
| 91 | + } |
| 92 | + |
| 93 | + return fmt.Sprintf("/dev/fd/%d", fd), nil |
| 94 | +} |
| 95 | + |
| 96 | +func buildSecretEnvVars(secrets []Secret) ([]string, error) { |
| 97 | + var envVars []string |
| 98 | + for _, secret := range secrets { |
| 99 | + switch secret.Type { |
| 100 | + case SecretTypePipeVar: |
| 101 | + secretPath, err := writeSecretToPipe(secret) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("writing secret to pipe for %s: %w", secret.Target, err) |
| 104 | + } |
| 105 | + envVars = append(envVars, fmt.Sprintf("%s=%s", secret.Target, secretPath)) |
| 106 | + case SecretTypePathVar: |
| 107 | + secretPath, err := writeSecretToMemfd(secret) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("writing secret to memfd for %s: %w", secret.Target, err) |
| 110 | + } |
| 111 | + envVars = append(envVars, fmt.Sprintf("%s=%s", secret.Target, secretPath)) |
| 112 | + case SecretTypeValueVar: |
| 113 | + envVars = append(envVars, fmt.Sprintf("%s=%s", secret.Target, secret.Value)) |
| 114 | + default: |
| 115 | + return nil, fmt.Errorf("unknown secret type %s for target %s", secret.Type, secret.Target) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return envVars, nil |
| 120 | +} |
| 121 | + |
| 122 | +func usage() { |
| 123 | + fmt.Fprintf(os.Stderr, "Usage: %s <command> [args...]\n", os.Args[0]) |
| 124 | + os.Exit(1) |
| 125 | +} |
| 126 | + |
| 127 | +func main() { |
| 128 | + // Retrieve subcommand and arguments |
| 129 | + args := os.Args[1:] |
| 130 | + if len(args) < 1 { |
| 131 | + usage() |
| 132 | + } |
| 133 | + |
| 134 | + // Validate command before waiting for secrets |
| 135 | + if strings.Contains(args[0], " ") { |
| 136 | + args = append([]string{"sh", "-c"}, args...) |
| 137 | + } |
| 138 | + |
| 139 | + cmdPath, err := exec.LookPath(args[0]) |
| 140 | + if err != nil { |
| 141 | + fmt.Fprintf(os.Stderr, "Error finding command %s: %v\n", args[0], err) |
| 142 | + os.Exit(1) |
| 143 | + } |
| 144 | + args[0] = cmdPath |
| 145 | + |
| 146 | + // Get secrets from named pipe |
| 147 | + secrets, err := getSecrets() |
| 148 | + if err != nil { |
| 149 | + fmt.Fprintf(os.Stderr, "Error getting secrets: %v\n", err) |
| 150 | + os.Exit(1) |
| 151 | + } |
| 152 | + |
| 153 | + secretEnvVars, err := buildSecretEnvVars(secrets) |
| 154 | + if err != nil { |
| 155 | + fmt.Fprintf(os.Stderr, "Error building secret env vars: %v\n", err) |
| 156 | + os.Exit(1) |
| 157 | + } |
| 158 | + |
| 159 | + // Replace current process with the specified command |
| 160 | + env := append(os.Environ(), secretEnvVars...) |
| 161 | + if err := unix.Exec(cmdPath, args, env); err != nil { |
| 162 | + fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err) |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | +} |
0 commit comments