2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-20 18:29:05 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
package authorization
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-01-02 17:39:17 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-12-20 18:29:05 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2019-01-02 17:39:17 +00:00
|
|
|
"net"
|
2018-12-20 18:29:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2018-12-20 18:29:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-01 17:28:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-20 18:29:05 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-01-02 17:39:17 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/peer"
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-09-05 16:11:21 +01:00
|
|
|
"storj.io/storj/pkg/certificate/certificateclient"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-12-20 18:29:05 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
var (
|
2019-04-08 19:15:19 +01:00
|
|
|
idents = testidentity.NewPregeneratedIdentities(storj.LatestIDVersion())
|
2019-01-02 17:39:17 +00:00
|
|
|
t1 = Token{
|
2019-06-18 01:28:40 +01:00
|
|
|
UserID: "user@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
Data: [tokenDataLength]byte{1, 2, 3},
|
|
|
|
}
|
|
|
|
t2 = Token{
|
2019-06-18 01:28:40 +01:00
|
|
|
UserID: "user2@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
Data: [tokenDataLength]byte{4, 5, 6},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
func TestNewDB(t *testing.T) {
|
2018-12-20 18:29:05 +00:00
|
|
|
ctx := testcontext.New(t)
|
2019-01-02 17:39:17 +00:00
|
|
|
defer ctx.Cleanup()
|
2019-02-01 17:28:40 +00:00
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
dbURL := "bolt://" + ctx.File("authorizations.db")
|
|
|
|
db, err := NewDB(dbURL, false)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-11 09:36:44 +01:00
|
|
|
defer ctx.Check(db.Close)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
require.NotNil(t, db)
|
|
|
|
require.NotNil(t, db.db)
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
func TestNewDBFromCfg(t *testing.T) {
|
2018-12-20 18:29:05 +00:00
|
|
|
ctx := testcontext.New(t)
|
2019-01-02 17:39:17 +00:00
|
|
|
defer ctx.Cleanup()
|
2019-02-01 17:28:40 +00:00
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
db, err := NewDBFromCfg(DBConfig{
|
|
|
|
URL: "bolt://" + ctx.File("authorizations.db"),
|
|
|
|
Overwrite: false,
|
|
|
|
})
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-11 09:36:44 +01:00
|
|
|
defer ctx.Check(db.Close)
|
|
|
|
|
|
|
|
require.NotNil(t, db)
|
|
|
|
require.NotNil(t, db.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationDB_Create(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
authDB := newTestAuthDB(t, ctx)
|
2019-01-02 17:39:17 +00:00
|
|
|
defer ctx.Check(authDB.Close)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID,
|
|
|
|
email string
|
|
|
|
startCount,
|
|
|
|
incCount,
|
|
|
|
newCount,
|
|
|
|
endCount int
|
|
|
|
errClass *errs.Class
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"first authorization",
|
2019-06-18 01:28:40 +01:00
|
|
|
"user1@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
0, 1, 1, 1,
|
|
|
|
nil, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"second authorization",
|
2019-06-18 01:28:40 +01:00
|
|
|
"user1@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
1, 2, 2, 3,
|
|
|
|
nil, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"large authorization",
|
2019-06-18 01:28:40 +01:00
|
|
|
"user2@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
0, 5, 5, 5,
|
|
|
|
nil, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"authorization error",
|
2019-06-18 01:28:40 +01:00
|
|
|
"user2@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
5, -1, 0, 5,
|
2019-09-11 09:36:44 +01:00
|
|
|
&ErrDB, ErrCount,
|
2018-12-20 18:29:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
2018-12-20 18:29:05 +00:00
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2019-05-29 14:30:16 +01:00
|
|
|
emailKey := storage.Key(testCase.email)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
if testCase.startCount == 0 {
|
2019-09-11 09:36:44 +01:00
|
|
|
_, err := authDB.db.Get(ctx, emailKey)
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
2019-09-05 16:11:21 +01:00
|
|
|
v, err := authDB.db.Get(ctx, emailKey)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, v)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var existingAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
err = existingAuths.Unmarshal(v)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-05-29 14:30:16 +01:00
|
|
|
require.Len(t, existingAuths, testCase.startCount)
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
expectedAuths, err := authDB.Create(ctx, testCase.email, testCase.incCount)
|
2019-05-29 14:30:16 +01:00
|
|
|
if testCase.errClass != nil {
|
|
|
|
assert.True(t, testCase.errClass.Has(err))
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
2019-05-29 14:30:16 +01:00
|
|
|
if testCase.err != nil {
|
|
|
|
assert.Equal(t, testCase.err, err)
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
2019-05-29 14:30:16 +01:00
|
|
|
if testCase.errClass == nil && testCase.err == nil {
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Len(t, expectedAuths, testCase.newCount)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
v, err := authDB.db.Get(ctx, emailKey)
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, v)
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var actualAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
err = actualAuths.Unmarshal(v)
|
|
|
|
assert.NoError(t, err)
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Len(t, actualAuths, testCase.endCount)
|
2018-12-20 18:29:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationDB_Get(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(authDB.Close)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var expectedAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
expectedAuths = append(expectedAuths, &Authorization{
|
2018-12-31 15:45:43 +00:00
|
|
|
Token: t1,
|
2018-12-20 18:29:05 +00:00
|
|
|
})
|
|
|
|
}
|
2019-02-01 17:28:40 +00:00
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
authsBytes, err := expectedAuths.Marshal()
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
err = authDB.db.Put(ctx, storage.Key("user@mail.test"), authsBytes)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID,
|
|
|
|
email string
|
2019-09-05 16:11:21 +01:00
|
|
|
result Group
|
2018-12-20 18:29:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-06-10 09:52:09 +01:00
|
|
|
"Non-existent email",
|
2019-06-18 01:28:40 +01:00
|
|
|
"nouser@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 09:52:09 +01:00
|
|
|
"Existing email",
|
2019-06-18 01:28:40 +01:00
|
|
|
"user@mail.test",
|
2018-12-20 18:29:05 +00:00
|
|
|
expectedAuths,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
|
|
|
t.Run(testCase.testID, func(t *testing.T) {
|
2019-06-04 12:36:27 +01:00
|
|
|
auths, err := authDB.Get(ctx, testCase.email)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-05-29 14:30:16 +01:00
|
|
|
if testCase.result != nil {
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NotEmpty(t, auths)
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Len(t, auths, len(testCase.result))
|
2018-12-20 18:29:05 +00:00
|
|
|
} else {
|
|
|
|
assert.Empty(t, auths)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
func TestAuthorizationDB_Claim_Valid(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(authDB.Close)
|
|
|
|
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
auths, err := authDB.Create(ctx, userID, 1)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, auths)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
ident, err := testidentity.NewTestIdentity(ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Port: 5,
|
|
|
|
}
|
|
|
|
grpcPeer := &peer.Peer{
|
|
|
|
Addr: addr,
|
|
|
|
AuthInfo: credentials.TLSInfo{
|
|
|
|
State: tls.ConnectionState{
|
|
|
|
PeerCertificates: []*x509.Certificate{ident.Leaf, ident.CA},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
req := &pb.SigningRequest{
|
|
|
|
AuthToken: auths[0].Token.String(),
|
|
|
|
Timestamp: now,
|
|
|
|
}
|
|
|
|
difficulty, err := ident.ID.Difficulty()
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
err = authDB.Claim(ctx, &ClaimOpts{
|
2019-01-02 17:39:17 +00:00
|
|
|
Req: req,
|
|
|
|
Peer: grpcPeer,
|
|
|
|
ChainBytes: [][]byte{ident.CA.Raw},
|
|
|
|
MinDifficulty: difficulty,
|
|
|
|
})
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
updatedAuths, err := authDB.Get(ctx, userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, updatedAuths)
|
2019-01-02 17:39:17 +00:00
|
|
|
assert.Equal(t, auths[0].Token, updatedAuths[0].Token)
|
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NotNil(t, updatedAuths[0].Claim)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
claim := updatedAuths[0].Claim
|
|
|
|
assert.Equal(t, grpcPeer.Addr.String(), claim.Addr)
|
|
|
|
assert.Equal(t, [][]byte{ident.CA.Raw}, claim.SignedChainBytes)
|
|
|
|
assert.Condition(t, func() bool {
|
|
|
|
return now-MaxClaimDelaySeconds < claim.Timestamp &&
|
|
|
|
claim.Timestamp < now+MaxClaimDelaySeconds
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationDB_Claim_Invalid(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(authDB.Close)
|
|
|
|
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2019-01-02 17:39:17 +00:00
|
|
|
claimedTime := int64(1000000)
|
|
|
|
claimedAddr := "6.7.8.9:0"
|
2019-02-01 17:28:40 +00:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
ident1, err := testidentity.NewTestIdentity(ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident1)
|
|
|
|
|
2019-01-30 20:47:21 +00:00
|
|
|
claimedIdent := &identity.PeerIdentity{
|
2019-01-02 17:39:17 +00:00
|
|
|
CA: ident1.CA,
|
|
|
|
Leaf: ident1.Leaf,
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
auths, err := authDB.Create(ctx, userID, 2)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, auths)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
claimedIndex, unclaimedIndex := 0, 1
|
|
|
|
|
|
|
|
auths[claimedIndex].Claim = &Claim{
|
|
|
|
Timestamp: claimedTime,
|
|
|
|
Addr: claimedAddr,
|
|
|
|
Identity: claimedIdent,
|
|
|
|
SignedChainBytes: [][]byte{claimedIdent.CA.Raw},
|
|
|
|
}
|
2019-06-04 12:36:27 +01:00
|
|
|
err = authDB.put(ctx, userID, auths)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
ident2, err := testidentity.NewTestIdentity(ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident2)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Port: 5,
|
|
|
|
}
|
|
|
|
grpcPeer := &peer.Peer{
|
|
|
|
Addr: addr,
|
|
|
|
AuthInfo: credentials.TLSInfo{
|
|
|
|
State: tls.ConnectionState{
|
|
|
|
PeerCertificates: []*x509.Certificate{ident2.Leaf, ident2.CA},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
difficulty2, err := ident2.ID.Difficulty()
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
t.Run("double claim", func(t *testing.T) {
|
2019-06-04 12:36:27 +01:00
|
|
|
err = authDB.Claim(ctx, &ClaimOpts{
|
2019-01-02 17:39:17 +00:00
|
|
|
Req: &pb.SigningRequest{
|
|
|
|
AuthToken: auths[claimedIndex].Token.String(),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
Peer: grpcPeer,
|
|
|
|
ChainBytes: [][]byte{ident2.CA.Raw},
|
|
|
|
MinDifficulty: difficulty2,
|
|
|
|
})
|
|
|
|
if assert.Error(t, err) {
|
2019-09-11 09:36:44 +01:00
|
|
|
assert.True(t, Error.Has(err))
|
2019-01-02 17:39:17 +00:00
|
|
|
// NB: token string shouldn't leak into error message
|
|
|
|
assert.NotContains(t, err.Error(), auths[claimedIndex].Token.String())
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
updatedAuths, err := authDB.Get(ctx, userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, updatedAuths)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
assert.Equal(t, auths[claimedIndex].Token, updatedAuths[claimedIndex].Token)
|
|
|
|
|
|
|
|
claim := updatedAuths[claimedIndex].Claim
|
|
|
|
assert.Equal(t, claimedAddr, claim.Addr)
|
|
|
|
assert.Equal(t, [][]byte{ident1.CA.Raw}, claim.SignedChainBytes)
|
|
|
|
assert.Equal(t, claimedTime, claim.Timestamp)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid timestamp", func(t *testing.T) {
|
2019-06-04 12:36:27 +01:00
|
|
|
err = authDB.Claim(ctx, &ClaimOpts{
|
2019-01-02 17:39:17 +00:00
|
|
|
Req: &pb.SigningRequest{
|
|
|
|
AuthToken: auths[unclaimedIndex].Token.String(),
|
|
|
|
// NB: 1 day ago
|
|
|
|
Timestamp: time.Now().Unix() - 86400,
|
|
|
|
},
|
|
|
|
Peer: grpcPeer,
|
|
|
|
ChainBytes: [][]byte{ident2.CA.Raw},
|
|
|
|
MinDifficulty: difficulty2,
|
|
|
|
})
|
|
|
|
if assert.Error(t, err) {
|
2019-09-11 09:36:44 +01:00
|
|
|
assert.True(t, Error.Has(err))
|
2019-01-02 17:39:17 +00:00
|
|
|
// NB: token string shouldn't leak into error message
|
|
|
|
assert.NotContains(t, err.Error(), auths[unclaimedIndex].Token.String())
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
updatedAuths, err := authDB.Get(ctx, userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, updatedAuths)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
assert.Equal(t, auths[unclaimedIndex].Token, updatedAuths[unclaimedIndex].Token)
|
|
|
|
assert.Nil(t, updatedAuths[unclaimedIndex].Claim)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid difficulty", func(t *testing.T) {
|
2019-06-04 12:36:27 +01:00
|
|
|
err = authDB.Claim(ctx, &ClaimOpts{
|
2019-01-02 17:39:17 +00:00
|
|
|
Req: &pb.SigningRequest{
|
|
|
|
AuthToken: auths[unclaimedIndex].Token.String(),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
Peer: grpcPeer,
|
|
|
|
ChainBytes: [][]byte{ident2.CA.Raw},
|
|
|
|
MinDifficulty: difficulty2 + 1,
|
|
|
|
})
|
|
|
|
if assert.Error(t, err) {
|
2019-09-11 09:36:44 +01:00
|
|
|
assert.True(t, Error.Has(err))
|
2019-01-02 17:39:17 +00:00
|
|
|
// NB: token string shouldn't leak into error message
|
|
|
|
assert.NotContains(t, err.Error(), auths[unclaimedIndex].Token.String())
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
updatedAuths, err := authDB.Get(ctx, userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, updatedAuths)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
assert.Equal(t, auths[unclaimedIndex].Token, updatedAuths[unclaimedIndex].Token)
|
|
|
|
assert.Nil(t, updatedAuths[unclaimedIndex].Claim)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
func TestNewAuthorization(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
auth, err := NewAuthorization(userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, auth)
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NotZero(t, auth.Token)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Equal(t, userID, auth.Token.UserID)
|
|
|
|
assert.NotEmpty(t, auth.Token.Data)
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Marshal(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
expectedAuths := Group{
|
2018-12-20 18:29:05 +00:00
|
|
|
{Token: t1},
|
|
|
|
{Token: t2},
|
|
|
|
}
|
|
|
|
|
|
|
|
authsBytes, err := expectedAuths.Marshal()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, authsBytes)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var actualAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
decoder := gob.NewDecoder(bytes.NewBuffer(authsBytes))
|
|
|
|
err = decoder.Decode(&actualAuths)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, actualAuths)
|
|
|
|
assert.Equal(t, expectedAuths, actualAuths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Unmarshal(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
expectedAuths := Group{
|
2018-12-20 18:29:05 +00:00
|
|
|
{Token: t1},
|
|
|
|
{Token: t2},
|
|
|
|
}
|
|
|
|
|
|
|
|
authsBytes, err := expectedAuths.Marshal()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, authsBytes)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var actualAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
err = actualAuths.Unmarshal(authsBytes)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, actualAuths)
|
|
|
|
assert.Equal(t, expectedAuths, actualAuths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Group(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
auths := make(Group, 10)
|
2018-12-20 18:29:05 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if i%2 == 0 {
|
|
|
|
auths[i] = &Authorization{
|
|
|
|
Token: t1,
|
|
|
|
Claim: &Claim{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auths[i] = &Authorization{
|
|
|
|
Token: t2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
claimed, open := auths.GroupByClaimed()
|
2018-12-20 18:29:05 +00:00
|
|
|
for _, a := range claimed {
|
|
|
|
assert.NotNil(t, a.Claim)
|
|
|
|
}
|
|
|
|
for _, a := range open {
|
|
|
|
assert.Nil(t, a.Claim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationDB_Emails(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(authDB.Close)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
var authErrs errs.Group
|
2018-12-20 18:29:05 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
2019-06-18 01:28:40 +01:00
|
|
|
_, err := authDB.Create(ctx, fmt.Sprintf("user%d@mail.test", i), 1)
|
2018-12-20 18:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
authErrs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 12:30:23 +00:00
|
|
|
require.NoError(t, authErrs.Err())
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
userIDs, err := authDB.UserIDs(ctx)
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.NotEmpty(t, userIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseToken_Valid(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
data := [tokenDataLength]byte{1, 2, 3}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
userID string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid token",
|
|
|
|
userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple delimiters",
|
2019-06-18 01:28:40 +01:00
|
|
|
"us" + tokenDelimiter + "er@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
|
|
|
t.Run(testCase.testID, func(t *testing.T) {
|
2018-12-31 15:45:43 +00:00
|
|
|
b58Data := base58.CheckEncode(data[:], tokenVersion)
|
2019-05-29 14:30:16 +01:00
|
|
|
tokenString := testCase.userID + tokenDelimiter + b58Data
|
2018-12-31 15:45:43 +00:00
|
|
|
token, err := ParseToken(tokenString)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, token)
|
2018-12-31 15:45:43 +00:00
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Equal(t, testCase.userID, token.UserID)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Equal(t, data[:], token.Data[:])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseToken_Invalid(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
data := [tokenDataLength]byte{1, 2, 3}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
tokenString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no delimiter",
|
|
|
|
userID + base58.CheckEncode(data[:], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing userID",
|
|
|
|
tokenDelimiter + base58.CheckEncode(data[:], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"not enough data",
|
|
|
|
userID + tokenDelimiter + base58.CheckEncode(data[:len(data)-10], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too much data",
|
|
|
|
userID + tokenDelimiter + base58.CheckEncode(append(data[:], []byte{0, 0, 0}...), tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data checksum/format error",
|
|
|
|
userID + tokenDelimiter + base58.CheckEncode(data[:], tokenVersion)[:len(base58.CheckEncode(data[:], tokenVersion))-4] + "0000",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
|
|
|
t.Run(testCase.testID, func(t *testing.T) {
|
|
|
|
token, err := ParseToken(testCase.tokenString)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Nil(t, token)
|
|
|
|
assert.True(t, ErrInvalidToken.Has(err))
|
|
|
|
})
|
|
|
|
}
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
func TestToken_Equal(t *testing.T) {
|
|
|
|
assert.True(t, t1.Equal(&t1))
|
|
|
|
assert.False(t, t1.Equal(&t2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
2019-02-01 17:28:40 +00:00
|
|
|
t.Skip("needs proper grpc listener to work")
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
ident, err := idents.NewIdentity()
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, listener)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(listener.Close)
|
|
|
|
ctx.Go(func() error {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{}, nil)
|
2019-02-11 11:17:32 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
clientTransport := transport.NewClient(tlsOptions)
|
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
t.Run("Basic", func(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
client, err := certificateclient.New(ctx, clientTransport, listener.Addr().String())
|
2019-02-01 17:28:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, client)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ClientFrom", func(t *testing.T) {
|
2019-02-11 11:17:32 +00:00
|
|
|
conn, err := clientTransport.DialAddress(ctx, listener.Addr().String())
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, conn)
|
|
|
|
|
|
|
|
defer ctx.Check(conn.Close)
|
|
|
|
|
|
|
|
pbClient := pb.NewCertificatesClient(conn)
|
|
|
|
require.NotNil(t, pbClient)
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
client := certificateclient.NewClientFrom(pbClient)
|
2019-02-01 17:28:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, client)
|
|
|
|
|
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
})
|
2019-01-02 17:39:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
func newTestAuthDB(t *testing.T, ctx *testcontext.Context) *DB {
|
2019-09-05 16:11:21 +01:00
|
|
|
dbURL := "bolt://" + ctx.File("authorizations.db")
|
2019-09-11 09:36:44 +01:00
|
|
|
db, err := NewDB(dbURL, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return db
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|