Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode/
dist/
*.boltdb
tmp/
tmp/
/vendor/
67 changes: 61 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,79 @@
package app

import (
"github.com/typomedia/patchouli/app/store/boltdb"
"github.com/typomedia/patchouli/app/structs"
)

var app *Application

type Application struct {
Name string
Version string
Author string
Description string
MailTemplate string
Name string
Version string
Author string
Description string
MailTemplate string
NotifyTemplate string
Config structs.Config
}

func new() *Application {
app = &Application{}
app.Name = "Patchouli"
app.Version = "0.2.0"
app.Version = "0.2.1"
app.Author = "Philipp Speck <philipp@typo.media>"
app.Description = "Patch Management Planner"
err := app.LoadConfig()
if err != nil {
panic(err)
}
if app.Config.Security.Generated {
err = app.StoreConfig()
if err != nil {
panic(err)
}
}

return app
}

func (app *Application) StoreConfig() error {
db := boltdb.New()
defer db.Close()

err := db.SetBucket("config")
if err != nil {
return err
}

err = db.Set("main", app.Config, "config")
if err != nil {
return err
}

return nil
}

func (app *Application) LoadConfig() error {
db := boltdb.New()
defer db.Close()

config, err := db.GetConfig()
if err != nil {
return err
}

if !config.Security.Generated {
err = config.GenerateCipherKey()
if err != nil {
return err
}
}

app.Config = config
return nil
}

func GetApp() *Application {
if app == nil {
app = new()
Expand Down
64 changes: 64 additions & 0 deletions app/encryption/encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package encryption

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"github.com/typomedia/patchouli/app"
"io"
)

func EncryptString(stringToEncrypt string) (string, error) {
key, err := app.GetApp().Config.GetCipherKey()
if err != nil {
return "", err
}
aesKey, _ := hex.DecodeString(key)
plaintext := []byte(stringToEncrypt)
block, err := aes.NewCipher(aesKey)
if err != nil {
return "", err
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}

ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext), nil
}

func DecryptString(encString string) (string, error) {
key, err := app.GetApp().Config.GetCipherKey()

aesKey, _ := hex.DecodeString(key)
enc, _ := hex.DecodeString(encString)
block, err := aes.NewCipher(aesKey)
if err != nil {
return "", err
}

aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonceSize := aesGCM.NonceSize()
if len(enc) < nonceSize {
return "", errors.New("ciphertext too short")
}
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}

return string(plaintext), nil
}
24 changes: 5 additions & 19 deletions app/handler/config/edit.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package config

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/typomedia/patchouli/app/store/boltdb"
"github.com/typomedia/patchouli/app/structs"
"golang.org/x/crypto/bcrypt"
"github.com/typomedia/patchouli/app"
)

func Edit(c *fiber.Ctx) error {
db := boltdb.New()

err := db.SetBucket("config")
if err != nil {
return err
}

var config structs.Config
db.Get("main", &config, "config")
if config.Smtp.Password != "" {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(config.Smtp.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
config.Smtp.Password = string(passwordHash)
config := app.GetApp().Config
if config.General.Hostname == "" {
config.General.Hostname = fmt.Sprintf("%s://%s", c.Protocol(), c.Hostname())
}
defer db.Close()
return c.Render("app/views/config/edit", fiber.Map{
"Config": config,
})
Expand Down
15 changes: 15 additions & 0 deletions app/handler/config/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/typomedia/patchouli/app"
"github.com/typomedia/patchouli/app/encryption"
"github.com/typomedia/patchouli/app/store/boltdb"
"github.com/typomedia/patchouli/app/structs"
)
Expand All @@ -20,8 +22,21 @@ func Save(c *fiber.Ctx) error {
log.Error(err)
}

// update config from body with security params from app
config.Security = app.GetApp().Config.Security

_, err = encryption.DecryptString(config.Smtp.Password)
if err != nil { // config.Smtp.Password is not encrypted, encrypt it
config.Smtp.Password, err = encryption.EncryptString(config.Smtp.Password)
if err != nil {
log.Error(err)
}
}

db.Set("main", config, "config")

app.GetApp().Config = config

defer db.Close()

return c.Redirect("/config")
Expand Down
79 changes: 8 additions & 71 deletions app/handler/dashboard/filter/operator.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,29 @@
package filter

import (
"encoding/json"
"sort"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/typomedia/patchouli/app/helper"
"github.com/typomedia/patchouli/app/store/boltdb"
"github.com/typomedia/patchouli/app/structs"
)

func Operator(c *fiber.Ctx) error {
id := c.Params("id")
operatorId := c.Params("id")
db := boltdb.New()
defer db.Close()

err := db.SetBucket("machine")
machines, err := db.GetActiveMachines()
if err != nil {
return err
}

machines, _ := db.GetAllByOperatorId(id, "machine")

Machines := structs.Machines{}
for _, v := range machines {
machine := structs.Machine{}
err = json.Unmarshal(v, &machine)
if err != nil {
return err
var operatorMachines structs.Machines
for _, m := range machines {
if m.Operator.Id == operatorId {
operatorMachines = append(operatorMachines, m)
}

lastUpdate, _ := db.GetLastByName(machine.Id, "history")

update := structs.Update{}
err = json.Unmarshal(lastUpdate, &update)
if err != nil {
log.Error(err)
}

machine.Update = update

Machines = append(Machines, machine)

}

// sort machines by oldest update first
sort.Sort(structs.ByDate(Machines))

err = db.SetBucket("config")
if err != nil {
return err
}

var config structs.Config
db.Get("main", &config, "config")

defer db.Close()

interval := config.General.Interval
for i := range Machines {
currentDate := time.Now()

if Machines[i].Update.Date == "" {
Machines[i].Update.Date = "0000-00-00"
Machines[i].Status = "danger"
continue
}

Machines[i].Update.Date = helper.UnixToDateString(Machines[i].Update.Date)

date, err := time.Parse("2006-01-02", Machines[i].Update.Date)
if err != nil {
log.Error(err)
}

Machines[i].Days = int(currentDate.Sub(date).Hours() / 24)
if Machines[i].Days > interval {
Machines[i].Status = "danger"
} else if Machines[i].Days > interval/3 {
Machines[i].Status = "warning"
} else {
Machines[i].Status = "success"
}
Machines[i].Days = interval - int(currentDate.Sub(date).Hours()/24)

}
return c.Render("app/views/dashboard/list", fiber.Map{
"Machines": Machines,
"Machines": operatorMachines,
})
}
Loading