Skip to content

Commit

Permalink
fixup! Introduce snippet-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed May 27, 2024
1 parent 583e7b2 commit b6be68f
Show file tree
Hide file tree
Showing 18 changed files with 509 additions and 143 deletions.
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ linters:
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted.
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
- execinquery # Execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds.
- exportloopref # checks for pointers to enclosing loop variables
# - forcetypeassert # finds forced type assertions
- gci # Gci control golang package import order and make it always deterministic.
Expand Down Expand Up @@ -97,22 +96,19 @@ linters:
- cyclop # checks function and package cyclomatic complexity
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- exhaustive # Check exhaustiveness of enum switch statements
- exhaustivestruct # Checks if all struct's fields are initialized
- exhaustruct # Checks if all structure fields are initialized.
- forbidigo # Forbids identifiers
- funlen # Tool for detection of long functions
- gochecknoglobals # Checks that no globals are present in Go code
- gochecknoinits # Checks that no init functions are present in Go code
- godot # Check if comments end in a period
- gomnd # An analyzer to detect magic numbers.
- ifshort # Checks that your code uses short syntax for if-statements whenever possible
- inamedparam # Reports interfaces with unnamed method parameters.
- interfacebloat # A linter that checks the number of methods inside an interface
- ireturn # Accept Interfaces, Return Concrete Types
- lll # Reports long lines
- maintidx # maintidx measures the maintainability index of each function.
- makezero # Finds slice declarations with non-zero initial length
- maligned # Tool to detect Go structs that would take less memory if their fields were sorted
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
- nonamedreturns # Reports all named returns
- paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test
Expand Down
12 changes: 6 additions & 6 deletions cloud2cloud-gateway/service/subscribeToDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ func TestRequestHandlerSubscribeToDevices(t *testing.T) {
r.StrictSlash(true)
r.HandleFunc(eventsURI, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h, err2 := events.ParseEventHeader(r)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
defer func() {
_ = r.Body.Close()
}()
assert.Equal(t, wantEventType, h.EventType)
buf, err2 := io.ReadAll(r.Body)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
var v interface{}
err2 = json.Decode(buf, &v)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
assert.Equal(t, wantEventContent, v)
w.WriteHeader(http.StatusOK)
err2 = eventsServer.Close()
Expand Down Expand Up @@ -171,16 +171,16 @@ func TestRequestHandlerSubscribeToDevicesOffline(t *testing.T) {
r.StrictSlash(true)
r.HandleFunc(eventsURI, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h, err2 := events.ParseEventHeader(r)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
defer func() {
_ = r.Body.Close()
}()
assert.Equal(t, wantEventType, h.EventType)
buf, err2 := io.ReadAll(r.Body)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
var v interface{}
err2 = json.Decode(buf, &v)
assert.NoError(t, err2) //nolint:testifylint
assert.NoError(t, err2)
assert.Equal(t, wantEventContent, v)
w.WriteHeader(http.StatusOK)
err2 = eventsServer.Close()
Expand Down
6 changes: 3 additions & 3 deletions cloud2cloud-gateway/test/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ func (s *EventsServer) Run(t *testing.T) EventChan {
r.StrictSlash(true)
r.HandleFunc(s.uri, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h, err := events.ParseEventHeader(r)
assert.NoError(t, err) //nolint:testifylint
assert.NoError(t, err)
defer func() {
_ = r.Body.Close()
}()
buf, err := io.ReadAll(r.Body)
assert.NoError(t, err) //nolint:testifylint
assert.NoError(t, err)

data, err := decodeEvent(h.EventType, buf)
assert.NoError(t, err) //nolint:testifylint
assert.NoError(t, err)
dataChan <- Event{
header: h,
data: data,
Expand Down
1 change: 0 additions & 1 deletion snippet-service/pb/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ service SnippetService {
};
}


rpc GetAppliedConfigurations(GetAppliedDeviceConfigurationsRequest) returns (stream AppliedDeviceConfiguration) {
option (google.api.http) = {
get: "/api/v1/configurations/applied";
Expand Down
69 changes: 67 additions & 2 deletions snippet-service/service/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package grpc

import (
"context"
"errors"

"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/snippet-service/pb"
"github.com/plgd-dev/hub/v2/snippet-service/store"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// SnippetServiceServer handles incoming requests.
Expand All @@ -29,12 +33,73 @@ func NewSnippetServiceServer(ownerClaim string, hubID string, store store.Store,
return s, nil
}

func (s *SnippetServiceServer) checkOwner(ctx context.Context, owner string) (string, error) {
ownerFromToken, err := grpc.OwnerFromTokenMD(ctx, s.ownerClaim)
if err != nil {
return "", err
}
if owner != "" && ownerFromToken != owner {
return "", errors.New("owner mismatch")
}
return ownerFromToken, nil
}

func (s *SnippetServiceServer) CreateConfiguration(ctx context.Context, conf *pb.Configuration) (*pb.Configuration, error) {
return s.store.CreateConfiguration(ctx, conf)
owner, err := s.checkOwner(ctx, conf.GetOwner())
if err != nil {
return nil, s.logger.LogAndReturnError(status.Errorf(codes.PermissionDenied, "cannot create configuration: %v", err))
}

conf.Owner = owner
c, err := s.store.CreateConfiguration(ctx, conf)
if err != nil {
return nil, s.logger.LogAndReturnError(status.Errorf(codes.Internal, "cannot create configuration: %v", err))
}
return c, nil
}

func (s *SnippetServiceServer) UpdateConfiguration(ctx context.Context, conf *pb.Configuration) (*pb.Configuration, error) {
return s.store.UpdateConfiguration(ctx, conf)
owner, err := s.checkOwner(ctx, conf.GetOwner())
if err != nil {
return nil, s.logger.LogAndReturnError(status.Errorf(codes.PermissionDenied, "cannot update configuration: %v", err))
}

conf.Owner = owner
c, err := s.store.UpdateConfiguration(ctx, conf)
if err != nil {
return nil, s.logger.LogAndReturnError(status.Errorf(codes.Internal, "cannot update configuration: %v", err))
}
return c, nil
}

func (s *SnippetServiceServer) GetConfigurations(req *pb.GetConfigurationsRequest, srv pb.SnippetService_GetConfigurationsServer) error {
owner, err := s.checkOwner(srv.Context(), "")
if err != nil {
return s.logger.LogAndReturnError(status.Errorf(codes.PermissionDenied, "cannot update configuration: %v", err))
}

err = s.store.GetConfigurations(srv.Context(), owner, req, func(ctx context.Context, iter store.Iterator[store.Configuration]) error {
storedCfg := store.Configuration{}
for iter.Next(ctx, &storedCfg) {
for _, version := range storedCfg.Versions {
errS := srv.Send(&pb.Configuration{
Id: storedCfg.Id,
Owner: storedCfg.Owner,
Name: storedCfg.Name,
Version: version.Version,
Resources: version.Resources,
})
if errS != nil {
return errS
}
}
}
return nil
})
if err != nil {
return s.logger.LogAndReturnError(status.Errorf(codes.Internal, "cannot get configurations: %v", err))
}
return nil
}

func (s *SnippetServiceServer) Close(ctx context.Context) error {
Expand Down
Loading

0 comments on commit b6be68f

Please sign in to comment.