From 1b278fa92b3e3cbd2a57ea481f3a28212cde90a3 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sun, 28 Jun 2015 16:54:38 -0400 Subject: [PATCH] Move logger lib to web-core repo --- .gitignore | 2 ++ LICENSE | 21 +++++++++++++++++++++ README.md | 3 +++ logger/logger.go | 14 ++++++++++++++ logger/logger_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 logger/logger.go create mode 100644 logger/logger_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b72f9be --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +*.swp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eb1cf67 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Write.as + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..16ef6d4 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Write.as web core +================= +[![Build Status](https://travis-ci.org/writeas/web-core.svg)](https://travis-ci.org/writeas/web-core) diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..d5214af --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,14 @@ +package logger + +import ( + "regexp" +) + +var postReg = regexp.MustCompile("(.*(/|id=))[a-zA-Z0-9]{12,13}(.*)") +var tokenReg = regexp.MustCompile("(.*t=)[a-zA-Z0-9]{32}(.*)") + +func ScrubID(uri string) string { + curStr := postReg.ReplaceAllString(uri, "$1[scrubbed]$3") + curStr = tokenReg.ReplaceAllString(curStr, "$1[scrubbed]$2") + return curStr +} diff --git a/logger/logger_test.go b/logger/logger_test.go new file mode 100644 index 0000000..f67155c --- /dev/null +++ b/logger/logger_test.go @@ -0,0 +1,39 @@ +package logger + +import ( + "testing" +) + +type scrubTest struct { + Input string + Expected string +} + +var uris = []scrubTest{ + scrubTest{Input: "/1234567890123", Expected: "/[scrubbed]"}, + scrubTest{Input: "/acnsd8ndsklao", Expected: "/[scrubbed]"}, + scrubTest{Input: "/ACNSD8NDSKLAO", Expected: "/[scrubbed]"}, + scrubTest{Input: "/acNsD8NdSKlaO", Expected: "/[scrubbed]"}, + scrubTest{Input: "/acnsd8ndsklao.txt", Expected: "/[scrubbed].txt"}, + scrubTest{Input: "/8sj2kkjsn192.json", Expected: "/[scrubbed].json"}, + scrubTest{Input: "/acnsd8Ndsklao", Expected: "/[scrubbed]"}, + scrubTest{Input: "/12345678901", Expected: "/12345678901"}, + scrubTest{Input: "GET /8s9dja0vjbklj", Expected: "GET /[scrubbed]"}, + scrubTest{Input: "POST /8s9dja0vjbklj?delete=true", Expected: "POST /[scrubbed]?delete=true"}, + scrubTest{Input: "GET /8s9dja0vjbkl", Expected: "GET /[scrubbed]"}, + scrubTest{Input: "GET /asdf90as.txt", Expected: "GET /asdf90as.txt"}, + scrubTest{Input: "GET /api/999999999999", Expected: "GET /api/[scrubbed]"}, + scrubTest{Input: "DELETE /api/?id=8s9dja0vjbkl&t=123456789012345678901234567890ab", Expected: "DELETE /api/?id=[scrubbed]&t=[scrubbed]"}, + scrubTest{Input: "DELETE /api/8s9dja0vjbkl?t=123456789012345678901234567890ab", Expected: "DELETE /api/[scrubbed]?t=[scrubbed]"}, +} + +func TestScrubID(t *testing.T) { + var scrubRes string + + for i := range uris { + scrubRes = ScrubID(uris[i].Input) + if scrubRes != uris[i].Expected { + t.Errorf("#%d got %v, expected %v", i, scrubRes, uris[i].Expected) + } + } +}