Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -output flag in spire server federation commands #3660

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/spire-server/cli/federation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const (
}`
)

var availableFormats = []string{"pretty", "json"}

type cmdTest struct {
stdin *bytes.Buffer
stdout *bytes.Buffer
Expand Down Expand Up @@ -260,3 +262,16 @@ func createJSONDataFile(t *testing.T, data string) string {
require.NoError(t, os.WriteFile(jsonDataFilePath, []byte(data), 0600))
return jsonDataFilePath
}

func requireOutputBasedOnFormat(t *testing.T, format, stdoutString string, expectedStdoutPretty, expectedStdoutJSON string) {
switch format {
case "pretty":
require.Contains(t, stdoutString, expectedStdoutPretty)
case "json":
if expectedStdoutJSON != "" {
require.JSONEq(t, expectedStdoutJSON, stdoutString)
} else {
require.Empty(t, stdoutString)
}
}
}
33 changes: 24 additions & 9 deletions cmd/spire-server/cli/federation/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/mitchellh/cli"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
common_cli "github.com/spiffe/spire/pkg/common/cli"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"google.golang.org/grpc/codes"
)

Expand All @@ -20,16 +22,19 @@ const (

// NewCreateCommand creates a new "create" subcommand for "federation" command.
func NewCreateCommand() cli.Command {
return newCreateCommand(common_cli.DefaultEnv)
return newCreateCommand(commoncli.DefaultEnv)
}

func newCreateCommand(env *common_cli.Env) cli.Command {
return util.AdaptCommand(env, new(createCommand))
func newCreateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &createCommand{env: env})
}

type createCommand struct {
path string
config *federationRelationshipConfig
path string
config *federationRelationshipConfig
env *commoncli.Env
printer cliprinter.Printer
federationRelationships []*types.FederationRelationship
}

func (*createCommand) Name() string {
Expand All @@ -44,13 +49,15 @@ func (c *createCommand) AppendFlags(f *flag.FlagSet) {
f.StringVar(&c.path, "data", "", "Path to a file containing federation relationships in JSON format (optional). If set to '-', read the JSON from stdin.")
c.config = &federationRelationshipConfig{}
appendConfigFlags(c.config, f)
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, c.prettyPrintCreate)
}

func (c *createCommand) Run(ctx context.Context, env *common_cli.Env, serverClient util.ServerClient) error {
func (c *createCommand) Run(ctx context.Context, env *commoncli.Env, serverClient util.ServerClient) error {
federationRelationships, err := getRelationships(c.config, c.path)
if err != nil {
return err
}
c.federationRelationships = federationRelationships

client := serverClient.NewTrustDomainClient()

Expand All @@ -61,17 +68,25 @@ func (c *createCommand) Run(ctx context.Context, env *common_cli.Env, serverClie
return fmt.Errorf("request failed: %w", err)
}

return c.printer.PrintProto(resp)
}

func (c *createCommand) prettyPrintCreate(env *commoncli.Env, results ...interface{}) error {
createResp, ok := results[0].(*trustdomainv1.BatchCreateFederationRelationshipResponse)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible that results is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theoretically yes, but with the current implementation, we are constantly passing at least one proto message to the printer call (line 68), so currently there is no way to reproduce this scenario

if !ok || len(c.federationRelationships) < len(createResp.Results) {
return cliprinter.ErrInternalCustomPrettyFunc
}
// Process results
var succeeded []*trustdomainv1.BatchCreateFederationRelationshipResponse_Result
var failed []*trustdomainv1.BatchCreateFederationRelationshipResponse_Result
for i, r := range resp.Results {
for i, r := range createResp.Results {
switch r.Status.Code {
case int32(codes.OK):
succeeded = append(succeeded, r)
default:
// The trust domain API does not include in the results the relationships that
// failed to be created, so we populate them from the request data.
r.FederationRelationship = federationRelationships[i]
r.FederationRelationship = c.federationRelationships[i]
failed = append(failed, r)
}
}
Expand Down
Loading