From 1cc0de75a671f3b9496ba901abfddae32b316401 Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 20 Aug 2021 17:28:10 +0200 Subject: Handle missing trailing slashes + Add Dockerfile --- Dockerfile | 12 ++++++++++++ main.go | 22 +++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..451e459 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.17.0-alpine3.14 as builder +ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 +RUN apk update && apk add --no-cache ca-certificates && update-ca-certificates +WORKDIR /opt +COPY main.go go.mod go.sum /opt/ +RUN go build . + +FROM scratch +COPY --from=builder /opt/bagage / +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +USER 1000:1000 +ENTRYPOINT ["/bagage"] diff --git a/main.go b/main.go index aec59a2..2595a83 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,8 @@ func EnvOrDefault(key, def string) string { } func main() { + log.Println("=== Starting Bagage ===") + HttpListen := EnvOrDefault("BAGAGE_HTTP_LISTEN", ":8080") pathPrefix := EnvOrDefault("BAGAGE_WEBDAV_PREFIX", "/webdav") LdapServer := EnvOrDefault("BAGAGE_LDAP_ENDPOINT", "127.0.0.1:1389") UserBaseDN := EnvOrDefault("BAGAGE_LDAP_USER_BASE_DN", "ou=users,dc=deuxfleurs,dc=fr") @@ -132,7 +134,7 @@ func main() { return }) - if err := http.ListenAndServe(":8080", nil); err != nil { + if err := http.ListenAndServe(HttpListen, nil); err != nil { log.Fatalf("Error with WebDAV server: %v", err) } } @@ -171,6 +173,7 @@ func (s *GarageFS) Mkdir(ctx context.Context, name string, perm os.FileMode) err } func (s *GarageFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) { + log.Println("Stat from GarageFS.OpenFile()", name) NewGarageStatFromFile(ctx, name) return NewGarageFile(ctx, name) } @@ -184,7 +187,7 @@ func (s *GarageFS) Rename(ctx context.Context, oldName, newName string) error { } func (s *GarageFS) Stat(ctx context.Context, name string) (os.FileInfo, error) { - log.Println("Stat from GarageFS") + log.Println("Stat from GarageFS.Stat()", name) return NewGarageStat(ctx, name) } @@ -319,6 +322,7 @@ func (gf *GarageFile) readDirRoot(count int) ([]fs.FileInfo, error) { entries := make([]fs.FileInfo, 0, len(buckets)) for _, bucket := range buckets { + log.Println("Stat from GarageFile.readDirRoot()", "/"+bucket.Name) ngf, err := NewGarageStat(gf.ctx, "/"+bucket.Name) if err != nil { return nil, err @@ -330,8 +334,13 @@ func (gf *GarageFile) readDirRoot(count int) ([]fs.FileInfo, error) { } func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) { + prefix := gf.path.key + if prefix[len(prefix)-1:] != "/" { + prefix = prefix + "/" + } + objs_info := gf.mc.ListObjects(gf.ctx, gf.path.bucket, minio.ListObjectsOptions{ - Prefix: gf.path.key, + Prefix: prefix, Recursive: false, }) @@ -340,6 +349,7 @@ func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) { if object.Err != nil { return nil, object.Err } + log.Println("Stat from GarageFile.readDirChild()", path.Join("/", gf.path.bucket, object.Key)) ngf, err := NewGarageStatFromObjectInfo(gf.ctx, gf.path.bucket, object) if err != nil { return nil, err @@ -351,6 +361,7 @@ func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) { } func (gf *GarageFile) Stat() (fs.FileInfo, error) { + log.Println("Stat from GarageFile.Stat()", gf.path.path) return NewGarageStatFromFile(gf.ctx, gf.path.path) } @@ -377,7 +388,9 @@ func NewGarageStatFromFile(ctx context.Context, path string) (*GarageStat, error gs := new(GarageStat) gs.ctx = ctx gs.path = NewS3Path(path) - gs.path.class = OBJECT // known because called from GarageFile + if gs.path.class == OPAQUE_KEY { + gs.path.class = OBJECT // known because called from GarageFile + } gs.obj.Key = gs.path.key gs.obj.LastModified = time.Now() @@ -409,7 +422,6 @@ func NewGarageStatFromObjectInfo(ctx context.Context, bucket string, obj minio.O * Stat a path without additional information */ func NewGarageStat(ctx context.Context, path string) (*GarageStat, error) { - log.Println("Probe file", path) cache := ctx.Value(garageEntry).(garageCtx).StatCache if entry, ok := cache[path]; ok { return entry, nil -- cgit v1.2.3