1
1
package main
2
2
3
3
import (
4
- "encoding/base64"
5
4
"flag"
6
5
"fmt"
7
6
"github.com/applauseoss/decrypt-and-start/lib"
8
- enc_sdk "github.com/applauseoss/decrypt-and-start/lib/aws_encryption_sdk"
9
7
"log"
10
8
"os"
11
9
"os/exec"
@@ -15,36 +13,49 @@ import (
15
13
16
14
// This function should work like an entrypoint: exec "${@}"
17
15
func Exec () {
18
- flag .Parse ()
19
- if len (os . Args ) == 1 {
16
+ args := flag .Args ()
17
+ if len (args ) == 0 {
20
18
return
21
19
}
22
- cmd , err := exec .LookPath (os . Args [ 1 ])
20
+ cmd , err := exec .LookPath (args [ 0 ])
23
21
if err != nil {
24
22
log .Fatal (err )
25
23
}
26
- if err := syscall .Exec (cmd , flag . Args () , os .Environ ()); err != nil {
24
+ if err := syscall .Exec (cmd , args , os .Environ ()); err != nil {
27
25
log .Fatal (err )
28
26
}
29
27
}
30
28
31
29
func main () {
32
- for _ , e := range os .Environ () {
33
- // e = each k=v pair/line, pair = split k = [0], v = [1] array
34
- pair := strings .SplitN (e , "=" , 2 )
35
- // See if value starts with 'decrypt:'
36
- if strings .HasPrefix (pair [1 ], "decrypt:" ) {
37
- fmt .Println ("Decrypting the value of " + pair [0 ] + "..." )
38
- ciphertext , err := base64 .StdEncoding .DecodeString (strings .TrimPrefix (pair [1 ], "decrypt:" ))
39
- if err != nil {
40
- log .Fatal (err )
41
- }
42
- kms_helper := enc_sdk .NewKmsHelper (lib .GetRegion ())
43
- decrypted_value , err := kms_helper .Decrypt (ciphertext )
44
- if err != nil {
45
- log .Fatal (err )
30
+ var workerCount int
31
+ flag .IntVar (& workerCount , "p" , 10 , "number of parallel workers (defaults to 10)" )
32
+ flag .Parse ()
33
+ workerPool := lib .NewWorkerPool (workerCount )
34
+ workerPool .Start ()
35
+ // Put encrypted env vars in queue for workers to process
36
+ go func () {
37
+ for _ , e := range os .Environ () {
38
+ // e = each k=v pair/line, pair = split k = [0], v = [1] array
39
+ pair := strings .SplitN (e , "=" , 2 )
40
+ // See if value starts with 'decrypt:'
41
+ if strings .HasPrefix (pair [1 ], "decrypt:" ) {
42
+ env := & lib.EnvVar {Name : pair [0 ], Value : pair [1 ]}
43
+ workerPool .InChan <- env
44
+ fmt .Println ("Decrypting the value of " + pair [0 ] + "..." )
46
45
}
47
- os .Setenv (pair [0 ], string (decrypted_value ))
46
+ }
47
+ // Close the input channel so workers know there's nothing left to process
48
+ close (workerPool .InChan )
49
+ }()
50
+ // Process decrypted values
51
+ for {
52
+ env , ok := <- workerPool .OutChan
53
+ if env != nil {
54
+ os .Setenv (env .Name , env .Value )
55
+ }
56
+ // If the output channel is closed, there are no more values to receive
57
+ if ! ok {
58
+ break
48
59
}
49
60
}
50
61
Exec ()
0 commit comments