Skip to content

Commit

Permalink
refactor: reworked salt mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
graza-io committed Feb 9, 2024
1 parent c27e88e commit 73dec50
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
6 changes: 5 additions & 1 deletion internal/cmdconfig/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmdconfig

import (
"github.com/turbot/flowpipe/internal/cache"
"maps"
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -105,10 +107,12 @@ func ensureGlobalSalt() error {
}

saltFileFullPath := filepath.Join(saltDir, "salt")
_, err = util.CreateFlowpipeSalt(saltFileFullPath, 32)
salt, err := util.CreateFlowpipeSalt(saltFileFullPath, 32)
if err != nil {
return err
}

cache.GetCache().SetWithTTL("salt", salt, 24*7*52*99*time.Hour)

return nil
}
21 changes: 15 additions & 6 deletions internal/service/api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (api *APIService) runTriggerHook(c *gin.Context) {
return
}

salt, ok := cache.GetCache().Get("salt")
if !ok {
salt, err := util.GetModSaltOrDefault()
if err != nil {
common.AbortWithError(c, perr.InternalWithMessage("salt not found"))
return
}
Expand All @@ -138,7 +138,7 @@ func (api *APIService) runTriggerHook(c *gin.Context) {
return
}

hashString := util.CalculateHash(webhookTriggerName, salt.(string))
hashString := util.CalculateHash(webhookTriggerName, salt)

if hashString != webhookTriggerHash {
common.AbortWithError(c, perr.UnauthorizedWithMessage("invalid hash for webhook "+webhookTriggerName))
Expand Down Expand Up @@ -336,12 +336,21 @@ func (api *APIService) runIntegrationHook(c *gin.Context) {
return
}

// TODO: validate correct hash

// determine integration type: integration.slack.example -> slack
nameParts := strings.Split(webhookUri.Hook, ".")
integrationType := nameParts[1]
// integrationName := nameParts[2]
integrationName := nameParts[2]

salt, err := util.GetGlobalSalt()
if err != nil {
common.AbortWithError(c, perr.InternalWithMessage("salt not found"))
return
}
hashString := util.CalculateHash(webhookUri.Hook, salt)
if hashString != webhookUri.Hash {
common.AbortWithError(c, perr.UnauthorizedWithMessage("invalid hash for integration "+integrationName))
return
}

bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions internal/service/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager
import (
"context"
"fmt"
"github.com/turbot/go-kit/files"
"log/slog"
"os"
"os/signal"
Expand Down Expand Up @@ -168,15 +169,16 @@ func (m *Manager) initializeModDirectory() error {
}

internalDir := filepaths.ModInternalDir()
err = util.EnsureDir(internalDir)
if err != nil {
return err
}

saltFileFullPath := filepath.Join(internalDir, "salt")
salt, err := util.CreateFlowpipeSalt(saltFileFullPath, 32)
if err != nil {
return err
modSaltPath := filepath.Join(internalDir, "salt")
if files.DirectoryExists(internalDir) && files.FileExists(modSaltPath) {
saltBytes, err := os.ReadFile(modSaltPath)
if err != nil {
return err
}
modSalt := string(saltBytes)
if modSalt != "" {
cache.GetCache().SetWithTTL("mod_salt", modSalt, 24*7*52*99*time.Hour)
}
}

err = store.InitializeFlowpipeDB()
Expand All @@ -187,8 +189,6 @@ func (m *Manager) initializeModDirectory() error {
// Force cleanup if it hasn't run for 1 day
store.ForceCleanup()

cache.GetCache().SetWithTTL("salt", salt, 24*7*52*99*time.Hour)

return nil
}

Expand Down
30 changes: 30 additions & 0 deletions internal/util/salt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package util
import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/turbot/flowpipe/internal/cache"
"github.com/turbot/flowpipe/internal/filepaths"
"log/slog"
"os"
"path/filepath"
)

// Assumes that the dir exists
Expand Down Expand Up @@ -42,3 +46,29 @@ func CreateFlowpipeSalt(filename string, length int) (string, error) {

return saltHex, nil
}

func GetModSaltOrDefault() (string, error) {
c := cache.GetCache()
if ms, exists := c.Get("mod_salt"); exists {
if modSalt, ok := ms.(string); ok {
return modSalt, nil
} else {
return modSalt, fmt.Errorf("mod specific salt not a string")
}
}

return GetGlobalSalt()
}

func GetGlobalSalt() (string, error) {
c := cache.GetCache()
if s, exists := c.Get("salt"); exists {
if salt, ok := s.(string); ok {
return salt, nil
} else {
return salt, fmt.Errorf("salt not a string")
}
}
globalSaltPath := filepath.Join(filepaths.GlobalInternalDir(), "salt")
return CreateFlowpipeSalt(globalSaltPath, 32)
}

0 comments on commit 73dec50

Please sign in to comment.