Skip to content

Commit 4473e1c

Browse files
committed
[FAB-6399] Benign panic in config update computation
This CR adds a check for the length of a config item's path and triggers a panic if it is zero. The decision to still trigger a panic stems from the fact that if the item's path is zero, it means something unexpected occurred in the validation logic and we should not proceed. The user who originally reported this bug likely hit a config issue that is no longer possible after the fix for FAB-5606. Change-Id: I4052174c04e0810714823c3ca8a533ab2f261804 Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent 97560bc commit 4473e1c

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

common/configtx/update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ func (cm *configManager) policyForItem(item comparable) (policies.Policy, bool)
178178
// If the mod_policy path is relative, get the right manager for the context
179179
// if the mod_policy path is absolute (starts with /) evaluate at the root
180180
if len(modPolicy) > 0 && modPolicy[0] != policies.PathSeparator[0] {
181-
// path is always at least of length 1
181+
// path should always be at least length 1, panic if not
182+
if len(item.path) == 0 {
183+
logger.Panicf("Empty path for item %s", item.key)
184+
}
182185
var ok bool
183186
manager, ok = manager.Manager(item.path[1:])
184187
if !ok {

common/configtx/update_test.go

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -152,41 +152,62 @@ func TestPolicyForItem(t *testing.T) {
152152
},
153153
}
154154

155-
policy, ok := cm.policyForItem(comparable{
156-
path: []string{"root"},
157-
ConfigValue: &cb.ConfigValue{
158-
ModPolicy: "rootPolicy",
159-
},
155+
t.Run("Root manager", func(t *testing.T) {
156+
policy, ok := cm.policyForItem(comparable{
157+
path: []string{"root"},
158+
ConfigValue: &cb.ConfigValue{
159+
ModPolicy: "rootPolicy",
160+
},
161+
})
162+
assert.True(t, ok)
163+
assert.Equal(t, policy, rootPolicy, "Should have found relative policy off the root manager")
160164
})
161-
assert.True(t, ok)
162-
assert.Equal(t, policy, rootPolicy, "Should have found relative policy off the root manager")
163165

164-
policy, ok = cm.policyForItem(comparable{
165-
path: []string{"root", "wrong"},
166-
ConfigValue: &cb.ConfigValue{
167-
ModPolicy: "rootPolicy",
168-
},
166+
t.Run("Nonexistent manager", func(t *testing.T) {
167+
_, ok := cm.policyForItem(comparable{
168+
path: []string{"root", "wrong"},
169+
ConfigValue: &cb.ConfigValue{
170+
ModPolicy: "rootPolicy",
171+
},
172+
})
173+
assert.False(t, ok, "Should not have found rootPolicy off a nonexistent manager")
169174
})
170-
assert.False(t, ok, "Should not have found rootPolicy off a non-existent manager")
171175

172-
policy, ok = cm.policyForItem(comparable{
173-
path: []string{"root", "foo"},
174-
ConfigValue: &cb.ConfigValue{
175-
ModPolicy: "foo",
176-
},
176+
t.Run("Foo manager", func(t *testing.T) {
177+
policy, ok := cm.policyForItem(comparable{
178+
path: []string{"root", "foo"},
179+
ConfigValue: &cb.ConfigValue{
180+
ModPolicy: "foo",
181+
},
182+
})
183+
assert.True(t, ok)
184+
assert.Equal(t, policy, fooPolicy, "Should have found relative foo policy off the foo manager")
177185
})
178-
assert.True(t, ok)
179-
assert.Equal(t, policy, fooPolicy, "Should not have found relative foo policy the foo manager")
180186

181-
policy, ok = cm.policyForItem(comparable{
182-
key: "foo",
183-
path: []string{"root"},
184-
ConfigGroup: &cb.ConfigGroup{
185-
ModPolicy: "foo",
186-
},
187+
t.Run("Foo group", func(t *testing.T) {
188+
policy, ok := cm.policyForItem(comparable{
189+
key: "foo",
190+
path: []string{"root"},
191+
ConfigGroup: &cb.ConfigGroup{
192+
ModPolicy: "foo",
193+
},
194+
})
195+
assert.True(t, ok)
196+
assert.Equal(t, policy, fooPolicy, "Should have found relative foo policy for foo group")
197+
})
198+
199+
t.Run("Empty item path", func(t *testing.T) {
200+
policyForItemEmptyPath := func() {
201+
cm.policyForItem(comparable{
202+
key: "foo",
203+
path: []string{},
204+
ConfigGroup: &cb.ConfigGroup{
205+
ModPolicy: "foo",
206+
},
207+
})
208+
}
209+
assert.Panics(t, policyForItemEmptyPath)
187210
})
188-
assert.True(t, ok)
189-
assert.Equal(t, policy, fooPolicy, "Should have found relative foo policy for foo group")
190211
}
191212

192213
func TestValidateModPolicy(t *testing.T) {

0 commit comments

Comments
 (0)