Skip to content

Commit b498100

Browse files
committed
(#1268) Add mappings action to auth command
mappings has `add`, `rm`, `list` and `info` commands. Add can take a --config flag pointing at a valid jwt, which it will then parse and extract the mappings from it.
1 parent 9bc753c commit b498100

File tree

3 files changed

+427
-2
lines changed

3 files changed

+427
-2
lines changed

cli/auth_account_command.go

+245
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strconv"
2323
"time"
2424

25+
"github.com/nats-io/jwt/v2"
2526
au "github.com/nats-io/natscli/internal/auth"
2627
iu "github.com/nats-io/natscli/internal/util"
2728

@@ -102,6 +103,10 @@ type authAccountCommand struct {
102103
tags []string
103104
rmTags []string
104105
signingKey string
106+
mapSource string
107+
mapTarget string
108+
mapWeight uint
109+
inputFile string
105110
}
106111

107112
func configureAuthAccountCommand(auth commandHost) {
@@ -280,6 +285,30 @@ func configureAuthAccountCommand(auth commandHost) {
280285
skrm.Flag("key", "The key to remove").StringVar(&c.skRole)
281286
skrm.Flag("operator", "Operator to act on").StringVar(&c.operatorName)
282287
skrm.Flag("force", "Removes without prompting").Short('f').UnNegatableBoolVar(&c.force)
288+
289+
mappings := acct.Command("mappings", "Manage account level subject mapping and partitioning").Alias("m")
290+
291+
mappingsaAdd := mappings.Command("add", "Add a new mapping").Alias("new").Alias("a").Action(c.mappingAddAction)
292+
mappingsaAdd.Arg("account", "Account to create the mappings on").StringVar(&c.accountName)
293+
mappingsaAdd.Arg("source", "The source subject of the mapping").StringVar(&c.mapSource)
294+
mappingsaAdd.Arg("target", "The target subject of the mapping").StringVar(&c.mapTarget)
295+
mappingsaAdd.Arg("weight", "The weight (%) of the mappingmapping").Default("100").UintVar(&c.mapWeight)
296+
mappingsaAdd.Flag("operator", "Operator to act on").StringVar(&c.operatorName)
297+
mappingsaAdd.Flag("config", "JWT file to read configuration from").ExistingFileVar(&c.inputFile)
298+
299+
mappingsls := mappings.Command("ls", "List mappings").Alias("list").Action(c.mappingListAction)
300+
mappingsls.Arg("account", "Account to create the mappings on").StringVar(&c.accountName)
301+
mappingsls.Flag("operator", "Operator to act on").StringVar(&c.operatorName)
302+
303+
mappingsrm := mappings.Command("rm", "Remove a mapping").Action(c.mappingRmAction)
304+
mappingsrm.Arg("account", "Account to create the mappings on").StringVar(&c.accountName)
305+
mappingsrm.Arg("source", "The source subject of the mapping").StringVar(&c.mapSource)
306+
mappingsrm.Flag("operator", "Operator to act on").StringVar(&c.operatorName)
307+
308+
mappingsinfo := mappings.Command("info", "Show information about a mapping").Alias("i").Alias("show").Alias("view").Action(c.mappingInfoAction)
309+
mappingsinfo.Arg("account", "Account to create the mappings on").StringVar(&c.accountName)
310+
mappingsinfo.Arg("source", "The source subject of the mapping").StringVar(&c.mapSource)
311+
mappingsinfo.Flag("operator", "Operator to act on").StringVar(&c.operatorName)
283312
}
284313

285314
func (c *authAccountCommand) selectAccount(pick bool) (*ab.AuthImpl, ab.Operator, ab.Account, error) {
@@ -1101,3 +1130,219 @@ func (c *authAccountCommand) validTiers(acct ab.Account) []int8 {
11011130

11021131
return tiers
11031132
}
1133+
1134+
func (c *authAccountCommand) loadJwt() (*jwt.AccountClaims, error) {
1135+
if c.inputFile != "" {
1136+
f, err := os.ReadFile(c.inputFile)
1137+
if err != nil {
1138+
return nil, err
1139+
}
1140+
1141+
claims, err := jwt.DecodeAccountClaims(string(f))
1142+
if err != nil {
1143+
return nil, fmt.Errorf("failed to decode JWT: %w", err)
1144+
}
1145+
return claims, nil
1146+
}
1147+
return nil, nil
1148+
1149+
}
1150+
1151+
func (c *authAccountCommand) parseJwtMappings(mappings map[string][]ab.Mapping, jwtMappings jwt.Mapping) {
1152+
for subject, weightedMappings := range jwtMappings {
1153+
mappings[string(subject)] = []ab.Mapping{}
1154+
for _, m := range weightedMappings {
1155+
mappings[string(subject)] = append(mappings[string(subject)], ab.Mapping{Weight: m.Weight, Subject: string(m.Subject), Cluster: m.Cluster})
1156+
}
1157+
}
1158+
}
1159+
1160+
func (c *authAccountCommand) mappingAddAction(_ *fisk.ParseContext) error {
1161+
mappings := map[string][]ab.Mapping{}
1162+
if c.inputFile != "" {
1163+
cfg, err := c.loadJwt()
1164+
if err != nil {
1165+
return err
1166+
}
1167+
c.accountName = cfg.Name
1168+
c.parseJwtMappings(mappings, cfg.Mappings)
1169+
}
1170+
1171+
auth, _, acct, err := c.selectAccount(true)
1172+
if err != nil {
1173+
return err
1174+
}
1175+
1176+
if c.inputFile == "" {
1177+
if c.mapSource == "" {
1178+
err := iu.AskOne(&survey.Input{
1179+
Message: "Source subject",
1180+
Help: "The source subject of the mapping",
1181+
}, &c.mapSource, survey.WithValidator(survey.Required))
1182+
if err != nil {
1183+
return err
1184+
}
1185+
}
1186+
1187+
if c.mapTarget == "" {
1188+
err := iu.AskOne(&survey.Input{
1189+
Message: "Target subject",
1190+
Help: "The target subject of the mapping",
1191+
}, &c.mapTarget, survey.WithValidator(survey.Required))
1192+
if err != nil {
1193+
return err
1194+
}
1195+
}
1196+
1197+
mapping := ab.Mapping{Subject: c.mapTarget, Weight: uint8(c.mapWeight)}
1198+
// check if there are mappings already set for the source
1199+
currentMappings := acct.SubjectMappings().Get(c.mapSource)
1200+
if len(currentMappings) > 0 {
1201+
// Check that we don't overwrite the current mapping
1202+
for _, m := range currentMappings {
1203+
if m.Subject == c.mapTarget {
1204+
return fmt.Errorf("mapping %s -> %s already exists", c.mapSource, c.mapTarget)
1205+
}
1206+
}
1207+
}
1208+
currentMappings = append(currentMappings, mapping)
1209+
mappings[c.mapSource] = currentMappings
1210+
}
1211+
1212+
for subject, m := range mappings {
1213+
err = acct.SubjectMappings().Set(subject, m...)
1214+
if err != nil {
1215+
return err
1216+
}
1217+
}
1218+
1219+
err = auth.Commit()
1220+
if err != nil {
1221+
return err
1222+
}
1223+
1224+
return c.fShowMappings(os.Stdout, mappings)
1225+
}
1226+
1227+
func (c *authAccountCommand) mappingInfoAction(_ *fisk.ParseContext) error {
1228+
_, _, acct, err := c.selectAccount(true)
1229+
if err != nil {
1230+
return err
1231+
}
1232+
1233+
accountMappings := acct.SubjectMappings().List()
1234+
if len(accountMappings) == 0 {
1235+
fmt.Println("No mappings defined")
1236+
return nil
1237+
}
1238+
1239+
if c.mapSource == "" {
1240+
err = iu.AskOne(&survey.Select{
1241+
Message: "Select a mapping to inspect",
1242+
Options: accountMappings,
1243+
PageSize: iu.SelectPageSize(len(accountMappings)),
1244+
}, &c.mapSource)
1245+
if err != nil {
1246+
return err
1247+
}
1248+
}
1249+
1250+
mappings := map[string][]ab.Mapping{
1251+
c.mapSource: acct.SubjectMappings().Get(c.mapSource),
1252+
}
1253+
1254+
return c.fShowMappings(os.Stdout, mappings)
1255+
}
1256+
1257+
func (c *authAccountCommand) mappingListAction(_ *fisk.ParseContext) error {
1258+
_, _, acct, err := c.selectAccount(true)
1259+
if err != nil {
1260+
return err
1261+
}
1262+
1263+
mappings := acct.SubjectMappings().List()
1264+
if len(mappings) == 0 {
1265+
fmt.Println("No mappings defined")
1266+
return nil
1267+
}
1268+
1269+
tbl := iu.NewTableWriter(opts(), "Subject mappings for account %s", acct.Name())
1270+
tbl.AddHeaders("Source Subject", "Target Subject", "Weight", "Cluster")
1271+
1272+
for _, fromMapping := range acct.SubjectMappings().List() {
1273+
subjectMaps := acct.SubjectMappings().Get(fromMapping)
1274+
for _, m := range subjectMaps {
1275+
tbl.AddRow(fromMapping, m.Subject, m.Weight, m.Cluster)
1276+
}
1277+
}
1278+
1279+
fmt.Println(tbl.Render())
1280+
return nil
1281+
}
1282+
1283+
func (c *authAccountCommand) mappingRmAction(_ *fisk.ParseContext) error {
1284+
auth, _, acct, err := c.selectAccount(true)
1285+
if err != nil {
1286+
return err
1287+
}
1288+
1289+
mappings := acct.SubjectMappings().List()
1290+
if len(mappings) == 0 {
1291+
fmt.Println("No mappings defined")
1292+
return nil
1293+
}
1294+
1295+
if c.mapSource == "" {
1296+
err = iu.AskOne(&survey.Select{
1297+
Message: "Select a mapping to delete",
1298+
Options: mappings,
1299+
PageSize: iu.SelectPageSize(len(mappings)),
1300+
}, &c.mapSource)
1301+
if err != nil {
1302+
return err
1303+
}
1304+
}
1305+
1306+
err = acct.SubjectMappings().Delete(c.mapSource)
1307+
if err != nil {
1308+
return err
1309+
}
1310+
1311+
err = auth.Commit()
1312+
if err != nil {
1313+
return err
1314+
}
1315+
1316+
fmt.Printf("Deleted mapping {%s}\n", c.mapSource)
1317+
return nil
1318+
}
1319+
1320+
func (c *authAccountCommand) fShowMappings(w io.Writer, mappings map[string][]ab.Mapping) error {
1321+
out, err := c.showMappings(mappings)
1322+
if err != nil {
1323+
return err
1324+
}
1325+
1326+
_, err = fmt.Fprintln(w, out)
1327+
return err
1328+
}
1329+
1330+
func (c *authAccountCommand) showMappings(mappings map[string][]ab.Mapping) (string, error) {
1331+
cols := newColumns("Subject mappings")
1332+
cols.AddSectionTitle("Configuration")
1333+
for source, m := range mappings {
1334+
totalWeight := 0
1335+
for _, wm := range m {
1336+
cols.AddRow("Source", source)
1337+
cols.AddRow("Target", wm.Subject)
1338+
cols.AddRow("Weight", wm.Weight)
1339+
cols.AddRow("Cluster", wm.Cluster)
1340+
cols.AddRow("", "")
1341+
totalWeight += int(wm.Weight)
1342+
}
1343+
cols.AddRow("Total weight:", totalWeight)
1344+
cols.AddRow("", "")
1345+
}
1346+
1347+
return cols.Render()
1348+
}

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
152152
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
153153
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
154154
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
155-
github.com/synadia-io/jwt-auth-builder.go v0.0.6 h1:F3bTGWlKzWHwRqtTt35fRmhrxXLgkI8qz8QvvzxKSko=
156-
github.com/synadia-io/jwt-auth-builder.go v0.0.6/go.mod h1:8WYR7+nLQcDMBpocuPgdFJ5/2UOr+HPll3qv+KNdGvs=
157155
github.com/synadia-io/jwt-auth-builder.go v0.0.7-0.20250307212657-0e3f1ee00864 h1:itO+DjIffRn+nN3jHxHNcCiJIsL1BMZF7p3wYeTN7xs=
158156
github.com/synadia-io/jwt-auth-builder.go v0.0.7-0.20250307212657-0e3f1ee00864/go.mod h1:8WYR7+nLQcDMBpocuPgdFJ5/2UOr+HPll3qv+KNdGvs=
159157
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f h1:SGznmvCovewbaSgBsHgdThtWsLj5aCLX/3ZXMLd1UD0=

0 commit comments

Comments
 (0)