simplify output writing, minor changes

pull/13/head
Simon Let 6 years ago
parent a7f1555daf
commit 0fb4d46174
  1. 34
      sanitize-history/resh-sanitize-history.go

@ -65,9 +65,9 @@ func main() {
defer inputFile.Close() defer inputFile.Close()
var writer *bufio.Writer var writer *bufio.Writer
useStdout := true if *outputPath == "" {
if len(*outputPath) > 0 { writer = bufio.NewWriter(os.Stdout)
useStdout = false } else {
outputFile, err := os.Create(*outputPath) outputFile, err := os.Create(*outputPath)
if err != nil { if err != nil {
log.Fatal("Create() output file error:", err) log.Fatal("Create() output file error:", err)
@ -75,6 +75,7 @@ func main() {
defer outputFile.Close() defer outputFile.Close()
writer = bufio.NewWriter(outputFile) writer = bufio.NewWriter(outputFile)
} }
defer writer.Flush()
scanner := bufio.NewScanner(inputFile) scanner := bufio.NewScanner(inputFile)
for scanner.Scan() { for scanner.Scan() {
@ -100,9 +101,6 @@ func main() {
log.Println("Line:", line) log.Println("Line:", line)
log.Fatal("Encoding error:", err) log.Fatal("Encoding error:", err)
} }
if useStdout {
fmt.Println(string(outLine))
} else {
// fmt.Println(string(outLine)) // fmt.Println(string(outLine))
n, err := writer.WriteString(string(outLine) + "\n") n, err := writer.WriteString(string(outLine) + "\n")
if err != nil { if err != nil {
@ -113,10 +111,6 @@ func main() {
} }
} }
} }
if useStdout == false {
writer.Flush()
}
}
type sanitizer struct { type sanitizer struct {
hashLength int hashLength int
@ -146,6 +140,7 @@ func loadData(fname string) map[string]bool {
} }
func (s *sanitizer) sanitizeRecord(record *common.Record) error { func (s *sanitizer) sanitizeRecord(record *common.Record) error {
// hash directories of the paths
record.Pwd = s.sanitizePath(record.Pwd) record.Pwd = s.sanitizePath(record.Pwd)
record.RealPwd = s.sanitizePath(record.RealPwd) record.RealPwd = s.sanitizePath(record.RealPwd)
record.PwdAfter = s.sanitizePath(record.PwdAfter) record.PwdAfter = s.sanitizePath(record.PwdAfter)
@ -155,6 +150,7 @@ func (s *sanitizer) sanitizeRecord(record *common.Record) error {
record.Home = s.sanitizePath(record.Home) record.Home = s.sanitizePath(record.Home)
record.ShellEnv = s.sanitizePath(record.ShellEnv) record.ShellEnv = s.sanitizePath(record.ShellEnv)
// hash the most sensitive info, do not tokenize
record.Host = s.hashToken(record.Host) record.Host = s.hashToken(record.Host)
record.Login = s.hashToken(record.Login) record.Login = s.hashToken(record.Login)
record.MachineId = s.hashToken(record.MachineId) record.MachineId = s.hashToken(record.MachineId)
@ -177,6 +173,7 @@ func (s *sanitizer) sanitizeRecord(record *common.Record) error {
log.Fatal("Cmd:", record.CmdLine, "; sanitization error:", err) log.Fatal("Cmd:", record.CmdLine, "; sanitization error:", err)
} }
// add a flag to signify that the record has been sanitized
record.Sanitized = true record.Sanitized = true
return nil return nil
} }
@ -347,23 +344,24 @@ func (s *sanitizer) sanitizeCmdToken(token string) (string, error) {
} }
isLettersOrDigits := true isLettersOrDigits := true
isDigits := true // isDigits := true
isOtherCharacters := true isOtherCharacters := true
for _, r := range token { for _, r := range token {
if unicode.IsDigit(r) == false && unicode.IsLetter(r) == false { if unicode.IsDigit(r) == false && unicode.IsLetter(r) == false {
isLettersOrDigits = false isLettersOrDigits = false
isDigits = false // isDigits = false
}
if unicode.IsDigit(r) == false {
isDigits = false
} }
// if unicode.IsDigit(r) == false {
// isDigits = false
// }
if unicode.IsDigit(r) || unicode.IsLetter(r) { if unicode.IsDigit(r) || unicode.IsLetter(r) {
isOtherCharacters = false isOtherCharacters = false
} }
} }
if isDigits { // I decided that I don't want a special sanitization for numbers
return s.hashNumericToken(token), nil // if isDigits {
} // return s.hashNumericToken(token), nil
// }
if isLettersOrDigits { if isLettersOrDigits {
return s.hashToken(token), nil return s.hashToken(token), nil
} }

Loading…
Cancel
Save