Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e118bfdb0 | |||
|
|
e0868841bf | ||
|
|
c558405d11 | ||
|
|
a1eb035af7 | ||
| 5eedcff042 | |||
|
|
6ac8eb04b4 | ||
|
|
4a4e4cd3fd | ||
|
|
1207093a56 | ||
| 2b6ae03d1a |
@@ -1,8 +1,17 @@
|
|||||||
|
arch:
|
||||||
|
- amd64
|
||||||
|
- ppc64le
|
||||||
sudo: false
|
sudo: false
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- tip
|
- tip
|
||||||
- 1.9
|
- 1.9
|
||||||
|
jobs:
|
||||||
|
exclude:
|
||||||
|
- go: 1.9
|
||||||
|
arch: amd64
|
||||||
|
- go: 1.9
|
||||||
|
arch: ppc64le
|
||||||
script:
|
script:
|
||||||
- go get golang.org/x/lint/golint
|
- go get golang.org/x/lint/golint
|
||||||
- go get golang.org/x/tools/cmd/cover
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
// Package config implements a simple global configuration system that
|
// Package config implements a simple global configuration system that
|
||||||
// supports a file with key=value pairs and environment variables. Note
|
// supports a file with key=value pairs and environment variables. Note
|
||||||
// that the config system is global.
|
// that the config system is global.
|
||||||
|
//
|
||||||
|
// This package is intended to be used for small daemons: some configuration
|
||||||
|
// file is optionally populated at program start, then this is used to
|
||||||
|
// transparently look up configuration values from either that file or the
|
||||||
|
// environment.
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -82,3 +87,24 @@ func GetDefault(key, def string) string {
|
|||||||
}
|
}
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Require retrieves a value from either a configuration file or the
|
||||||
|
// environment. If the key isn't present, it will call log.Fatal, printing
|
||||||
|
// the missing key.
|
||||||
|
func Require(key string) string {
|
||||||
|
if v, ok := vars[key]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := os.LookupEnv(prefix + key)
|
||||||
|
if !ok {
|
||||||
|
var envMessage string
|
||||||
|
if prefix != "" {
|
||||||
|
envMessage = " (note: looked for the key " + prefix + key
|
||||||
|
envMessage += " in the local env)"
|
||||||
|
}
|
||||||
|
log.Fatalf("missing required configuration value %s%s", key, envMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|||||||
@@ -277,3 +277,60 @@ func (lw *LogWriter) SetLevel(l Level) {
|
|||||||
|
|
||||||
// Close is a no-op that satisfies the Logger interface.
|
// Close is a no-op that satisfies the Logger interface.
|
||||||
func (lw *LogWriter) Close() error { return nil }
|
func (lw *LogWriter) Close() error { return nil }
|
||||||
|
|
||||||
|
// Multi allows combining of loggers.
|
||||||
|
type Multi struct {
|
||||||
|
loggers []Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMulti(loggers ...Logger) *Multi {
|
||||||
|
return &Multi{loggers: loggers}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Debug(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Debug(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Info(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Info(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Warn(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Warn(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Error(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Error(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Critical(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Critical(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) Fatal(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.Fatal(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) FatalCode(exitcode int, actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.FatalCode(exitcode, actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Multi) FatalNoDie(actor, event string, attrs map[string]string) {
|
||||||
|
for _, l := range m.loggers {
|
||||||
|
l.FatalNoDie(actor, event, attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
49
rand/rand.go
Normal file
49
rand/rand.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
// Package rand contains utilities for interacting with math/rand, including
|
||||||
|
// seeding from a random sed.
|
||||||
|
package rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
mrand "math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CryptoUint64 generates a cryptographically-secure 64-bit integer.
|
||||||
|
func CryptoUint64() (uint64, error) {
|
||||||
|
bs := make([]byte, 8)
|
||||||
|
_, err := rand.Read(bs)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return binary.BigEndian.Uint64(bs), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed initialises the non-cryptographic PRNG with a random,
|
||||||
|
// cryptographically secure value. This is done just as a good
|
||||||
|
// way to make this random. The returned 64-bit value is the seed.
|
||||||
|
func Seed() (uint64, error) {
|
||||||
|
seed, err := CryptoUint64()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NB: this is permitted.
|
||||||
|
mrand.Seed(int64(seed))
|
||||||
|
return seed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int is a wrapper for math.Int so only one package needs to be imported.
|
||||||
|
func Int() int {
|
||||||
|
return mrand.Int()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intn is a wrapper for math.Intn so only one package needs to be imported.
|
||||||
|
func Intn(max int) int {
|
||||||
|
return mrand.Intn(max)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intn2 returns a random value between min and max, inclusive.
|
||||||
|
func Intn2(min, max int) int {
|
||||||
|
return Intn(max-min) + min
|
||||||
|
}
|
||||||
74
rand/rand_test.go
Normal file
74
rand/rand_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
mrand "math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCryptoUint64(t *testing.T) {
|
||||||
|
n1, err := CryptoUint64()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
n2, err := CryptoUint64()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This has such a low chance of occurring that it's likely to be
|
||||||
|
// indicative of a bad CSPRNG.
|
||||||
|
if n1 == n2 {
|
||||||
|
t.Fatalf("repeated random uint64s: %d", n1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntn(t *testing.T) {
|
||||||
|
expected := []int{3081, 4887, 4847, 1059, 3081}
|
||||||
|
mrand.Seed(1)
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
n := Intn2(1000, 5000)
|
||||||
|
|
||||||
|
if n != expected[i] {
|
||||||
|
fmt.Printf("invalid sequence at %d: expected %d, have %d", i, expected[i], n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeed(t *testing.T) {
|
||||||
|
seed1, err := Seed()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var seed2 uint64
|
||||||
|
n1 := Int()
|
||||||
|
tries := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
seed2, err = Seed()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if seed1 != seed2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
tries++
|
||||||
|
|
||||||
|
if tries > 3 {
|
||||||
|
t.Fatal("can't generate two unique seeds")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
n2 := Int()
|
||||||
|
|
||||||
|
// Again, this not impossible, merely statistically improbably and a
|
||||||
|
// potential canary for RNG issues.
|
||||||
|
if n1 == n2 {
|
||||||
|
t.Fatalf("repeated integers fresh from two unique seeds: %d/%d -> %d",
|
||||||
|
seed1, seed2, n1)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user