Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for editing policies on buckets #65

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build:

.PHONY: run
run:
go run
go run main.go

.PHONY: lint
lint:
Expand Down
39 changes: 39 additions & 0 deletions internal/app/s3manager/bucket_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package s3manager

import (
"fmt"
"io"
"net/http"

"github.com/gorilla/mux"
)

func HandleGetBucketPolicy(s3 S3) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
policy, err := s3.GetBucketPolicy(r.Context(), bucketName)
if err != nil {
handleHTTPError(w, fmt.Errorf("error getting bucket policy: %w", err))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(policy))
}
}

func HandlePutBucketPolicy(s3 S3) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
policy, err := io.ReadAll(r.Body)
if err != nil {
handleHTTPError(w, fmt.Errorf("error reading request body: %w", err))
return
}
err = s3.SetBucketPolicy(r.Context(), bucketName, string(policy))
if err != nil {
handleHTTPError(w, fmt.Errorf("error setting bucket policy: %w", err))
return
}
w.WriteHeader(http.StatusNoContent)
}
}
2 changes: 1 addition & 1 deletion internal/app/s3manager/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func handleHTTPError(w http.ResponseWriter, err error) {
code = http.StatusNotFound
}

http.Error(w, http.StatusText(code), code)
http.Error(w, err.Error(), code)

// Log if server error
if code >= http.StatusInternalServerError {
Expand Down
2 changes: 2 additions & 0 deletions internal/app/s3manager/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type S3 interface {
PutObject(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (minio.UploadInfo, error)
RemoveBucket(ctx context.Context, bucketName string) error
RemoveObject(ctx context.Context, bucketName, objectName string, opts minio.RemoveObjectOptions) error
GetBucketPolicy(ctx context.Context, bucketName string) (string, error)
SetBucketPolicy(ctx context.Context, bucketName string, policy string) error
}

type SSEType struct {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func main() {
if configuration.AllowDelete {
r.Handle("/api/buckets/{bucketName}/objects/{objectName:.*}", s3manager.HandleDeleteObject(s3)).Methods(http.MethodDelete)
}
r.Handle("/api/buckets/{bucketName}/policy", s3manager.HandleGetBucketPolicy(s3)).Methods(http.MethodGet)
r.Handle("/api/buckets/{bucketName}/policy", s3manager.HandlePutBucketPolicy(s3)).Methods(http.MethodPut)

lr := logging.Handler(os.Stdout)(r)
srv := &http.Server{
Expand Down
Loading