1
|
|
|
package cmd |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"os" |
5
|
|
|
"strings" |
6
|
|
|
|
7
|
|
|
"github.com/Permify/permify/pkg/cmd/flags" |
8
|
|
|
|
9
|
|
|
"github.com/gookit/color" |
10
|
|
|
"github.com/olekukonko/tablewriter" |
11
|
|
|
"github.com/spf13/cobra" |
12
|
|
|
"github.com/spf13/viper" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
func NewConfigCommand() *cobra.Command { |
16
|
|
|
cmd := &cobra.Command{ |
17
|
|
|
Use: "config", |
18
|
|
|
Short: "Inspect permify configuration and environment variables ", |
19
|
|
|
RunE: func(cmd *cobra.Command, args []string) error { |
20
|
|
|
data := prepareConfigData(cmd) |
21
|
|
|
renderConfigTable(data) |
22
|
|
|
return nil |
23
|
|
|
}, |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
flags.RegisterServeFlags(cmd) |
27
|
|
|
|
28
|
|
|
return cmd |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func prepareConfigData(cmd *cobra.Command) [][]string { |
32
|
|
|
data := [][]string{} |
33
|
|
|
for _, key := range viper.AllKeys() { |
34
|
|
|
viperKey, viperValue, source := getConfigDetails(key, cmd) |
35
|
|
|
data = append(data, []string{color.FgCyan.Render(viperKey), viperValue, source}) |
36
|
|
|
} |
37
|
|
|
return data |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
func getConfigDetails(key string, cmd *cobra.Command) (string, string, string) { |
41
|
|
|
envKey := strings.ToUpper(strings.ReplaceAll(key, ".", "_")) |
42
|
|
|
viperKey := "PERMIFY_" + envKey |
43
|
|
|
viperValue := viper.GetString(key) |
44
|
|
|
envValue, envExists := os.LookupEnv(viperKey) |
45
|
|
|
|
46
|
|
|
source := getKeyOrigin(envExists, cmd, key) |
47
|
|
|
value := viperValue |
48
|
|
|
if envExists { |
49
|
|
|
value = envValue |
50
|
|
|
} |
51
|
|
|
return viperKey, obfuscateSecrets(viperKey, value), source |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func getKeyOrigin(envExists bool, cmd *cobra.Command, key string) string { |
55
|
|
|
if cmd.Flags().Changed(strings.ReplaceAll(key, ".", "-")) { |
56
|
|
|
return color.FgLightGreen.Render("flag") |
57
|
|
|
} |
58
|
|
|
if envExists { |
59
|
|
|
return color.FgLightBlue.Render("env") |
60
|
|
|
} |
61
|
|
|
return color.FgYellow.Render("file") |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
func renderConfigTable(data [][]string) { |
65
|
|
|
table := tablewriter.NewWriter(os.Stdout) |
66
|
|
|
table.SetHeader([]string{"Key", "Value", "Source"}) |
67
|
|
|
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER}) |
68
|
|
|
table.SetCenterSeparator("|") |
69
|
|
|
for _, v := range data { |
70
|
|
|
table.Append(v) |
71
|
|
|
} |
72
|
|
|
table.Render() |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
func obfuscateSecrets(key, value string) string { |
76
|
|
|
secrets := []string{ |
77
|
|
|
"PERMIFY_DATABASE_URI", |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
for _, wKey := range secrets { |
81
|
|
|
if key == wKey { |
82
|
|
|
if len(value) < 3 { |
83
|
|
|
return value |
84
|
|
|
} |
85
|
|
|
if lastColon := strings.LastIndex(value, ":"); lastColon != -1 { |
86
|
|
|
typeEnd := strings.Index(value[lastColon:], "/") |
87
|
|
|
if typeEnd != -1 { |
88
|
|
|
if len(value)-3 > lastColon+typeEnd+1 { |
89
|
|
|
return value[:lastColon+typeEnd+1] + "***" + value[len(value)-3:] |
90
|
|
|
} |
91
|
|
|
return value |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return value[:len(value)-3] + "***" |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return value |
98
|
|
|
} |
99
|
|
|
|