Skip to content

Commit

Permalink
Delete (#6)
Browse files Browse the repository at this point in the history
* log requests

* delete file

* fix some error messages
  • Loading branch information
theleeeo authored Oct 23, 2024
1 parent 0f8745c commit 726f9de
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 21 deletions.
2 changes: 2 additions & 0 deletions authorization/plugin/allow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func NewAllowTypesPlugin(args []string) (*AllowTypesPlugin, error) {
reqType = authorization.RequestType_REQUEST_TYPE_GET_METADATA
case "list":
reqType = authorization.RequestType_REQUEST_TYPE_LIST
case "delete":
reqType = authorization.RequestType_REQUEST_TYPE_DELETE
default:
return nil, status.Error(codes.InvalidArgument, "unknown request type: "+arg)
}
Expand Down
29 changes: 17 additions & 12 deletions authorization/v1/authorization.pb.go

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

9 changes: 9 additions & 0 deletions mocks/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,12 @@ func (p *Provider) ListObjects(ctx context.Context, prefix string) (provider.Lis

return called.Get(0).(provider.ListObjectsResponse), called.Error(1)
}

func (p *Provider) DeleteObject(ctx context.Context, key string) error {
called := p.Called(ctx, key)
if called.Get(0) == nil {
return nil
}

return called.Error(0)
}
1 change: 1 addition & 0 deletions proto/authorization/v1/authorization.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum RequestType {
REQUEST_TYPE_UPLOAD = 2;
REQUEST_TYPE_GET_METADATA = 3;
REQUEST_TYPE_LIST = 4;
REQUEST_TYPE_DELETE = 5;
}

message AuthorizeRequest {
Expand Down
12 changes: 12 additions & 0 deletions provider/gocloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ func (n *GocloudProvider) ListObjects(ctx context.Context, prefix string) (ListO
Keys: files,
}, nil
}

func (n *GocloudProvider) DeleteObject(ctx context.Context, key string) error {
if err := n.bucket.Delete(ctx, key); err != nil {
if gcerrors.Code(err) == gcerrors.NotFound {
return ErrNotFound
}

return err
}

return nil
}
5 changes: 5 additions & 0 deletions provider/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ func (n *LogProvider) ListObjects(ctx context.Context, prefix string) (ListObjec
log.Printf("ListObjects %s\n", prefix)
return ListObjectsResponse{}, nil
}

func (n *LogProvider) DeleteObject(ctx context.Context, key string) error {
log.Printf("DeleteObject %s\n", key)
return nil
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Provider interface {
PutObject(ctx context.Context, key string, data io.Reader, opts PutOptions) error
GetTags(ctx context.Context, key string) (map[string]string, error)
ListObjects(ctx context.Context, prefix string) (ListObjectsResponse, error)
DeleteObject(ctx context.Context, key string) error
}

type GetOptions struct {
Expand Down
13 changes: 13 additions & 0 deletions provider/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,16 @@ func (s *S3Provider) ListObjects(ctx context.Context, prefix string) (ListObject
Keys: files,
}, nil
}

func (s *S3Provider) DeleteObject(ctx context.Context, key string) error {
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: &s.bucketName,
Key: &key,
})
if err != nil {
// S3 seems to return a 200 OK even if the object does not exist so no need to check for that.
return err
}

return nil
}
4 changes: 4 additions & 0 deletions provider/void.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func (n *VoidProvider) GetTags(ctx context.Context, key string) (map[string]stri
func (n *VoidProvider) ListObjects(ctx context.Context, prefix string) (ListObjectsResponse, error) {
return ListObjectsResponse{}, nil
}

func (n *VoidProvider) DeleteObject(ctx context.Context, key string) error {
return nil
}
36 changes: 27 additions & 9 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (s *Server) handleFile(w http.ResponseWriter, r *http.Request) {
reqType = authorization.RequestType_REQUEST_TYPE_DOWNLOAD
case http.MethodPut, http.MethodPost:
reqType = authorization.RequestType_REQUEST_TYPE_UPLOAD
case http.MethodDelete:
reqType = authorization.RequestType_REQUEST_TYPE_DELETE
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
Expand Down Expand Up @@ -84,8 +86,28 @@ func (s *Server) handleFile(w http.ResponseWriter, r *http.Request) {
return
}

if err := s.handleUpload(r, p, key); err != nil {
lerr.ToHTTP(w, err)
if reqType == authorization.RequestType_REQUEST_TYPE_UPLOAD {
if err := s.handleUpload(r, p, key); err != nil {
lerr.ToHTTP(w, err)
return
}

w.WriteHeader(http.StatusOK)
return
}

if reqType == authorization.RequestType_REQUEST_TYPE_DELETE {
if err := p.DeleteObject(r.Context(), key); err != nil {
if errors.Is(err, provider.ErrNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}

http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
return
}

Expand Down Expand Up @@ -177,14 +199,14 @@ func getDataSource(r *http.Request, allowRawBody bool) (io.ReadCloser, error) {
if err := r.ParseMultipartForm(10 << 20); err != nil {
log.Println("error parsing multipart form:", err)

return nil, lerr.Wrap(err, http.StatusBadRequest, "error parsing multipart form:")
return nil, lerr.Wrap(err, http.StatusBadRequest, "error parsing multipart form")
}

file, _, err := r.FormFile("file")
if err != nil {
log.Println("error getting file from form:", err)

return nil, lerr.Wrap(err, http.StatusBadRequest, "error getting file from form:")
return nil, lerr.Wrap(err, http.StatusBadRequest, "error getting file from form")
}
data = file
} else {
Expand Down Expand Up @@ -257,11 +279,7 @@ func (s *Server) authorizeRequest(ctx context.Context, reqType authorization.Req
}

if err := authPlugin.Authorize(ctx, req); err != nil {
s, ok := status.FromError(err)
if !ok {
return lerr.Newf(http.StatusInternalServerError, "plugin error not a grpc status! error=%s", err.Error())
}

s := status.Convert(err)
if s.Code() == codes.Unauthenticated {
return lerr.Newf(http.StatusUnauthorized, "Unauthenticated: %s", s.Message())
}
Expand Down
2 changes: 2 additions & 0 deletions server/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func InternalErrorRedacter(next http.Handler) http.Handler {
return
}

log.Printf("%s %s %d", r.Method, r.URL.Path, respCatcher.Code)

copyHeaders(w.Header(), respCatcher.Header())
w.WriteHeader(respCatcher.Code)
_, _ = w.Write(respCatcher.Body.Bytes())
Expand Down

0 comments on commit 726f9de

Please sign in to comment.