Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefix errors produced by ong with a constant string #147

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Most recent version is listed first.


## v0.0.12
- Prefix errors produced by ong with a constant string: https://github.com/komuw/ong/pull/147

## v0.0.11
- Add secure/encrypted cookies: https://github.com/komuw/ong/pull/143

Expand Down
10 changes: 5 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (c *Client) log(ctx context.Context, url, method string) func(resp *http.Re
}
}

const errPrefix = "ong"
const errPrefix = "ong/client:"

func ssrfSocketControl(ssrfSafe bool) func(network, address string, conn syscall.RawConn) error {
if !ssrfSafe {
Expand All @@ -200,7 +200,7 @@ func ssrfSocketControl(ssrfSafe bool) func(network, address string, conn syscall

return func(network, address string, conn syscall.RawConn) error {
if !(network == "tcp4" || network == "tcp6") {
return fmt.Errorf("%s: %s is not a safe network type", errPrefix, network)
return fmt.Errorf("%s %s is not a safe network type", errPrefix, network)
}

if err := isSafeAddress(address); err != nil {
Expand All @@ -223,13 +223,13 @@ func isSafeAddress(address string) error {
}

if addr.IsLoopback() {
return fmt.Errorf("%s: address %s IsLoopback", errPrefix, addr)
return fmt.Errorf("%s address %s IsLoopback", errPrefix, addr)
}
if addr.IsLinkLocalUnicast() {
return fmt.Errorf("%s: address %s IsLinkLocalUnicast", errPrefix, addr)
return fmt.Errorf("%s address %s IsLinkLocalUnicast", errPrefix, addr)
}
if addr.IsPrivate() {
return fmt.Errorf("%s: address %s IsPrivate", errPrefix, addr)
return fmt.Errorf("%s address %s IsPrivate", errPrefix, addr)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cry/enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func New(key string) Enc {
//

if len(key) < 4 {
panic(errors.New("short key"))
panic(errors.New("ong/cry: short key"))
}

// derive a key.
Expand Down Expand Up @@ -126,7 +126,7 @@ func (e Enc) Encrypt(plainTextMsg string) (encryptedMsg []byte) {
// Decrypt authenticates and un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decrypted bytes.
func (e Enc) Decrypt(encryptedMsg []byte) (decryptedMsg []byte, err error) {
if len(encryptedMsg) < e.aead.NonceSize() {
return nil, errors.New("ciphertext too short")
return nil, errors.New("ong/cry: ciphertext too short")
}

// get salt
Expand Down
6 changes: 3 additions & 3 deletions cry/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func Eql(password, hash string) error {
params := strings.Split(hash, "$")

if len(params) != 3 {
return errors.New("unable to parse")
return errors.New("ong/cry: unable to parse")
}

pVer, err := strconv.Atoi(params[0])
if err != nil {
return err
}
if pVer != version {
return errors.New("version mismatch")
return errors.New("ong/cry: version mismatch")
}

pSalt, err := hex.DecodeString(params[1])
Expand All @@ -86,5 +86,5 @@ func Eql(password, hash string) error {
return nil
}

return errors.New("password mismatch")
return errors.New("ong/cry: password mismatch")
}
2 changes: 1 addition & 1 deletion middleware/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// errCsrfTokenNotFound is returned when a request using a non-safe http method
// either does not supply a csrf token, or the supplied token is not recognized by the server.
var errCsrfTokenNotFound = errors.New("csrf token not found")
var errCsrfTokenNotFound = errors.New("ong/middleware: csrf token not found")

type csrfContextKey string

Expand Down
4 changes: 2 additions & 2 deletions middleware/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (grw *gzipRW) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := grw.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, fmt.Errorf("http.Hijacker interface is not supported")
return nil, nil, fmt.Errorf("ong/middleware: http.Hijacker interface is not supported")
}

// ReadFrom implements io.ReaderFrom
Expand All @@ -232,7 +232,7 @@ func (grw *gzipRW) Push(target string, opts *http.PushOptions) error {
if p, ok := grw.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return fmt.Errorf("http.Pusher interface is not supported")
return fmt.Errorf("ong/middleware: http.Pusher interface is not supported")
}

// shouldGzipReq checks whether the request is eligible to be gzipped.
Expand Down
2 changes: 1 addition & 1 deletion middleware/loadshed.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func LoadShedder(wrappedHandler http.HandlerFunc) http.HandlerFunc {
p99 := lq.getP99(minSampleSize)
if p99 > breachLatency && !sendProbe {
// drop request
err := fmt.Errorf("server is overloaded, retry after %s", retryAfter)
err := fmt.Errorf("ong/middleware: server is overloaded, retry after %s", retryAfter)
w.Header().Set(ongMiddlewareErrorHeader, fmt.Sprintf("%s. p99latency: %s. breachLatency: %s", err.Error(), p99, breachLatency))
w.Header().Set(retryAfterHeader, fmt.Sprintf("%d", int(retryAfter.Seconds()))) // header should be in seconds(decimal-integer).
http.Error(
Expand Down
4 changes: 2 additions & 2 deletions middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (lrw *logRW) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := lrw.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, fmt.Errorf("http.Hijacker interface is not supported")
return nil, nil, fmt.Errorf("ong/middleware: http.Hijacker interface is not supported")
}

// ReadFrom implements io.ReaderFrom
Expand All @@ -164,7 +164,7 @@ func (lrw *logRW) Push(target string, opts *http.PushOptions) error {
if p, ok := lrw.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return fmt.Errorf("http.Pusher interface is not supported")
return fmt.Errorf("ong/middleware: http.Pusher interface is not supported")
}

// getLogId returns a logID from the request or autogenerated if not available from the request.
Expand Down
2 changes: 1 addition & 1 deletion middleware/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func RateLimiter(wrappedHandler http.HandlerFunc) http.HandlerFunc {
tb := rl.get(host, rateLimiterSendRate)

if !tb.allow() {
err := fmt.Errorf("rate limited, retry after %s", retryAfter)
err := fmt.Errorf("ong/middleware: rate limited, retry after %s", retryAfter)
w.Header().Set(ongMiddlewareErrorHeader, err.Error())
w.Header().Set(retryAfterHeader, fmt.Sprintf("%d", int(retryAfter.Seconds()))) // header should be in seconds(decimal-integer).
http.Error(
Expand Down
3 changes: 0 additions & 3 deletions server/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http/pprof"
"time"

ongErrors "github.com/komuw/ong/errors"
"github.com/komuw/ong/log"
)

Expand Down Expand Up @@ -50,7 +49,6 @@ func startPprofServer(logger log.Logger) {
cfg := listenerConfig()
l, err := cfg.Listen(ctx, "tcp", pprofSrv.Addr)
if err != nil {
err = ongErrors.Wrap(err)
logger.Error(err, log.F{"msg": "pprof server, unable to create listener"})
return
}
Expand All @@ -60,7 +58,6 @@ func startPprofServer(logger log.Logger) {
})
errPprofSrv := pprofSrv.Serve(l)
if errPprofSrv != nil {
errPprofSrv = ongErrors.Wrap(errPprofSrv)
logger.Error(errPprofSrv, log.F{"msg": "unable to start pprof server"})
}
}()
Expand Down
7 changes: 2 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/komuw/ong/automax"
ongErrors "github.com/komuw/ong/errors"
"github.com/komuw/ong/log"

"golang.org/x/sys/unix" // syscall package is deprecated
Expand Down Expand Up @@ -268,7 +267,6 @@ func serve(ctx context.Context, srv *http.Server, o opts, logger log.Logger) err
redirectSrvCfg := listenerConfig()
redirectSrvListener, errL := redirectSrvCfg.Listen(ctx, "tcp", redirectSrv.Addr)
if errL != nil {
errL = ongErrors.Wrap(errL)
logger.Error(errL, log.F{"msg": "redirect server, unable to create listener"})
return
}
Expand All @@ -278,7 +276,6 @@ func serve(ctx context.Context, srv *http.Server, o opts, logger log.Logger) err
})
errRedirectSrv := redirectSrv.Serve(redirectSrvListener)
if errRedirectSrv != nil {
errRedirectSrv = ongErrors.Wrap(errRedirectSrv)
logger.Error(errRedirectSrv, log.F{"msg": "unable to start redirect server"})
}
}()
Expand All @@ -289,7 +286,7 @@ func serve(ctx context.Context, srv *http.Server, o opts, logger log.Logger) err
cfg := listenerConfig()
l, err := cfg.Listen(ctx, o.network, o.serverAddress)
if err != nil {
return ongErrors.Wrap(err)
return err
}
logger.Info(log.F{
"msg": fmt.Sprintf("https server listening at %s", o.serverAddress),
Expand All @@ -300,7 +297,7 @@ func serve(ctx context.Context, srv *http.Server, o opts, logger log.Logger) err
"",
"",
); errS != nil {
return ongErrors.Wrap(errS)
return errS
}
}

Expand Down
19 changes: 9 additions & 10 deletions server/tls_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package server
import (
"context"
"crypto/tls"
"errors"
"fmt"
"os"
"strings"

"golang.org/x/net/idna"

ongErrors "github.com/komuw/ong/errors"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)
Expand Down Expand Up @@ -63,11 +63,10 @@ func getTlsConfig(o opts) (*tls.Config, error) {
// 2. get from disk.
//
if len(o.tls.keyFile) < 1 {
return nil, ongErrors.New("keyFile cannot be empty if certFile is also specified")
return nil, errors.New("ong/server: keyFile cannot be empty if certFile is also specified")
}
c, err := tls.LoadX509KeyPair(o.tls.certFile, o.tls.keyFile)
if err != nil {
err = ongErrors.Wrap(err)
return nil, err
}

Expand All @@ -88,27 +87,27 @@ func getTlsConfig(o opts) (*tls.Config, error) {
}

// 3. non-tls traffic.
return nil, ongErrors.New("ong only serves https")
return nil, errors.New("ong/server: ong only serves https")
}

func validateDomain(domain string) error {
if len(domain) < 1 {
return ongErrors.New("domain cannot be empty")
return errors.New("ong/server: domain cannot be empty")
}
if strings.Count(domain, "*") > 1 {
return ongErrors.New("domain can only contain one wildcard character")
return errors.New("ong/server: domain can only contain one wildcard character")
}
if strings.Contains(domain, "*") && !strings.HasPrefix(domain, "*") {
return ongErrors.New("wildcard character should be a prefix")
return errors.New("ong/server: wildcard character should be a prefix")
}
if strings.Contains(domain, "*") && domain[1] != '.' {
return ongErrors.New("wildcard character should be followed by a `.` character")
return errors.New("ong/server: wildcard character should be followed by a `.` character")
}

if !strings.Contains(domain, "*") {
// not wildcard
if _, err := idna.Lookup.ToASCII(domain); err != nil {
return ongErrors.Wrap(err)
return err
}
}

Expand Down Expand Up @@ -167,7 +166,7 @@ func customHostWhitelist(domain string) autocert.HostPolicy {
}
}

return ongErrors.New(fmt.Sprintf("ong/acme: host %q not configured in HostWhitelist", host))
return fmt.Errorf("ong/server: host %q not configured in HostWhitelist", host)
}
}

Expand Down