-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
localauthority jwt show
command to the server (#5412)
* Add `localauthority jwt show command` to the server Signed-off-by: Agustín Martínez Fayó <[email protected]>
- Loading branch information
1 parent
399ce63
commit 4f34e43
Showing
10 changed files
with
326 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package jwt | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"time" | ||
|
||
"github.com/mitchellh/cli" | ||
localauthorityv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/localauthority/v1" | ||
"github.com/spiffe/spire/cmd/spire-server/util" | ||
commoncli "github.com/spiffe/spire/pkg/common/cli" | ||
"github.com/spiffe/spire/pkg/common/cliprinter" | ||
) | ||
|
||
// NewJWTShowCommand creates a new "jwt show" subcommand for "localauthority" command. | ||
func NewJWTShowCommand() cli.Command { | ||
return NewJWTShowCommandWithEnv(commoncli.DefaultEnv) | ||
} | ||
|
||
// NewJWTShowCommandWithEnv creates a new "jwt show" subcommand for "localauthority" command | ||
// using the environment specified | ||
func NewJWTShowCommandWithEnv(env *commoncli.Env) cli.Command { | ||
return util.AdaptCommand(env, &jwtShowCommand{env: env}) | ||
} | ||
|
||
type jwtShowCommand struct { | ||
printer cliprinter.Printer | ||
|
||
env *commoncli.Env | ||
} | ||
|
||
func (c *jwtShowCommand) Name() string { | ||
return "localauthority jwt show" | ||
} | ||
|
||
func (*jwtShowCommand) Synopsis() string { | ||
return "Shows the local JWT authorities" | ||
} | ||
|
||
func (c *jwtShowCommand) AppendFlags(f *flag.FlagSet) { | ||
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintJWTShow) | ||
} | ||
|
||
// Run executes all logic associated with a single invocation of the | ||
// `spire-server localauthority jwt show` CLI command | ||
func (c *jwtShowCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error { | ||
client := serverClient.NewLocalAuthorityClient() | ||
resp, err := client.GetJWTAuthorityState(ctx, &localauthorityv1.GetJWTAuthorityStateRequest{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.printer.PrintProto(resp) | ||
} | ||
|
||
func prettyPrintJWTShow(env *commoncli.Env, results ...any) error { | ||
r, ok := results[0].(*localauthorityv1.GetJWTAuthorityStateResponse) | ||
if !ok { | ||
return errors.New("internal error: cli printer; please report this bug") | ||
} | ||
|
||
env.Println("Active JWT authority:") | ||
if r.Active != nil { | ||
env.Printf(" Authority ID: %s\n", r.Active.AuthorityId) | ||
env.Printf(" Expires at: %s\n", time.Unix(r.Active.ExpiresAt, 0).UTC()) | ||
} else { | ||
env.Println(" No active JWT authority found") | ||
} | ||
env.Println() | ||
env.Println("Prepared JWT authority:") | ||
if r.Prepared != nil { | ||
env.Printf(" Authority ID: %s\n", r.Prepared.AuthorityId) | ||
env.Printf(" Expires at: %s\n", time.Unix(r.Prepared.ExpiresAt, 0).UTC()) | ||
} else { | ||
env.Println(" No prepared JWT authority found") | ||
} | ||
env.Println() | ||
env.Println("Old JWT authority:") | ||
if r.Old != nil { | ||
env.Printf(" Authority ID: %s\n", r.Old.AuthorityId) | ||
env.Printf(" Expires at: %s\n", time.Unix(r.Old.ExpiresAt, 0).UTC()) | ||
} else { | ||
env.Println(" No old JWT authority found") | ||
} | ||
return nil | ||
} |
12 changes: 12 additions & 0 deletions
12
cmd/spire-server/cli/localauthority/jwt/jwt_show_posix_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !windows | ||
|
||
package jwt_test | ||
|
||
var ( | ||
jwtShowUsage = `Usage of localauthority jwt show: | ||
-output value | ||
Desired output format (pretty, json); default: pretty. | ||
-socketPath string | ||
Path to the SPIRE Server API socket (default "/tmp/spire-server/private/api.sock") | ||
` | ||
) |
Oops, something went wrong.