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

feat: CacheDir is configurable #18

Merged
merged 1 commit into from
Oct 3, 2023
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
6 changes: 4 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
)

const (
CacheDir = "/var/lib/yara-rule-server"
RulePath = "/var/lib/yara-rule-server/compiled"
CompiledRuleFileName = "compiled"
TempDirName = "tmp"
)

type Config struct {
Expand All @@ -33,6 +33,7 @@ type Config struct {
RuleUpdateSchedule string `mapstructure:"rule_update_schedule"`
ServerAddress string `mapstructure:"server_address"`
HealthCheckAddressAddress string `mapstructure:"health_check_address"`
CacheDir string `mapstructure:"cache_dir"`
}

type RuleSource struct {
Expand Down Expand Up @@ -71,4 +72,5 @@ func setDefaults() {
viper.SetDefault("rule_update_schedule", "0 0 * * *")
viper.SetDefault("server_address", ":8080")
viper.SetDefault("health_check_address", ":8082")
viper.SetDefault("cache_dir", "/var/lib/yara-rule-server")
}
6 changes: 4 additions & 2 deletions pkg/fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package fileserver
import (
"errors"
"net/http"
"path"

"github.com/sirupsen/logrus"

"github.com/openclarity/yara-rule-server/pkg/config"
)

func Start(cfg *config.Config, logger *logrus.Entry) *http.Server {
logger.Infof("Starting file server. Rule file: %s", config.RulePath)
compiledRulePath := path.Join(cfg.CacheDir, config.CompiledRuleFileName)
logger.Infof("Starting file server. Rule file: %s", compiledRulePath)
sFile := func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, config.RulePath)
http.ServeFile(w, req, compiledRulePath)
}

http.HandleFunc("/", sFile)
Expand Down
8 changes: 5 additions & 3 deletions pkg/rules/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -56,7 +57,7 @@ func createYarFileListToIndex(sourceDir string, reg *regexp.Regexp) ([]string, e
return yarFilesToIndex, err
}

func generateIndexAndCompile(yaracPATH string, yarFilesToIndex []string, tempDir string) error {
func generateIndexAndCompile(cfg *config.Config, yarFilesToIndex []string, tempDir string) error {
// Write index file so that we can pass it to yarac
tmpIndexFile, err := os.CreateTemp(tempDir, "index")
if err != nil {
Expand All @@ -73,7 +74,7 @@ func generateIndexAndCompile(yaracPATH string, yarFilesToIndex []string, tempDir
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
}
err = compile(yaracPATH, tmpIndexFile.Name(), tmpCompiledFile.Name())
err = compile(cfg.YaracPath, tmpIndexFile.Name(), tmpCompiledFile.Name())
if err != nil {
return fmt.Errorf("failed to compile %s: %v", tmpIndexFile.Name(), err)
}
Expand All @@ -83,7 +84,8 @@ func generateIndexAndCompile(yaracPATH string, yarFilesToIndex []string, tempDir

// Now we have the compile rules atomically move
// it into the location to be served by the http server.
if err := os.Rename(tmpCompiledFile.Name(), config.RulePath); err != nil {
compiledRulePath := path.Join(cfg.CacheDir, config.CompiledRuleFileName)
if err := os.Rename(tmpCompiledFile.Name(), compiledRulePath); err != nil {
return fmt.Errorf("failed to move compiled yara rules: %v", err)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import (
func DownloadAndCompile(cfg *config.Config, logger *logrus.Entry) error {
// First try to download new copies of all the sources
yarFilesToIndex := make([]string, 0)
tempDir := path.Join(config.CacheDir, "tmp")
tempDir := path.Join(cfg.CacheDir, config.TempDirName)
if err := os.MkdirAll(tempDir, 0755); err != nil {
return fmt.Errorf("failed to create yara rule server temp directory. tempDir=%s: %v", tempDir, err)
}
for _, source := range cfg.RuleSources {
// Create directory for this source if it doesn't exist
sourceDir := path.Join(config.CacheDir, "sources", source.Name)
sourceDir := path.Join(cfg.CacheDir, "sources", source.Name)
if err := os.MkdirAll(sourceDir, 0755); err != nil {
logger.WithError(err).Errorf("Failed to create directory %s. Using the last successful download if available", sourceDir)
continue
Expand Down Expand Up @@ -67,7 +67,7 @@ func DownloadAndCompile(cfg *config.Config, logger *logrus.Entry) error {
yarFilesToIndex = append(yarFilesToIndex, yarFiles...)
}

if err := generateIndexAndCompile(cfg.YaracPath, yarFilesToIndex, tempDir); err != nil {
if err := generateIndexAndCompile(cfg, yarFilesToIndex, tempDir); err != nil {
return fmt.Errorf("failed to create compiled rules: %v", err)
}

Expand Down
Loading