Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

have dwn go through the svc endpoint #115

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/server/router/dwn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func TestDWNRouter(t *testing.T) {

serviceConfig := config.DWNServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "dwn"}}
keyStore := testKeyStoreService(t, bolt)
dwnService, err := dwn.NewDWNService(serviceConfig, bolt, keyStore)
credentialService := testCredentialService(t, bolt, keyStore)
manifestService := testManifestService(t, bolt, keyStore, credentialService)
dwnService, err := dwn.NewDWNService(serviceConfig, bolt, keyStore, manifestService)
assert.NoError(tt, err)
assert.NotEmpty(tt, dwnService)

Expand Down
10 changes: 10 additions & 0 deletions pkg/server/router/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/pkg/service/credential"
"github.com/tbd54566975/ssi-service/pkg/service/keystore"
"github.com/tbd54566975/ssi-service/pkg/service/manifest"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

Expand All @@ -28,3 +29,12 @@ func testCredentialService(t *testing.T, db *storage.BoltDB, keyStore *keystore.
require.NotEmpty(t, credentialService)
return credentialService
}

func testManifestService(t *testing.T, db *storage.BoltDB, keyStore *keystore.Service, credential *credential.Service) *manifest.Service {
serviceConfig := config.ManifestServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "manifest"}}
// create a manifest service
manifestService, err := manifest.NewManifestService(serviceConfig, db, keyStore, credential)
require.NoError(t, err)
require.NotEmpty(t, manifestService)
return manifestService
}
45 changes: 20 additions & 25 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestDIDAPI(t *testing.T) {
_ = os.Remove(storage.DBFile)
})

_, keyStoreService := newKeyStore(tt, bolt)
_, keyStoreService := testKeyStore(tt, bolt)
didService := testDIDRouter(tt, bolt, keyStoreService)

// get DID methods
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestDIDAPI(t *testing.T) {
_ = os.Remove(storage.DBFile)
})

_, keyStoreService := newKeyStore(tt, bolt)
_, keyStoreService := testKeyStore(tt, bolt)
didService := testDIDRouter(tt, bolt, keyStoreService)

// create DID by method - key - missing body
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestDIDAPI(t *testing.T) {
_ = os.Remove(storage.DBFile)
})

_, keyStore := newKeyStore(tt, bolt)
_, keyStore := testKeyStore(tt, bolt)
didService := testDIDRouter(tt, bolt, keyStore)

// get DID by method
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

// missing required field: Manifest
badManifestRequest := router.CreateManifestRequest{}
Expand Down Expand Up @@ -763,7 +763,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

w := httptest.NewRecorder()

Expand Down Expand Up @@ -820,7 +820,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

w := httptest.NewRecorder()

Expand Down Expand Up @@ -860,7 +860,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

// good request
createManifestRequest := getValidManifestRequest()
Expand Down Expand Up @@ -915,7 +915,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

// missing required field: Application
badManifestRequest := router.SubmitApplicationRequest{
Expand Down Expand Up @@ -975,7 +975,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)
w := httptest.NewRecorder()

// get a application that doesn't exit
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func TestManifestAPI(t *testing.T) {

keyStoreService := testKeyStoreService(tt, bolt)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestRouter := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, _ := testManifest(tt, bolt, keyStoreService, credentialService)

// good manifest request
createManifestRequest := getValidManifestRequest()
Expand Down Expand Up @@ -1140,9 +1140,9 @@ func TestDWNAPI(t *testing.T) {
})

keyStoreService := testKeyStoreService(tt, bolt)
dwnService := testDWNRouter(tt, bolt, keyStoreService)
credentialService := testCredentialService(tt, bolt, keyStoreService)
manifestService := testManifestRouter(tt, bolt, keyStoreService, credentialService)
manifestRouter, manifestService := testManifest(tt, bolt, keyStoreService, credentialService)
dwnService := testDWNRouter(tt, bolt, keyStoreService, manifestService)

w := httptest.NewRecorder()

Expand All @@ -1151,7 +1151,7 @@ func TestDWNAPI(t *testing.T) {

requestValue := newRequestValue(tt, createManifestRequest)
req := httptest.NewRequest(http.MethodPut, "https://ssi-service.com/v1/manifests", requestValue)
err = manifestService.CreateManifest(newRequestContext(), w, req)
err = manifestRouter.CreateManifest(newRequestContext(), w, req)
assert.NoError(tt, err)

var resp router.CreateManifestResponse
Expand Down Expand Up @@ -1180,7 +1180,7 @@ func TestKeyStoreAPI(t *testing.T) {
_ = os.Remove(storage.DBFile)
})

keyStoreRouter, _ := newKeyStore(tt, bolt)
keyStoreRouter, _ := testKeyStore(tt, bolt)
w := httptest.NewRecorder()

// bad key type
Expand Down Expand Up @@ -1229,7 +1229,7 @@ func TestKeyStoreAPI(t *testing.T) {
_ = os.Remove(storage.DBFile)
})

keyStoreService, _ := newKeyStore(tt, bolt)
keyStoreService, _ := testKeyStore(tt, bolt)
w := httptest.NewRecorder()

// store a valid key
Expand Down Expand Up @@ -1367,7 +1367,7 @@ func getValidApplicationRequest(manifestID string, submissionDescriptorId string
return createApplicationRequest
}

func newKeyStore(t *testing.T, bolt *storage.BoltDB) (*router.KeyStoreRouter, *keystore.Service) {
func testKeyStore(t *testing.T, bolt *storage.BoltDB) (*router.KeyStoreRouter, *keystore.Service) {
keyStoreService := testKeyStoreService(t, bolt)

// create router for service
Expand Down Expand Up @@ -1452,28 +1452,23 @@ func testCredentialRouter(t *testing.T, bolt *storage.BoltDB, keyStore *keystore
return credentialRouter
}

func testManifestService(t *testing.T, db *storage.BoltDB, keyStore *keystore.Service, credential *credential.Service) *manifest.Service {
func testManifest(t *testing.T, db *storage.BoltDB, keyStore *keystore.Service, credential *credential.Service) (*router.ManifestRouter, *manifest.Service) {
serviceConfig := config.ManifestServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "manifest"}}
// create a manifest service
manifestService, err := manifest.NewManifestService(serviceConfig, db, keyStore, credential)
require.NoError(t, err)
require.NotEmpty(t, manifestService)
return manifestService
}

func testManifestRouter(t *testing.T, bolt *storage.BoltDB, keyStore *keystore.Service, credential *credential.Service) *router.ManifestRouter {
manifestService := testManifestService(t, bolt, keyStore, credential)

// create router for service
manifestRouter, err := router.NewManifestRouter(manifestService)
require.NoError(t, err)
require.NotEmpty(t, manifestRouter)

return manifestRouter
return manifestRouter, manifestService
}

func testDWNRouter(t *testing.T, bolt *storage.BoltDB, keyStore *keystore.Service) *router.DWNRouter {
dwnService, err := dwn.NewDWNService(config.DWNServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "test-dwn"}, DWNEndpoint: "test-endpoint"}, bolt, keyStore)
func testDWNRouter(t *testing.T, bolt *storage.BoltDB, keyStore *keystore.Service, manifest *manifest.Service) *router.DWNRouter {
dwnService, err := dwn.NewDWNService(config.DWNServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "test-dwn"}, DWNEndpoint: "test-endpoint"}, bolt, keyStore, manifest)
require.NoError(t, err)
require.NotEmpty(t, dwnService)

Expand Down
40 changes: 24 additions & 16 deletions pkg/service/dwn/dwn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,69 @@ package dwn
import (
"fmt"

sdkutil "github.com/TBD54566975/ssi-sdk/util"
"github.com/sirupsen/logrus"

"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/keystore"
manifeststorage "github.com/tbd54566975/ssi-service/pkg/service/manifest/storage"
"github.com/tbd54566975/ssi-service/pkg/service/manifest"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

type Service struct {
config config.DWNServiceConfig
manifestStorage manifeststorage.Storage
config config.DWNServiceConfig

// external dependencies
keyStore *keystore.Service
manifest *manifest.Service
}

func (s Service) Type() framework.Type {
return framework.DWN
}

func (s Service) Status() framework.Status {
if s.manifestStorage == nil {
err := sdkutil.NewAppendError()
if s.keyStore == nil {
err.AppendString("keystore not set")
}
if s.manifest == nil {
err.AppendString("manifest not set")
}
if gotErr := err.Error(); gotErr != nil {
return framework.Status{
Status: framework.StatusNotReady,
Message: "no manifestStorage",
Message: gotErr.Error(),
}
}

return framework.Status{Status: framework.StatusReady}
}

func (s Service) Config() config.DWNServiceConfig {
return s.config
}

func NewDWNService(config config.DWNServiceConfig, s storage.ServiceStorage, keyStore *keystore.Service) (*Service, error) {
manifestStorage, err := manifeststorage.NewManifestStorage(s)
if err != nil {
errMsg := "could not instantiate manifestStorage for the dwn service"
return nil, util.LoggingErrorMsg(err, errMsg)
func NewDWNService(config config.DWNServiceConfig, s storage.ServiceStorage, keyStore *keystore.Service, manifest *manifest.Service) (*Service, error) {
if keyStore == nil {
return nil, fmt.Errorf("keystore not set")
}
if manifest == nil {
return nil, fmt.Errorf("manifest not set")
}

return &Service{
config: config,
manifestStorage: manifestStorage,
keyStore: keyStore,
config: config,
keyStore: keyStore,
manifest: manifest,
}, nil
}

func (s Service) GetManifest(request DWNPublishManifestRequest) (*DWNPublishManifestResponse, error) {

logrus.Debugf("getting manifest: %s", request.ManifestID)

gotManifest, err := s.manifestStorage.GetManifest(request.ManifestID)
gotManifest, err := s.manifest.GetManifest(manifest.GetManifestRequest{ID: request.ManifestID})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup this is exactly what I was thinking about doing for a clean up 2nd pass.

We can even delete the dwn/model.go file and use CreateManifestRequest in manifest too, but that may be too much mixing

if err != nil {
errMsg := fmt.Sprintf("could not get manifest: %s", request.ManifestID)
return nil, util.LoggingErrorMsg(err, errMsg)
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func instantiateServices(config config.ServicesConfig) ([]framework.Service, err
return nil, util.LoggingErrorMsg(err, "could not instantiate the manifest service")
}

dwnService, err := dwn.NewDWNService(config.DWNConfig, storageProvider, keyStoreService)
dwnService, err := dwn.NewDWNService(config.DWNConfig, storageProvider, keyStoreService, manifestService)
if err != nil {
return nil, util.LoggingErrorMsg(err, "could not instantiate the dwn service")
}
Expand Down