Skip to content

Commit

Permalink
litcli: per feature channel and peer restrictions
Browse files Browse the repository at this point in the history
Different features interprete the restrictions differently, which is why
we add the possibility of specifying restrictions per feature.
  • Loading branch information
bitromortac committed Aug 29, 2023
1 parent e9cfa4b commit ebac616
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions cmd/litcli/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ var addAutopilotSessionCmd = cli.Command{
Description: `
Initialize an Autopilot session.
If set for any feature, configuration flags need to be repeated for each
feature that is registered, corresponding to the order of features.`,
If set for any feature, configuration flags and channel and peer
restrict lists need to be repeated for each feature that is registered,
corresponding to the order of features.`,
Action: initAutopilotSession,
Flags: []cli.Flag{
labelFlag,
Expand All @@ -55,15 +56,15 @@ var addAutopilotSessionCmd = cli.Command{
Name: "feature",
Required: true,
},
cli.StringFlag{
Name: "channel-restrict-list",
cli.StringSliceFlag{
Name: "feature-channel-restrict-list",
Usage: "list of channel IDs that the " +
"Autopilot server should not " +
"perform actions on. In the " +
"form of: chanID1,chanID2,...",
},
cli.StringFlag{
Name: "peer-restrict-list",
cli.StringSliceFlag{
Name: "feature-peer-restrict-list",
Usage: "list of peer IDs that the " +
"Autopilot server should not " +
"perform actions on. In the " +
Expand Down Expand Up @@ -191,45 +192,63 @@ func initAutopilotSession(ctx *cli.Context) error {
defer cleanup()
client := litrpc.NewAutopilotClient(clientConn)

ruleMap := &litrpc.RulesMap{
Rules: make(map[string]*litrpc.RuleValue),
features := ctx.StringSlice("feature")

perFeatureRules := make([]*litrpc.RulesMap, len(features))
for i := range features {
perFeatureRules[i] = &litrpc.RulesMap{
Rules: make(map[string]*litrpc.RuleValue),
}
}

chanRestrictList := ctx.StringSlice("feature-channel-restrict-list")
if len(chanRestrictList) > 0 && len(features) != len(chanRestrictList) {
return fmt.Errorf("number of features (%v) and channel "+
"restrict list (%v) must match", len(features),
len(chanRestrictList))
}

chanRestrictList := ctx.String("channel-restrict-list")
if chanRestrictList != "" {
for i, chanRestrict := range chanRestrictList {
var chanIDs []uint64
chans := strings.Split(chanRestrictList, ",")
chans := strings.Split(chanRestrict, ",")
for _, c := range chans {
i, err := strconv.ParseUint(c, 10, 64)
cid, err := strconv.ParseUint(c, 10, 64)
if err != nil {
return err
}
chanIDs = append(chanIDs, i)
chanIDs = append(chanIDs, cid)
}

ruleMap.Rules[rules.ChannelRestrictName] = &litrpc.RuleValue{
Value: &litrpc.RuleValue_ChannelRestrict{
ChannelRestrict: &litrpc.ChannelRestrict{
ChannelIds: chanIDs,
perFeatureRules[i].Rules[rules.ChannelRestrictName] =
&litrpc.RuleValue{
Value: &litrpc.RuleValue_ChannelRestrict{
ChannelRestrict: &litrpc.ChannelRestrict{
ChannelIds: chanIDs,
},
},
},
}
}
}

peerRestrictList := ctx.StringSlice("feature-peer-restrict-list")
if len(peerRestrictList) > 0 && len(features) != len(peerRestrictList) {
return fmt.Errorf("number of features (%v) and peer "+
"restrict list (%v) must match", len(features),
len(peerRestrictList))
}

peerRestrictList := ctx.String("peer-restrict-list")
if peerRestrictList != "" {
peerIDs := strings.Split(peerRestrictList, ",")
for i, peerRestrict := range peerRestrictList {
peerIDs := strings.Split(peerRestrict, ",")

ruleMap.Rules[rules.PeersRestrictName] = &litrpc.RuleValue{
Value: &litrpc.RuleValue_PeerRestrict{
PeerRestrict: &litrpc.PeerRestrict{
PeerIds: peerIDs,
perFeatureRules[i].Rules[rules.PeersRestrictName] =
&litrpc.RuleValue{
Value: &litrpc.RuleValue_PeerRestrict{
PeerRestrict: &litrpc.PeerRestrict{
PeerIds: peerIDs,
},
},
},
}
}
}

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

featureMap[feature] = &litrpc.FeatureConfig{
Rules: ruleMap,
Rules: perFeatureRules[i],
Config: config,
}
}
Expand Down

0 comments on commit ebac616

Please sign in to comment.