Skip to content

Commit

Permalink
logging refactor
Browse files Browse the repository at this point in the history
drop some of the examples, as the hex examples are not much different
than the base64 examples, and are easy to extrapolate.
  • Loading branch information
dropwhile committed Aug 24, 2023
1 parent d2fc69d commit cd940a9
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 138 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ toc::[]

== HEAD
* fix htrie matching of non punycode (eg. unicode) idna hostnames
* slightly faster logging (update to mlog dependency)
* address a logging issue with missing url path output in
`"built outgoing request"` debug log

== v2.4.4 - 2023-07-25
* update dependencies
Expand Down
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ Application Options:
-H, --header= Add additional header to each response. This option can
be used multiple times to add multiple headers
--listen= Address:Port to bind to for HTTP (default: 0.0.0.0:8080)
--socket-listen= Path for unix domain socket to bind to for HTTP
--ssl-listen= Address:Port to bind to for HTTPS/SSL/TLS
--socket-listen= Path for unix domain socket to bind to for HTTP
--ssl-key= ssl private key (key.pem) path
--ssl-cert= ssl cert (cert.pem) path
--max-size= Max allowed response size (KB)
Expand Down
1 change: 1 addition & 0 deletions examples/go-base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

package main
Expand Down
34 changes: 0 additions & 34 deletions examples/go-hex.go

This file was deleted.

25 changes: 0 additions & 25 deletions examples/python-base64.py

This file was deleted.

22 changes: 0 additions & 22 deletions examples/python-hex.py

This file was deleted.

11 changes: 8 additions & 3 deletions examples/python3-base64-filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
CAMO_HOST = 'https://img.example.com'


def wrap_encode(data):
"""A little helper method to wrap b64encoding"""
return base64.urlsafe_b64encode(data).strip(b'=').decode('utf-8')


def camo_url(hmac_key, image_url):
url = urlsplit(image_url)

Expand All @@ -32,10 +37,10 @@ def camo_url(hmac_key, image_url):
hmac_key = hmac_key.encode() if isinstance(hmac_key, str) else hmac_key
image_url = image_url.encode() if isinstance(image_url, str) else image_url

b64digest = base64.urlsafe_b64encode(
b64digest = wrap_encode(
hmac.new(hmac_key, image_url, hashlib.sha1).digest()
).strip(b'=').decode('utf-8')
b64url = base64.urlsafe_b64encode(image_url).strip(b'=').decode('utf-8')
)
b64url = wrap_encode(image_url)
requrl = '%s/%s/%s' % (CAMO_HOST, b64digest, b64url)
return requrl

Expand Down
43 changes: 43 additions & 0 deletions examples/python3-base64.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2012-2019 Eli Janssen
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file.

import hashlib
import hmac
import base64


CAMO_HOST = 'https://img.example.com'


def wrap_encode(data):
"""A little helper method to wrap b64encoding"""
return base64.urlsafe_b64encode(data).strip(b'=').decode('utf-8')


def camo_url(hmac_key, image_url):
if image_url.startswith("https:"):
return image_url

hmac_key = hmac_key.encode() if isinstance(hmac_key, str) else hmac_key
image_url = image_url.encode() if isinstance(image_url, str) else image_url

# setup the hmac construction
mac = hmac.new(hmac_key, digestmod=hashlib.sha1)
# add image_url
mac.update(image_url)

# generate digest
digest = mac.digest()

## now build url
b64digest = wrap_encode(digest)
b64url = wrap_encode(image_url)
requrl = '%s/%s/%s' % (CAMO_HOST, b64digest, b64url)
return requrl


print(
camo_url("test", "http://golang.org/doc/gopher/frontpage.png")
)
# https://img.example.org/D23vHLFHsOhPOcvdxeoQyAJTpvM/aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n
19 changes: 0 additions & 19 deletions examples/ruby-hex.rb

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cactus/go-camo/v2
go 1.19

require (
github.com/cactus/mlog v1.0.6
github.com/cactus/mlog v1.0.9
github.com/jessevdk/go-flags v1.5.0
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.44.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cactus/mlog v1.0.5 h1:TjaBRUD0ToZMTzv30SRfwDJfvz3IxDUxcw1dNk6dKAQ=
github.com/cactus/mlog v1.0.5/go.mod h1:z0zgmK5rbywKn+lJPzhLEFZOJejzfRBBW1RqC/1i588=
github.com/cactus/mlog v1.0.6 h1:e119rK5LhOyV/mER2Jx7T0QNT314kyziCSokiiTrq0o=
github.com/cactus/mlog v1.0.6/go.mod h1:z0zgmK5rbywKn+lJPzhLEFZOJejzfRBBW1RqC/1i588=
github.com/cactus/mlog v1.0.9 h1:34fiSfH7Ds9N50fXP2/c5xNvp1mZBZZCda9GJ1+fwxE=
github.com/cactus/mlog v1.0.9/go.mod h1:z0zgmK5rbywKn+lJPzhLEFZOJejzfRBBW1RqC/1i588=
github.com/cactus/tai64 v1.0.2 h1:c5rm3aQ9z3b6Vva2LXRSICx/Rpu9rj4MHEzRG1g7dK0=
github.com/cactus/tai64 v1.0.2/go.mod h1:gu5LAXd6eWwrRD/HPw+aTrJF5WkieYswRVLSNslKGg4=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down
5 changes: 3 additions & 2 deletions man/go-camo.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ For exmaple, if the HMAC key is provided on the command line, it will override
*-H*, *--header*=<__HEADER__>::
+
--
Extra header to return for each response. This option can be used
multiple times to add multiple headers.
Add additional header to each response.
This option can be used multiple times to add multiple headers.
When specified, these headers are set unconditionally on all responses.

See __<<ADD_HEADERS>>__ for more info.
--
Expand Down
1 change: 0 additions & 1 deletion pkg/camo/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func bodyAssert(t *testing.T, expected string, resp *http.Response) {

func headerAssert(t *testing.T, expected, name string, resp *http.Response) {
assert.Check(t, is.Equal(expected, resp.Header.Get(name)), "Expected response header mismatch")

}

func statusCodeAssert(t *testing.T, expected int, resp *http.Response) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/camo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func httpReqToMlogMap(req *http.Request) mlog.Map {
return mlog.Map{
"method": req.Method,
"path": req.RequestURI,
"path": req.URL.Path,
"proto": req.Proto,
"header": req.Header,
"content_length": req.ContentLength,
Expand Down
Loading

0 comments on commit cd940a9

Please sign in to comment.