Skip to content

Commit

Permalink
Add database config as part of server config
Browse files Browse the repository at this point in the history
Database configuration is now part of the main configuration file.
Code clean up was done to prevent unnecessary database connection to
be opened. Removed test files that are no longer necessary after
these recent code changes. Added instructions to README on how to
register a user.

https://jira.hyperledger.org/browse/FAB-1086

Change-Id: I179ef4246e20d3ffc908b13c59a02c246acf4b6b
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Nov 22, 2016
1 parent 8698933 commit 1114d56
Show file tree
Hide file tree
Showing 25 changed files with 251 additions and 199 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ The following command gets an ecert for the admin user.
# ./cop client enroll admin adminpw ../testdata/csr.json http://localhost:8888
```


### Register a new user

Create a JSON file as defined below for the user being registered.

```
registerRequest.json:
{
"id": "User1",
"type": "client",
"group": "bank_a",
"attrs": [{"name":"AttributeName","value":"AttributeValue"}]
}
```

The following command will register the user.
```
# cd $COP/bin
# ./cop client register ../testdata/registerRequest.json http://localhost:8888
```

### Run the COP tests

To run the COP test, do the following.
Expand Down
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ type Enrollment struct {
// UserRecord used for inserting into database
type UserRecord struct {
ID string `db:"id"`
EnrollmentID string `db:"enrollmentId"`
EnrollmentID string `db:"enrollment_id"`
Token string `db:"token"`
Type string `db:"type"`
Metadata string `db:"metadata"`
State int `db:"state"`
Key int `db:"key"`
SerialNumber string `db:"serial_number"`
}

// Accessor abstracts the CRUD of certdb objects from a DB.
Expand Down
1 change: 1 addition & 0 deletions api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
EnrollingUserError
RegisteringUserError
AuthorizationError
DatabaseError
)

// Error is an interface with a Code method
Expand Down
1 change: 0 additions & 1 deletion cli/client/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func registerMain(args []string, c cli.Config) error {
log.Error(err)
return err
}
fmt.Printf("id: %+v", id)

copServer, _, err := cli.PopFirstArgument(args)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/cop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Admin struct {
const (
CERT string = "../testdata/ec.pem"
KEY string = "../testdata/ec-key.pem"
CFG string = "../testdata/cop.json"
CFG string = "../testdata/testconfig.json"
CSR string = "../testdata/csr.json"
REG string = "../testdata/registerrequest.json"
DBCONFIG string = "../testdata/enrolltest.json"
Expand Down
19 changes: 12 additions & 7 deletions cli/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cloudflare/cfssl/api"
cerr "github.com/cloudflare/cfssl/errors"
"github.com/cloudflare/cfssl/log"
cop "github.com/hyperledger/fabric-cop/api"
"github.com/hyperledger/fabric-cop/util"
)

Expand Down Expand Up @@ -101,14 +102,18 @@ func (ah *copAuthHandler) serveHTTP(w http.ResponseWriter, r *http.Request) erro
log.Debug("No users are defined")
return authError
}
user := cfg.Users[user]
if user == nil {
log.Debug("User not found")
return authError
userRecord, err := cfg.DBAccessor.GetUser(user)
if err != nil {
log.Errorf("Failed to get user [error: %s]", err)
return err
}
if user.Pass != pwd {
log.Debug("Invalid password")
return authError
if userRecord.ID == "" {
log.Debug("User '%s' not found", user)
return cop.NewError(cop.EnrollingUserError, "User '%s' not found", user)
}
if userRecord.Token != pwd {
log.Debug("Incorrect password for '%s'; received %s but expected %s", userRecord.ID, pwd, userRecord.Token)
return cop.NewError(cop.EnrollingUserError, "Incorrect password for '%s'; received %s but expected %s", userRecord.ID, pwd, userRecord.Token)
}
log.Debug("User/pass was correct")
// TODO: Do the following
Expand Down
34 changes: 14 additions & 20 deletions cli/server/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,18 @@ import (
"strings"

"github.com/cloudflare/cfssl/log"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
)

// Bootstrap is used for bootstrapping database
type Bootstrap struct {
db *sqlx.DB
dbAccessor *Accessor
cfg *Config
cfg *Config
}

// BootstrapDB is a constructor to bootstrap the database at server startup
func BootstrapDB(db *sqlx.DB, cfg *Config) *Bootstrap {
func BootstrapDB() *Bootstrap {
b := new(Bootstrap)
b.db = db
b.dbAccessor = NewDBAccessor()
b.dbAccessor.SetDB(b.db)
b.cfg = cfg
b.cfg = CFG
return b
}

Expand All @@ -63,19 +57,19 @@ func (b *Bootstrap) PopulateUsersTable() error {
return nil
}

func (b *Bootstrap) populateGroup(name, parent, key string, level int, db *sqlx.DB) {
registerGroup(name, parent, db)
func (b *Bootstrap) populateGroup(name, parent, key string, level int) {
b.registerGroup(name, parent)
newKey := key + "." + name

if level == 0 {
affiliationGroups := viper.GetStringSlice(newKey)
for ci := range affiliationGroups {
registerGroup(affiliationGroups[ci], name, db)
b.registerGroup(affiliationGroups[ci], name)
}
} else {
affiliationGroups := viper.GetStringMapString(newKey)
for childName := range affiliationGroups {
b.populateGroup(childName, name, newKey, level-1, db)
b.populateGroup(childName, name, newKey, level-1)
}
}
}
Expand Down Expand Up @@ -103,28 +97,28 @@ func (b *Bootstrap) PopulateGroupsTable() {

key := "groups"
affiliationGroups := viper.GetStringMapString(key)
if len(affiliationGroups) == 0 {
log.Info("No groups specified in configuration file")
}
for name := range affiliationGroups {
b.populateGroup(name, "", key, 1, b.db)
b.populateGroup(name, "", key, 1)
}
}

func registerGroup(name string, parentName string, db *sqlx.DB) error {
func (b *Bootstrap) registerGroup(name string, parentName string) error {
mutex.Lock()
defer mutex.Unlock()

log.Debug("Registering affiliation group " + name + " parent " + parentName + ".")

dbAccessor := NewDBAccessor()
dbAccessor.SetDB(db)

var err error
_, _, err = dbAccessor.GetGroup(name)
_, _, err = b.cfg.DBAccessor.GetGroup(name)
if err == nil {
log.Error("Group already registered")
return errors.New("Group already registered")
}

err = dbAccessor.InsertGroup(name, parentName)
err = b.cfg.DBAccessor.InsertGroup(name, parentName)
if err != nil {
log.Error(err)
}
Expand Down
22 changes: 13 additions & 9 deletions cli/server/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package server

import (
"os"
"path/filepath"
"testing"

"github.com/cloudflare/cfssl/cli"
"github.com/hyperledger/fabric-cop/util"
)

// var testDB *sqlx.DB
Expand All @@ -41,15 +39,21 @@ func prepBootstrap() *Bootstrap {
os.RemoveAll(bootPath)
os.MkdirAll(bootPath, 0755)
}

cfg := new(cli.Config)
cfg.ConfigFile = "../../testdata/cop.json"
cfg.DBConfigFile = "../../testdata/bootstraptest.json"
cfg.ConfigFile = "../../testdata/testconfig.json"
configInit(cfg)

bootCFG = CFG
bootCFG.Home = bootPath
dataSource := filepath.Join(bootCFG.Home, bootCFG.DataSource)
db, _ := util.CreateTables(bootCFG.DBdriver, dataSource)
b := BootstrapDB(db, bootCFG)

db, _ := GetDB(bootCFG)

CFG.DB = db
CFG.DBAccessor = NewDBAccessor()
CFG.DBAccessor.SetDB(db)

b := BootstrapDB()
return b
}

Expand All @@ -65,7 +69,7 @@ func TestAllBootstrap(t *testing.T) {
func testBootstrapGroup(b *Bootstrap, t *testing.T) {
b.PopulateGroupsTable()

_, _, err := b.dbAccessor.GetGroup("bank_b")
_, _, err := b.cfg.DBAccessor.GetGroup("bank_b")

if err != nil {
t.Error("Failed bootstrapping groups table")
Expand All @@ -75,7 +79,7 @@ func testBootstrapGroup(b *Bootstrap, t *testing.T) {
func testBootstrapUsers(b *Bootstrap, t *testing.T) {
b.PopulateUsersTable()

_, err := b.dbAccessor.GetUser("admin")
_, err := b.cfg.DBAccessor.GetUser("admin")

if err != nil {
t.Error("Failed bootstrapping users table")
Expand Down
17 changes: 4 additions & 13 deletions cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cloudflare/cfssl/cli"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-cop/idp"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // Needed to support sqlite
)

Expand All @@ -36,10 +37,11 @@ type Config struct {
DBdriver string `json:"driver"`
DataSource string `json:"data_source"`
Home string
DBConfigFile string
ConfigFile string
CACert string
CAKey string
DB *sqlx.DB
DBAccessor *Accessor
}

// User information
Expand Down Expand Up @@ -73,6 +75,7 @@ func configInit(cfg *cli.Config) {
}
if cfg.ConfigFile != "" {
CFG.ConfigFile = cfg.ConfigFile
cfg.DBConfigFile = cfg.ConfigFile
body, err := ioutil.ReadFile(cfg.ConfigFile)
if err != nil {
panic(err.Error())
Expand All @@ -84,18 +87,6 @@ func configInit(cfg *cli.Config) {
}
}

if cfg.DBConfigFile != "" {
CFG.DBConfigFile = cfg.DBConfigFile
body, err := ioutil.ReadFile(cfg.DBConfigFile)
if err != nil {
panic(err.Error())
}
err = json.Unmarshal(body, CFG)
if err != nil {
panic(fmt.Sprintf("error parsing %s: %s", cfg.ConfigFile, err.Error()))
}
}

dbg := os.Getenv("COP_DEBUG")
if dbg != "" {
CFG.Debug = dbg == "true"
Expand Down
13 changes: 6 additions & 7 deletions cli/server/dasqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package server

import (
"fmt"
"os"
"strings"
"testing"

api "github.com/hyperledger/fabric-cop/api"
"github.com/hyperledger/fabric-cop/util"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
Expand Down Expand Up @@ -55,7 +55,11 @@ func TestSQLite(t *testing.T) {
os.MkdirAll(dbPath, 0755)
}

db, err := util.CreateTables("sqlite3", dbPath+"/testing.db")
var cfg Config
cfg.DBdriver = "sqlite3"
cfg.DataSource = dbPath + "/cop.db"
fmt.Println("cfg: ", cfg)
db, err := GetDB(&cfg)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -83,11 +87,6 @@ func Truncate(db *sqlx.DB) {
}
}

func createTables(db *sqlx.DB) {
db.Exec("CREATE TABLE IF NOT EXISTS Users (id TEXT, enrollmentId TEXT, token BLOB, metadata TEXT, state INTEGER, key BLOB)")
db.Exec("CREATE TABLE IF NOT EXISTS Groups (name TEXT, parentID TEXT)")
}

func removeDatabase() {
os.RemoveAll(dbPath)
}
Expand Down
Loading

0 comments on commit 1114d56

Please sign in to comment.