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

Enhance HTTP Server Configuration and File Permissions in file.go #65

Merged
merged 4 commits into from
May 9, 2024
Merged
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
25 changes: 25 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 The Archivista Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

run:
timeout: 6m
linters:
enable:
- gosec
issues:
max-same-issues: 50
exclude-rules:
- path: _test.go
linters:
- gosec
12 changes: 8 additions & 4 deletions cmd/archivista/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@
if err != nil {
logrus.Fatalf("unable to start http listener: %+v", err)
}

go func() {
if err := http.Serve(listener, handlers.CORS(
srv := &http.Server{
Handler: handlers.CORS(

Check warning on line 145 in cmd/archivista/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/archivista/main.go#L144-L145

Added lines #L144 - L145 were not covered by tests
handlers.AllowedOrigins(cfg.CORSAllowOrigins),
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}),
)(server.Router())); err != nil {
)(server.Router()),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,

Check warning on line 151 in cmd/archivista/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/archivista/main.go#L149-L151

Added lines #L149 - L151 were not covered by tests
}
go func() {
if err := srv.Serve(listener); err != nil {

Check warning on line 154 in cmd/archivista/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/archivista/main.go#L153-L154

Added lines #L153 - L154 were not covered by tests
logrus.Fatalf("unable to start http server: %+v", err)
}
}()
Expand Down
21 changes: 18 additions & 3 deletions internal/objectstorage/filestore/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"net/http"
"os"
"path/filepath"
"time"

"github.com/gorilla/handlers"
)
Expand All @@ -32,9 +33,23 @@
func New(ctx context.Context, directory string, address string) (*Store, <-chan error, error) {
errCh := make(chan error)
go func() {
server := handlers.CompressHandler(http.FileServer(http.Dir(directory)))
log.Fatalln(http.ListenAndServe(address, server))
server := &http.Server{
Addr: address,
Handler: handlers.CompressHandler(http.FileServer(http.Dir(directory))),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,

Check warning on line 40 in internal/objectstorage/filestore/file.go

View check run for this annotation

Codecov / codecov/patch

internal/objectstorage/filestore/file.go#L36-L40

Added lines #L36 - L40 were not covered by tests
}

if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v\n", address, err)

Check warning on line 44 in internal/objectstorage/filestore/file.go

View check run for this annotation

Codecov / codecov/patch

internal/objectstorage/filestore/file.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

<-ctx.Done()

if err := server.Shutdown(context.Background()); err != nil {
log.Fatalf("Server Shutdown Failed:%+v", err)

Check warning on line 50 in internal/objectstorage/filestore/file.go

View check run for this annotation

Codecov / codecov/patch

internal/objectstorage/filestore/file.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}

close(errCh)
}()

Expand All @@ -53,7 +68,7 @@

func (s *Store) Store(ctx context.Context, gitoid string, payload []byte) error {
if filepath.IsLocal(gitoid) {
return os.WriteFile(filepath.Join(s.prefix, gitoid+".json"), payload, 0644)
return os.WriteFile(filepath.Join(s.prefix, gitoid+".json"), payload, 0o600)
} else {
return filepath.ErrBadPattern
}
Expand Down
Loading