Skip to content

Commit 03d43c3

Browse files
committed
[FAB-2487] Cleanup couchdb name translation logic
This CR cleansup the translation logic in the couch specific code. After adding more restriction on the channel name in the CR - https://gerrit.hyperledger.org/r/#/c/10535, the only transformation required is to replace all the occurences of '.' to '_'. Change-Id: I7b74b892de3b2a6b8a024c720baecfcc97df132f Signed-off-by: manish <[email protected]>
1 parent 94d7e9a commit 03d43c3

File tree

4 files changed

+33
-56
lines changed

4 files changed

+33
-56
lines changed

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/txmgr_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -278,27 +278,27 @@ func TestIterator(t *testing.T) {
278278
for _, testEnv := range testEnvs {
279279
t.Logf("Running test for TestEnv = %s", testEnv.getName())
280280

281-
testLedgerID := "testiterator_1"
281+
testLedgerID := "testiterator.1"
282282
testEnv.init(t, testLedgerID)
283283
testIterator(t, testEnv, 10, 2, 7)
284284
testEnv.cleanup()
285285

286-
testLedgerID = "testiterator_2"
286+
testLedgerID = "testiterator.2"
287287
testEnv.init(t, testLedgerID)
288288
testIterator(t, testEnv, 10, 1, 11)
289289
testEnv.cleanup()
290290

291-
testLedgerID = "testiterator_3"
291+
testLedgerID = "testiterator.3"
292292
testEnv.init(t, testLedgerID)
293293
testIterator(t, testEnv, 10, 0, 0)
294294
testEnv.cleanup()
295295

296-
testLedgerID = "testiterator_4"
296+
testLedgerID = "testiterator.4"
297297
testEnv.init(t, testLedgerID)
298298
testIterator(t, testEnv, 10, 5, 0)
299299
testEnv.cleanup()
300300

301-
testLedgerID = "testiterator_5"
301+
testLedgerID = "testiterator.5"
302302
testEnv.init(t, testLedgerID)
303303
testIterator(t, testEnv, 10, 0, 5)
304304
testEnv.cleanup()
@@ -608,7 +608,7 @@ func TestValidateKey(t *testing.T) {
608608
nonUTF8Key := string([]byte{0xff, 0xff})
609609
dummyValue := []byte("dummyValue")
610610
for _, testEnv := range testEnvs {
611-
testLedgerID := "TestValidateKey"
611+
testLedgerID := "test.validate.key"
612612
testEnv.init(t, testLedgerID)
613613
txSimulator, _ := testEnv.getTxMgr().NewTxSimulator()
614614
err := txSimulator.SetState("ns1", nonUTF8Key, dummyValue)

core/ledger/util/couchdb/couchdb_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestDBBadDatabaseName(t *testing.T) {
256256
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
257257
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
258258
_, dberr := CreateCouchDatabase(*couchInstance, "testDB")
259-
testutil.AssertNoError(t, dberr, fmt.Sprintf("Error when testing a valid database name"))
259+
testutil.AssertError(t, dberr, "Error should have been thrown for an invalid db name")
260260

261261
//create a new instance and database object using a valid database name letters and numbers
262262
couchInstance, err = CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
@@ -270,20 +270,19 @@ func TestDBBadDatabaseName(t *testing.T) {
270270
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
271271
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
272272
_, dberr = CreateCouchDatabase(*couchInstance, "test1234~!@#$%^&*()[]{}.")
273-
testutil.AssertNoError(t, dberr, fmt.Sprintf("Error when testing a valid database name"))
273+
testutil.AssertError(t, dberr, "Error should have been thrown for an invalid db name")
274274

275275
//create a new instance and database object using a invalid database name - too long /*
276276
couchInstance, err = CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
277277
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
278278
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
279-
_, dberr = CreateCouchDatabase(*couchInstance, "A12345678901234567890123456789012345678901234"+
279+
_, dberr = CreateCouchDatabase(*couchInstance, "a12345678901234567890123456789012345678901234"+
280280
"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890"+
281281
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456"+
282282
"78901234567890123456789012345678901234567890")
283283
testutil.AssertError(t, dberr, fmt.Sprintf("Error should have been thrown for invalid database name"))
284284

285285
}
286-
287286
}
288287

289288
func TestDBBadConnection(t *testing.T) {

core/ledger/util/couchdb/couchdbutil.go

+14-30
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"time"
2626
)
2727

28-
var validNamePattern = `^[a-z][a-z0-9_$(),+/-]+`
28+
var expectedChannelNamePattern = `[a-z][a-z0-9.-]*`
2929
var maxLength = 249
3030

3131
//CreateCouchInstance creates a CouchDB instance
@@ -142,44 +142,28 @@ func CreateSystemDatabasesIfNotExist(couchInstance CouchInstance) error {
142142
//_, $, (, ), +, -, and / are allowed. Must begin with a letter.
143143
//
144144
//Restictions have already been applied to the database name from Orderer based on
145-
//restrictions required by Kafka
145+
//restrictions required by Kafka and couchDB (except a '.' char). The databaseName
146+
// passed in here is expected to follow `[a-z][a-z0-9.-]*` pattern.
146147
//
147-
//The validation will validate upper case, the string will be lower cased
148-
//Replace any characters not allowed in CouchDB with an "_"
149-
//Check for a leading letter, if not present, the prepend "db_"
148+
//This validation will simply check whether the database name matches the above pattern and will replace
149+
// all occurence of '.' by '-'. This will not cause collisions in the trnasformed named
150150
func mapAndValidateDatabaseName(databaseName string) (string, error) {
151-
152151
// test Length
153152
if len(databaseName) <= 0 {
154153
return "", fmt.Errorf("Database name is illegal, cannot be empty")
155154
}
156155
if len(databaseName) > maxLength {
157156
return "", fmt.Errorf("Database name is illegal, cannot be longer than %d", maxLength)
158157
}
159-
160-
//force the name to all lowercase
161-
databaseName = strings.ToLower(databaseName)
162-
163-
//Replace any characters not allowed in CouchDB with an "_"
164-
replaceString := regexp.MustCompile(`[^a-z0-9_$(),+/-]`)
165-
166-
//Set up the replace pattern for special characters
167-
validatedDatabaseName := replaceString.ReplaceAllString(databaseName, "_")
168-
169-
//if the first character is not a letter, then prepend "db_"
170-
testLeadingLetter := regexp.MustCompile("^[a-z]")
171-
isLeadingLetter := testLeadingLetter.MatchString(validatedDatabaseName)
172-
if !isLeadingLetter {
173-
validatedDatabaseName = "db_" + validatedDatabaseName
158+
re, err := regexp.Compile(expectedChannelNamePattern)
159+
if err != nil {
160+
return "", err
174161
}
175-
176-
//create the expression for valid characters
177-
validString := regexp.MustCompile(validNamePattern)
178-
179-
// Illegal characters
180-
matched := validString.MatchString(validatedDatabaseName)
181-
if !matched {
182-
return "", fmt.Errorf("Database name '%s' contains illegal characters", validatedDatabaseName)
162+
matched := re.FindString(databaseName)
163+
if len(matched) != len(databaseName) {
164+
return "", fmt.Errorf("databaseName '%s' does not matches pattern '%s'", databaseName, expectedChannelNamePattern)
183165
}
184-
return validatedDatabaseName, nil
166+
// replace all '.' to '_'. The databaseName passed in will never contain an '_'. So, this translation will not cause collisions
167+
databaseName = strings.Replace(databaseName, ".", "_", -1)
168+
return databaseName, nil
185169
}

core/ledger/util/couchdb/couchdbutil_test.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,29 @@ func TestCreateCouchDBSystemDBs(t *testing.T) {
8484

8585
}
8686
func TestDatabaseMapping(t *testing.T) {
87-
8887
//create a new instance and database object using a database name mixed case
89-
databaseName, err := mapAndValidateDatabaseName("testDB")
90-
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
91-
testutil.AssertEquals(t, databaseName, "testdb")
92-
93-
//create a new instance and database object using a database name with numerics
94-
databaseName, err = mapAndValidateDatabaseName("test1234DB")
95-
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
96-
testutil.AssertEquals(t, databaseName, "test1234db")
88+
_, err := mapAndValidateDatabaseName("testDB")
89+
testutil.AssertError(t, err, "Error expected because the name contains capital letters")
9790

9891
//create a new instance and database object using a database name with special characters
99-
databaseName, err = mapAndValidateDatabaseName("test1234_$(),+-/~!@#%^&*[]{}.")
100-
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
101-
testutil.AssertEquals(t, databaseName, "test1234_$(),+-/_____________")
92+
_, err = mapAndValidateDatabaseName("test1234_1")
93+
testutil.AssertError(t, err, "Error expected because the name contains illegal chars")
10294

10395
//create a new instance and database object using a database name with special characters
104-
databaseName, err = mapAndValidateDatabaseName("5test1234")
105-
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
106-
testutil.AssertEquals(t, databaseName, "db_5test1234")
96+
_, err = mapAndValidateDatabaseName("5test1234")
97+
testutil.AssertError(t, err, "Error expected because the name starts with a number")
10798

10899
//create a new instance and database object using an empty string
109100
_, err = mapAndValidateDatabaseName("")
110101
testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
111102

112-
_, err = mapAndValidateDatabaseName("A12345678901234567890123456789012345678901234" +
103+
_, err = mapAndValidateDatabaseName("a12345678901234567890123456789012345678901234" +
113104
"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
114105
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456" +
115106
"78901234567890123456789012345678901234567890")
116107
testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
117108

109+
transformedName, err := mapAndValidateDatabaseName("test.my.db-1")
110+
testutil.AssertNoError(t, err, "")
111+
testutil.AssertEquals(t, transformedName, "test_my_db-1")
118112
}

0 commit comments

Comments
 (0)