SpacePod 23 Obfuscated Logging
Whenever we run our app it debug mode it prints every network request and response. The request and response contain the apiKey. It’s only a matter of time until I accidentally leak it during a screencast. Let’s fix that right now by replacing all occurrences of our apiKey with something else.
Problem
- Our apiKey get printed to the console whenever we make a request.
Goals
- Obfuscate the key before we print it so that I don’t accidentally show it on screen.
Steps
- Move our print statement inside a private function.
- Ensure we only print in production.
- Replace our api key with a slug.
- Clean up our existing print statements.
1, 2, 3
private func log(_ string: String) {
#if DEBUG
print(string.replacingOccurrences(of: apiKey, with: "?api_key=" + "YOUR_OBFUSCATED_API_KEY"))
#endif
}
4
log("🌎 request: " + request.debugDescription)
log("🌎 response: " + response.debugDescription)
log("🌎 error: " + error.localizedDescription)
Network.swift Complete
import Foundation
class Network {
let baseUrl = "https://api.nasa.gov/planetary/apod"
let apiKey = "?api_key=" + (File.data(from: "Secrets", withExtension: .json)?.toSecret?.apiKey.description ?? "DEMO_KEY")
let count = "&count=20"
let thumbs = "&thumbs=true"
func getPods() async -> [Pod]? {
let url = URL(string: "\(baseUrl)\(apiKey)\(count)\(thumbs)")!
do {
let request = URLRequest(url: url)
log("🌎 request: " + request.debugDescription)
let (data, response) = try await URLSession.shared.data(for: request)
log("🌎 response: " + response.debugDescription)
return data.toPods
}
catch {
log("🌎 error: " + error.localizedDescription)
return nil
}
}
private func log(_ string: String) {
#if DEBUG
print(string.replacingOccurrences(of: apiKey, with: "?api_key=" + "YOUR_OBFUSCATED_API_KEY"))
#endif
}
}