From f62bcadf262edf0db9abd5866413482555a11113 Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Wed, 17 Feb 2021 10:47:59 +0000 Subject: [PATCH] File service changes: ownership, auth etc (#70) --- .github/workflows/docs.yml | 4 +- files/handler/files.go | 32 ++++++++++--- files/main.go | 2 +- files/proto/files.pb.go | 97 +++++++++++++++++++++++--------------- files/proto/files.proto | 17 ++++--- streams/skip | 0 6 files changed, 96 insertions(+), 56 deletions(-) create mode 100644 streams/skip diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index f7e015f..11b149c 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -78,7 +78,7 @@ jobs: # publish to github first under micro/services # .npmrc has settings for it - uses: JS-DevTools/npm-publish@v1 - if: github.ref == 'refs/heads/master' + #if: github.ref == 'refs/heads/master' with: access: public package: services/clients/ts/package.json @@ -92,7 +92,7 @@ jobs: sed -i 's/micro/m3o/g' clients/ts/package.json - uses: JS-DevTools/npm-publish@v1 - if: github.ref == 'refs/heads/master' + #if: github.ref == 'refs/heads/master' with: access: public package: services/clients/ts/package.json diff --git a/files/handler/files.go b/files/handler/files.go index 083b56f..47f1253 100644 --- a/files/handler/files.go +++ b/files/handler/files.go @@ -2,8 +2,10 @@ package handler import ( "context" + "errors" "strings" + "github.com/micro/micro/v3/service/auth" log "github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/model" files "github.com/micro/services/files/proto" @@ -17,21 +19,37 @@ func NewFiles() *Files { i := model.ByEquality("project") i.Order.Type = model.OrderTypeUnordered - db := model.NewModel( - model.WithIndexes(i), + db := model.New( + files.File{}, + &model.Options{ + Key: "Id", + Indexes: []model.Index{i}, + }, ) - db.Register(new(files.File)) - return &Files{ db: db, } } func (e *Files) Save(ctx context.Context, req *files.SaveRequest, rsp *files.SaveResponse) error { - log.Info("Received Files.Call request") + // @todo return proper micro errors + acc, ok := auth.AccountFromContext(ctx) + if !ok { + return errors.New("Files.Save requires authentication") + } + + log.Info("Received Files.Save request") for _, file := range req.Files { - err := e.db.Create(file) + f := files.File{} + err := e.db.Read(model.QueryEquals("Id", file.Id), &f) + if err != nil && err != model.ErrorNotFound { + return err + } + if f.Owner != acc.ID { + return errors.New("Not authorized") + } + err = e.db.Create(file) if err != nil { return err } @@ -40,7 +58,7 @@ func (e *Files) Save(ctx context.Context, req *files.SaveRequest, rsp *files.Sav } func (e *Files) List(ctx context.Context, req *files.ListRequest, rsp *files.ListResponse) error { - log.Info("Received Files.Call request") + log.Info("Received Files.List request") rsp.Files = []*files.File{} err := e.db.Read(model.QueryEquals("project", req.GetProject()), &rsp.Files) if err != nil { diff --git a/files/main.go b/files/main.go index 02a4181..cadc0cb 100644 --- a/files/main.go +++ b/files/main.go @@ -16,7 +16,7 @@ func main() { ) // Register handler - pb.RegisterFilesHandler(srv.Server(), new(handler.Files)) + pb.RegisterFilesHandler(srv.Server(), handler.NewFiles()) // Run service if err := srv.Run(); err != nil { diff --git a/files/proto/files.pb.go b/files/proto/files.pb.go index c8fdd66..b8261bb 100644 --- a/files/proto/files.pb.go +++ b/files/proto/files.pb.go @@ -30,18 +30,21 @@ type File struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // A custom string for namespacing purposes // eg. files-of-mywebsite.com - Project string `protobuf:"bytes,1,opt,name=project,proto3" json:"project,omitempty"` + Project string `protobuf:"bytes,2,opt,name=project,proto3" json:"project,omitempty"` // Name of folder or file. - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"` // Path. Default is '/', ie. top level - Path string `protobuf:"bytes,3,opt,name=path,proto3" json:"path,omitempty"` - IsDirectory bool `protobuf:"varint,4,opt,name=isDirectory,proto3" json:"isDirectory,omitempty"` + Path string `protobuf:"bytes,4,opt,name=path,proto3" json:"path,omitempty"` + IsDirectory bool `protobuf:"varint,5,opt,name=is_directory,json=isDirectory,proto3" json:"is_directory,omitempty"` // File contents. Empty for directories. - FileContents string `protobuf:"bytes,5,opt,name=fileContents,proto3" json:"fileContents,omitempty"` - Created int64 `protobuf:"varint,6,opt,name=created,proto3" json:"created,omitempty"` - Updated int64 `protobuf:"varint,7,opt,name=updated,proto3" json:"updated,omitempty"` + FileContents string `protobuf:"bytes,6,opt,name=file_contents,json=fileContents,proto3" json:"file_contents,omitempty"` + Created int64 `protobuf:"varint,7,opt,name=created,proto3" json:"created,omitempty"` + Updated int64 `protobuf:"varint,8,opt,name=updated,proto3" json:"updated,omitempty"` + // owner + Owner string `protobuf:"bytes,9,opt,name=owner,proto3" json:"owner,omitempty"` } func (x *File) Reset() { @@ -76,6 +79,13 @@ func (*File) Descriptor() ([]byte, []int) { return file_proto_files_proto_rawDescGZIP(), []int{0} } +func (x *File) GetId() string { + if x != nil { + return x.Id + } + return "" +} + func (x *File) GetProject() string { if x != nil { return x.Project @@ -125,6 +135,13 @@ func (x *File) GetUpdated() int64 { return 0 } +func (x *File) GetOwner() string { + if x != nil { + return x.Owner + } + return "" +} + // The save endpoint lets you batch save text files. type SaveRequest struct { state protoimpl.MessageState @@ -323,39 +340,41 @@ var File_proto_files_proto protoreflect.FileDescriptor var file_proto_files_proto_rawDesc = []byte{ 0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x22, 0xc2, 0x01, 0x0a, 0x04, 0x46, - 0x69, 0x6c, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x01, + 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x22, 0xea, 0x01, 0x0a, 0x04, 0x46, + 0x69, 0x6c, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x12, 0x12, 0x0a, - 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, - 0x65, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x20, 0x0a, 0x0b, 0x69, 0x73, 0x44, 0x69, 0x72, 0x65, 0x63, - 0x74, 0x6f, 0x72, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x44, 0x69, - 0x72, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x22, 0x0a, 0x0c, 0x66, 0x69, 0x6c, 0x65, 0x43, - 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x66, - 0x69, 0x6c, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x63, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x63, 0x72, - 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, - 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x22, - 0x30, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, - 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, - 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, - 0x73, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x22, 0x3b, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, - 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22, 0x31, - 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, - 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, - 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, - 0x73, 0x32, 0x6d, 0x0a, 0x05, 0x46, 0x69, 0x6c, 0x65, 0x73, 0x12, 0x31, 0x0a, 0x04, 0x53, 0x61, - 0x76, 0x65, 0x12, 0x12, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x53, - 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x31, 0x0a, - 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x12, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x4c, 0x69, - 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x66, 0x69, 0x6c, 0x65, - 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, - 0x42, 0x0d, 0x5a, 0x0b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x21, 0x0a, 0x0c, 0x69, 0x73, 0x5f, 0x64, 0x69, 0x72, 0x65, + 0x63, 0x74, 0x6f, 0x72, 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x44, + 0x69, 0x72, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x23, 0x0a, 0x0d, 0x66, 0x69, 0x6c, 0x65, + 0x5f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0c, 0x66, 0x69, 0x6c, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x18, 0x0a, + 0x07, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, + 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, + 0x65, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, + 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x22, 0x30, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x46, 0x69, + 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x61, 0x76, + 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x3b, 0x0a, 0x0b, 0x4c, 0x69, 0x73, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a, + 0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, + 0x63, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22, 0x31, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x46, 0x69, + 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x32, 0x6d, 0x0a, 0x05, 0x46, 0x69, 0x6c, + 0x65, 0x73, 0x12, 0x31, 0x0a, 0x04, 0x53, 0x61, 0x76, 0x65, 0x12, 0x12, 0x2e, 0x66, 0x69, 0x6c, + 0x65, 0x73, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, + 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x12, 0x2e, + 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x13, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0d, 0x5a, 0x0b, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x3b, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/files/proto/files.proto b/files/proto/files.proto index 867dda6..38c9260 100644 --- a/files/proto/files.proto +++ b/files/proto/files.proto @@ -10,18 +10,21 @@ service Files { } message File { + string id = 1; // A custom string for namespacing purposes // eg. files-of-mywebsite.com - string project = 1; + string project = 2; // Name of folder or file. - string name = 2; + string name = 3; // Path. Default is '/', ie. top level - string path = 3; - bool isDirectory = 4; + string path = 4; + bool is_directory = 5; // File contents. Empty for directories. - string fileContents = 5; - int64 created = 6; - int64 updated = 7; + string file_contents = 6; + int64 created = 7; + int64 updated = 8; + // owner + string owner = 9; } // The save endpoint lets you batch save text files. diff --git a/streams/skip b/streams/skip new file mode 100644 index 0000000..e69de29