From f5cdbe1bd3c94adce51a401bb600e6661aaebf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sat, 6 Jan 2024 14:17:22 +0100 Subject: [PATCH] Code cleanup --- api/api.go | 2 +- api/middleware.go | 8 ++++---- api/routes.go | 5 +++-- domain/resource/file_manager.go | 9 ++++----- domain/resource/s3_manager.go | 14 +++++++------- security/generator.go | 8 ++++++++ 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/api/api.go b/api/api.go index eca70d9..c3aa5b3 100644 --- a/api/api.go +++ b/api/api.go @@ -143,7 +143,7 @@ func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc { path := c.Query("path") log.Printf("Deleting '%s'...", path) if err := resourceManager.Delete(path); err != nil { - c.AbortWithError(400, err) + _ = c.AbortWithError(400, err) } else { log.Printf("Deleted '%s'", path) c.Status(204) diff --git a/api/middleware.go b/api/middleware.go index 1372790..2b9bac0 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -32,14 +32,14 @@ func Auth() gin.HandlerFunc { authHeader := c.GetHeader("Authorization") if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth { if strings.TrimPrefix(authHeader, "Basic ") == basicAuth { - c.Set("secure", "basic") + c.Set("secure", security.TypeBasic) return } } if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth { key := strings.TrimPrefix(authHeader, "Api ") if hasValidApiKey(apiAuths, key) { - c.Set("secure", "api") + c.Set("secure", security.TypeApi) return } } @@ -47,13 +47,13 @@ func Auth() gin.HandlerFunc { } } -func Secure(types ...string) gin.HandlerFunc { +func Secure(types ...security.Type) gin.HandlerFunc { return func(c *gin.Context) { value, exists := c.Get("secure") if !exists { abort(c, nil, http.StatusUnauthorized, "missing auth") } else { - securityType := value.(string) + securityType := security.Type(value.(string)) for _, t := range types { if t == securityType { return diff --git a/api/routes.go b/api/routes.go index 648519d..b70c86c 100644 --- a/api/routes.go +++ b/api/routes.go @@ -6,6 +6,7 @@ import ( "os" "resource-manager/domain/cache" "resource-manager/domain/resource" + "resource-manager/security" "time" ) @@ -31,13 +32,13 @@ func RegisterRoutes(server *gin.Engine) { resourceManager := resource.NewManager(cacheManager, expiration) - resourcesGroup := server.Group("/api/v1/resources", Secure("basic", "api")) + resourcesGroup := server.Group("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi)) resourcesGroup.POST("", HandleUpload(resourceManager)) resourcesGroup.GET("presign", HandlePresign(resourceManager)) resourcesGroup.PUT("copy", HandleCopy(resourceManager)) resourcesGroup.DELETE("", HandleDelete(resourceManager)) - server.GET("/api/v1/resources", Secure("basic", "api", "token"), HandleDownload(resourceManager)) + server.GET("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi, security.TypeToken), HandleDownload(resourceManager)) } func loadExpiration() time.Duration { diff --git a/domain/resource/file_manager.go b/domain/resource/file_manager.go index f464e9f..480c670 100644 --- a/domain/resource/file_manager.go +++ b/domain/resource/file_manager.go @@ -2,7 +2,6 @@ package resource import ( "context" - "io/ioutil" "log" "os" "path/filepath" @@ -46,9 +45,9 @@ func checkFileExists(path string) bool { _, err := os.Stat(path) return err == nil } -func (f *fileManager) Download(ctx context.Context, path string) (file []byte, err error) { +func (f *fileManager) Download(_ context.Context, path string) (file []byte, err error) { fullPath := filepath.Join(f.path, path) - file, err = ioutil.ReadFile(fullPath) + file, err = os.ReadFile(fullPath) if err != nil { log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error()) } @@ -99,11 +98,11 @@ func copyFolder(source, destination string, overwrite bool) error { doesExist = true } if !doesExist || overwrite { - var data, err1 = ioutil.ReadFile(filepath.Join(source, relPath)) + var data, err1 = os.ReadFile(filepath.Join(source, relPath)) if err1 != nil { return err1 } - return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0777) + return os.WriteFile(filepath.Join(destination, relPath), data, 0777) } else { return nil } diff --git a/domain/resource/s3_manager.go b/domain/resource/s3_manager.go index 7d3e2d1..af16aaa 100644 --- a/domain/resource/s3_manager.go +++ b/domain/resource/s3_manager.go @@ -66,22 +66,22 @@ func (a *awsManager) Upload(request UploadRequest) { a.addToCurrentlyUploading(request.Path) a.requests <- request } -func (a *awsManager) Presign(ctx context.Context, path string) (string, error) { +func (a *awsManager) Presign(_ context.Context, path string) (string, error) { if !a.waitForUploadComplete(path) { return "", errors.New("failed upload") } // ask cache for latest url - if url, err := a.cache.Get(path); err == nil { - return url, nil + if uri, err := a.cache.Get(path); err == nil { + return uri, nil } // if there is no value in cache, presign url and add it to cache - url, err := a.presign(path, a.expiration) + uri, err := a.presign(path, a.expiration) if err != nil { - a.cache.Set(path, url, a.expiration) + _ = a.cache.Set(path, uri, a.expiration) } - return url, err + return uri, err } -func (a *awsManager) Download(ctx context.Context, path string) (file []byte, err error) { +func (a *awsManager) Download(_ context.Context, path string) (file []byte, err error) { return a.download(path) } func (a *awsManager) Copy(from string, to string, overwrite bool) error { diff --git a/security/generator.go b/security/generator.go index b57b27c..22279a7 100644 --- a/security/generator.go +++ b/security/generator.go @@ -11,6 +11,14 @@ type PresignToken struct { Path string } +type Type string + +const ( + TypeBasic Type = "basic" + TypeApi Type = "api" + TypeToken Type = "token" +) + func CreateToken(tokenInfo PresignToken, duration time.Duration) (string, error) { // jwt token atClaims := jwt.MapClaims{}