-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-1349] Enforce restrictions on chain IDs
https://jira.hyperledger.org/browse/FAB-1349 This changeset introduces a function which rejects chain IDs (i.e. channel names) that do NOT comply with the following restrictions: 1. Contain only ASCII alphanumerics, dots '.', dashes '-'. 2. Are shorter than 250 characters. 3. Are not "." or "..". Our hand here is forced by Kafka, since a channel in Fabric maps to a Kafka topic, and there are restrictions for the allowed topic names [1]. Note that underscores are allowed in Kafka, but topics with a period or underscore could collide. (Thanks to Luis Sanchez for bringing this to my attention.) We therefore remove support for underscores altoghether to keep the checks simpler. UPDATE: Switched from a filter to a function because filters are not evaluated during the system chain creation. [1] https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/common/Topic.scala#L29 Change-Id: I14b95477e485fea27868338e2f33772588b8a770 Signed-off-by: Kostas Christidis <kostas@christidis.io>
- Loading branch information
1 parent
7833d4d
commit 05a0edf
Showing
4 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package configtx | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
// TestValidchainID checks that the constraints on chain IDs are enforced properly | ||
func TestValidChainID(t *testing.T) { | ||
acceptMsg := "Should have accepted valid chain ID" | ||
rejectMsg := "Should have rejected invalid chain ID" | ||
|
||
t.Run("ZeroLength", func(t *testing.T) { | ||
if err := validateChainID(""); err == nil { | ||
t.Fatal(rejectMsg) | ||
} | ||
}) | ||
|
||
t.Run("LongerThanMaxAllowed", func(t *testing.T) { | ||
if err := validateChainID(randomAlphaString(maxLength + 1)); err == nil { | ||
t.Fatal(rejectMsg) | ||
} | ||
}) | ||
|
||
t.Run("HasIllegalName", func(t *testing.T) { | ||
for illegalName := range illegalNames { | ||
if err := validateChainID(illegalName); err == nil { | ||
t.Fatal(rejectMsg) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("ContainsIllegalCharacter", func(t *testing.T) { | ||
if err := validateChainID("foo_bar"); err == nil { | ||
t.Fatal(rejectMsg) | ||
} | ||
}) | ||
|
||
t.Run("ValidName", func(t *testing.T) { | ||
if err := validateChainID("foo.bar"); err != nil { | ||
t.Fatal(acceptMsg) | ||
} | ||
}) | ||
} | ||
|
||
// Helper functions | ||
|
||
func randomAlphaString(size int) string { | ||
letters := []rune("abcdefghijklmnopqrstuvwxyz") | ||
output := make([]rune, size) | ||
for i := range output { | ||
output[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return string(output) | ||
} |