Skip to content

Commit

Permalink
Merge pull request trustbloc#42 from DRK3/SeparateTransientStore
Browse files Browse the repository at this point in the history
fix: Separate providers for transient and persistent storage
  • Loading branch information
talwinder50 authored Jul 24, 2020
2 parents 38fdc3e + 3066e3b commit bb6ec17
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/auth-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func startAuthService(parameters *authRestParameters, srv server) error {
}

// TODO #34 add the handlers from this controller to the router so that the endpoints are reachable
_, err = restapi.New(&operation.Config{Provider: provider})
_, err = restapi.New(&operation.Config{TransientStoreProvider: memstore.NewProvider(), StoreProvider: provider})
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/restapi/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func config() (*operation.Config, func()) {
path, cleanup := newTestOIDCProvider()

return &operation.Config{
OIDCProviderURL: path,
Provider: memstore.NewProvider(),
OIDCProviderURL: path,
TransientStoreProvider: memstore.NewProvider(),
StoreProvider: memstore.NewProvider(),
}, cleanup
}

Expand Down
19 changes: 10 additions & 9 deletions pkg/restapi/operation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ type Operation struct {

// Config defines configuration for rp operations.
type Config struct {
TLSConfig *tls.Config
RequestTokens map[string]string
OIDCProviderURL string
OIDCClientID string
OIDCClientSecret string
OIDCCallbackURL string
Provider storage.Provider
TLSConfig *tls.Config
RequestTokens map[string]string
OIDCProviderURL string
OIDCClientID string
OIDCClientSecret string
OIDCCallbackURL string
TransientStoreProvider storage.Provider
StoreProvider storage.Provider
}

type createOIDCRequestResponse struct {
Expand Down Expand Up @@ -161,7 +162,7 @@ func New(config *Config) (*Operation, error) {

svc.oidcProvider = &oidcProviderImpl{op: idp}

svc.transientStore, err = createStore(config.Provider)
svc.transientStore, err = createStore(config.TransientStoreProvider)
if err != nil {
return nil, fmt.Errorf("failed to create store : %w", err)
}
Expand All @@ -183,7 +184,7 @@ func New(config *Config) (*Operation, error) {
}
}

bootstrapStore, err := openBootstrapStore(config.Provider)
bootstrapStore, err := openBootstrapStore(config.StoreProvider)
if err != nil {
return nil, err
}
Expand Down
59 changes: 35 additions & 24 deletions pkg/restapi/operation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestNew(t *testing.T) {
config, cleanup := config()
defer cleanup()

config.Provider = memstore.NewProvider()
config.TransientStoreProvider = memstore.NewProvider()

err := config.Provider.CreateStore(bootstrapStoreName)
err := config.TransientStoreProvider.CreateStore(bootstrapStoreName)
require.NoError(t, err)

svc, err := New(config)
Expand All @@ -64,7 +64,7 @@ func TestNew(t *testing.T) {
t.Run("error if unable to open transient store", func(t *testing.T) {
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{
config.TransientStoreProvider = &mockstore.Provider{
ErrOpenStoreHandle: errors.New("test"),
}
_, err := New(config)
Expand All @@ -74,7 +74,7 @@ func TestNew(t *testing.T) {
t.Run("error if unable to create transient store", func(t *testing.T) {
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{
config.TransientStoreProvider = &mockstore.Provider{
ErrCreateStore: errors.New("generic"),
}
_, err := New(config)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestCreateOIDCRequest(t *testing.T) {
t.Run("internal server error if transient store fails", func(t *testing.T) {
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{
config.TransientStoreProvider = &mockstore.Provider{
Store: &mockstore.MockStore{
Store: make(map[string][]byte),
ErrPut: errors.New("test"),
Expand All @@ -144,7 +144,7 @@ func TestHandleOIDCCallback(t *testing.T) {
config, configCleanup := config()
defer configCleanup()

config.Provider = &mockstore.Provider{
config.TransientStoreProvider = &mockstore.Provider{
Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestHandleOIDCCallback(t *testing.T) {
config, cleanup := config()
defer cleanup()

config.Provider = &mockstore.Provider{
config.TransientStoreProvider = &mockstore.Provider{
Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
Expand All @@ -232,21 +232,26 @@ func TestHandleOIDCCallback(t *testing.T) {
})

t.Run("generic bootstrap store FETCH error", func(t *testing.T) {
sub := uuid.New().String()
id := uuid.New().String()
state := uuid.New().String()
config, cleanup := config()
defer cleanup()

config.Provider = &mockstorage.Provider{
config.TransientStoreProvider = &mockstorage.Provider{
Stores: map[string]storage.Store{
transientStoreName: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
},
},
}

config.StoreProvider = &mockstorage.Provider{
Stores: map[string]storage.Store{
bootstrapStoreName: &mockstore.MockStore{
Store: map[string][]byte{
sub: {},
id: {},
},
ErrGet: errors.New("generic"),
},
Expand All @@ -268,7 +273,7 @@ func TestHandleOIDCCallback(t *testing.T) {
oidcClaimsFunc: func(v interface{}) error {
c, ok := v.(*oidcClaims)
require.True(t, ok)
c.Sub = sub
c.Sub = id

return nil
},
Expand All @@ -282,21 +287,26 @@ func TestHandleOIDCCallback(t *testing.T) {
})

t.Run("generic bootstrap store PUT error while onboarding user", func(t *testing.T) {
sub := uuid.New().String()
id := uuid.New().String()
state := uuid.New().String()
config, cleanup := config()
defer cleanup()

config.Provider = &mockstorage.Provider{
config.TransientStoreProvider = &mockstorage.Provider{
Stores: map[string]storage.Store{
transientStoreName: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
},
},
}

config.StoreProvider = &mockstorage.Provider{
Stores: map[string]storage.Store{
bootstrapStoreName: &mockstore.MockStore{
Store: map[string][]byte{
sub: []byte("{}"),
id: []byte("{}"),
},
ErrGet: storage.ErrValueNotFound,
ErrPut: errors.New("generic"),
Expand All @@ -319,7 +329,7 @@ func TestHandleOIDCCallback(t *testing.T) {
oidcClaimsFunc: func(v interface{}) error {
c, ok := v.(*oidcClaims)
require.True(t, ok)
c.Sub = sub
c.Sub = id

return nil
},
Expand All @@ -336,7 +346,7 @@ func TestHandleOIDCCallback(t *testing.T) {
state := uuid.New().String()
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{Store: &mockstore.MockStore{
config.TransientStoreProvider = &mockstore.Provider{Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
Expand All @@ -357,7 +367,7 @@ func TestHandleOIDCCallback(t *testing.T) {
state := uuid.New().String()
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{Store: &mockstore.MockStore{
config.TransientStoreProvider = &mockstore.Provider{Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
Expand All @@ -379,7 +389,7 @@ func TestHandleOIDCCallback(t *testing.T) {
state := uuid.New().String()
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{Store: &mockstore.MockStore{
config.TransientStoreProvider = &mockstore.Provider{Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
Expand All @@ -401,7 +411,7 @@ func TestHandleOIDCCallback(t *testing.T) {
state := uuid.New().String()
config, cleanup := config()
defer cleanup()
config.Provider = &mockstore.Provider{Store: &mockstore.MockStore{
config.TransientStoreProvider = &mockstore.Provider{Store: &mockstore.MockStore{
Store: map[string][]byte{
state: []byte(state),
},
Expand Down Expand Up @@ -458,11 +468,12 @@ func config() (*Config, func()) {
path, oidcCleanup := newTestOIDCProvider()

return &Config{
OIDCProviderURL: path,
OIDCClientID: uuid.New().String(),
OIDCClientSecret: uuid.New().String(),
OIDCCallbackURL: "http://test.com",
Provider: memstore.NewProvider(),
OIDCProviderURL: path,
OIDCClientID: uuid.New().String(),
OIDCClientSecret: uuid.New().String(),
OIDCCallbackURL: "http://test.com",
TransientStoreProvider: memstore.NewProvider(),
StoreProvider: memstore.NewProvider(),
}, func() {
oidcCleanup()
}
Expand Down

0 comments on commit bb6ec17

Please sign in to comment.