-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobfuscator.go
More file actions
100 lines (89 loc) · 2.67 KB
/
obfuscator.go
File metadata and controls
100 lines (89 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"strings"
"time"
)
var obfuscation = "[OBFUSCATED]"
// Datacenter holds datacenter passwords
type Datacenter struct {
Credentials struct {
Pwd string `json:"password"`
AccessKeyID string `json:"aws_access_key_id"`
SecretAccessKey string `json:"aws_secret_access_key"`
AzureEnv string `json:"azure_environment"`
SubscriptionID string `json:"azure_subscription_id"`
ClientID string `json:"azure_client_id"`
ClientSecret string `json:"azure_client_secret"`
TenantID string `json:"azure_tenant_id"`
} `json:"credentials"`
}
// Obfuscate : obfuscates sensible data on the given stack
func Obfuscate(subject, stack string) string {
stack = PreProcess(stack)
if subject == "datacenter.set" || subject == "datacenter.del" {
patternsToObfuscate = make([]string, 0)
}
if needles, err := getNeedles(); err != nil {
stack = "[ An error occurred trying to obfuscate this message ]"
} else {
for _, needle := range needles {
if needle != "" {
stack = strings.Replace(stack, needle, obfuscation, -1)
}
}
}
return stack
}
func getNeedles() (needles []string, err error) {
if len(patternsToObfuscate) > 0 {
return patternsToObfuscate, nil
}
var datacenters []Datacenter
msg, err := nc.Request("datacenter.find", []byte("{}"), time.Second)
if err != nil {
return needles, err
}
err = json.Unmarshal(msg.Data, &datacenters)
if err != nil {
return needles, err
}
if len(datacenters) == 0 {
needles = append(needles, "")
} else {
for _, d := range datacenters {
addDatacenterPatterns(d, &needles)
}
}
patternsToObfuscate = needles
return needles, nil
}
func addDatacenterPatterns(d Datacenter, needles *[]string) {
if d.Credentials.Pwd != "" {
*needles = append(*needles, d.Credentials.Pwd)
}
if d.Credentials.AccessKeyID != "" {
*needles = append(*needles, d.Credentials.AccessKeyID)
}
if d.Credentials.SecretAccessKey != "" {
*needles = append(*needles, d.Credentials.SecretAccessKey)
}
if d.Credentials.SubscriptionID != "" {
*needles = append(*needles, d.Credentials.SubscriptionID)
}
if d.Credentials.AzureEnv != "" {
*needles = append(*needles, d.Credentials.AzureEnv)
}
if d.Credentials.ClientID != "" {
*needles = append(*needles, d.Credentials.ClientID)
}
if d.Credentials.ClientSecret != "" {
*needles = append(*needles, d.Credentials.ClientSecret)
}
if d.Credentials.TenantID != "" {
*needles = append(*needles, d.Credentials.TenantID)
}
}