Skip to content

Commit

Permalink
[FAB-5726] 6. Dynamic Cfg - Aff: CLI
Browse files Browse the repository at this point in the history
This changeset lays implements the client side command
'affiliation' that will be used to either get information
on a specific affiliation, get all affiliations that the
caller is authorized to view, or add/modify/remove
affiliations for which the caller is authorized.

Next changeset will construct the handler on the server side
that will be used to handle affiliation related requests.

Change-Id: I0e390623ebe96478dd4221d46d1c8ca0d356275a
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Jan 4, 2018
1 parent 332d940 commit bc33398
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 6 deletions.
49 changes: 49 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,55 @@ type IdentityInfo struct {
MaxEnrollments int `mapstructure:"max_enrollments" json:"max_enrollments" def:"-1" help:"The maximum number of times the secret can be reused to enroll."`
}

// GetAllAffiliationsResponse is the response from the GetAllAffiliations call
type GetAllAffiliationsResponse struct {
Affiliations []AffiliationInfo `json:"affiliations"`
CAName string `json:"caname,omitempty"`
}

// AddAffiliationRequest represents the request to add a new affiliation to the
// fabric-ca-server
type AddAffiliationRequest struct {
Info AffiliationInfo `json:"info"`
Force bool `json:"force"`
CAName string `json:"caname,omitempty"`
}

// ModifyAffiliationRequest represents the request to modify an existing affiliation on the
// fabric-ca-server
type ModifyAffiliationRequest struct {
Name string `json:"name"`
Info AffiliationInfo `json:"info"`
Force bool `json:"force"`
CAName string `json:"caname,omitempty"`
}

// RemoveAffiliationRequest represents the request to remove an existing affiliation from the
// fabric-ca-server
type RemoveAffiliationRequest struct {
Name string `json:"name"`
Force bool `json:"force"`
CAName string `json:"caname,omitempty"`
}

// RemoveAffiliationResponse contains the response from removing an affiliation request
type RemoveAffiliationResponse struct {
Affiliations []AffiliationInfo `json:"affiliations"`
Identities []IdentityInfo `json:"identities"`
CAName string `json:"caname,omitempty"`
}

// AffiliationResponse is the response from the any add/modify/remove affiliation call
type AffiliationResponse struct {
Info AffiliationInfo `json:"info"`
CAName string `json:"caname,omitempty"`
}

// AffiliationInfo contains information about the affiliation
type AffiliationInfo struct {
Name string `json:"name"`
}

// CSRInfo is Certificate Signing Request (CSR) Information
type CSRInfo struct {
CN string `json:"CN"`
Expand Down
11 changes: 8 additions & 3 deletions api/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ type ModifyIdentityRequestNet struct {
ModifyIdentityRequest
}

// RemoveIdentityRequestNet is a network request for removing an existing identity
type RemoveIdentityRequestNet struct {
RemoveIdentityRequest
// AddAffiliationRequestNet is a network request for adding a new affiliation
type AddAffiliationRequestNet struct {
Info AffiliationInfo `json:"info"`
}

// ModifyAffiliationRequestNet is a network request for modifying an existing affiliation
type ModifyAffiliationRequestNet struct {
Info AffiliationInfo `json:"info"`
}

// KeySig is a public key, signature, and signature algorithm tuple
Expand Down
231 changes: 231 additions & 0 deletions cmd/fabric-ca-client/affiliation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"

"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/spf13/cobra"
)

type affiliationArgs struct {
affiliation string
add api.AddAffiliationRequest
modify api.ModifyAffiliationRequest
remove api.RemoveAffiliationRequest
}

func (c *ClientCmd) newAffiliationCommand() *cobra.Command {
affiliationCmd := &cobra.Command{
Use: "affiliation",
Short: "Manage affiliations",
Long: "Manage affiliations",
}
affiliationCmd.AddCommand(c.newListAffiliationCommand())
affiliationCmd.AddCommand(c.newAddAffiliationCommand())
affiliationCmd.AddCommand(c.newModifyAffiliationCommand())
affiliationCmd.AddCommand(c.newRemoveAffiliationCommand())
return affiliationCmd
}

func (c *ClientCmd) newListAffiliationCommand() *cobra.Command {
affiliationListCmd := &cobra.Command{
Use: "list",
Short: "List affiliations",
Long: "List affiliations visible to caller",
PreRunE: func(cmd *cobra.Command, args []string) error {
err := c.configInit()
if err != nil {
return err
}

log.Debugf("Client configuration settings: %+v", c.clientCfg)

return nil
},
RunE: c.runListAffiliation,
}
flags := affiliationListCmd.Flags()
flags.StringVarP(
&c.dynamicAffiliation.affiliation, "affiliation", "", "", "Get affiliation information from the fabric-ca server")
return affiliationListCmd
}

func (c *ClientCmd) newAddAffiliationCommand() *cobra.Command {
affiliationAddCmd := &cobra.Command{
Use: "add <affiliation>",
Short: "Add affiliation",
Long: "Add affiliation",
PreRunE: c.affiliationPreRunE,
RunE: c.runAddAffiliation,
}
flags := affiliationAddCmd.Flags()
flags.BoolVarP(
&c.dynamicAffiliation.add.Force, "force", "", false, "Creates parent affiliations if they do not exist")
return affiliationAddCmd
}

func (c *ClientCmd) newModifyAffiliationCommand() *cobra.Command {
affiliationModifyCmd := &cobra.Command{
Use: "modify <affiliation>",
Short: "Modify affiliation",
Long: "Modify existing affiliation",
PreRunE: c.affiliationPreRunE,
RunE: c.runModifyAffiliation,
}
flags := affiliationModifyCmd.Flags()
flags.StringVarP(
&c.dynamicAffiliation.modify.Info.Name, "name", "", "", "Rename the affiliation")
flags.BoolVarP(
&c.dynamicAffiliation.modify.Force, "force", "", false, "Forces identities using old affiliation to use new affiliation")
return affiliationModifyCmd
}

func (c *ClientCmd) newRemoveAffiliationCommand() *cobra.Command {
affiliationRemoveCmd := &cobra.Command{
Use: "remove <affiliation>",
Short: "Remove affiliation",
Long: "Remove affiliation",
PreRunE: c.affiliationPreRunE,
RunE: c.runRemoveAffiliation,
}
flags := affiliationRemoveCmd.Flags()
flags.BoolVarP(
&c.dynamicAffiliation.remove.Force, "force", "", false, "Forces removal of any child affiliations and any identities associated with removed affiliations")
return affiliationRemoveCmd
}

// The client side logic for listing affiliation information
func (c *ClientCmd) runListAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runListAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
if err != nil {
return err
}

if c.dynamicAffiliation.affiliation != "" {
resp, err := id.GetAffiliation(c.dynamicAffiliation.affiliation, c.clientCfg.CAName)
if err != nil {
return err
}

fmt.Printf("%+v\n", resp.Info)
return nil
}

resp, err := id.GetAllAffiliations(c.clientCfg.CAName)
if err != nil {
return err
}

for _, aff := range resp.Affiliations {
fmt.Printf("%+v\n", aff)
}

return nil
}

// The client side logic for adding an affiliation
func (c *ClientCmd) runAddAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runAddAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
if err != nil {
return err
}

req := &api.AddAffiliationRequest{}
req.Info.Name = args[0]
req.CAName = c.clientCfg.CAName
req.Force = c.dynamicAffiliation.add.Force

resp, err := id.AddAffiliation(req)
if err != nil {
return err
}

fmt.Printf("Successfully added affiliation: %+v\n", resp)

return nil
}

// The client side logic for modifying an affiliation
func (c *ClientCmd) runModifyAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runModifyAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
if err != nil {
return err
}

req := &api.ModifyAffiliationRequest{}
req.Name = args[0]
req.CAName = c.clientCfg.CAName
req.Force = c.dynamicAffiliation.modify.Force

resp, err := id.ModifyAffiliation(req)
if err != nil {
return err
}

fmt.Printf("Successfully modified affiliation: %+v\n", resp)

return nil
}

// The client side logic for removing an affiliation
func (c *ClientCmd) runRemoveAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runRemoveAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
if err != nil {
return err
}

req := &api.RemoveAffiliationRequest{}
req.Name = args[0]
req.CAName = c.clientCfg.CAName
req.Force = c.dynamicAffiliation.remove.Force

resp, err := id.RemoveAffiliation(req)
if err != nil {
return err
}

fmt.Printf("Successfully modified affiliation: %+v\n", resp)

return nil
}

func (c *ClientCmd) affiliationPreRunE(cmd *cobra.Command, args []string) error {
err := argsCheck(args, "affiliation")
if err != nil {
return err
}

err = c.configInit()
if err != nil {
return err
}

log.Debugf("Client configuration settings: %+v", c.clientCfg)

return nil
}
5 changes: 4 additions & 1 deletion cmd/fabric-ca-client/clientcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type ClientCmd struct {
}
// Dynamically configuring identities
dynamicIdentity identityArgs
// Dynamically configuring affiliations
dynamicAffiliation affiliationArgs
}

// NewCommand returns new ClientCmd ready for running
Expand Down Expand Up @@ -148,7 +150,8 @@ func (c *ClientCmd) init() {
c.newGetCACertCommand(),
c.newGenCsrCommand(),
c.newGenCRLCommand(),
c.newIdentityCommand())
c.newIdentityCommand(),
c.newAffiliationCommand())
c.rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Prints Fabric CA Client version",
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *ClientCmd) runListIdentity(cmd *cobra.Command, args []string) error {
return err
}

fmt.Printf("Identity: %+v\n", resp.IdentityInfo)
fmt.Printf("%+v\n", resp.IdentityInfo)
return nil
}

Expand Down
Loading

0 comments on commit bc33398

Please sign in to comment.