Skip to content

Commit 13c1f3b

Browse files
committed
mask shell cmd rendering and add retrieve value fromVault template func
1 parent 44856ee commit 13c1f3b

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed

biz/impl/shellfunc.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"io"
1818
"os"
1919
"os/exec"
20+
"regexp"
2021
"strconv"
2122
"strings"
2223
"time"
@@ -167,13 +168,17 @@ func (f *ShellFuncAction) Exec() {
167168
for idx, tcmd := range f.Cmds {
168169
u.Pfv("cmd(%2d):\n", idx+1)
169170
u.Pvv(tcmd)
171+
cleansed := func() string {
172+
re := regexp.MustCompile(`{{.*\.secure_.*}}`)
173+
return re.ReplaceAllString(tcmd, `SECURE_SENSITIVE_INFO_MASKED`)
174+
}()
170175
cmd := Render(tcmd, f.Vars)
171-
u.Pfvvvv("cmd=>:\n%s\n", color.HiBlueString("%s", cmd))
176+
cleansedCmd := Render(cleansed, f.Vars)
177+
u.Pfvvvv("cmd=>:\n%s\n", color.HiBlueString("%s", cleansedCmd))
172178
runCmd(f, cmd, idx+1)
173179
u.SubStepStatus("..", f.Result.Code)
174180
u.Dvvvvv(f.Result)
175181
}
176182

177183
StepRuntime().Result = &f.Result
178-
179184
}

biz/impl/template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ func FuncMapInit() {
9696

9797
return encrypted
9898
},
99+
//retrieve value from vault
100+
"fromVault": func(varname string) string {
101+
var val string = "NotExistInVault"
102+
opt := GetVault().Get(varname)
103+
if opt != nil {
104+
val = opt.(string)
105+
}
106+
107+
return val
108+
},
99109
//regObj will keep the golang object intact and register it to cache
100110
//same effect of: '{{ func_return_a_obj arg | objToYml|ymlToObj|reg "instances" }}'
101111
"regObj": func(varname string, obj interface{}) interface{} {

tests/functests/c0203.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
doc_meta: |
2+
folder: security
3+
title: mask senstive info in shell execution
4+
head: |
5+
When you use verbose level greater than vvv, it will print out the debugging final shell scripts rendered, which possiblly contains the secure vars. In such a case, upcmd will automatically mask the senstive variable with SECURE_SENSITIVE_INFO_MASKED
6+
7+
sections:
8+
- title: Demo
9+
log: yes
10+
11+
tasks:
12+
-
13+
name: task
14+
task:
15+
-
16+
func: shell
17+
dvars:
18+
- name: enc_key
19+
value: my_enc_key
20+
flags:
21+
- secret
22+
23+
- name: value_encrypted
24+
value: '{{ "ENV_AAA" | encryptAES .enc_key }}'
25+
flags:
26+
- vvvv
27+
- taskScope
28+
29+
- name: ENV_AAA
30+
value: '{{.value_encrypted}}'
31+
flags:
32+
- secure
33+
34+
do: |
35+
echo "hello, this is a secrt value: {{.secure_ENV_AAA}}"

tests/functests/c0204.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
doc_meta: |
2+
folder: security
3+
title: retrieve secret from valut
4+
head: |
5+
When you put senstive information into vault, you can use a template func to retrieve it and use it in template rendering
6+
7+
This example shows that you can not get the secret value in general cache, unless you use fromVault template func
8+
9+
sections:
10+
- title: Demo
11+
log: yes
12+
13+
tasks:
14+
-
15+
name: task
16+
task:
17+
-
18+
func: cmd
19+
dvars:
20+
- name: my_secret
21+
value: you_will_never_know
22+
flags:
23+
- secret
24+
25+
do:
26+
- name: print
27+
cmd: |
28+
hello, this is a secrt value: {{.my_secret}}
29+
hello, this is a secrt value: {{ "my_secret" | fromVault}}
30+
hello, this is a secrt value: {{ "a_secret_does_not_exist_in_vault" | fromVault}}

utils/shared.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ var (
3434
"black": color.FgBlack,
3535
"red": color.FgRed,
3636
"green": color.FgGreen,
37-
"yello": color.FgYellow,
37+
"yellow": color.FgYellow,
3838
"blue": color.FgBlue,
3939
"magenta": color.FgMagenta,
4040
"cyan": color.FgCyan,
4141
"white": color.FgWhite,
4242
"hiblack": color.FgHiBlack,
4343
"hiRed": color.FgHiRed,
4444
"higreen": color.FgHiGreen,
45-
"hiyello": color.FgHiYellow,
45+
"hiyellow": color.FgHiYellow,
4646
"hiblue": color.FgHiBlue,
4747
"himagenta": color.FgHiMagenta,
4848
"hicyan": color.FgHiCyan,
@@ -53,15 +53,15 @@ var (
5353
"black": color.BgBlack,
5454
"red": color.BgRed,
5555
"green": color.BgGreen,
56-
"yello": color.BgYellow,
56+
"yellow": color.BgYellow,
5757
"blue": color.BgBlue,
5858
"magenta": color.BgMagenta,
5959
"cyan": color.BgCyan,
6060
"white": color.BgWhite,
6161
"hiblack": color.BgHiBlack,
6262
"hiRed": color.BgHiRed,
6363
"higreen": color.BgHiGreen,
64-
"hiyello": color.BgHiYellow,
64+
"hiyellow": color.BgHiYellow,
6565
"hiblue": color.BgHiBlue,
6666
"himagenta": color.BgHiMagenta,
6767
"hicyan": color.BgHiCyan,

0 commit comments

Comments
 (0)