gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/identity.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package command 18 19 import ( 20 "fmt" 21 22 "gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/api" 23 calog "gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/log" 24 "gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/util" 25 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib" 26 log "gitee.com/zhaochuninhefei/zcgolog/zclog" 27 "github.com/pkg/errors" 28 "github.com/spf13/cobra" 29 ) 30 31 type identityArgs struct { 32 id string 33 json string 34 add api.AddIdentityRequest 35 modify api.ModifyIdentityRequest 36 remove api.RemoveIdentityRequest 37 } 38 39 func (c *ClientCmd) newIdentityCommand() *cobra.Command { 40 identityCmd := &cobra.Command{ 41 Use: "identity", 42 Short: "Manage identities", 43 Long: "Manage identities", 44 } 45 identityCmd.AddCommand(c.newListIdentityCommand()) 46 identityCmd.AddCommand(c.newAddIdentityCommand()) 47 identityCmd.AddCommand(c.newModifyIdentityCommand()) 48 identityCmd.AddCommand(c.newRemoveIdentityCommand()) 49 return identityCmd 50 } 51 52 func (c *ClientCmd) newListIdentityCommand() *cobra.Command { 53 identityListCmd := &cobra.Command{ 54 Use: "list", 55 Short: "List identities", 56 Long: "List identities visible to caller", 57 PreRunE: func(cmd *cobra.Command, args []string) error { 58 c.SetDefaultLogLevel(calog.WARNING) 59 err := c.ConfigInit() 60 if err != nil { 61 return err 62 } 63 64 log.Debugf("Client configuration settings: %+v", c.clientCfg) 65 66 return nil 67 }, 68 RunE: c.runListIdentity, 69 } 70 flags := identityListCmd.Flags() 71 flags.StringVarP( 72 &c.dynamicIdentity.id, "id", "", "", "Get identity information from the fabric-ca server") 73 return identityListCmd 74 } 75 76 func (c *ClientCmd) newAddIdentityCommand() *cobra.Command { 77 identityAddCmd := &cobra.Command{ 78 Use: "add <id>", 79 Short: "Add identity", 80 Long: "Add an identity", 81 Example: "fabric-ca-client identity add user1 --type peer", 82 PreRunE: c.identityPreRunE, 83 RunE: c.runAddIdentity, 84 } 85 flags := identityAddCmd.Flags() 86 util.RegisterFlags(c.myViper, flags, &c.dynamicIdentity.add, nil) 87 flags.StringSliceVarP( 88 &c.cfgAttrs, "attrs", "", nil, "A list of comma-separated attributes of the form <name>=<value> (e.g. foo=foo1,bar=bar1)") 89 flags.StringVarP( 90 &c.dynamicIdentity.json, "json", "", "", "JSON string for adding a new identity") 91 return identityAddCmd 92 } 93 94 func (c *ClientCmd) newModifyIdentityCommand() *cobra.Command { 95 identityModifyCmd := &cobra.Command{ 96 Use: "modify <id>", 97 Short: "Modify identity", 98 Long: "Modify an existing identity", 99 Example: "fabric-ca-client identity modify user1 --type peer", 100 PreRunE: c.identityPreRunE, 101 RunE: c.runModifyIdentity, 102 } 103 flags := identityModifyCmd.Flags() 104 tags := map[string]string{ 105 "skip.id": "true", 106 } 107 util.RegisterFlags(c.myViper, flags, &c.dynamicIdentity.modify, tags) 108 flags.StringSliceVarP( 109 &c.cfgAttrs, "attrs", "", nil, "A list of comma-separated attributes of the form <name>=<value> (e.g. foo=foo1,bar=bar1)") 110 flags.StringVarP( 111 &c.dynamicIdentity.json, "json", "", "", "JSON string for modifying an existing identity") 112 return identityModifyCmd 113 } 114 115 func (c *ClientCmd) newRemoveIdentityCommand() *cobra.Command { 116 identityRemoveCmd := &cobra.Command{ 117 Use: "remove <id>", 118 Short: "Remove identity", 119 Long: "Remove an identity", 120 Example: "fabric-ca-client identity remove user1", 121 PreRunE: c.identityPreRunE, 122 RunE: c.runRemoveIdentity, 123 } 124 flags := identityRemoveCmd.Flags() 125 flags.BoolVarP( 126 &c.dynamicIdentity.remove.Force, "force", "", false, "Forces removing your own identity") 127 return identityRemoveCmd 128 } 129 130 // The client side logic for executing list identity command 131 func (c *ClientCmd) runListIdentity(cmd *cobra.Command, args []string) error { 132 log.Debug("Entered runListIdentity") 133 134 id, err := c.LoadMyIdentity() 135 if err != nil { 136 return err 137 } 138 139 if c.dynamicIdentity.id != "" { 140 resp, err := id.GetIdentity(c.dynamicIdentity.id, c.clientCfg.CAName) 141 if err != nil { 142 return err 143 } 144 145 fmt.Printf("Name: %s, Type: %s, Affiliation: %s, Max Enrollments: %d, Attributes: %+v\n", resp.ID, resp.Type, resp.Affiliation, resp.MaxEnrollments, resp.Attributes) 146 return nil 147 } 148 149 err = id.GetAllIdentities(c.clientCfg.CAName, lib.IdentityDecoder) 150 if err != nil { 151 return err 152 } 153 154 return nil 155 } 156 157 // The client side logic for adding an identity 158 func (c *ClientCmd) runAddIdentity(cmd *cobra.Command, args []string) error { 159 log.Debugf("Entered runAddIdentity: %+v", c.dynamicIdentity) 160 if c.dynamicIdentity.json != "" && checkOtherFlags(cmd) { 161 return errors.Errorf("Can't use 'json' flag in conjunction with other flags") 162 } 163 164 id, err := c.LoadMyIdentity() 165 if err != nil { 166 return err 167 } 168 169 req := &api.AddIdentityRequest{} 170 171 if c.dynamicIdentity.json != "" { 172 err := util.Unmarshal([]byte(c.dynamicIdentity.json), &req, "addIdentity") 173 if err != nil { 174 return errors.Wrap(err, "Invalid value for --json option") 175 } 176 } else { 177 req = &c.dynamicIdentity.add 178 req.Attributes = c.clientCfg.ID.Attributes 179 } 180 181 req.ID = args[0] 182 req.CAName = c.clientCfg.CAName 183 resp, err := id.AddIdentity(req) 184 if err != nil { 185 return err 186 } 187 188 fmt.Printf("Successfully added identity - Name: %s, Type: %s, Affiliation: %s, Max Enrollments: %d, Secret: %s, Attributes: %+v\n", resp.ID, resp.Type, resp.Affiliation, resp.MaxEnrollments, resp.Secret, resp.Attributes) 189 return nil 190 } 191 192 // The client side logic for modifying an identity 193 func (c *ClientCmd) runModifyIdentity(cmd *cobra.Command, args []string) error { 194 log.Debugf("Entered runModifyIdentity: %+v", c.dynamicIdentity) 195 if c.dynamicIdentity.json != "" && checkOtherFlags(cmd) { 196 return errors.Errorf("Can't use 'json' flag in conjunction with other flags") 197 } 198 199 req := &api.ModifyIdentityRequest{} 200 201 id, err := c.LoadMyIdentity() 202 if err != nil { 203 return err 204 } 205 206 if c.dynamicIdentity.json != "" { 207 err := util.Unmarshal([]byte(c.dynamicIdentity.json), req, "modifyIdentity") 208 if err != nil { 209 return errors.Wrap(err, "Invalid value for --json option") 210 } 211 } else { 212 req = &c.dynamicIdentity.modify 213 req.Attributes = c.clientCfg.ID.Attributes 214 } 215 216 req.ID = args[0] 217 req.CAName = c.clientCfg.CAName 218 resp, err := id.ModifyIdentity(req) 219 if err != nil { 220 return err 221 } 222 223 fmt.Printf("Successfully modified identity - Name: %s, Type: %s, Affiliation: %s, Max Enrollments: %d, Secret: %s, Attributes: %+v\n", resp.ID, resp.Type, resp.Affiliation, resp.MaxEnrollments, resp.Secret, resp.Attributes) 224 return nil 225 } 226 227 // The client side logic for removing an identity 228 func (c *ClientCmd) runRemoveIdentity(cmd *cobra.Command, args []string) error { 229 log.Debugf("Entered runRemoveIdentity: %+v", c.dynamicIdentity) 230 231 id, err := c.LoadMyIdentity() 232 if err != nil { 233 return err 234 } 235 236 req := &c.dynamicIdentity.remove 237 req.ID = args[0] 238 req.CAName = c.clientCfg.CAName 239 resp, err := id.RemoveIdentity(req) 240 if err != nil { 241 return err 242 } 243 244 fmt.Printf("Successfully removed identity - Name: %s, Type: %s, Affiliation: %s, Max Enrollments: %d, Attributes: %+v\n", resp.ID, resp.Type, resp.Affiliation, resp.MaxEnrollments, resp.Attributes) 245 return nil 246 } 247 248 func (c *ClientCmd) identityPreRunE(cmd *cobra.Command, args []string) error { 249 err := argsCheck(args, "Identity") 250 if err != nil { 251 return err 252 } 253 254 err = c.ConfigInit() 255 if err != nil { 256 return err 257 } 258 259 log.Debugf("Client configuration settings: %+v", c.clientCfg) 260 261 return nil 262 } 263 264 // checkOtherFlags returns true if other flags besides '--json' are set 265 // Viper.IsSet does not work correctly if there are defaults defined for 266 // flags. This is a workaround until this bug is addressed in Viper. 267 // Viper Bug: https://github.com/spf13/viper/issues/276 268 func checkOtherFlags(cmd *cobra.Command) bool { 269 checkFlags := []string{"id", "type", "affiliation", "secret", "maxenrollments", "attrs"} 270 flags := cmd.Flags() 271 for _, checkFlag := range checkFlags { 272 flag := flags.Lookup(checkFlag) 273 if flag != nil { 274 if flag.Changed { 275 return true 276 } 277 } 278 } 279 280 return false 281 } 282 283 func argsCheck(args []string, field string) error { 284 if len(args) == 0 { 285 return errors.Errorf("%s name is required", field) 286 } 287 if len(args) > 1 { 288 return errors.Errorf("Unknown argument '%s', only the identity name should be passed in as non-flag argument", args[1]) 289 } 290 return nil 291 }