Skip to content

Commit ebac616

Browse files
committed
litcli: per feature channel and peer restrictions
Different features interprete the restrictions differently, which is why we add the possibility of specifying restrictions per feature.
1 parent e9cfa4b commit ebac616

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

cmd/litcli/autopilot.go

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ var addAutopilotSessionCmd = cli.Command{
4343
Description: `
4444
Initialize an Autopilot session.
4545
46-
If set for any feature, configuration flags need to be repeated for each
47-
feature that is registered, corresponding to the order of features.`,
46+
If set for any feature, configuration flags and channel and peer
47+
restrict lists need to be repeated for each feature that is registered,
48+
corresponding to the order of features.`,
4849
Action: initAutopilotSession,
4950
Flags: []cli.Flag{
5051
labelFlag,
@@ -55,15 +56,15 @@ var addAutopilotSessionCmd = cli.Command{
5556
Name: "feature",
5657
Required: true,
5758
},
58-
cli.StringFlag{
59-
Name: "channel-restrict-list",
59+
cli.StringSliceFlag{
60+
Name: "feature-channel-restrict-list",
6061
Usage: "list of channel IDs that the " +
6162
"Autopilot server should not " +
6263
"perform actions on. In the " +
6364
"form of: chanID1,chanID2,...",
6465
},
65-
cli.StringFlag{
66-
Name: "peer-restrict-list",
66+
cli.StringSliceFlag{
67+
Name: "feature-peer-restrict-list",
6768
Usage: "list of peer IDs that the " +
6869
"Autopilot server should not " +
6970
"perform actions on. In the " +
@@ -191,45 +192,63 @@ func initAutopilotSession(ctx *cli.Context) error {
191192
defer cleanup()
192193
client := litrpc.NewAutopilotClient(clientConn)
193194

194-
ruleMap := &litrpc.RulesMap{
195-
Rules: make(map[string]*litrpc.RuleValue),
195+
features := ctx.StringSlice("feature")
196+
197+
perFeatureRules := make([]*litrpc.RulesMap, len(features))
198+
for i := range features {
199+
perFeatureRules[i] = &litrpc.RulesMap{
200+
Rules: make(map[string]*litrpc.RuleValue),
201+
}
202+
}
203+
204+
chanRestrictList := ctx.StringSlice("feature-channel-restrict-list")
205+
if len(chanRestrictList) > 0 && len(features) != len(chanRestrictList) {
206+
return fmt.Errorf("number of features (%v) and channel "+
207+
"restrict list (%v) must match", len(features),
208+
len(chanRestrictList))
196209
}
197210

198-
chanRestrictList := ctx.String("channel-restrict-list")
199-
if chanRestrictList != "" {
211+
for i, chanRestrict := range chanRestrictList {
200212
var chanIDs []uint64
201-
chans := strings.Split(chanRestrictList, ",")
213+
chans := strings.Split(chanRestrict, ",")
202214
for _, c := range chans {
203-
i, err := strconv.ParseUint(c, 10, 64)
215+
cid, err := strconv.ParseUint(c, 10, 64)
204216
if err != nil {
205217
return err
206218
}
207-
chanIDs = append(chanIDs, i)
219+
chanIDs = append(chanIDs, cid)
208220
}
209221

210-
ruleMap.Rules[rules.ChannelRestrictName] = &litrpc.RuleValue{
211-
Value: &litrpc.RuleValue_ChannelRestrict{
212-
ChannelRestrict: &litrpc.ChannelRestrict{
213-
ChannelIds: chanIDs,
222+
perFeatureRules[i].Rules[rules.ChannelRestrictName] =
223+
&litrpc.RuleValue{
224+
Value: &litrpc.RuleValue_ChannelRestrict{
225+
ChannelRestrict: &litrpc.ChannelRestrict{
226+
ChannelIds: chanIDs,
227+
},
214228
},
215-
},
216-
}
229+
}
230+
}
231+
232+
peerRestrictList := ctx.StringSlice("feature-peer-restrict-list")
233+
if len(peerRestrictList) > 0 && len(features) != len(peerRestrictList) {
234+
return fmt.Errorf("number of features (%v) and peer "+
235+
"restrict list (%v) must match", len(features),
236+
len(peerRestrictList))
217237
}
218238

219-
peerRestrictList := ctx.String("peer-restrict-list")
220-
if peerRestrictList != "" {
221-
peerIDs := strings.Split(peerRestrictList, ",")
239+
for i, peerRestrict := range peerRestrictList {
240+
peerIDs := strings.Split(peerRestrict, ",")
222241

223-
ruleMap.Rules[rules.PeersRestrictName] = &litrpc.RuleValue{
224-
Value: &litrpc.RuleValue_PeerRestrict{
225-
PeerRestrict: &litrpc.PeerRestrict{
226-
PeerIds: peerIDs,
242+
perFeatureRules[i].Rules[rules.PeersRestrictName] =
243+
&litrpc.RuleValue{
244+
Value: &litrpc.RuleValue_PeerRestrict{
245+
PeerRestrict: &litrpc.PeerRestrict{
246+
PeerIds: peerIDs,
247+
},
227248
},
228-
},
229-
}
249+
}
230250
}
231251

232-
features := ctx.StringSlice("feature")
233252
configs := ctx.StringSlice("feature-config")
234253
if len(configs) > 0 && len(features) != len(configs) {
235254
return fmt.Errorf("number of features (%v) and configurations "+
@@ -257,7 +276,7 @@ func initAutopilotSession(ctx *cli.Context) error {
257276
}
258277

259278
featureMap[feature] = &litrpc.FeatureConfig{
260-
Rules: ruleMap,
279+
Rules: perFeatureRules[i],
261280
Config: config,
262281
}
263282
}

0 commit comments

Comments
 (0)