Skip to content

Commit

Permalink
Add a version RPC and CLI arg for returning the embedded one. (#108)
Browse files Browse the repository at this point in the history
* Add a version RPC and CLI arg for returning the embedded one.

Expose in CLI/proxy-server as it's own method (similar to logging).

Integrate both methods into integration test

* Add license block
  • Loading branch information
sfc-gh-jchacon authored May 5, 2022
1 parent c570010 commit c271eaa
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 29 deletions.
12 changes: 11 additions & 1 deletion cmd/proxy-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
_ "github.com/Snowflake-Labs/sansshell/services/localfile"
_ "github.com/Snowflake-Labs/sansshell/services/packages"
_ "github.com/Snowflake-Labs/sansshell/services/process"
_ "github.com/Snowflake-Labs/sansshell/services/sansshell/server"
ssserver "github.com/Snowflake-Labs/sansshell/services/sansshell/server"
_ "github.com/Snowflake-Labs/sansshell/services/service"
)

Expand All @@ -58,11 +58,21 @@ var (
verbosity = flag.Int("v", 0, "Verbosity level. > 0 indicates more extensive logging")
validate = flag.Bool("validate", false, "If true will evaluate the policy and then exit (non-zero on error)")
justification = flag.Bool("justification", false, "If true then justification (which is logged and possibly validated) must be passed along in the client context Metadata with the key '"+rpcauth.ReqJustKey+"'")
version bool
)

func init() {
flag.BoolVar(&version, "version", false, "Returns the server built version from the sansshell server package")
}

func main() {
flag.Parse()

if version {
fmt.Printf("Version: %s\n", ssserver.Version)
os.Exit(0)
}

logOpts := log.Ldate | log.Ltime | log.Lshortfile
logger := stdr.New(log.New(os.Stderr, "", logOpts)).WithName("sanshell-proxy")
stdr.SetVerbosity(*verbosity)
Expand Down
12 changes: 11 additions & 1 deletion cmd/sansshell-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
_ "github.com/Snowflake-Labs/sansshell/services/localfile/server"
_ "github.com/Snowflake-Labs/sansshell/services/packages/server"
_ "github.com/Snowflake-Labs/sansshell/services/process/server"
_ "github.com/Snowflake-Labs/sansshell/services/sansshell/server"
ssserver "github.com/Snowflake-Labs/sansshell/services/sansshell/server"
_ "github.com/Snowflake-Labs/sansshell/services/service/server"
)

Expand All @@ -61,11 +61,21 @@ var (
verbosity = flag.Int("v", 0, "Verbosity level. > 0 indicates more extensive logging")
validate = flag.Bool("validate", false, "If true will evaluate the policy and then exit (non-zero on error)")
justification = flag.Bool("justification", false, "If true then justification (which is logged and possibly validated) must be passed along in the client context Metadata with the key '"+rpcauth.ReqJustKey+"'")
version bool
)

func init() {
flag.BoolVar(&version, "version", false, "Returns the server built version from the sansshell server package")
}

func main() {
flag.Parse()

if version {
fmt.Printf("Version: %s\n", ssserver.Version)
os.Exit(0)
}

logOpts := log.Ldate | log.Ltime | log.Lshortfile
logger := stdr.New(log.New(os.Stderr, "", logOpts)).WithName("sanshell-server")
stdr.SetVerbosity(*verbosity)
Expand Down
69 changes: 69 additions & 0 deletions services/sansshell/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func setup(f *flag.FlagSet) *subcommands.Commander {
c.Register(&getVerbosityCmd{}, "")
c.Register(&setProxyVerbosityCmd{}, "")
c.Register(&getProxyVerbosityCmd{}, "")
c.Register(&versionCmd{}, "")
c.Register(&proxyVersionCmd{}, "")

return c
}

Expand Down Expand Up @@ -212,3 +215,69 @@ func (g *getProxyVerbosityCmd) Execute(ctx context.Context, f *flag.FlagSet, arg
fmt.Fprintf(state.Out[0], "Proxy current logging level %d\n", resp.Level)
return subcommands.ExitSuccess
}

type versionCmd struct{}

func (*versionCmd) Name() string { return "version" }
func (*versionCmd) Synopsis() string { return "Get the server version." }
func (*versionCmd) Usage() string {
return "version"
}

func (s *versionCmd) SetFlags(f *flag.FlagSet) {}

func (s *versionCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
state := args[0].(*util.ExecuteState)
c := pb.NewStateClientProxy(state.Conn)

ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
resp, err := c.VersionOneMany(ctx, &emptypb.Empty{})
if err != nil {
// Emit this to every error file as it's not specific to a given target.
for _, e := range state.Err {
fmt.Fprintf(e, "Could not get version: %v\n", err)
}
return subcommands.ExitFailure
}

retCode := subcommands.ExitSuccess
for r := range resp {
if r.Error != nil {
fmt.Fprintf(state.Err[r.Index], "Getting version for target %s (%d) returned error: %v\n", r.Target, r.Index, r.Error)
retCode = subcommands.ExitFailure
continue
}
fmt.Fprintf(state.Out[r.Index], "Target: %s (%d) Version %s\n", r.Target, r.Index, r.Resp.Version)
}
return retCode
}

type proxyVersionCmd struct{}

func (*proxyVersionCmd) Name() string { return "proxy-version" }
func (*proxyVersionCmd) Synopsis() string { return "Get the proxy version" }
func (*proxyVersionCmd) Usage() string {
return "proxy-version"
}

func (*proxyVersionCmd) SetFlags(f *flag.FlagSet) {}

func (g *proxyVersionCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
state := args[0].(*util.ExecuteState)
if len(state.Out) > 1 {
fmt.Fprintf(os.Stderr, "can't call proxy version with multiple targets")
}
// Get a real connection to the proxy
c := pb.NewStateClient(state.Conn.Proxy())

ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
resp, err := c.Version(ctx, &emptypb.Empty{})
if err != nil {
fmt.Fprintf(state.Err[0], "Could not get proxy version: %v\n", err)
return subcommands.ExitFailure
}
fmt.Fprintf(state.Out[0], "Proxy version %s\n", resp.Version)
return subcommands.ExitSuccess
}
117 changes: 93 additions & 24 deletions services/sansshell/sansshell.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion services/sansshell/sansshell.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ service Logging {
rpc GetVerbosity(google.protobuf.Empty) returns (VerbosityReply) {}
}

service State {
// Version will return the build version as embedded in the running
// server.option
rpc Version(google.protobuf.Empty) returns (VersionResponse) {}
}

message SetVerbosityRequest { int32 Level = 1; }

message VerbosityReply { int32 Level = 1; }
message VerbosityReply { int32 Level = 1; }

message VersionResponse { string version = 1; }
Loading

0 comments on commit c271eaa

Please sign in to comment.