Skip to content

Commit

Permalink
Group Prekey, Serial Number, and Max Enrollments
Browse files Browse the repository at this point in the history
A prekey is generated for a group when a new group is created
and is stored in the groups table. Serial number and AKI has
been added to the users table and it will hold the serial number
and AKI for the current certificate for an enrolled. A configuration
option for repeated enrollment request by a user has been added. User
will be allowed a certain number of maximum enrollments which will
be specified in the server configuration file.

https://jira.hyperledger.org/browse/FAB-943
https://jira.hyperledger.org/browse/FAB-983

Change-Id: I34dff40a9eb8309da83395b242b24bb2e2c95873
Signed-off-by: Saad Karim <skarim@us.ibm.com>
  • Loading branch information
Saad Karim committed Dec 12, 2016
1 parent 01bdebd commit 690c33c
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 118 deletions.
2 changes: 1 addition & 1 deletion cli/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (ah *copAuthHandler) serveHTTP(w http.ResponseWriter, r *http.Request) erro
log.Debugf("Basic auth is not allowed; found %s", authHdr)
return errBasicAuthNotAllowed
}
_, err := cfg.UserRegistery.LoginUserBasicAuth(user, pwd)
_, err := cfg.UserRegistry.LoginUserBasicAuth(user, pwd)
if err != nil {
log.Errorf("Failed authorizing user, [error: %s]", err)
return err
Expand Down
12 changes: 5 additions & 7 deletions cli/server/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ import (

// Bootstrap is used for bootstrapping database
type Bootstrap struct {
cfg *Config
}

// BootstrapDB is a constructor to bootstrap the database at server startup
func BootstrapDB() *Bootstrap {
b := new(Bootstrap)
b.cfg = CFG
return b
}

// PopulateUsersTable populates the user table with the users defined in the server configuration file
func (b *Bootstrap) PopulateUsersTable() error {
log.Debug("populateUsersTable")
for name, info := range b.cfg.Users {
for name, info := range CFG.Users {

reg := NewRegisterUser()
reg.RegisterUser(name, info.Type, info.Group, info.Attributes, "", info.Pass)
Expand Down Expand Up @@ -71,15 +69,15 @@ func (b *Bootstrap) PopulateGroupsTable() {
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)

base := filepath.Base(b.cfg.ConfigFile)
base := filepath.Base(CFG.ConfigFile)
filename := strings.Split(base, ".")
name := filename[0]
typ := filename[1]

viper.SetConfigName(name)
viper.SetConfigType(typ)

configPath := filepath.Dir(b.cfg.ConfigFile)
configPath := filepath.Dir(CFG.ConfigFile)
viper.AddConfigPath(configPath)
err := viper.ReadInConfig()
if err != nil {
Expand All @@ -103,13 +101,13 @@ func (b *Bootstrap) registerGroup(name string, parentName string) error {
log.Debugf("Registering affiliation group (%s) with parent (%s)", name, parentName)

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

err = b.cfg.UserRegistery.InsertGroup(name, parentName)
err = CFG.UserRegistry.InsertGroup(name, parentName)
if err != nil {
log.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cli/server/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func prepBootstrap() (*Bootstrap, error) {
bootCFG.Home = bootPath
bootCFG.DataSource = bootCFG.Home + "/cop.db"

CFG.UserRegistery, err = NewUserRegistry(bootCFG.DBdriver, bootCFG.DataSource)
CFG.UserRegistry, err = NewUserRegistry(bootCFG.DBdriver, bootCFG.DataSource)
if err != nil {
return nil, err
}
Expand All @@ -73,7 +73,7 @@ func TestAllBootstrap(t *testing.T) {
func testBootstrapGroup(b *Bootstrap, t *testing.T) {
b.PopulateGroupsTable()

_, err := b.cfg.UserRegistery.GetGroup("bank_b")
_, err := CFG.UserRegistry.GetGroup("bank_b")

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

_, err := b.cfg.UserRegistery.GetUser("admin")
_, err := CFG.UserRegistry.GetUser("admin")

if err != nil {
t.Error("Failed bootstrapping users table")
Expand Down
12 changes: 11 additions & 1 deletion cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ type Config struct {
Users map[string]*User `json:"users,omitempty"`
DBdriver string `json:"driver"`
DataSource string `json:"data_source"`
UsrReg UserReg `json:"user_registry"`
Home string
ConfigFile string
CACert string
CAKey string
DB *sqlx.DB
certDBAccessor certdb.Accessor
Signer signer.Signer
UserRegistery spi.UserRegistry
UserRegistry spi.UserRegistry
}

// UserReg defines the user registry properties
type UserReg struct {
MaxEnrollments int `json:"max_enrollments"`
}

// User information
Expand Down Expand Up @@ -92,6 +98,10 @@ func configInit(cfg *cli.Config) {
}
}

if CFG.UsrReg.MaxEnrollments == 0 {
CFG.UsrReg.MaxEnrollments = 1
}

dbg := os.Getenv("COP_DEBUG")
if dbg != "" {
CFG.Debug = dbg == "true"
Expand Down
37 changes: 31 additions & 6 deletions cli/server/dasqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ DELETE FROM Groups;
)

type TestAccessor struct {
Accessor spi.UserRegistry
Accessor *Accessor
DB *sqlx.DB
}

Expand All @@ -55,19 +55,16 @@ func TestSQLite(t *testing.T) {
os.RemoveAll(dbPath)
os.MkdirAll(dbPath, 0755)
}

var cfg = new(Config)
cfg.DBdriver = "sqlite3"
cfg.DataSource = dbPath + "/cop.db"

accessor, err := NewUserRegistry(cfg.DBdriver, cfg.DataSource)
if err != nil {
t.Error("Failed to get new user registery")
}
db, err := dbutil.GetDB(cfg.DBdriver, cfg.DataSource)
if err != nil {
t.Error("Failed to open connection to DB")
}
accessor := NewDBAccessor()
accessor.SetDB(db)

ta := TestAccessor{
Accessor: accessor,
Expand Down Expand Up @@ -102,6 +99,7 @@ func testEverything(ta TestAccessor, t *testing.T) {
testUpdateUser(ta, t)
testInsertAndGetGroup(ta, t)
testDeleteGroup(ta, t)
testUpdateAndGetField(ta, t)
}

func testInsertAndGetUser(ta TestAccessor, t *testing.T) {
Expand Down Expand Up @@ -234,3 +232,30 @@ func testDeleteGroup(ta TestAccessor, t *testing.T) {
t.Error("Should have errored, and not returned any results")
}
}

func testUpdateAndGetField(ta TestAccessor, t *testing.T) {
ta.Truncate()

insert := spi.UserInfo{
Name: "testId",
Pass: "123456",
Type: "client",
Attributes: []idp.Attribute{},
}

err := ta.Accessor.InsertUser(insert)
if err != nil {
t.Errorf("Error occured during insert query of ID: %s, error: %s", insert.Name, err)
}

err = ta.Accessor.UpdateField(insert.Name, serialNumber, "1234567890")
if err != nil {
t.Errorf("Error occured during updating of field serial_number for ID: %s, error: %s", insert.Name, err)
}

_, err = ta.Accessor.GetField(insert.Name, serialNumber)
if err != nil {
t.Errorf("Error occured during get of field serial_number for ID: %s, error: %s", insert.Name, err)
}

}
Loading

0 comments on commit 690c33c

Please sign in to comment.