-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper, hash, versioner and logger tools were added
- Loading branch information
Showing
14 changed files
with
769 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module github.com/gateway-fm/scriptorium | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/gofrs/uuid v4.2.0+incompatible | ||
go.uber.org/zap v1.19.1 | ||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e | ||
) | ||
|
||
require ( | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.6.0 // indirect | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= | ||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= | ||
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= | ||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= | ||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= | ||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4= | ||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= | ||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= | ||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= | ||
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI= | ||
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8= | ||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= | ||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= | ||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package hash | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
"golang.org/x/crypto/sha3" | ||
|
||
"golang.org/x/crypto/blake2b" | ||
|
||
"golang.org/x/crypto/argon2" | ||
) | ||
|
||
type hashParams struct { | ||
memory uint32 | ||
iterations uint32 | ||
parallelism uint8 | ||
keyLength uint32 | ||
} | ||
|
||
// constant salt from GUID: f060a42abac94a3bab8d1e7a9ba1d556 | ||
var salt = []byte{102, 48, 54, 48, 97, 52, 50, 97, 98, 97, 99, 57, 52, 97, 51, 98, 97, 98, 56, 100, 49, 101, 55, 97, 57, 98, 97, 49, 100, 53, 53, 54} | ||
|
||
func getHashParams() *hashParams { | ||
return &hashParams{ | ||
memory: 1 * 1024, | ||
iterations: 2, | ||
parallelism: 2, | ||
keyLength: 32, | ||
} | ||
} | ||
|
||
// GenerateHashArgon2 hashes the secret and returns base64 of the hash | ||
func GenerateHashArgon2(secret string) (encodedHash string, err error) { | ||
p := getHashParams() | ||
hash := argon2.Key([]byte(secret), salt, p.iterations, p.memory, p.parallelism, p.keyLength) | ||
b64Hash := base64.RawStdEncoding.EncodeToString(hash) | ||
return b64Hash, nil | ||
} | ||
|
||
// GenerateHashBlake2b hashes the secret and returns base64 of the hash | ||
func GenerateHashBlake2b(secret string) (encodedHash string) { | ||
h := blake2b.Sum512([]byte(secret)) | ||
return base64.URLEncoding.EncodeToString(h[:]) | ||
} | ||
|
||
// GenerateHashSha512 hashes the secret and returns base64 of the hash | ||
func GenerateHashSha512(secret string) (encodedHash string) { | ||
h := sha3.Sum512([]byte(secret)) | ||
return base64.URLEncoding.EncodeToString(h[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package hash | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const testString = "examplestringtohash" | ||
|
||
// nolint | ||
func Benchmark_Argon2(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
GenerateHashArgon2(testString) | ||
} | ||
} | ||
|
||
// nolint | ||
func Benchmark_Blake2b(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
GenerateHashBlake2b(testString) | ||
} | ||
} | ||
|
||
// nolint | ||
func Benchmark_Sha512(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
GenerateHashSha512(testString) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/gofrs/uuid" | ||
) | ||
|
||
// ContextKey is used for context.Context value. The value requires a key that is not primitive type. | ||
type ContextKey string | ||
|
||
// ContextKeyRequestID is the ContextKey for RequestID | ||
const ContextKeyRequestID ContextKey = "requestID" | ||
|
||
const RequestIDPrefix string = "reqid://" | ||
|
||
// SetRequestID will attach a brand new request ID to a http request | ||
func SetRequestID(ctx context.Context) context.Context { | ||
reqID, err := uuid.NewV4() | ||
if err != nil { | ||
return ctx | ||
} | ||
return context.WithValue(ctx, ContextKeyRequestID, RequestIDPrefix+reqID.String()) | ||
} | ||
|
||
// GetRequestID will get reqID from a http request and return it as a string | ||
func GetRequestID(ctx context.Context) string { | ||
reqID := ctx.Value(ContextKeyRequestID) | ||
if ret, ok := reqID.(string); ok { | ||
return ret | ||
} | ||
return "" | ||
} | ||
|
||
func GetContextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { | ||
requestId := GetRequestID(ctx) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
ctx = context.WithValue(ctx, ContextKeyRequestID, requestId) | ||
return ctx, cancel | ||
} | ||
|
||
// DVFAuthentication is used for context.Context value. | ||
type DVFAuthentication string | ||
|
||
// DvfAuthToken is the DVFAuthentication indicates | ||
const DvfAuthToken DVFAuthentication = "dvfAuthToken" | ||
|
||
func GetDVFAuthToken(ctx context.Context) string { | ||
dvfAuthToken := ctx.Value(DvfAuthToken) | ||
if token, ok := dvfAuthToken.(string); ok { | ||
return token | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestGetRequestID(t *testing.T) { | ||
ctx := context.WithValue(context.Background(), ContextKeyRequestID, "ReqId") | ||
|
||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{"ctx", "ReqId"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := GetRequestID(ctx) | ||
if got != tt.want { | ||
t.Errorf("GetRequestID() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package scriptorium | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gateway-fm/scriptorium/logger" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// ILogger implements logger's functions | ||
type ILogger interface { | ||
InitLogger() error | ||
} | ||
type Logger struct { | ||
logger *logger.Zaplog | ||
Env string | ||
} | ||
|
||
// Logger constructor | ||
func NewLogger(logenv string) ILogger { | ||
return &Logger{ | ||
logger: logger.Log(), | ||
Env: logenv, | ||
} | ||
} | ||
|
||
// InitLogger initializes logger on | ||
// application level | ||
func (c *Logger) InitLogger() error { | ||
env, err := logger.EnvFromStr(c.Env) | ||
if err != nil { | ||
return fmt.Errorf("failed when getting the app env: %w", err) | ||
} | ||
switch env { | ||
case logger.Local: | ||
//experimental | ||
cfg := zap.NewDevelopmentConfig() | ||
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
cfg.EncoderConfig.TimeKey = "" | ||
cfg.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder | ||
// | ||
logger.Log().Logger, _ = cfg.Build() | ||
c.logger.Logger = logger.Log().Logger | ||
case logger.Production, logger.Development: | ||
c.logger.Logger, _ = zap.NewProduction() | ||
default: | ||
return fmt.Errorf("env wasn't specified correctly: %v", c.Env) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type AppEnv int | ||
|
||
const ( | ||
Production AppEnv = iota | ||
Development | ||
Local | ||
Wrong | ||
) | ||
|
||
var Envs = [...]string{ | ||
Production: "prod", | ||
Development: "dev", | ||
Local: "local", | ||
} | ||
|
||
func (s AppEnv) String() string { | ||
return Envs[s] | ||
} | ||
|
||
func EnvFromStr(s string) (AppEnv, error) { | ||
for i, r := range Envs { | ||
if strings.ToLower(s) == r { | ||
return AppEnv(i), nil | ||
} | ||
} | ||
return Wrong, fmt.Errorf("wrong env type where provided %q", s) | ||
} |
Oops, something went wrong.