Commit bcad7f09 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Copy configuration store to storage folder

parent 35fb735e
......@@ -201,8 +201,8 @@ func TestMetadataAttribute(t *testing.T) {
}
func TestMetadataCompatibility(t *testing.T) {
store := NewConfigurationStore()
require.NoError(t, store.ParseFolder("testdata/irma_configuration"))
store := NewConfigurationStore("testdata")
require.NoError(t, store.ParseFolder())
// An actual metadata attribute of an IRMA credential extracted from the IRMA app
attr := MetadataFromInt(s2big("49043481832371145193140299771658227036446546573739245068"), store)
......@@ -222,8 +222,8 @@ func TestMetadataCompatibility(t *testing.T) {
}
func TestAttributeDisjunctionMarshaling(t *testing.T) {
store := NewConfigurationStore()
store.ParseFolder("testdata/irma_configuration")
store := NewConfigurationStore("testdata")
store.ParseFolder()
disjunction := AttributeDisjunction{}
var _ json.Unmarshaler = &disjunction
......
......@@ -3,6 +3,7 @@ package irmago
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"strconv"
......@@ -37,6 +38,30 @@ func PathExists(path string) (bool, error) {
return true, err
}
func ensureDirectoryExists(path string) error {
exists, err := PathExists(path)
if err != nil {
return err
}
if exists {
return nil
}
return os.Mkdir(path, 0700)
}
// writeFile writes the contents of reader to a new or truncated file at dest.
func writeFile(reader io.Reader, dest string) error {
destfile, err := os.Create(dest)
if err != nil {
return err
}
if _, err := io.Copy(destfile, reader); err != nil {
destfile.Close()
return err
}
return destfile.Close()
}
// NewCredentialManager deserializes the credentials from storage.
func NewCredentialManager(
storagePath string,
......@@ -49,8 +74,9 @@ func NewCredentialManager(
keyshareServers: make(map[SchemeManagerIdentifier]*keyshareServer),
}
cm.Store = NewConfigurationStore()
if err = cm.Store.ParseFolder(irmaConfigurationPath); err != nil {
cm.Store = NewConfigurationStore(storagePath)
cm.Store.Copy(irmaConfigurationPath)
if err = cm.Store.ParseFolder(); err != nil {
return nil, err
}
......@@ -110,15 +136,7 @@ func (cm *CredentialManager) ensureStorageExists() (err error) {
return errors.New("credential storage path does not exist")
}
exist, err = PathExists(cm.path(signaturesDir))
if err != nil {
return err
}
if !exist {
err = os.Mkdir(cm.path(signaturesDir), 0700)
}
return
return ensureDirectoryExists(cm.path(signaturesDir))
}
func (cm *CredentialManager) storeSecretKey(sk *big.Int) error {
......
package irmago
import (
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"io/ioutil"
......@@ -9,6 +8,8 @@ import (
"path/filepath"
"strconv"
"crypto/sha256"
"github.com/mhe/gabi"
)
......@@ -24,17 +25,42 @@ type ConfigurationStore struct {
initialized bool
}
func NewConfigurationStore() (store *ConfigurationStore) {
// NewConfigurationStore returns a new configuration store. After this
// ParseFolder() should be called to parse the specified path.
func NewConfigurationStore(path string) (store *ConfigurationStore) {
store = &ConfigurationStore{
SchemeManagers: make(map[SchemeManagerIdentifier]*SchemeManager),
Issuers: make(map[IssuerIdentifier]*Issuer),
Credentials: make(map[CredentialTypeIdentifier]*CredentialType),
publicKeys: make(map[IssuerIdentifier][]*gabi.PublicKey),
reverseHashes: make(map[string]CredentialTypeIdentifier),
path: path + "/irma_configuration",
}
return
}
// ParseFolder populates the current store by parsing the storage path,
// listing the containing scheme managers, issuers and credential types.
func (store *ConfigurationStore) ParseFolder() error {
err := iterateSubfolders(store.path, func(dir string) error {
manager := &SchemeManager{}
exists, err := pathToDescription(dir+"/description.xml", manager)
if err != nil {
return err
}
if exists {
store.SchemeManagers[manager.Identifier()] = manager
return store.parseIssuerFolders(dir)
}
return nil
})
if err != nil {
return err
}
store.initialized = true
return nil
}
// PublicKey returns the specified public key, or nil if not present in the ConfigurationStore.
func (store *ConfigurationStore) PublicKey(id IssuerIdentifier, counter int) (*gabi.PublicKey, error) {
if _, contains := store.publicKeys[id]; !contains {
......@@ -70,29 +96,6 @@ func (store *ConfigurationStore) IsInitialized() bool {
return store.initialized
}
// ParseFolder populates the current store by parsing the specified irma_configuration folder,
// listing the containing scheme managers, issuers, credential types and public keys.
func (store *ConfigurationStore) ParseFolder(path string) error {
store.path = path
err := iterateSubfolders(path, func(dir string) error {
manager := &SchemeManager{}
exists, err := pathToDescription(dir+"/description.xml", manager)
if err != nil {
return err
}
if exists {
store.SchemeManagers[manager.Identifier()] = manager
return store.parseIssuerFolders(dir)
}
return nil
})
if err != nil {
return err
}
store.initialized = true
return nil
}
func (store *ConfigurationStore) parseIssuerFolders(path string) error {
return iterateSubfolders(path, func(dir string) error {
issuer := &Issuer{}
......@@ -203,3 +206,34 @@ func (store *ConfigurationStore) Contains(cred CredentialTypeIdentifier) bool {
store.Issuers[cred.IssuerIdentifier()] != nil &&
store.Credentials[cred] != nil
}
func (store *ConfigurationStore) Copy(source string) error {
if err := ensureDirectoryExists(store.path); err != nil {
return err
}
// TODO skip existing scheme managers? individual files?
return filepath.Walk(source, filepath.WalkFunc(
func(path string, info os.FileInfo, err error) error {
if path == source {
return nil
}
subpath := path[len(source):]
if info.IsDir() {
if err := ensureDirectoryExists(store.path + subpath); err != nil {
return err
}
} else {
srcfile, err := os.Open(path)
if err != nil {
return err
}
defer srcfile.Close()
if err := writeFile(srcfile, store.path+subpath); err != nil {
return err
}
}
return nil
}),
)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment