Real-World Examples
Here are some realistic examples of using Censor in common scenarios where data sanitization is crucial.
Payment Processing System
In a payment processing system, you need to handle sensitive financial information while ensuring it's properly masked in logs:
package main
import (
"github.com/vpakhuchyi/censor"
censorlog "github.com/vpakhuchyi/censor/handlers/zap"
"go.uber.org/zap"
)
type PaymentRequest struct {
OrderID string `censor:"display"` // Safe to display
Amount float64 `censor:"display"` // Safe to display
Currency string `censor:"display"` // Safe to display
CardNumber string // Sensitive - will be masked
CVV string // Sensitive - will be masked
ExpiryDate string // Sensitive - will be masked
BillingInfo BillingInfo
}
type BillingInfo struct {
Name string `censor:"display"` // Safe to display
Email string // Sensitive - will be masked
Address string // Sensitive - will be masked
}
func main() {
// Configure Censor with credit card pattern
cfg := censor.Config{
Formatter: censor.FormatterConfig{
MaskValue: "[REDACTED]",
ExcludePatterns: []string{
`\d{4}-\d{4}-\d{4}-\d{4}`, // Credit card pattern
`\d{3,4}`, // CVV pattern
},
},
}
// Initialize logger with Censor
logger := initLogger()
// Process payment
payment := PaymentRequest{
OrderID: "ORD-123456",
Amount: 99.99,
Currency: "USD",
CardNumber: "4111-1111-1111-1111",
CVV: "123",
ExpiryDate: "12/25",
BillingInfo: BillingInfo{
Name: "John Doe",
Email: "john@example.com",
Address: "123 Main St, City, Country",
},
}
// Log payment processing - sensitive data will be masked
logger.Info("Processing payment",
zap.Any("payment", payment),
)
// Output will mask card number, CVV, and personal information
}
func initLogger() *zap.Logger {
// Logger initialization code...
}
User Authentication System
Example of handling user authentication data with proper masking of sensitive information:
package main
import (
"encoding/json"
"net/http"
"github.com/vpakhuchyi/censor"
)
type AuthRequest struct {
Username string `censor:"display" json:"username"`
Password string `json:"password"` // Will be masked
Token string `json:"token"` // Will be masked
Device DeviceInfo `json:"device"`
}
type DeviceInfo struct {
ID string `censor:"display" json:"id"`
IP string `json:"ip"` // Will be masked
UserAgent string `censor:"display" json:"user_agent"`
}
type AuthResponse struct {
Success bool `censor:"display" json:"success"`
Token string `json:"token"` // Will be masked
}
func handleAuth(w http.ResponseWriter, r *http.Request) {
var req AuthRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Log the request with sensitive data masked
logger.Info("Auth request received",
"request", censor.Format(req),
)
// Process authentication...
resp := AuthResponse{
Success: true,
Token: "eyJhbGciOiJIUzI1NiIs...",
}
// Log the response with sensitive data masked
logger.Info("Auth response",
"response", censor.Format(resp),
)
json.NewEncoder(w).Encode(resp)
}
Healthcare Data Processing
Example of handling sensitive healthcare information:
package main
import (
"github.com/vpakhuchyi/censor"
)
type PatientRecord struct {
PatientID string `censor:"display"` // Safe to display
VisitDate string `censor:"display"` // Safe to display
Department string `censor:"display"` // Safe to display
// Sensitive information - will be masked
Name string
SSN string
Diagnosis string
Medications []string
Notes string
// Contact information - will be masked
Contact ContactInfo
}
type ContactInfo struct {
Phone string
Email string
Address string
}
func processPatientRecord(record PatientRecord) {
// Configure Censor with healthcare-specific patterns
cfg := censor.Config{
Formatter: censor.FormatterConfig{
MaskValue: "[PHI REDACTED]",
ExcludePatterns: []string{
`\d{3}-\d{2}-\d{4}`, // SSN pattern
`\d{3}[-.]?\d{3}[-.]?\d{4}`, // Phone number pattern
},
},
}
censor.NewWithOpts(censor.WithConfig(&cfg))
// Process and log the record
// All sensitive health information will be masked
logger.Info("Processing patient record",
"record", censor.Format(record),
)
}
Best Practices Demonstrated
These examples demonstrate several best practices:
- Selective Masking: Using
censor:"display"
tag to show safe information while masking sensitive data - Pattern Matching: Using regex patterns to catch sensitive data formats
- Nested Structures: Proper handling of nested sensitive information
- Integration: Seamless integration with logging and HTTP handlers
- Compliance: Adhering to data protection requirements (GDPR, HIPAA, PCI DSS)
Each example shows how Censor can be used in real-world scenarios where data protection is critical. The examples cover different aspects of data sensitivity and show how to properly configure Censor for each use case.