github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/deletemapping.go (about) 1 /* 2 * Copyright 2021 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 21 "github.com/nats-io/jwt/v2" 22 "github.com/nats-io/nkeys" 23 "github.com/nats-io/nsc/v2/cmd/store" 24 "github.com/spf13/cobra" 25 ) 26 27 func createDeleteMappingCmd() *cobra.Command { 28 var params DeleteMappingParams 29 cmd := &cobra.Command{ 30 Use: "mapping", 31 Short: "Delete a mapping", 32 Args: MaxArgs(0), 33 SilenceUsage: true, 34 RunE: func(cmd *cobra.Command, args []string) error { 35 return RunAction(cmd, args, ¶ms) 36 }, 37 } 38 cmd.Flags().StringVarP((*string)(¶ms.from), "from", "f", "", "map from subject (required)") 39 cmd.Flags().StringVarP((*string)(¶ms.to), "to", "t", "", "to subject. When present, only that particular mapping is removed. Otherwise all mappings for from subject are.") 40 params.AccountContextParams.BindFlags(cmd) 41 return cmd 42 } 43 44 func init() { 45 deleteCmd.AddCommand(createDeleteMappingCmd()) 46 } 47 48 type DeleteMappingParams struct { 49 AccountContextParams 50 SignerParams 51 claim *jwt.AccountClaims 52 from jwt.Subject 53 to jwt.Subject 54 } 55 56 func (p *DeleteMappingParams) SetDefaults(ctx ActionCtx) error { 57 p.AccountContextParams.SetDefaults(ctx) 58 p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx) 59 return nil 60 } 61 62 func (p *DeleteMappingParams) PreInteractive(ctx ActionCtx) error { 63 var err error 64 if err = p.AccountContextParams.Edit(ctx); err != nil { 65 return err 66 } 67 return nil 68 } 69 70 func (p *DeleteMappingParams) Load(ctx ActionCtx) error { 71 var err error 72 73 if err = p.AccountContextParams.Validate(ctx); err != nil { 74 return err 75 } 76 77 p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name) 78 if err != nil { 79 return err 80 } 81 82 return nil 83 } 84 85 func (p *DeleteMappingParams) PostInteractive(ctx ActionCtx) error { 86 return nil 87 } 88 89 func (p *DeleteMappingParams) Validate(ctx ActionCtx) error { 90 if p.from == "" { 91 return fmt.Errorf("from subject is required") 92 } 93 if err := p.SignerParams.Resolve(ctx); err != nil { 94 return err 95 } 96 return nil 97 } 98 99 func (p *DeleteMappingParams) Run(ctx ActionCtx) (store.Status, error) { 100 r := store.NewDetailedReport(true) 101 if p.to != "" { 102 list := p.claim.Mappings[p.from] 103 for i, l := range list { 104 if l.Subject == p.to { 105 if i == 0 { 106 if len(list) == 1 { 107 delete(p.claim.Mappings, p.from) 108 } else { 109 p.claim.Mappings[p.from] = list[1:] 110 } 111 } else { 112 p.claim.Mappings[p.from] = append(list[0:i], list[i+i:]...) 113 } 114 break 115 } 116 } 117 } 118 if p.to == "" || len(p.claim.Mappings[p.from]) == 0 { 119 delete(p.claim.Mappings, p.from) 120 } 121 if len(p.claim.Mappings) == 0 { 122 p.claim.Mappings = nil 123 } 124 token, err := p.claim.Encode(p.signerKP) 125 if err != nil { 126 return nil, err 127 } 128 if p.to == "" { 129 r.AddOK("deleted all mapping for %s", p.from) 130 } else { 131 r.AddOK("deleted mapping %s -> %s", p.from, p.to) 132 } 133 rs, err := ctx.StoreCtx().Store.StoreClaim([]byte(token)) 134 if rs != nil { 135 r.Add(rs) 136 } 137 if err != nil { 138 r.AddFromError(err) 139 } 140 return r, nil 141 }