Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* RedisConfig -> Redis * moved redis config to seperate file * bugfix in config test during parallel processing * implement config.Configurable in Redis config * use Context in GetRedisCache * use Context in New * caching resolver test fix * use Context in PublishEnabled * use Context in getResponse * remove ctx field * bugfix in api interface test * propperly close channels * set ruler for go files from 80 to 111 * line break because function length is to long * only execute redis.New if it is enabled in config * stabilized flaky tests * Update config/redis.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * Update config/redis_test.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * Update config/redis_test.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * Update config/redis_test.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * Update config/redis.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * Update config/redis_test.go Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com> * fix ruler * redis test refactoring * vscode setting cleanup * removed else if chain * Update redis_test.go * context race fix * test fail on missing seintinel servers * cleanup context usage * cleanup2 * context fixes * added context util * disabled nil context rule for tests * copy paste error ctxSend -> CtxSend * use util.CtxSend * fixed comment * fixed flaky test * failsafe and tests --------- Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com>
- Loading branch information
1 parent
15bd383
commit fda2dbe
Showing
20 changed files
with
571 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,3 +98,7 @@ issues: | |
- gochecknoinits | ||
- gochecknoglobals | ||
- gosec | ||
- path: _test\.go | ||
linters: | ||
- staticcheck | ||
text: "SA1012:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Redis configuration for the redis connection | ||
type Redis struct { | ||
Address string `yaml:"address"` | ||
Username string `yaml:"username" default:""` | ||
Password string `yaml:"password" default:""` | ||
Database int `yaml:"database" default:"0"` | ||
Required bool `yaml:"required" default:"false"` | ||
ConnectionAttempts int `yaml:"connectionAttempts" default:"3"` | ||
ConnectionCooldown Duration `yaml:"connectionCooldown" default:"1s"` | ||
SentinelUsername string `yaml:"sentinelUsername" default:""` | ||
SentinelPassword string `yaml:"sentinelPassword" default:""` | ||
SentinelAddresses []string `yaml:"sentinelAddresses"` | ||
} | ||
|
||
// IsEnabled implements `config.Configurable` | ||
func (c *Redis) IsEnabled() bool { | ||
return c.Address != "" | ||
} | ||
|
||
// LogConfig implements `config.Configurable` | ||
func (c *Redis) LogConfig(logger *logrus.Entry) { | ||
if len(c.SentinelAddresses) == 0 { | ||
logger.Info("address: ", c.Address) | ||
} | ||
|
||
logger.Info("username: ", c.Username) | ||
logger.Info("password: ", obfuscatePassword(c.Password)) | ||
logger.Info("database: ", c.Database) | ||
logger.Info("required: ", c.Required) | ||
logger.Info("connectionAttempts: ", c.ConnectionAttempts) | ||
logger.Info("connectionCooldown: ", c.ConnectionCooldown) | ||
|
||
if len(c.SentinelAddresses) > 0 { | ||
logger.Info("sentinel:") | ||
logger.Info(" master: ", c.Address) | ||
logger.Info(" username: ", c.SentinelUsername) | ||
logger.Info(" password: ", obfuscatePassword(c.SentinelPassword)) | ||
logger.Info(" addresses:") | ||
|
||
for _, addr := range c.SentinelAddresses { | ||
logger.Info(" - ", addr) | ||
} | ||
} | ||
} | ||
|
||
// obfuscatePassword replaces all characters of a password except the first and last with * | ||
func obfuscatePassword(pass string) string { | ||
return strings.Repeat("*", len(pass)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/0xERR0R/blocky/log" | ||
"github.com/creasty/defaults" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Redis", func() { | ||
var ( | ||
c Redis | ||
err error | ||
) | ||
|
||
suiteBeforeEach() | ||
|
||
BeforeEach(func() { | ||
err = defaults.Set(&c) | ||
Expect(err).Should(Succeed()) | ||
}) | ||
|
||
Describe("IsEnabled", func() { | ||
When("all fields are default", func() { | ||
It("should be disabled", func() { | ||
Expect(c.IsEnabled()).Should(BeFalse()) | ||
}) | ||
}) | ||
|
||
When("Address is set", func() { | ||
BeforeEach(func() { | ||
c.Address = "localhost:6379" | ||
}) | ||
|
||
It("should be enabled", func() { | ||
Expect(c.IsEnabled()).Should(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("LogConfig", func() { | ||
BeforeEach(func() { | ||
logger, hook = log.NewMockEntry() | ||
}) | ||
|
||
When("all fields are default", func() { | ||
It("should log default values", func() { | ||
c.LogConfig(logger) | ||
|
||
Expect(hook.Messages).Should( | ||
SatisfyAll(ContainElement(ContainSubstring("address: ")), | ||
ContainElement(ContainSubstring("username: ")), | ||
ContainElement(ContainSubstring("password: ")), | ||
ContainElement(ContainSubstring("database: ")), | ||
ContainElement(ContainSubstring("required: ")), | ||
ContainElement(ContainSubstring("connectionAttempts: ")), | ||
ContainElement(ContainSubstring("connectionCooldown: ")))) | ||
}) | ||
}) | ||
|
||
When("Address is set", func() { | ||
BeforeEach(func() { | ||
c.Address = "localhost:6379" | ||
}) | ||
|
||
It("should log address", func() { | ||
c.LogConfig(logger) | ||
|
||
Expect(hook.Messages).Should(ContainElement(ContainSubstring("address: localhost:6379"))) | ||
}) | ||
}) | ||
|
||
When("SentinelAddresses is set", func() { | ||
BeforeEach(func() { | ||
c.SentinelAddresses = []string{"localhost:26379", "localhost:26380"} | ||
}) | ||
|
||
It("should log sentinel addresses", func() { | ||
c.LogConfig(logger) | ||
|
||
Expect(hook.Messages).Should( | ||
SatisfyAll( | ||
ContainElement(ContainSubstring("sentinel:")), | ||
ContainElement(ContainSubstring(" addresses:")), | ||
ContainElement(ContainSubstring(" - localhost:26379")), | ||
ContainElement(ContainSubstring(" - localhost:26380")))) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("obfuscatePassword", func() { | ||
When("password is empty", func() { | ||
It("should return empty string", func() { | ||
Expect(obfuscatePassword("")).Should(Equal("")) | ||
}) | ||
}) | ||
|
||
When("password is not empty", func() { | ||
It("should return obfuscated password", func() { | ||
Expect(obfuscatePassword("test123")).Should(Equal("*******")) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.