From 0c9f202233dc201e772d34a3f89c904b7e67f7dc Mon Sep 17 00:00:00 2001 From: husobee Date: Mon, 14 Nov 2022 07:28:04 -0500 Subject: [PATCH] Move linting step into CI github workflow. Linting corrections, test corrections. --- .github/workflows/challenge-bypass-tests.yaml | 2 + .github/workflows/golangci-lint.yaml | 26 -------- .golangci.yaml | 18 +++--- btd/issuer.go | 8 ++- go.mod | 12 ++-- go.sum | 37 +++++++---- kafka/signed_blinded_token_issuer_handler.go | 8 +-- kafka/signed_token_redeem_handler.go | 8 +-- main.go | 6 +- server/db.go | 62 ++++++++----------- server/dynamo.go | 22 ++++--- server/issuers.go | 4 +- server/server.go | 24 ++++--- server/server_test.go | 44 +++++++------ server/tokens.go | 9 ++- utils/errors.go | 1 + utils/ptr/ptr.go | 1 + 17 files changed, 144 insertions(+), 148 deletions(-) delete mode 100644 .github/workflows/golangci-lint.yaml diff --git a/.github/workflows/challenge-bypass-tests.yaml b/.github/workflows/challenge-bypass-tests.yaml index da2f2ebe..2c9e0ee8 100644 --- a/.github/workflows/challenge-bypass-tests.yaml +++ b/.github/workflows/challenge-bypass-tests.yaml @@ -10,5 +10,7 @@ jobs: steps: - name: checkout repo uses: actions/checkout@v3 + - name: run lint + run: make lint - name: run tests run: make docker-test diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml deleted file mode 100644 index d899b556..00000000 --- a/.github/workflows/golangci-lint.yaml +++ /dev/null @@ -1,26 +0,0 @@ -name: golangci-lint -on: - push: - tags: - - v* - branches: - - master - - main - pull_request: -permissions: - contents: read - -jobs: - golangci: - name: lint - runs-on: ubuntu-latest - steps: - - uses: actions/setup-go@v3 - with: - go-version: 1.18 - - uses: actions/checkout@v3 - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.49.0 - args: -v diff --git a/.golangci.yaml b/.golangci.yaml index 5f2b1a37..1e5d90a3 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -3,14 +3,14 @@ run: timeout: 3m linters-settings: - cyclop: + #cyclop: # The maximal code complexity to report. # Default: 10 - max-complexity: 10 + # max-complexity: 10 # The maximal average package complexity. # If it's higher than 0.0 (float) the check is enabled # Default: 0.0 - package-average: 10.0 + #package-average: 10.0 errcheck: # Report about not checking of errors in type assertions: `a := b.(MyStruct)`. @@ -41,23 +41,23 @@ linters: disable-all: true enable: ## enabled by default - - deadcode # Finds unused code + #- deadcode # Finds unused code - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases - gosimple # Linter for Go source code that specializes in simplifying a code - govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string - ineffassign # Detects when assignments to existing variables are not used - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks - - structcheck # Finds unused struct fields + #- structcheck # Finds unused struct fields - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code - unused # Checks Go code for unused constants, variables, functions and types - - varcheck # Finds unused global variables and constants + #- varcheck # Finds unused global variables and constants # ## disabled by default - contextcheck # check the function whether use a non-inherited context - - cyclop # checks function and package cyclomatic complexity + #- cyclop # checks function and package cyclomatic complexity - errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error. - gocritic # Provides diagnostics that check for bugs, performance and style issues. - - gocyclo # Computes and checks the cyclomatic complexity of functions - - nestif # Reports deeply nested if statements + #- gocyclo # Computes and checks the cyclomatic complexity of functions + #- nestif # Reports deeply nested if statements - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed - stylecheck # Stylecheck is a replacement for golint diff --git a/btd/issuer.go b/btd/issuer.go index 1a93a141..51b1d225 100644 --- a/btd/issuer.go +++ b/btd/issuer.go @@ -9,8 +9,10 @@ import ( ) var ( - ErrInvalidMAC = errors.New("binding MAC didn't match derived MAC") - ErrInvalidBatchProof = errors.New("New batch proof for signed tokens is invalid") + // ErrInvalidMAC - the mac was invalid + ErrInvalidMAC = errors.New("binding MAC didn't match derived MAC") + // ErrInvalidBatchProof - the batch proof was invalid + ErrInvalidBatchProof = errors.New("new batch proof for signed tokens is invalid") latencyBuckets = []float64{.25, .5, 1, 2.5, 5, 10} @@ -74,7 +76,7 @@ func init() { func ApproveTokens(blindedTokens []*crypto.BlindedToken, key *crypto.SigningKey) ([]*crypto.SignedToken, *crypto.BatchDLEQProof, error) { var err error if len(blindedTokens) < 1 { - err = errors.New("Provided blindedTokens array was empty.") + err = errors.New("provided blindedTokens array was empty") return []*crypto.SignedToken{}, nil, err } diff --git a/go.mod b/go.mod index 014a56a9..4caf8099 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.18 require ( github.com/actgardner/gogen-avro/v10 v10.2.1 - github.com/aws/aws-sdk-go v1.44.124 - github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.3 + github.com/aws/aws-sdk-go v1.44.136 + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.4 github.com/brave-intl/bat-go/libs v0.0.0-20220913154833-730f36b772de github.com/brave-intl/challenge-bypass-ristretto-ffi v0.0.0-20190717223301-f88d942ddfaf github.com/getsentry/raven-go v0.2.0 @@ -21,9 +21,9 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/rs/zerolog v1.28.0 github.com/satori/go.uuid v1.2.0 - github.com/segmentio/kafka-go v0.4.35 + github.com/segmentio/kafka-go v0.4.38 github.com/sirupsen/logrus v1.9.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.1 ) require ( @@ -42,7 +42,7 @@ require ( github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect - github.com/klauspost/compress v1.15.7 // indirect + github.com/klauspost/compress v1.15.9 // indirect github.com/linkedin/goavro v2.1.0+incompatible // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect @@ -58,7 +58,7 @@ require ( github.com/throttled/throttled v2.2.5+incompatible // indirect go.uber.org/atomic v1.9.0 // indirect golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect - golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect + golang.org/x/sys v0.1.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 81824bdd..81bddd4f 100644 --- a/go.sum +++ b/go.sum @@ -131,8 +131,8 @@ github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0= github.com/aws/aws-sdk-go v1.17.7/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= -github.com/aws/aws-sdk-go v1.44.124 h1:Xe1WQRUUekZf6ZFm3SD0vplB/AP/hymVqMiRS9LQRIs= -github.com/aws/aws-sdk-go v1.44.124/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= +github.com/aws/aws-sdk-go v1.44.136 h1:J1KJJssa8pjU8jETYUxwRS37KTcxjACfKd9GK8t+5ZU= +github.com/aws/aws-sdk-go v1.44.136/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v1.8.0/go.mod h1:xEFuWz+3TYdlPRuo+CqATbeDWIWyaT5uAPwPaWtgse0= github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2 v1.17.1/go.mod h1:JLnGeGONAyi2lWXI1p0PCIOIy333JMVK1U7Hf0aRFLw= @@ -148,8 +148,8 @@ github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.25/go.mod h1:Zb29PYkf42 github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.19/go.mod h1:6Q0546uHDp421okhmmGfbxzq2hBqbXFNpi4k+Q1JnQA= github.com/aws/aws-sdk-go-v2/internal/ini v1.2.0/go.mod h1:Q5jATQc+f1MfZp3PDMhn6ry18hGvE0i8yvbXoKbnZaE= github.com/aws/aws-sdk-go-v2/internal/ini v1.2.4/go.mod h1:ZcBrrI3zBKlhGFNYWvju0I3TR93I7YIgAfy82Fh4lcQ= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.3 h1:2oB4ikNEMLaPtu6lbNFJyTSayBILvrOfa2VfOffcuvU= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.3/go.mod h1:BiglbKCG56L8tmMnUEyEQo422BO9xnNR8vVHnOsByf8= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.4 h1:mN72saOOYAq2qBczDTi2LznXFf98lvimpSethXyVnOQ= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.4/go.mod h1:BiglbKCG56L8tmMnUEyEQo422BO9xnNR8vVHnOsByf8= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.2.2/go.mod h1:EASdTcM1lGhUe1/p4gkojHwlGJkeoRjjr1sRCzup3Is= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.3.0/go.mod h1:v8ygadNyATSm6elwJ/4gzJwcFhri9RqS8skgHKiwXPU= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.10/go.mod h1:9cBNUHI2aW4ho0A5T87O294iPDuuUOSIEDjnd1Lq/z0= @@ -785,8 +785,8 @@ github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdY github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.15.7 h1:7cgTQxJCU/vy+oP/E3B9RGbQTgbiVzIJWIKOLoAsPok= -github.com/klauspost/compress v1.15.7/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= +github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -1054,8 +1054,8 @@ github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24 github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= -github.com/segmentio/kafka-go v0.4.35 h1:TAsQ7q1SjS39PcFvU0zDJhCuVAxHomy7xOAfbdSuhzs= -github.com/segmentio/kafka-go v0.4.35/go.mod h1:GAjxBQJdQMB5zfNA21AhpaqOB2Mu+w3De4ni3Gbm8y0= +github.com/segmentio/kafka-go v0.4.38 h1:iQdOBbUSdfuYlFpvjuALgj7N6DrdPA0HfB4AhREOdtg= +github.com/segmentio/kafka-go v0.4.38/go.mod h1:ikyuGon/60MN/vXFgykf7Zm8P5Be49gJU6vezwjnnhU= github.com/shengdoushi/base58 v1.0.0 h1:tGe4o6TmdXFJWoI31VoSWvuaKxf0Px3gqa3sUWhAxBs= github.com/shengdoushi/base58 v1.0.0/go.mod h1:m5uIILfzcKMw6238iWAhP4l3s5+uXyF3+bJKUNhAL9I= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= @@ -1105,6 +1105,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -1113,8 +1114,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= @@ -1161,6 +1163,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 h1:k/gmLsJDWwWqbLCur2yWnJzwQEKRcAHXo6seXGuSwWw= github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs= github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA= @@ -1298,6 +1301,7 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1366,8 +1370,10 @@ golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220111093109-d55c255bac03/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 h1:8NSylCMxLW4JvserAndSgFL7aPli6A68yf0bYFTcWCM= golang.org/x/net v0.0.0-20220706163947-c90051bbdb60/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -1399,6 +1405,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1523,13 +1530,15 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= -golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1538,8 +1547,9 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -1627,6 +1637,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/kafka/signed_blinded_token_issuer_handler.go b/kafka/signed_blinded_token_issuer_handler.go index 22510426..a9bf2b24 100644 --- a/kafka/signed_blinded_token_issuer_handler.go +++ b/kafka/signed_blinded_token_issuer_handler.go @@ -16,7 +16,7 @@ import ( ) /* - SignedBlindedTokenIssuerHandler emits signed, blinded tokens based on provided blinded tokens. +SignedBlindedTokenIssuerHandler emits signed, blinded tokens based on provided blinded tokens. In cases where there are unrecoverable errors that prevent progress we will return nil. These permanent failure cases are different from cases where we encounter temporary errors inside the request data. For permanent failures inside the data processing loop we @@ -533,7 +533,6 @@ func handlePermanentIssuanceError( producer *kafka.Writer, logger *zerolog.Logger, ) { - processingResult := avroIssuerErrorResultFromError( message, marshalledBlindedTokens, @@ -547,6 +546,7 @@ func handlePermanentIssuanceError( logger, ) - Emit(producer, processingResult.Message, logger) - return + if err := Emit(producer, processingResult.Message, logger); err != nil { + logger.Error().Err(err).Msg("failed to emit") + } } diff --git a/kafka/signed_token_redeem_handler.go b/kafka/signed_token_redeem_handler.go index 5e3ebd19..5cd80de1 100644 --- a/kafka/signed_token_redeem_handler.go +++ b/kafka/signed_token_redeem_handler.go @@ -16,7 +16,7 @@ import ( ) /* - SignedTokenRedeemHandler emits payment tokens that correspond to the signed confirmation +SignedTokenRedeemHandler emits payment tokens that correspond to the signed confirmation tokens provided. If it encounters a permanent error, it emits a permanent result for that item. If the error is temporary, an error is returned to indicate that progress cannot be made. @@ -262,7 +262,6 @@ func SignedTokenRedeemHandler( // in a duplicate error upon save that was not detected previously // we will check equivalence upon receipt of a duplicate error. if strings.Contains(err.Error(), "Duplicate") { - _, equivalence, err := server.CheckRedeemedTokenEquivalence(verifiedIssuer, &tokenPreimage, string(request.Binding), msg.Offset) if err != nil { message := fmt.Sprintf("request %s: failed to check redemption equivalence", tokenRedeemRequestSet.Request_id) @@ -410,6 +409,7 @@ func handlePermanentRedemptionError( int32(avroSchema.RedeemResultStatusError), logger, ) - Emit(producer, processingResult.Message, logger) - return + if err := Emit(producer, processingResult.Message, logger); err != nil { + logger.Error().Err(err).Msg("failed to emit") + } } diff --git a/main.go b/main.go index d6df565f..5843d4d1 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func main() { srv := *server.DefaultServer flag.StringVar(&configFile, "config", "", "local config file for development (overrides cli options)") - flag.StringVar(&srv.DbConfigPath, "db_config", "", "path to the json file with database configuration") + flag.StringVar(&srv.DBConfigPath, "db_config", "", "path to the json file with database configuration") flag.IntVar(&srv.ListenPort, "p", 2416, "port to listen on") flag.Parse() @@ -52,7 +52,7 @@ func main() { } } - err = srv.InitDbConfig() + err = srv.InitDBConfig() if err != nil { logger.Panic(err) } @@ -60,7 +60,7 @@ func main() { zeroLogger.Trace().Msg("Initializing persistence and cron jobs") // Initialize databases and cron tasks before the Kafka processors and server start - srv.InitDb() + srv.InitDB() srv.InitDynamo() // Run the cron job unless it's explicitly disabled. if os.Getenv("CRON_ENABLED") != "false" { diff --git a/server/db.go b/server/db.go index d69e1967..8bc66959 100644 --- a/server/db.go +++ b/server/db.go @@ -32,8 +32,8 @@ type CachingConfig struct { ExpirationSec int `json:"expirationSec"` } -// DbConfig defines app configurations -type DbConfig struct { +// DBConfig defines app configurations +type DBConfig struct { ConnectionURI string `json:"connectionURI"` CachingConfig CachingConfig `json:"caching"` MaxConnection int `json:"maxConnection"` @@ -135,13 +135,13 @@ var ( errRedemptionNotFound = errors.New("redemption with the given id does not exist") ) -// LoadDbConfig loads config into server variable -func (c *Server) LoadDbConfig(config DbConfig) { +// LoadDBConfig loads config into server variable +func (c *Server) LoadDBConfig(config DBConfig) { c.dbConfig = config } -// InitDb initialzes the database connection based on a server's configuration -func (c *Server) InitDb() { +// InitDB initialzes the database connection based on a server's configuration +func (c *Server) InitDB() { cfg := c.dbConfig db, err := sqlx.Open("postgres", cfg.ConnectionURI) @@ -254,7 +254,7 @@ func incrementCounter(c prometheus.Counter) { c.Add(1) } -func (c *Server) fetchIssuer(issuerID string) (*Issuer, *utils.ProcessingError) { +func (c *Server) fetchIssuer(issuerID string) (*Issuer, error) { defer incrementCounter(fetchIssuerCounter) var ( @@ -518,7 +518,7 @@ func (c *Server) fetchIssuers(issuerType string) (*[]Issuer, *utils.ProcessingEr // FetchAllIssuers fetches all issuers from a cache or a database, saving them in the cache // if it has to query the database. -func (c *Server) FetchAllIssuers() (*[]Issuer, *utils.ProcessingError) { +func (c *Server) FetchAllIssuers() (*[]Issuer, error) { if c.caches != nil { if cached, found := c.caches["issuers"].Get("all"); found { return cached.(*[]Issuer), nil @@ -624,7 +624,6 @@ func (c *Server) rotateIssuers() error { issuer := c.convertDBIssuer(v) // populate keys in db if err := txPopulateIssuerKeys(c.Logger, tx, *issuer); err != nil { - tx.Rollback() return fmt.Errorf("failed to populate v3 issuer keys: %w", err) } @@ -683,12 +682,10 @@ func (c *Server) rotateIssuersV3() error { for _, issuer := range fetchedIssuers { issuerDTO := parseIssuer(issuer) if err != nil { - tx.Rollback() return fmt.Errorf("error failed to parse db issuer to dto: %w", err) } // populate the buffer of keys for the v3 issuer if err := txPopulateIssuerKeys(c.Logger, tx, issuerDTO); err != nil { - tx.Rollback() return fmt.Errorf("failed to close rows on v3 issuer creation: %w", err) } // denote that the v3 issuer was rotated at this time @@ -719,7 +716,7 @@ func (c *Server) deleteIssuerKeys(duration string) (int64, error) { } // createIssuer - creation of a v3 issuer -func (c *Server) createV3Issuer(issuer Issuer) error { +func (c *Server) createV3Issuer(issuer Issuer) (err error) { defer incrementCounter(createIssuerCounter) if issuer.MaxTokens == 0 { issuer.MaxTokens = 40 @@ -731,6 +728,13 @@ func (c *Server) createV3Issuer(issuer Issuer) error { } tx := c.db.MustBegin() + defer func() { + if err != nil { + err = tx.Rollback() + return + } + err = tx.Commit() + }() queryTimer := prometheus.NewTimer(createTimeLimitedIssuerDBDuration) row := tx.QueryRowx( @@ -761,16 +765,14 @@ func (c *Server) createV3Issuer(issuer Issuer) error { ) // get the newly inserted issuer identifier if err := row.Scan(&issuer.ID); err != nil { - tx.Rollback() return fmt.Errorf("failed to get v3 issuer id: %w", err) } if err := txPopulateIssuerKeys(c.Logger, tx, issuer); err != nil { - tx.Rollback() return fmt.Errorf("failed to close rows on v3 issuer creation: %w", err) } queryTimer.ObserveDuration() - return tx.Commit() + return nil } // on the transaction, populate v3 issuer keys for the v3 issuer @@ -826,7 +828,6 @@ func txPopulateIssuerKeys(logger *logrus.Logger, tx *sqlx.Tx, issuer Issuer) err // start/end, increment every iteration end, err = duration.From(*start) if err != nil { - tx.Rollback() return fmt.Errorf("unable to calculate end time: %w", err) } } @@ -834,21 +835,18 @@ func txPopulateIssuerKeys(logger *logrus.Logger, tx *sqlx.Tx, issuer Issuer) err signingKey, err := crypto.RandomSigningKey() if err != nil { logger.Error("Error generating key") - tx.Rollback() return err } signingKeyTxt, err := signingKey.MarshalText() if err != nil { logger.Error("Error marshalling signing key") - tx.Rollback() return err } pubKeyTxt, err := signingKey.PublicKey().MarshalText() if err != nil { logger.Error("Error marshalling public key") - tx.Rollback() return err } logger.Infof("iteration key pubkey: %+v", pubKeyTxt) @@ -867,7 +865,7 @@ func txPopulateIssuerKeys(logger *logrus.Logger, tx *sqlx.Tx, issuer Issuer) err keys = append(keys, k) - if issuer.ValidFrom != nil && !(*start).Equal(*issuer.ValidFrom) { + if issuer.ValidFrom != nil && !start.Equal(*issuer.ValidFrom) { valueFmtStr += ", " } valueFmtStr += fmt.Sprintf("($%d, $%d, $%d, $%d, $%d, $%d)", @@ -909,10 +907,10 @@ func txPopulateIssuerKeys(logger *logrus.Logger, tx *sqlx.Tx, issuer Issuer) err VALUES %s`, valueFmtStr), values...) if err != nil { logger.Error("Could not insert the new issuer keys into the DB") - tx.Rollback() return err } - return rows.Close() + defer rows.Close() + return nil } func (c *Server) createIssuerV2(issuerType string, issuerCohort int16, maxTokens int, expiresAt *time.Time) error { @@ -960,7 +958,7 @@ func (c *Server) RedeemToken(issuerForRedemption *Issuer, preimage *crypto.Token } else if issuerForRedemption.Version == 2 || issuerForRedemption.Version == 3 { return c.redeemTokenWithDynamo(issuerForRedemption, preimage, payload, offset) } - return errors.New("Wrong Issuer Version") + return errors.New("wrong issuer version") } func redeemTokenWithDB(db Queryable, stringIssuer string, preimage *crypto.TokenPreimage, payload string) error { @@ -972,37 +970,29 @@ func redeemTokenWithDB(db Queryable, stringIssuer string, preimage *crypto.Token queryTimer := prometheus.NewTimer(createRedemptionDBDuration) rows, err := db.Query( `INSERT INTO redemptions(id, issuer_type, ts, payload) VALUES ($1, $2, NOW(), $3)`, preimageTxt, stringIssuer, payload) - defer func() error { - if rows != nil { - err := rows.Close() - if err != nil { - return err - } - } - return nil - }() if err != nil { if err, ok := err.(*pq.Error); ok && err.Code == "23505" { // unique constraint violation return errDuplicateRedemption } return err } + defer rows.Close() queryTimer.ObserveDuration() return nil } -func (c *Server) fetchRedemption(issuerType, ID string) (*Redemption, error) { +func (c *Server) fetchRedemption(issuerType, id string) (*Redemption, error) { defer incrementCounter(fetchRedemptionCounter) if c.caches != nil { - if cached, found := c.caches["redemptions"].Get(fmt.Sprintf("%s:%s", issuerType, ID)); found { + if cached, found := c.caches["redemptions"].Get(fmt.Sprintf("%s:%s", issuerType, id)); found { return cached.(*Redemption), nil } } queryTimer := prometheus.NewTimer(fetchRedemptionDBDuration) rows, err := c.db.Query( - `SELECT id, issuer_id, ts, payload FROM redemptions WHERE id = $1 AND issuer_type = $2`, ID, issuerType) + `SELECT id, issuer_id, ts, payload FROM redemptions WHERE id = $1 AND issuer_type = $2`, id, issuerType) queryTimer.ObserveDuration() if err != nil { @@ -1019,7 +1009,7 @@ func (c *Server) fetchRedemption(issuerType, ID string) (*Redemption, error) { } if c.caches != nil { - c.caches["redemptions"].SetDefault(fmt.Sprintf("%s:%s", issuerType, ID), redemption) + c.caches["redemptions"].SetDefault(fmt.Sprintf("%s:%s", issuerType, id), redemption) } return redemption, nil diff --git a/server/dynamo.go b/server/dynamo.go index cd6d3081..bb3c55b2 100644 --- a/server/dynamo.go +++ b/server/dynamo.go @@ -1,6 +1,7 @@ package server import ( + "errors" "os" "time" @@ -182,19 +183,22 @@ func (c *Server) CheckRedeemedTokenEquivalence(issuer *Issuer, preimage *crypto. // to determine whether the body is equivalent to what was provided or just the // id. if err == nil { - if redemption.Payload == *&existingRedemption.Payload { + if redemption.Payload == existingRedemption.Payload { return &redemption, BindingEquivalence, nil } return &redemption, IDEquivalence, nil } - var ok bool - if err, ok = err.(*awsDynamoTypes.ProvisionedThroughputExceededException); ok { - temporary = true - } - if err, ok = err.(*awsDynamoTypes.RequestLimitExceeded); ok { - temporary = true - } - if err, ok = err.(*awsDynamoTypes.InternalServerError); ok { + + var ( + ptee *awsDynamoTypes.ProvisionedThroughputExceededException + rle *awsDynamoTypes.RequestLimitExceeded + ise *awsDynamoTypes.InternalServerError + ) + + // is this a temporary error? + if errors.As(err, &ptee) || + errors.As(err, &rle) || + errors.As(err, &ise) { temporary = true } return &redemption, NoEquivalence, utils.ProcessingErrorFromError(err, temporary) diff --git a/server/issuers.go b/server/issuers.go index 4a1d01d4..611c2e9f 100644 --- a/server/issuers.go +++ b/server/issuers.go @@ -47,6 +47,7 @@ type issuerFetchRequestV2 struct { Cohort int16 `json:"cohort"` } +// GetLatestIssuer - get the latest issuer by type/cohort func (c *Server) GetLatestIssuer(issuerType string, issuerCohort int16) (*Issuer, *handlers.AppError) { issuer, err := c.fetchIssuersByCohort(issuerType, issuerCohort) if err != nil { @@ -68,6 +69,7 @@ func (c *Server) GetLatestIssuer(issuerType string, issuerCohort int16) (*Issuer return &(*issuer)[0], nil } +// GetLatestIssuerKafka - get the issuer and any processing error func (c *Server) GetLatestIssuerKafka(issuerType string, issuerCohort int16) (*Issuer, *utils.ProcessingError) { issuer, err := c.fetchIssuersByCohort(issuerType, issuerCohort) if err != nil { @@ -77,6 +79,7 @@ func (c *Server) GetLatestIssuerKafka(issuerType string, issuerCohort int16) (*I return &(*issuer)[0], nil } +// GetIssuers - get all issuers by issuer type func (c *Server) GetIssuers(issuerType string) (*[]Issuer, error) { issuers, err := c.getIssuers(issuerType) if err != nil { @@ -235,7 +238,6 @@ func (c *Server) issuerV3CreateHandler(w http.ResponseWriter, r *http.Request) * ValidFrom: req.ValidFrom, Duration: &req.Duration, }); err != nil { - var pqErr *pq.Error if errors.As(err, &pqErr) { if pqErr.Code == "23505" { // unique violation diff --git a/server/server.go b/server/server.go index 21c63a97..8504c87f 100644 --- a/server/server.go +++ b/server/server.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "net/http" "os" "strconv" @@ -23,11 +22,15 @@ import ( ) var ( + // Version - the version? Version = "dev" maxRequestSize = int64(1024 * 1024) // 1MiB - ErrNoSecretKey = errors.New("server config does not contain a key") - ErrRequestTooLarge = errors.New("request too large to process") + // ErrNoSecretKey - configuration error, no secret key + ErrNoSecretKey = errors.New("server config does not contain a key") + // ErrRequestTooLarge - processing error, request is too big + ErrRequestTooLarge = errors.New("request too large to process") + // ErrUnrecognizedRequest - processing error, request unrecognized ErrUnrecognizedRequest = errors.New("received unrecognized request type") ) @@ -45,13 +48,14 @@ func init() { prometheus.MustRegister(fetchRedemptionDBDuration) } +// Server - base server type type Server struct { ListenPort int `json:"listen_port,omitempty"` MaxTokens int `json:"max_tokens,omitempty"` - DbConfigPath string `json:"db_config_path"` + DBConfigPath string `json:"db_config_path"` Logger *logrus.Logger `json:",omitempty"` dynamo *dynamodb.DynamoDB - dbConfig DbConfig + dbConfig DBConfig db *sqlx.DB caches map[string]CacheInterface @@ -65,7 +69,7 @@ var DefaultServer = &Server{ // LoadConfigFile loads a file into conf and returns func LoadConfigFile(filePath string) (Server, error) { conf := *DefaultServer - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return conf, err } @@ -76,9 +80,9 @@ func LoadConfigFile(filePath string) (Server, error) { return conf, nil } -// InitDbConfig reads os environment and update conf -func (c *Server) InitDbConfig() error { - conf := DbConfig{ +// InitDBConfig reads os environment and update conf +func (c *Server) InitDBConfig() error { + conf := DBConfig{ DefaultDaysBeforeExpiry: 7, DefaultIssuerValidDays: 30, MaxConnection: 100, @@ -111,7 +115,7 @@ func (c *Server) InitDbConfig() error { } } - c.LoadDbConfig(conf) + c.LoadDBConfig(conf) return nil } diff --git a/server/server_test.go b/server/server_test.go index 2e5f786c..1771626c 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -46,12 +45,12 @@ func (suite *ServerTestSuite) SetupSuite() { suite.srv = &Server{} - err = suite.srv.InitDbConfig() + err = suite.srv.InitDBConfig() suite.Require().NoError(err, "Failed to setup db conn") suite.handler = chi.ServerBaseContext(suite.srv.setupRouter(SetupLogger(context.Background()))) - suite.srv.InitDb() + suite.srv.InitDB() suite.srv.InitDynamo() err = test.SetupDynamodbTables(suite.srv.dynamo) @@ -77,7 +76,7 @@ func (suite *ServerTestSuite) TestPing() { suite.Assert().Equal(http.StatusOK, resp.StatusCode) expected := "." - actual, err := ioutil.ReadAll(resp.Body) + actual, err := io.ReadAll(resp.Body) suite.Assert().NoError(err, "Reading response body should succeed") suite.Assert().Equal(expected, string(actual), "Message should match") } @@ -135,7 +134,7 @@ func (suite *ServerTestSuite) TestIssueRedeemV2() { suite.Assert().NoError(err, "HTTP Request should complete") suite.Assert().Equal(http.StatusOK, resp.StatusCode, "Attempted redemption request should succeed") - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Redemption response body read must succeed") var issuerResp blindedTokenRedeemResponse @@ -157,14 +156,15 @@ func (suite *ServerTestSuite) TestIssueRedeemV2() { suite.Assert().NoError(err, "HTTP Request should complete") suite.Assert().Equal(http.StatusOK, resp.StatusCode, "Attempted redemption request should succeed") - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) suite.Require().NoError(err, "Redemption response body read must succeed") err = json.Unmarshal(body, &issuerResp) suite.Require().NoError(err, "Redemption response body unmarshal must succeed") suite.Assert().NotEqual(issuerResp.Cohort, 1-issuerCohort, "Redemption of a token should return the same cohort with which it was signed") - _, err = suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, -1), issuer.ID) + r, err := suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, -1), issuer.ID) suite.Require().NoError(err, "failed to expire issuer") + defer r.Close() // keys are what rotate now, not the issuer itself issuer, _ = suite.srv.GetLatestIssuer(issuerType, issuerCohort) @@ -176,8 +176,9 @@ func (suite *ServerTestSuite) TestIssueRedeemV2() { var signingKey = issuer.Keys[len(issuer.Keys)-1].SigningKey publicKey = signingKey.PublicKey() - _, err = suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, +1), issuer.ID) + r, err = suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, +1), issuer.ID) suite.Require().NoError(err, "failed to unexpire issuer") + defer r.Close() unblindedToken = suite.createToken(server.URL, issuerType, publicKey) preimageText, sigText = suite.prepareRedemption(unblindedToken, msg) @@ -204,7 +205,7 @@ func (suite *ServerTestSuite) TestNewIssueRedeemV2() { suite.Assert().NoError(err, "HTTP Request should complete") suite.Assert().Equal(http.StatusOK, resp.StatusCode, "Attempted redemption request should succeed") - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Redemption response body read must succeed") var issuerResp blindedTokenRedeemResponse @@ -225,15 +226,16 @@ func (suite *ServerTestSuite) TestNewIssueRedeemV2() { suite.Assert().NoError(err, "HTTP Request should complete") suite.Assert().Equal(http.StatusOK, resp.StatusCode, "Attempted redemption request should succeed") - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) suite.Require().NoError(err, "Redemption response body read must succeed") err = json.Unmarshal(body, &issuerResp) suite.Require().NoError(err, "Redemption response body unmarshal must succeed") suite.Assert().NotEqual(issuerResp.Cohort, 1-issuerCohort, "Redemption of a token should return the same cohort with which it was signed") - _, err = suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, -1), issuer.ID) + r, err := suite.srv.db.Query(`UPDATE v3_issuers SET expires_at=$1 WHERE issuer_id=$2`, time.Now().AddDate(0, 0, -1), issuer.ID) suite.Require().NoError(err, "failed to expire issuer") + defer r.Close() resp, err = suite.attemptRedeem(server.URL, preimageText2, sigText2, issuerType, msg) suite.Assert().NoError(err, "HTTP Request should complete") @@ -261,6 +263,7 @@ func (suite *ServerTestSuite) TestRedeemV3() { suite.Require().NoError(err) issuerKey, err := suite.srv.GetLatestIssuer(issuer.IssuerType, issuer.IssuerCohort) + fmt.Println(err) tokens := make([]*crypto.Token, 1) token, err := crypto.RandomToken() @@ -277,8 +280,10 @@ func (suite *ServerTestSuite) TestRedeemV3() { // sign some tokens signedTokens, DLEQProof, err := btd.ApproveTokens(blindedTokensSlice, issuerKey.Keys[1].SigningKey) + suite.Require().NoError(err) unblindedTokens, err := DLEQProof.VerifyAndUnblind(tokens, blindedTokensSlice, signedTokens, issuerKey.Keys[1].SigningKey.PublicKey()) + suite.Require().NoError(err) msg := "test message" preimageText, sigText := suite.prepareRedemption(unblindedTokens[0], msg) @@ -316,6 +321,7 @@ func (suite *ServerTestSuite) TestCreateIssuerV3() { createIssuerURL := fmt.Sprintf("%s/v3/issuer/", server.URL) resp, err := suite.request("POST", createIssuerURL, bytes.NewBuffer(payload)) + suite.Require().NoError(err) suite.Assert().Equal(http.StatusCreated, resp.StatusCode) @@ -401,13 +407,13 @@ func (suite *ServerTestSuite) TestRunRotate() { suite.Require().NoError(err) } -func (suite *ServerTestSuite) request(method string, URL string, payload io.Reader) (*http.Response, error) { +func (suite *ServerTestSuite) request(method string, url string, payload io.Reader) (*http.Response, error) { var req *http.Request var err error if payload != nil { - req, err = http.NewRequest(method, URL, payload) + req, err = http.NewRequest(method, url, payload) } else { - req, err = http.NewRequest(method, URL, nil) + req, err = http.NewRequest(method, url, nil) } if err != nil { return nil, err @@ -432,7 +438,7 @@ func (suite *ServerTestSuite) createIssuer(serverURL string, issuerType string, suite.Require().NoError(err, "Issuer fetch must succeed") suite.Assert().Equal(http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Issuer fetch body read must succeed") var issuerResp issuerResponse @@ -452,7 +458,7 @@ func (suite *ServerTestSuite) getAllIssuers(serverURL string) []issuerResponse { suite.Require().NoError(err, "Getting alll Issuers must succeed") suite.Assert().Equal(http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Issuer fetch body read must succeed") var issuerResp []issuerResponse @@ -481,7 +487,7 @@ func (suite *ServerTestSuite) createIssuerWithExpiration(serverURL string, issue suite.Require().NoError(err, "Issuer fetch must succeed") suite.Assert().Equal(http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Issuer fetch body read must succeed") var issuerResp issuerResponse @@ -523,7 +529,7 @@ func (suite *ServerTestSuite) createTokens(serverURL string, issuerType string, suite.Require().NoError(err, "Token signing must succeed") suite.Assert().Equal(http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Token signing body read must succeed") var decodedResp blindedTokenIssueResponse @@ -588,7 +594,7 @@ func (suite *ServerTestSuite) createCohortTokens(serverURL string, issuerType st suite.Require().NoError(err, "Token signing must succeed") suite.Assert().Equal(http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) suite.Require().NoError(err, "Token signing body read must succeed") var decodedResp blindedTokenIssueResponse diff --git a/server/tokens.go b/server/tokens.go index 428d77b3..4bcc3ff7 100644 --- a/server/tokens.go +++ b/server/tokens.go @@ -25,6 +25,7 @@ type blindedTokenIssueRequest struct { BlindedTokens []*crypto.BlindedToken `json:"blinded_tokens"` } +// BlindedTokenIssueRequestV2 - version 2 blinded token issue request type BlindedTokenIssueRequestV2 struct { BlindedTokens []*crypto.BlindedToken `json:"blinded_tokens"` IssuerCohort int16 `json:"cohort"` @@ -46,23 +47,24 @@ type blindedTokenRedeemResponse struct { Cohort int16 `json:"cohort"` } +// BlindedTokenRedemptionInfo - this is the redemption information type BlindedTokenRedemptionInfo struct { TokenPreimage *crypto.TokenPreimage `json:"t"` Signature *crypto.VerificationSignature `json:"signature"` Issuer string `json:"issuer"` } +// BlindedTokenBulkRedeemRequest - this is the redemption in bulk form type BlindedTokenBulkRedeemRequest struct { Payload string `json:"payload"` Tokens []BlindedTokenRedemptionInfo `json:"tokens"` } +// BlindedTokenIssuerHandlerV2 - handler for token issuer v2 func (c *Server) BlindedTokenIssuerHandlerV2(w http.ResponseWriter, r *http.Request) *handlers.AppError { var response blindedTokenIssueResponse if issuerType := chi.URLParam(r, "type"); issuerType != "" { - var request BlindedTokenIssueRequestV2 - if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxRequestSize)).Decode(&request); err != nil { c.Logger.WithError(err) return handlers.WrapError(err, "Could not parse the request body", 400) @@ -170,7 +172,6 @@ func (c *Server) blindedTokenIssuerHandler(w http.ResponseWriter, r *http.Reques func (c *Server) blindedTokenRedeemHandlerV3(w http.ResponseWriter, r *http.Request) *handlers.AppError { var response blindedTokenRedeemResponse if issuerType := chi.URLParam(r, "type"); issuerType != "" { - issuer, err := c.fetchIssuerByType(r.Context(), issuerType) if err != nil { switch { @@ -263,7 +264,6 @@ func (c *Server) blindedTokenRedeemHandlerV3(w http.ResponseWriter, r *http.Requ Message: "Could not mark token redemption", Code: http.StatusInternalServerError, } - } response = blindedTokenRedeemResponse{issuer.IssuerCohort} } @@ -419,7 +419,6 @@ func (c *Server) blindedTokenBulkRedeemHandler(w http.ResponseWriter, r *http.Re Code: http.StatusInternalServerError, } } - } err = tx.Commit() if err != nil { diff --git a/utils/errors.go b/utils/errors.go index ad978667..64ca464f 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -25,6 +25,7 @@ func (e ProcessingError) Cause() error { return e.OriginalError } +// ProcessingErrorFromError - given an error turn it into a processing error func ProcessingErrorFromError(err error, temporary bool) *ProcessingError { return &ProcessingError{ OriginalError: err, diff --git a/utils/ptr/ptr.go b/utils/ptr/ptr.go index a9fd5007..0714bc46 100644 --- a/utils/ptr/ptr.go +++ b/utils/ptr/ptr.go @@ -20,6 +20,7 @@ func StringOr(s *string, or string) string { return *s } +// FromTime - return the pointer from a time? func FromTime(t time.Time) *time.Time { return &t }