The Great Linting Fixes, Drone configuration fix (again) and flatfile changes.

Great linting fixes has been applied, thanks to golangci-lint for
extensive reporting.

Fixed Drone configuration to use array for when-branch statement in
Docker plugin.

Flatfile storage from now will write files with 0600 permission for
greater security.
This commit is contained in:
2021-11-20 22:19:58 +05:00
parent 218e0bf667
commit 2b44a60ee7
14 changed files with 154 additions and 156 deletions

View File

@@ -31,16 +31,16 @@ func (c *Context) initializeHTTPServer() {
// Wrapper around previous function.
func (c *Context) echoReqLogger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error {
return func(ectx echo.Context) error {
c.Logger.Info().
Str("IP", ec.RealIP()).
Str("Host", ec.Request().Host).
Str("Method", ec.Request().Method).
Str("Path", ec.Request().URL.Path).
Str("UA", ec.Request().UserAgent()).
Str("IP", ectx.RealIP()).
Str("Host", ectx.Request().Host).
Str("Method", ectx.Request().Method).
Str("Path", ectx.Request().URL.Path).
Str("UA", ectx.Request().UserAgent()).
Msg("HTTP request")
return next(ec)
return next(ectx)
}
}
}

View File

@@ -11,14 +11,14 @@ import (
)
// Puts memory usage into log lines.
func (c *Context) getMemoryUsage(e *zerolog.Event, level zerolog.Level, message string) {
func (c *Context) getMemoryUsage(event *zerolog.Event, level zerolog.Level, message string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
e.Str("memalloc", fmt.Sprintf("%dMB", m.Alloc/1024/1024))
e.Str("memsys", fmt.Sprintf("%dMB", m.Sys/1024/1024))
e.Str("numgc", fmt.Sprintf("%d", m.NumGC))
event.Str("memalloc", fmt.Sprintf("%dMB", m.Alloc/1024/1024))
event.Str("memsys", fmt.Sprintf("%dMB", m.Sys/1024/1024))
event.Str("numgc", fmt.Sprintf("%d", m.NumGC))
}
// Initializes logger.
@@ -26,26 +26,26 @@ func (c *Context) initializeLogger() {
// Устанавливаем форматирование логгера.
// nolint:exhaustivestruct
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
output.FormatLevel = func(lvlRaw interface{}) string {
var v string
if ii, ok := i.(string); ok {
ii = strings.ToUpper(ii)
switch ii {
if lvl, ok := lvlRaw.(string); ok {
lvl = strings.ToUpper(lvl)
switch lvl {
case "DEBUG":
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
case "ERROR":
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
case "FATAL":
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
case "INFO":
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
case "PANIC":
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
case "WARN":
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", ii)
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
default:
v = ii
v = lvl
}
}

View File

@@ -250,16 +250,16 @@ func (ff *FlatFiles) Initialize() {
}
}
func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
func (ff *FlatFiles) SavePaste(paste *structs.Paste) (int64, error) {
ff.writeMutex.Lock()
// Write paste data on disk.
filesOnDisk, _ := ioutil.ReadDir(filepath.Join(ff.path, "pastes"))
pasteID := len(filesOnDisk) + 1
p.ID = pasteID
paste.ID = pasteID
c.Logger.Debug().Int("new paste ID", pasteID).Msg("Writing paste to disk")
data, err := json.Marshal(p)
data, err := json.Marshal(paste)
if err != nil {
ff.writeMutex.Unlock()
@@ -267,8 +267,7 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
return 0, err
}
// nolint:gosec
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0o600)
if err != nil {
ff.writeMutex.Unlock()
@@ -280,7 +279,7 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
// nolint:exhaustivestruct
indexData := Index{}
indexData.ID = pasteID
indexData.Private = p.Private
indexData.Private = paste.Private
ff.pastesIndex = append(ff.pastesIndex, indexData)
ff.writeMutex.Unlock()
@@ -297,8 +296,7 @@ func (ff *FlatFiles) Shutdown() {
return
}
// nolint:gosec
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0o600)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")

View File

@@ -28,14 +28,14 @@ import (
"database/sql"
)
func PasswordedPastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` ADD `password` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password for paste (scrypted and sha256ed).'")
func PasswordedPastesUp(txn *sql.Tx) error {
_, err := txn.Exec("ALTER TABLE `pastes` ADD `password` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password for paste (scrypted and sha256ed).'")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE `pastes` ADD `password_salt` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password salt (sha256ed).'")
_, err1 := txn.Exec("ALTER TABLE `pastes` ADD `password_salt` varchar(64) NOT NULL DEFAULT '' COMMENT 'Password salt (sha256ed).'")
if err1 != nil {
// nolint:wrapcheck
return err1
@@ -44,14 +44,14 @@ func PasswordedPastesUp(tx *sql.Tx) error {
return nil
}
func PasswordedPastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `password`")
func PasswordedPastesDown(txn *sql.Tx) error {
_, err := txn.Exec("ALTER TABLE `pastes` DROP COLUMN `password`")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `password_salt`")
_, err1 := txn.Exec("ALTER TABLE `pastes` DROP COLUMN `password_salt`")
if err1 != nil {
// nolint:wrapcheck
return err1

View File

@@ -81,15 +81,15 @@ func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
// nolint:exhaustivestruct
p := &structs.Paste{}
paste := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM `pastes` WHERE id=?"), pasteID)
err := db.db.Get(paste, db.db.Rebind("SELECT * FROM `pastes` WHERE id=?"), pasteID)
if err != nil {
// nolint:wrapcheck
return nil, err
}
return p, nil
return paste, nil
}
func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
@@ -195,13 +195,13 @@ func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
return 0, err
}
ID, err1 := result.LastInsertId()
lastInsertID, err1 := result.LastInsertId()
if err1 != nil {
// nolint:wrapcheck
return 0, err
}
return ID, nil
return lastInsertID, nil
}
func (db *Database) Shutdown() {

View File

@@ -28,14 +28,14 @@ import (
"database/sql"
)
func PasswordedPastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes ADD COLUMN password VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password IS 'Password for paste (scrypted and sha256ed).';")
func PasswordedPastesUp(txn *sql.Tx) error {
_, err := txn.Exec("ALTER TABLE pastes ADD COLUMN password VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password IS 'Password for paste (scrypted and sha256ed).';")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE pastes ADD COLUMN password_salt VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password_salt IS 'Password salt (sha256ed).';")
_, err1 := txn.Exec("ALTER TABLE pastes ADD COLUMN password_salt VARCHAR(64) NOT NULL DEFAULT ''; COMMENT ON COLUMN pastes.password_salt IS 'Password salt (sha256ed).';")
if err1 != nil {
// nolint:wrapcheck
return err1
@@ -44,14 +44,14 @@ func PasswordedPastesUp(tx *sql.Tx) error {
return nil
}
func PasswordedPastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE pastes DROP COLUMN password")
func PasswordedPastesDown(txn *sql.Tx) error {
_, err := txn.Exec("ALTER TABLE pastes DROP COLUMN password")
if err != nil {
// nolint:wrapcheck
return err
}
_, err1 := tx.Exec("ALTER TABLE pastes DROP COLUMN password_salt")
_, err1 := txn.Exec("ALTER TABLE pastes DROP COLUMN password_salt")
if err1 != nil {
// nolint:wrapcheck
return err1

View File

@@ -84,9 +84,9 @@ func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
// nolint:exhaustivestruct
p := &structs.Paste{}
paste := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM pastes WHERE id=$1"), pasteID)
err := db.db.Get(paste, db.db.Rebind("SELECT * FROM pastes WHERE id=$1"), pasteID)
if err != nil {
// nolint:wrapcheck
return nil, err
@@ -96,10 +96,10 @@ func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
// timestamps in server's local timezone. We should convert them.
loc, _ := time.LoadLocation("UTC")
utcCreatedAt := p.CreatedAt.In(loc)
p.CreatedAt = &utcCreatedAt
utcCreatedAt := paste.CreatedAt.In(loc)
paste.CreatedAt = &utcCreatedAt
return p, nil
return paste, nil
}
func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
@@ -197,7 +197,7 @@ func (db *Database) Initialize() {
migrations.Migrate()
}
func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
func (db *Database) SavePaste(paste *structs.Paste) (int64, error) {
db.check()
stmt, err := db.db.PrepareNamed("INSERT INTO pastes (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt) RETURNING id")
@@ -208,7 +208,7 @@ func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
var id int64
err = stmt.Get(&id, p)
err = stmt.Get(&id, paste)
if err != nil {
// nolint:wrapcheck
return 0, err

View File

@@ -50,11 +50,11 @@ func GetErrorTemplate(ec echo.Context, errorText string) string {
}
// GetRawTemplate returns only raw template data.
func GetRawTemplate(ec echo.Context, templateName string, data map[string]string) string {
func GetRawTemplate(ectx echo.Context, templateName string, data map[string]string) string {
// Getting main template.
tplRaw, err := assets.Data.ReadFile(templateName)
if err != nil {
_ = ec.String(http.StatusBadRequest, templateName+" not found.")
_ = ectx.String(http.StatusBadRequest, templateName+" not found.")
return ""
}
@@ -69,13 +69,13 @@ func GetRawTemplate(ec echo.Context, templateName string, data map[string]string
}
// GetTemplate returns formatted template that can be outputted to client.
func GetTemplate(ec echo.Context, name string, data map[string]string) string {
func GetTemplate(ectx echo.Context, name string, data map[string]string) string {
log.Debug().Str("name", name).Msg("Requested template")
// Getting main template.
mainhtml, err := assets.Data.ReadFile("main.html")
if err != nil {
_ = ec.String(http.StatusBadRequest, "main.html not found.")
_ = ectx.String(http.StatusBadRequest, "main.html not found.")
return ""
}
@@ -83,7 +83,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
// Getting navigation.
navhtml, err1 := assets.Data.ReadFile("navigation.html")
if err1 != nil {
_ = ec.String(http.StatusBadRequest, "navigation.html not found.")
_ = ectx.String(http.StatusBadRequest, "navigation.html not found.")
return ""
}
@@ -91,7 +91,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
// Getting footer.
footerhtml, err2 := assets.Data.ReadFile("footer.html")
if err2 != nil {
_ = ec.String(http.StatusBadRequest, "footer.html not found.")
_ = ectx.String(http.StatusBadRequest, "footer.html not found.")
return ""
}
@@ -105,7 +105,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
// Get requested template.
reqhtml, err3 := assets.Data.ReadFile(name)
if err3 != nil {
_ = ec.String(http.StatusBadRequest, name+" not found.")
_ = ectx.String(http.StatusBadRequest, name+" not found.")
return ""
}