Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from spotify/20200131-expose-lexer
Expose redactor
- Loading branch information
Showing
4 changed files
with
151 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2020 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import Foundation | ||
|
||
public class LexRedactor: LogRedactor { | ||
private static let redactedTemplate = "/Users/<redacted>/" | ||
private lazy var userDirRegex: NSRegularExpression? = { | ||
do { | ||
return try NSRegularExpression(pattern: "\\/Users\\/(\\w*)\\/") | ||
} catch { | ||
return nil | ||
} | ||
}() | ||
public var userDirToRedact: String? | ||
|
||
public func redactUserDir(string: String) -> String { | ||
guard let regex = userDirRegex else { | ||
return string | ||
} | ||
if let userDirToRedact = userDirToRedact { | ||
return string.replacingOccurrences(of: userDirToRedact, with: Self.redactedTemplate) | ||
} else { | ||
guard let firstMatch = regex.firstMatch(in: string, | ||
options: [], | ||
range: NSRange(location: 0, length: string.count)) else { | ||
return string | ||
} | ||
let userDir = string.substring(firstMatch.range) | ||
userDirToRedact = userDir | ||
return string.replacingOccurrences(of: userDir, with: Self.redactedTemplate) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2020 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
public protocol LogRedactor { | ||
/// Predefined (or inferrred during redation process) user home path. | ||
/// Introduced for better performance. | ||
var userDirToRedact: String? {get set} | ||
|
||
/// Redacts a string by replacing sensitive username path with a template | ||
/// - parameter string: The string to redact | ||
/// - returns: Redacted text with | ||
func redactUserDir(string: String) -> String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2020 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import XCLogParser | ||
|
||
class LexRedactorTests: XCTestCase { | ||
let redactor = LexRedactor() | ||
|
||
func testRedacting() { | ||
|
||
let redactedText = redactor.redactUserDir(string: "Some /Users/private/path") | ||
|
||
XCTAssertEqual(redactedText, "Some /Users/<redacted>/path") | ||
} | ||
|
||
func testMultiplePathsRedacting() { | ||
|
||
let redactedText = redactor.redactUserDir(string: "Some /Users/private/path and other /Users/private/path2") | ||
|
||
XCTAssertEqual(redactedText, "Some /Users/<redacted>/path and other /Users/<redacted>/path2") | ||
} | ||
|
||
func testRedactingFillsUserDir() { | ||
_ = redactor.redactUserDir(string: "Some /Users/private/path") | ||
|
||
XCTAssertEqual(redactor.userDirToRedact, "/Users/private/") | ||
} | ||
|
||
func testPredefinedUserDirIsRedacted() { | ||
redactor.userDirToRedact = "/Users/private/" | ||
|
||
let redactedText = redactor.redactUserDir(string: "Some /Users/private/path") | ||
|
||
XCTAssertEqual(redactedText, "Some /Users/<redacted>/path") | ||
} | ||
|
||
func testNotInPredefinedUserDirIsNotRedacted() { | ||
redactor.userDirToRedact = "/Users/priv/" | ||
|
||
let redactedText = redactor.redactUserDir(string: "Some /Users/private/path") | ||
|
||
XCTAssertEqual(redactedText, "Some /Users/private/path") | ||
} | ||
} |