github.com/Axway/agent-sdk@v1.1.101/pkg/traceability/redaction/globalredaction.go (about) 1 package redaction 2 3 import ( 4 "regexp" 5 6 "github.com/Axway/agent-sdk/pkg/util/log" 7 ) 8 9 // Global Agent redactions 10 var agentRedactions Redactions 11 12 // SetupGlobalRedaction - set up redactionRegex based on the redactionConfig 13 func SetupGlobalRedaction(cfg Config) error { 14 var err error 15 agentRedactions, err = cfg.SetupRedactions() 16 return err 17 } 18 19 func setupShowRegex(showFilters []Show) ([]showRegex, error) { 20 newShowRegex := make([]showRegex, 0) 21 for _, filter := range showFilters { 22 if filter.KeyMatch == "" { 23 continue // ignore blank keymatches as they match nothing 24 } 25 kc, err := regexp.Compile(filter.KeyMatch) 26 if err != nil { 27 err = ErrInvalidRegex.FormatError("keyMatch", filter.KeyMatch, err) 28 log.Error(err) 29 return []showRegex{}, err 30 } 31 32 newShowRegex = append(newShowRegex, showRegex{ 33 keyMatch: kc, 34 }) 35 } 36 return newShowRegex, nil 37 } 38 39 func setupSanitizeRegex(sanitizeFilters []Sanitize) ([]sanitizeRegex, error) { 40 newSanitizeRegex := make([]sanitizeRegex, 0) 41 for _, filter := range sanitizeFilters { 42 if filter.KeyMatch == "" { 43 continue // ignore blank keymatches as they match nothing 44 } 45 kc, err := regexp.Compile(filter.KeyMatch) 46 if err != nil { 47 err = ErrInvalidRegex.FormatError("keyMatch", filter.KeyMatch, err) 48 log.Error(err) 49 return []sanitizeRegex{}, err 50 } 51 52 vc, err := regexp.Compile(filter.ValueMatch) 53 if err != nil { 54 err = ErrInvalidRegex.FormatError("valueMatch", filter.ValueMatch, err) 55 log.Error(err) 56 return []sanitizeRegex{}, err 57 58 } 59 60 newSanitizeRegex = append(newSanitizeRegex, sanitizeRegex{ 61 keyMatch: kc, 62 valueMatch: vc, 63 }) 64 } 65 return newSanitizeRegex, nil 66 } 67 68 // URIRedaction - takes a uri and returns the redacted version of that URI 69 func URIRedaction(fullURI string) (string, error) { 70 if agentRedactions == nil { 71 return "", ErrGlobalRedactionCfg 72 } 73 return agentRedactions.URIRedaction(fullURI) 74 } 75 76 // PathRedaction - returns a string that has only allowed path elements 77 func PathRedaction(path string) (string, error) { 78 if agentRedactions == nil { 79 return "", ErrGlobalRedactionCfg 80 } 81 return agentRedactions.PathRedaction(path), nil 82 } 83 84 // QueryArgsRedaction - accepts a string for arguments and returns the same string with redacted 85 func QueryArgsRedaction(args map[string][]string) (map[string][]string, error) { 86 if agentRedactions == nil { 87 return map[string][]string{}, ErrGlobalRedactionCfg 88 } 89 return agentRedactions.QueryArgsRedaction(args) 90 } 91 92 // QueryArgsRedactionString - accepts a string for arguments and returns the same string with redacted 93 func QueryArgsRedactionString(args string) (string, error) { 94 if agentRedactions == nil { 95 return "", ErrGlobalRedactionCfg 96 } 97 return agentRedactions.QueryArgsRedactionString(args) 98 } 99 100 // RequestHeadersRedaction - accepts a map of response headers and returns the redacted and sanitize map 101 func RequestHeadersRedaction(headers map[string]string) (map[string]string, error) { 102 if agentRedactions == nil { 103 return map[string]string{}, ErrGlobalRedactionCfg 104 } 105 return agentRedactions.RequestHeadersRedaction(headers) 106 } 107 108 // ResponseHeadersRedaction - accepts a map of response headers and returns the redacted and sanitize map 109 func ResponseHeadersRedaction(headers map[string]string) (map[string]string, error) { 110 if agentRedactions == nil { 111 return map[string]string{}, ErrGlobalRedactionCfg 112 } 113 return agentRedactions.ResponseHeadersRedaction(headers) 114 } 115 116 // JMSPropertiesRedaction - accepts a map of response headers and returns the redacted and sanitize map 117 func JMSPropertiesRedaction(properties map[string]string) (map[string]string, error) { 118 if agentRedactions == nil { 119 return map[string]string{}, ErrGlobalRedactionCfg 120 } 121 return agentRedactions.JMSPropertiesRedaction(properties) 122 } 123 124 func isValidValueToShow(value string, matchers []showRegex) bool { 125 for _, matcher := range matchers { 126 if matcher.keyMatch.MatchString(value) { 127 return true 128 } 129 } 130 return false 131 } 132 133 func shouldSanitize(value string, matchers []sanitizeRegex) (bool, *regexp.Regexp) { 134 for _, matcher := range matchers { 135 if matcher.keyMatch.MatchString(value) { 136 return true, matcher.valueMatch 137 } 138 } 139 return false, nil 140 }