Skip to content

Commit

Permalink
add LicenseState() to SystemView interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thyton committed Aug 10, 2024
1 parent bf0e156 commit be0d397
Show file tree
Hide file tree
Showing 9 changed files with 679 additions and 279 deletions.
3 changes: 3 additions & 0 deletions changelog/27930.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
sdk: Add new method to system view to allow plugins to request license state
```
9 changes: 9 additions & 0 deletions sdk/helper/license/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package license

import "time"

type State struct {
State string
ExpiryTime time.Time
Terminated bool
}
7 changes: 7 additions & 0 deletions sdk/logical/system_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type SystemView interface {

// GenerateIdentityToken returns an identity token for the requesting plugin.
GenerateIdentityToken(ctx context.Context, req *pluginutil.IdentityTokenRequest) (*pluginutil.IdentityTokenResponse, error)

// LicenseState returns the license state of the currently running Vault
LicenseState() (*license.State, error)
}

type PasswordPolicy interface {
Expand Down Expand Up @@ -285,3 +288,7 @@ func (d StaticSystemView) GenerateIdentityToken(_ context.Context, _ *pluginutil
func (d StaticSystemView) APILockShouldBlockRequest() (bool, error) {
return d.APILockShouldBlockRequestVal, nil
}

func (d StaticSystemView) LicenseState() (*license.State, error) {
return nil, errors.New("LicenseState is not implemented in StaticSystemView")
}
57 changes: 55 additions & 2 deletions sdk/plugin/grpc_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)

var errMissingSystemView = errors.New("missing system view implementation: this method should not be called during plugin Setup, but only during and after Initialize")
Expand Down Expand Up @@ -132,8 +133,14 @@ func (s *gRPCSystemViewClient) MlockEnabled() bool {
}

func (s *gRPCSystemViewClient) HasFeature(feature license.Features) bool {
// Not implemented
return false
reply, err := s.client.HasFeature(context.Background(), &pb.HasFeatureRequest{
Feature: uint32(feature),
})
if err != nil {
return false
}

return reply.HasFeature
}

func (s *gRPCSystemViewClient) LocalMount() bool {
Expand Down Expand Up @@ -226,6 +233,24 @@ func (s *gRPCSystemViewClient) GenerateIdentityToken(ctx context.Context, req *p
}, nil
}

func (s *gRPCSystemViewClient) LicenseState() (*license.State, error) {
reply, err := s.client.LicenseState(context.Background(), &pb.Empty{})
if err != nil {
return nil, err
}

var expiryTime time.Time
if reply.ExpiryTime != nil {
expiryTime = reply.ExpiryTime.AsTime()
}

return &license.State{
State: reply.State,
ExpiryTime: expiryTime,
Terminated: reply.Terminated,
}, nil
}

type gRPCSystemViewServer struct {
pb.UnimplementedSystemViewServer

Expand Down Expand Up @@ -429,3 +454,31 @@ func (s *gRPCSystemViewServer) GenerateIdentityToken(ctx context.Context, req *p
TTL: int64(res.TTL.Seconds()),
}, nil
}

func (s *gRPCSystemViewServer) LicenseState(ctx context.Context, _ *pb.Empty) (*pb.LicenseStateReply, error) {
if s.impl == nil {
return nil, errMissingSystemView
}

licenseState, err := s.impl.LicenseState()
if err != nil {
return &pb.LicenseStateReply{}, status.Errorf(codes.Internal,
err.Error())
}

return &pb.LicenseStateReply{
State: licenseState.State,
ExpiryTime: timestamppb.New(licenseState.ExpiryTime),
Terminated: licenseState.Terminated,
}, nil
}

func (s *gRPCSystemViewServer) HasFeature(ctx context.Context, req *pb.HasFeatureRequest) (*pb.HasFeatureReply, error) {
if s.impl == nil {
return nil, errMissingSystemView
}

return &pb.HasFeatureReply{
HasFeature: s.impl.HasFeature(license.Features(req.Feature)),
}, nil
}
Loading

0 comments on commit be0d397

Please sign in to comment.