Skip to content

Commit 710311b

Browse files
committed
refactor: improve secrets and retry lint
1 parent 119b864 commit 710311b

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

cmd/inventory/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424

2525
// Load database configuration from secrets
2626
dbConfig := secretHelper.LoadDatabaseConfig()
27-
dsn, err := secretHelper.BuildDSN(dbConfig)
27+
dsn, err := secrets.BuildDSN(dbConfig)
2828
if err != nil {
2929
log.Fatalf("inventory dsn: %v", err)
3030
}

cmd/migrate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626

2727
// Load database configuration
2828
dbConfig := secretHelper.LoadDatabaseConfig()
29-
databaseURL, err := secretHelper.BuildDSN(dbConfig)
29+
databaseURL, err := secrets.BuildDSN(dbConfig)
3030
if err != nil {
3131
log.Fatalf("migrate dsn: %v", err)
3232
}

pkg/retry/retry.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@ type Config struct {
1717
MaxAttempts int
1818
}
1919

20+
const (
21+
defaultMultiplier = 1.5
22+
defaultInitialInterval = 500 * time.Millisecond
23+
defaultMaxInterval = 30 * time.Second
24+
minMultiplier = 0.1
25+
minInterval = 10 * time.Millisecond
26+
)
27+
2028
func (c *Config) backoff() backoff.BackOff {
2129
bo := backoff.NewExponentialBackOff()
22-
if c.InitialInterval > 0 {
30+
if c.InitialInterval >= minInterval {
2331
bo.InitialInterval = c.InitialInterval
32+
} else {
33+
bo.InitialInterval = defaultInitialInterval
2434
}
25-
if c.MaxInterval > 0 {
35+
if c.MaxInterval >= minInterval {
2636
bo.MaxInterval = c.MaxInterval
37+
} else {
38+
bo.MaxInterval = defaultMaxInterval
2739
}
28-
if c.Multiplier > 0 {
40+
if c.Multiplier >= minMultiplier {
2941
bo.Multiplier = c.Multiplier
42+
} else {
43+
bo.Multiplier = defaultMultiplier
3044
}
3145
if c.MaxElapsedTime > 0 {
3246
bo.MaxElapsedTime = c.MaxElapsedTime

pkg/secrets/azure.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func createAzureCredential(cfg Config) (azcore.TokenCredential, error) {
7373

7474
// GetSecret retrieves a secret from Azure Key Vault
7575
func (m *AzureManager) GetSecret(ctx context.Context, key string) (string, error) {
76-
secretName := m.normalizeSecretName(key)
76+
secretName := normalizeSecretName(key)
7777

7878
// Get the latest version of the secret
7979
resp, err := m.client.GetSecret(ctx, secretName, emptyString, nil)
@@ -105,7 +105,7 @@ func (m *AzureManager) GetSecrets(ctx context.Context, keys []string) (map[strin
105105

106106
// SetSecret stores a secret in Azure Key Vault
107107
func (m *AzureManager) SetSecret(ctx context.Context, key, value string) error {
108-
secretName := m.normalizeSecretName(key)
108+
secretName := normalizeSecretName(key)
109109

110110
parameters := azsecrets.SetSecretParameters{
111111
Value: &value,
@@ -121,7 +121,7 @@ func (m *AzureManager) SetSecret(ctx context.Context, key, value string) error {
121121

122122
// normalizeSecretName ensures the secret name follows Azure Key Vault naming rules
123123
// Secret names can only contain alphanumeric characters and dashes
124-
func (m *AzureManager) normalizeSecretName(name string) string {
124+
func normalizeSecretName(name string) string {
125125
// Replace invalid characters with dashes
126126
normalized := strings.Map(func(r rune) rune {
127127
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == dashRune {

pkg/secrets/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (h *Helper) LoadAPIKeys() map[string]string {
129129
}
130130

131131
// BuildDSN builds a database connection string from secret values
132-
func (h *Helper) BuildDSN(dbConfig map[string]string) (string, error) {
132+
func BuildDSN(dbConfig map[string]string) (string, error) {
133133
password, ok := dbConfig["POSTGRES_PASSWORD"]
134134
if !ok || password == "" {
135135
return "", fmt.Errorf("POSTGRES_PASSWORD is required")

pkg/secrets/vault.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (m *VaultManager) GetSecrets(ctx context.Context, keys []string) (map[strin
169169
}
170170

171171
// SetSecret stores a secret in Vault
172-
func (m *VaultManager) SetSecret(ctx context.Context, key, value string) error {
172+
func (m *VaultManager) SetSecret(_ context.Context, key, value string) error {
173173
secretPath := m.buildSecretPath(key)
174174

175175
data := map[string]interface{}{

0 commit comments

Comments
 (0)