Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Mar 7, 2024
1 parent 1b8b10c commit 2af1d4c
Show file tree
Hide file tree
Showing 30 changed files with 206 additions and 141 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ linters-settings:
min-occurrences: 5
lll:
line-length: 120
funlen:
lines: 80
statements: 45
gomoddirectives:
replace-local: true
govet:
Expand Down
2 changes: 1 addition & 1 deletion pkg/modgearman/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type command struct {
InternalCheck InternalCheck
}

func parseCommand(rawCommand string, config *configurationStruct) *command {
func parseCommand(rawCommand string, config *config) *command {
parsed := &command{
ExecType: Shell,
Command: rawCommand,
Expand Down
16 changes: 8 additions & 8 deletions pkg/modgearman/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestCommandParser_1(t *testing.T) {
cmdLine := "/bin/command"
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "/bin/command", cmd.Command)
assert.Equal(t, []string{}, cmd.Args)
Expand All @@ -17,7 +17,7 @@ func TestCommandParser_1(t *testing.T) {

func TestCommandParser_2(t *testing.T) {
cmdLine := "/bin/command args1"
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "/bin/command", cmd.Command)
assert.Equal(t, []string{"args1"}, cmd.Args)
Expand All @@ -26,7 +26,7 @@ func TestCommandParser_2(t *testing.T) {

func TestCommandParser_3(t *testing.T) {
cmdLine := `ENV1='var' ./bin/command "args 123" "TEST"`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "./bin/command", cmd.Command)
assert.Equal(t, []string{"args 123", "TEST"}, cmd.Args)
Expand All @@ -35,7 +35,7 @@ func TestCommandParser_3(t *testing.T) {

func TestCommandParser_3b(t *testing.T) {
cmdLine := `ENV1='var space' ./bin/command "args 123" "TEST"`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "./bin/command", cmd.Command)
assert.Equal(t, []string{"args 123", "TEST"}, cmd.Args)
Expand All @@ -45,7 +45,7 @@ func TestCommandParser_3b(t *testing.T) {
// unparsable trailing quotes -> shell
func TestCommandParser_4(t *testing.T) {
cmdLine := `ENV1='var' ./bin/command "args 123" "TEST`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Shell, cmd.ExecType)
assert.Equal(t, "/bin/sh", cmd.Command)
assert.Equal(t, []string{"-c", cmdLine}, cmd.Args)
Expand All @@ -54,7 +54,7 @@ func TestCommandParser_4(t *testing.T) {

func TestCommandParser_5(t *testing.T) {
cmdLine := `trap 'echo Booh!' SIGINT SIGTERM; sleep 2`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Shell, cmd.ExecType)
assert.Equal(t, "/bin/sh", cmd.Command)
assert.Equal(t, []string{"-c", cmdLine}, cmd.Args)
Expand All @@ -63,7 +63,7 @@ func TestCommandParser_5(t *testing.T) {

func TestCommandParser_BackslashDouble(t *testing.T) {
cmdLine := `test.sh "\t\n"`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "test.sh", cmd.Command)
assert.Equal(t, []string{`\t\n`}, cmd.Args)
Expand All @@ -72,7 +72,7 @@ func TestCommandParser_BackslashDouble(t *testing.T) {

func TestCommandParser_BackslashSingle(t *testing.T) {
cmdLine := `test.sh '\t\n'`
cmd := parseCommand(cmdLine, &configurationStruct{})
cmd := parseCommand(cmdLine, &config{})
assert.Equal(t, Exec, cmd.ExecType)
assert.Equal(t, "test.sh", cmd.Command)
assert.Equal(t, []string{`\t\n`}, cmd.Args)
Expand Down
19 changes: 12 additions & 7 deletions pkg/modgearman/configuration_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

type configurationStruct struct {
type config struct {
binary string
build string
identifier string
Expand Down Expand Up @@ -79,7 +79,7 @@ type configurationStruct struct {
}

// setDefaultValues sets reasonable defaults
func (config *configurationStruct) setDefaultValues() {
func (config *config) setDefaultValues() {
config.logmode = "automatic"
config.encryption = true
config.showErrorOutput = true
Expand Down Expand Up @@ -118,7 +118,7 @@ func (config *configurationStruct) setDefaultValues() {
}

// removeDuplicates removes duplicate entries from all string lists
func (config *configurationStruct) removeDuplicates() {
func (config *config) removeDuplicates() {
config.server = removeDuplicateStrings(config.server)
config.dupserver = removeDuplicateStrings(config.dupserver)
config.hostgroups = removeDuplicateStrings(config.hostgroups)
Expand All @@ -127,7 +127,7 @@ func (config *configurationStruct) removeDuplicates() {
}

// dump logs all config items
func (config *configurationStruct) dump() {
func (config *config) dump() {
logger.Debugf("binary %s\n", config.binary)
logger.Debugf("build %s\n", config.build)
logger.Debugf("identifier %s\n", config.identifier)
Expand Down Expand Up @@ -182,7 +182,7 @@ func (config *configurationStruct) dump() {
}

// parses the key value pairs and stores them in the configuration struct
func (config *configurationStruct) parseConfigItem(raw string) error {
func (config *config) parseConfigItem(raw string) error {
values := strings.SplitN(raw, "=", 2)
if len(values) <= 1 {
return fmt.Errorf("parse error, expected key=value in %s", raw)
Expand Down Expand Up @@ -348,7 +348,7 @@ func (config *configurationStruct) parseConfigItem(raw string) error {
}

// read settings from file or folder
func (config *configurationStruct) readSettingsPath(path string) error {
func (config *config) readSettingsPath(path string) error {
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("cannot read %s: %s", path, err.Error())
Expand Down Expand Up @@ -376,7 +376,7 @@ func (config *configurationStruct) readSettingsPath(path string) error {

// opens the config file and reads all key value pairs, separated through = and commented out with #
// also reads the config files specified in the config= value
func (config *configurationStruct) readSettingsFile(path string) error {
func (config *config) readSettingsFile(path string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("cannot read file %s: %s", path, err.Error())
Expand Down Expand Up @@ -419,6 +419,7 @@ func getInt(input string) int {
logger.Debugf("Error converting %s to int, try with float", input)
result = int(getFloat(input))
}

return result
}

Expand All @@ -431,13 +432,15 @@ func getFloat(input string) float64 {
logger.Errorf("error Converting %s to float", input)
result = 0
}

return result
}

func getBool(input string) bool {
if input == "yes" || input == "on" || input == "1" {
return true
}

return false
}

Expand All @@ -451,6 +454,7 @@ func fixGearmandServerAddress(address string) string {
if len(parts) == 2 && parts[0] == "" {
return "0.0.0.0:" + parts[1]
}

return address
}

Expand All @@ -464,5 +468,6 @@ func removeDuplicateStrings(elements []string) []string {
uniq = append(uniq, elements[v])
}
}

return uniq
}
4 changes: 2 additions & 2 deletions pkg/modgearman/configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestReadSettingsFile(t *testing.T) {
var testConfig configurationStruct
var testConfig config

// set default values so we can check if they get overwritten
testConfig.setDefaultValues()
Expand Down Expand Up @@ -59,7 +59,7 @@ server=hostname2
}

func TestReadSettingsPath(t *testing.T) {
var testConfig configurationStruct
var testConfig config

// set default values so we can check if they get overwritten
testConfig.setDefaultValues()
Expand Down
5 changes: 5 additions & 0 deletions pkg/modgearman/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ func createCipher(key []byte, encrypt bool) cipher.Block {
if err != nil {
logger.Panic(err)
}

return newCipher
}

return nil
}

Expand All @@ -67,6 +69,7 @@ func createCipher(key []byte, encrypt bool) cipher.Block {
*/
func decodeBase64(data string) []byte {
decodedBase, _ := b64.StdEncoding.DecodeString(data)

return decodedBase
}

Expand Down Expand Up @@ -123,6 +126,7 @@ func parseTimeStringToFloat64(input string) float64 {
if err != nil {
return 0
}

return floatValue
}

Expand All @@ -146,5 +150,6 @@ func createMap(input []byte) map[string]string {
resultMap[access[0]] = access[1]
}
}

return resultMap
}
2 changes: 1 addition & 1 deletion pkg/modgearman/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestCreateMap(t *testing.T) {
}

func TestDecrypt(t *testing.T) {
config := configurationStruct{}
config := config{}
config.encryption = true
config.key = "LaDPjcEqfZuKnUJStXHX27bxkHLAHSbD"
key := getKey(&config)
Expand Down
21 changes: 13 additions & 8 deletions pkg/modgearman/dupserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type dupServerConsumer struct {
queue chan *answer
address string
terminationRequest chan bool
config *configurationStruct
config *config
}

var dupServerConsumers map[string]*dupServerConsumer

func initializeDupServerConsumers(config *configurationStruct) {
func initializeDupServerConsumers(config *config) {
if len(config.dupserver) > 0 {
dupServerConsumers = make(map[string]*dupServerConsumer)
for _, dupAddress := range config.dupserver {
Expand All @@ -42,6 +42,7 @@ func terminateDupServerConsumers() bool {
}
logger.Debugf("Completed all consumer termination")
dupServerConsumers = nil

return true
}

Expand All @@ -56,6 +57,7 @@ func runDupServerConsumer(dupServer *dupServerConsumer) {
if client != nil {
client.Close()
}

return
case item = <-dupServer.queue:
for {
Expand All @@ -65,29 +67,29 @@ func runDupServerConsumer(dupServer *dupServerConsumer) {
logger.Debugf("failed to send back result (to dupserver): %w", err)
select {
case <-dupServer.terminationRequest:
if client != nil {
client.Close()
}
return
default:
time.Sleep(ConnectionRetryInterval * time.Second)

continue
}
}

break
}
}
}
}

func sendResultDup(client *client.Client, item *answer, dupAddress string, config *configurationStruct) (*client.Client, error) {
func sendResultDup(client *client.Client, item *answer, dupAddress string, config *config) (*client.Client, error) {
if config.dupResultsArePassive {
item.active = "passive"
}

return sendAnswer(client, item, dupAddress, config.encryption)
}

func enqueueDupServerResult(config *configurationStruct, result *answer) {
func enqueueDupServerResult(config *config, result *answer) {
if len(config.dupserver) == 0 {
return
}
Expand All @@ -98,7 +100,10 @@ func enqueueDupServerResult(config *configurationStruct, result *answer) {
select {
case channel <- &duplicateResult:
default:
logger.Debugf("channel is at capacity (%d), dropping message (to dupserver): %s", config.dupServerBacklogQueueSize, dupAddress)
logger.Debugf("channel is at capacity (%d), dropping message (to dupserver): %s",
config.dupServerBacklogQueueSize,
dupAddress,
)
}
}
}
2 changes: 2 additions & 0 deletions pkg/modgearman/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EncryptionBlockSize = 16

func createAnswer(value *answer, withEncrypt bool) []byte {
encrypted := encrypt([]byte(value.String()), withEncrypt)

return encodeBase64(encrypted)
}

Expand Down Expand Up @@ -43,5 +44,6 @@ func encrypt(data []byte, encrypt bool) []byte {
*/
func encodeBase64(data []byte) []byte {
encodedBase := b64.StdEncoding.EncodeToString(data)

return []byte(encodedBase)
}
2 changes: 1 addition & 1 deletion pkg/modgearman/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestEncodeBase64(t *testing.T) {
}

func TestEncrypt(t *testing.T) {
config := configurationStruct{}
config := config{}
config.key = "LaDPjcEqfZuKnUJStXHX27bxkHLAHSbD"
config.encryption = true
myCipher = createCipher(getKey(&config), config.encryption)
Expand Down
Loading

0 comments on commit 2af1d4c

Please sign in to comment.