This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
/
health.go
47 lines (37 loc) · 1.65 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package models
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
type HealthConfig struct {
Port int `yaml:"port"`
HealthCheckUsername string `yaml:"username"`
HealthCheckUsernameHash string `yaml:"username_hash"`
HealthCheckPassword string `yaml:"password"`
HealthCheckPasswordHash string `yaml:"password_hash"`
}
func (c *HealthConfig) Validate(component string) error {
if c.HealthCheckUsername != "" && c.HealthCheckUsernameHash != "" {
return fmt.Errorf("Configuration error: both %s healthcheck username and %s healthcheck username_hash are set, please provide only one of them", component, component)
}
if c.HealthCheckPassword != "" && c.HealthCheckPasswordHash != "" {
return fmt.Errorf("Configuration error: both %s healthcheck password and %s healthcheck password_hash are empty, please provide one of them", component, component)
}
if c.HealthCheckUsernameHash != "" {
if _, err := bcrypt.Cost([]byte(c.HealthCheckUsernameHash)); err != nil {
return fmt.Errorf("Configuration error: %s healthcheck username_hash is not a valid bcrypt hash", component)
}
}
if c.HealthCheckPasswordHash != "" {
if _, err := bcrypt.Cost([]byte(c.HealthCheckPasswordHash)); err != nil {
return fmt.Errorf("Configuration error: %s healthcheck password_hash is not a valid bcrypt hash", component)
}
}
if c.HealthCheckUsername == "" && c.HealthCheckPassword != "" {
return fmt.Errorf("Configuration error: %s healthcheck username is empty", component)
}
if c.HealthCheckUsername != "" && c.HealthCheckPassword == "" {
return fmt.Errorf("Configuration error: %s healthcheck password is empty", component)
}
return nil
}