diff --git a/pkg/registry/apis/provisioning/jobs/exporter.go b/pkg/registry/apis/provisioning/jobs/exporter.go index 95d4bc5b065..aaa6e1bf874 100644 --- a/pkg/registry/apis/provisioning/jobs/exporter.go +++ b/pkg/registry/apis/provisioning/jobs/exporter.go @@ -115,13 +115,13 @@ func (r *exporter) Export(ctx context.Context, if err != nil && !(errors.Is(err, repository.ErrFileNotFound) || apierrors.IsNotFound(err)) { logger.Error("failed to check if folder exists before writing", "error", err) return fmt.Errorf("failed to check if folder exists before writing: %w", err) - } else if err != nil { // ErrFileNotFound - err = r.repository.Create(ctx, p, ref, nil, "export of folder `"+p+"` in namespace "+ns) - } else { + } else if err == nil { logger.Info("folder already exists") + return nil } - if err != nil { + // ErrFileNotFound + if err := r.repository.Create(ctx, p, ref, nil, "export of folder `"+p+"` in namespace "+ns); err != nil { logger.Error("failed to write a folder in repository", "error", err) return fmt.Errorf("failed to write folder in repo: %w", err) } diff --git a/pkg/registry/apis/provisioning/repository/local.go b/pkg/registry/apis/provisioning/repository/local.go index 6e48d750ce1..3837fd887f5 100644 --- a/pkg/registry/apis/provisioning/repository/local.go +++ b/pkg/registry/apis/provisioning/repository/local.go @@ -191,14 +191,14 @@ func (r *localRepository) Read(ctx context.Context, filePath string, ref string) filePath, err := safepath.Join(r.path, filePath) if err != nil { - return nil, err + return nil, fmt.Errorf("join path: %w", err) } info, err := os.Stat(filePath) if errors.Is(err, os.ErrNotExist) { return nil, ErrFileNotFound } else if err != nil { - return nil, err + return nil, fmt.Errorf("stat file: %w", err) } path := strings.TrimPrefix(filePath, safepath.Clean(r.path)) @@ -214,12 +214,12 @@ func (r *localRepository) Read(ctx context.Context, filePath string, ref string) //nolint:gosec data, err := os.ReadFile(filePath) if err != nil { - return nil, err + return nil, fmt.Errorf("read file: %w", err) } hash, _, err := r.calculateFileHash(filePath) if err != nil { - return nil, err + return nil, fmt.Errorf("calculate hash of file: %w", err) } return &FileInfo{ @@ -291,11 +291,13 @@ func (r *localRepository) Create(ctx context.Context, sanitisedPath string, ref return err } + // before sanitizing, check if the path is a directory + isDir := strings.HasSuffix(sanitisedPath, "/") inputUnsafePath := sanitisedPath // do NOT use in path operations, only for responses! sanitisedPath, err := safepath.Join(r.path, sanitisedPath) if err != nil { - return err + return fmt.Errorf("join path: %w", err) } _, err = os.Stat(sanitisedPath) @@ -306,7 +308,7 @@ func (r *localRepository) Create(ctx context.Context, sanitisedPath string, ref return apierrors.NewAlreadyExists(provisioning.RepositoryResourceInfo.GroupResource(), inputUnsafePath) } - if strings.HasSuffix(sanitisedPath, "/") { + if isDir { if data != nil { return apierrors.NewBadRequest("data cannot be provided for a directory") } @@ -314,6 +316,8 @@ func (r *localRepository) Create(ctx context.Context, sanitisedPath string, ref if err := os.MkdirAll(sanitisedPath, 0700); err != nil { return apierrors.NewInternalError(fmt.Errorf("failed to create path: %w", err)) } + + return nil } if err := os.MkdirAll(path.Dir(sanitisedPath), 0700); err != nil { @@ -330,7 +334,7 @@ func (r *localRepository) Update(ctx context.Context, path string, ref string, d path, err := safepath.Join(r.path, path) if err != nil { - return err + return fmt.Errorf("join path: %w", err) } if strings.HasSuffix(path, "/") {