Skip to content

Commit

Permalink
Update teller and example
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Feb 24, 2022
1 parent 3864019 commit 6474795
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ replace (
github.com/go-openapi/spec => github.com/go-openapi/spec v0.19.8
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4

github.com/spectralops/teller => github.com/pkosiec/teller v1.4.1-0.20220223143025-e3f18e5c70c2
github.com/spectralops/teller => github.com/pkosiec/teller v1.4.1-0.20220224131849-7cde9fef3b6c
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1609,8 +1609,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pkosiec/teller v1.4.1-0.20220223143025-e3f18e5c70c2 h1:bcWuKCL54vNtNkD1MEBKmSfqdernUCCTjlwSkFBpV1g=
github.com/pkosiec/teller v1.4.1-0.20220223143025-e3f18e5c70c2/go.mod h1:x2B4YYysArnF/yH+IWmEwSef+5kK+CFD/ySgQHEboC0=
github.com/pkosiec/teller v1.4.1-0.20220224131849-7cde9fef3b6c h1:RBLmCK5i8F2OWoUybUSjDx61xW1AbMUa/uVOp51GkAs=
github.com/pkosiec/teller v1.4.1-0.20220224131849-7cde9fef3b6c/go.mod h1:x2B4YYysArnF/yH+IWmEwSef+5kK+CFD/ySgQHEboC0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
19 changes: 15 additions & 4 deletions internal/secret-storage-backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"google.golang.org/grpc/status"
)

// AdditionalParameters holds Secret storage backend specific parameters.
type AdditionalParameters struct {
Provider string `json:"provider"`
}

var _ pb.StorageBackendServer = &Handler{}

// Handler handles incoming requests to the Secret storage backend gRPC server.
type Handler struct {
pb.UnimplementedStorageBackendServer

Expand All @@ -35,16 +37,19 @@ const (
)

var (
// NilRequestInputError describes an error with an invalid request.
NilRequestInputError = status.Error(codes.InvalidArgument, "request data cannot be nil")
)

// NewHandler returns new Handler.
func NewHandler(log *zap.Logger, providers map[string]tellercore.Provider) *Handler {
return &Handler{
log: log,
providers: providers,
}
}

// GetValue returns a value for a given TypeInstance. It returns nil as value if a given secret is not found.
func (h *Handler) GetValue(_ context.Context, request *pb.GetValueRequest) (*pb.GetValueResponse, error) {
if request == nil {
return nil, NilRequestInputError
Expand All @@ -71,6 +76,7 @@ func (h *Handler) GetValue(_ context.Context, request *pb.GetValueRequest) (*pb.
}, nil
}

// GetLockedBy returns a locked by data for a given TypeInstance. It returns nil as value if a given secret is not found.
func (h *Handler) GetLockedBy(_ context.Context, request *pb.GetLockedByRequest) (*pb.GetLockedByResponse, error) {
if request == nil {
return nil, NilRequestInputError
Expand All @@ -97,6 +103,7 @@ func (h *Handler) GetLockedBy(_ context.Context, request *pb.GetLockedByRequest)
}, nil
}

// OnCreate handles TypeInstance creation by creating secret in a given provider.
func (h *Handler) OnCreate(_ context.Context, request *pb.OnCreateRequest) (*pb.OnCreateResponse, error) {
if request == nil {
return nil, NilRequestInputError
Expand All @@ -115,6 +122,7 @@ func (h *Handler) OnCreate(_ context.Context, request *pb.OnCreateRequest) (*pb.
return &pb.OnCreateResponse{}, nil
}

// OnUpdate handles TypeInstance update by updating secret in a given provider.
func (h *Handler) OnUpdate(_ context.Context, request *pb.OnUpdateRequest) (*pb.OnUpdateResponse, error) {
if request == nil {
return nil, NilRequestInputError
Expand All @@ -133,7 +141,8 @@ func (h *Handler) OnUpdate(_ context.Context, request *pb.OnUpdateRequest) (*pb.
return &pb.OnUpdateResponse{}, nil
}

// OnLock doesn't check whether a given TypeInstance is already locked, but overrides the value in place
// OnLock handles TypeInstance locking by setting a secret entry in a given provider.
// It doesn't check whether a given TypeInstance is already locked, but overrides the value in place
// TODO(review): Is that valid assumption? Is there a need to complicate the flow here?
func (h *Handler) OnLock(_ context.Context, request *pb.OnLockRequest) (*pb.OnLockResponse, error) {
if request == nil {
Expand All @@ -154,6 +163,7 @@ func (h *Handler) OnLock(_ context.Context, request *pb.OnLockRequest) (*pb.OnLo
return &pb.OnLockResponse{}, nil
}

// OnUnlock handles TypeInstance unlocking by removing secret entry in a given provider.
func (h *Handler) OnUnlock(_ context.Context, request *pb.OnUnlockRequest) (*pb.OnUnlockResponse, error) {
if request == nil {
return nil, NilRequestInputError
Expand All @@ -173,7 +183,8 @@ func (h *Handler) OnUnlock(_ context.Context, request *pb.OnUnlockRequest) (*pb.
return &pb.OnUnlockResponse{}, nil
}

// OnDelete doesn't check whether a given TypeInstance is locked. It assumes the caller ensured it's unlocked state.
// OnDelete handles TypeInstance deletion by removing a secret in a given provider.
// It doesn't check whether a given TypeInstance is locked. It assumes the caller ensured it's unlocked state.
// TODO(review): Is that a valid assumption?
func (h *Handler) OnDelete(_ context.Context, request *pb.OnDeleteRequest) (*pb.OnDeleteResponse, error) {
if request == nil {
Expand Down Expand Up @@ -290,10 +301,10 @@ func (h *Handler) storagePathForTypeInstance(provider tellercore.Provider, tiID
func (h *Handler) ensureEntryDoesNotExist(provider tellercore.Provider, key tellercore.KeyPath) error {
entry, err := h.getEntry(provider, key)
if err != nil {
return h.internalError(errors.Wrapf(err, "while getting entry for path %q", key.Path))
return h.internalError(errors.Wrapf(err, "while getting field %q for path %q", key.Field, key.Path))
}
if entry.IsFound {
return status.Error(codes.AlreadyExists, fmt.Sprintf("entry %q in provider %q already exist", key.Path, provider.Name()))
return status.Error(codes.AlreadyExists, fmt.Sprintf("field %q for path %q in provider %q already exist", key.Field, key.Path, provider.Name()))
}

return nil
Expand Down
33 changes: 18 additions & 15 deletions pkg/hub/api/grpc/storage_backend/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ExampleNewStorageBackendClient() {
client := pb.NewStorageBackendClient(conn)

// create
fmt.Println("create TI", typeInstanceID)
fmt.Printf("Creating TI %q...\n", typeInstanceID)

_, err = client.OnCreate(ctx, &pb.OnCreateRequest{
TypeinstanceId: typeInstanceID,
Expand All @@ -44,19 +44,20 @@ func ExampleNewStorageBackendClient() {

// get value

var resourceVersion uint32 = 1
res, err := client.GetValue(ctx, &pb.GetValueRequest{
TypeinstanceId: typeInstanceID,
ResourceVersion: 1,
ResourceVersion: resourceVersion,
AdditionalParameters: reqAdditionalParams,
})
if err != nil {
panic(err)
}

fmt.Println("first get - resource version 1", string(res.Value))
fmt.Printf("Getting TI %q: resource version %d: %s\n", typeInstanceID, resourceVersion, string(res.Value))

// update
fmt.Println("update TI", typeInstanceID)
fmt.Printf("Updating TI %q...\n", typeInstanceID)

newValueBytes := []byte(`{"key": "updated"}`)
_, err = client.OnUpdate(ctx, &pb.OnUpdateRequest{
Expand All @@ -73,29 +74,30 @@ func ExampleNewStorageBackendClient() {

res, err = client.GetValue(ctx, &pb.GetValueRequest{
TypeinstanceId: typeInstanceID,
ResourceVersion: 1,
ResourceVersion: resourceVersion,
AdditionalParameters: reqAdditionalParams,
})
if err != nil {
panic(err)
}

fmt.Println("get after update - resource version 1", string(res.Value))
fmt.Printf("Getting TI %q: resource version %d: %s\n", typeInstanceID, resourceVersion, string(res.Value))

resourceVersion = 2
res, err = client.GetValue(ctx, &pb.GetValueRequest{
TypeinstanceId: typeInstanceID,
ResourceVersion: 2,
ResourceVersion: resourceVersion,
AdditionalParameters: reqAdditionalParams,
})
if err != nil {
panic(err)
}

fmt.Println("get after update - resource version 2", string(res.Value))
fmt.Printf("Getting TI %q: resource version %d: %s\n", typeInstanceID, resourceVersion, string(res.Value))

// lock

fmt.Println("locking")
fmt.Printf("Locking TI %q...\n", typeInstanceID)

_, err = client.OnLock(ctx, &pb.OnLockRequest{
TypeinstanceId: typeInstanceID,
Expand All @@ -120,11 +122,11 @@ func ExampleNewStorageBackendClient() {
panic("lockedBy cannot be nil")
}

fmt.Println("first get - lockedBy", *lockedByRes.LockedBy)
fmt.Printf("Getting TI %q: locked by %q\n", typeInstanceID, *lockedByRes.LockedBy)

// unlock

fmt.Println("unlocking")
fmt.Printf("Unlocking TI %q...\n", typeInstanceID)

_, err = client.OnUnlock(ctx, &pb.OnUnlockRequest{
TypeinstanceId: typeInstanceID,
Expand All @@ -144,10 +146,10 @@ func ExampleNewStorageBackendClient() {
panic(err)
}

fmt.Println("second get - lockedBy", lockedByRes.LockedBy)
fmt.Printf("Getting TI %q: locked by: %v\n", typeInstanceID, lockedByRes.LockedBy)

// delete
fmt.Println("delete TI", typeInstanceID)
fmt.Printf("Deleting TI %q...\n", typeInstanceID)

_, err = client.OnDelete(ctx, &pb.OnDeleteRequest{
TypeinstanceId: typeInstanceID,
Expand All @@ -159,14 +161,15 @@ func ExampleNewStorageBackendClient() {

// last get

resourceVersion = 1
res, err = client.GetValue(ctx, &pb.GetValueRequest{
TypeinstanceId: typeInstanceID,
ResourceVersion: 1,
ResourceVersion: resourceVersion,
AdditionalParameters: reqAdditionalParams,
})
if err != nil {
panic(err)
}

fmt.Println("last get after delete - value", string(res.Value))
fmt.Printf("Getting TI %q: resource version %d: is nil: %v\n", typeInstanceID, resourceVersion, res.Value == nil)
}

0 comments on commit 6474795

Please sign in to comment.