Skip to content

Commit

Permalink
Refactoring [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Jun 3, 2017
1 parent b6c635b commit ef8df13
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (h *handler) mainLoop() {
}
}

// Starts listening to rsyslog and outputting it to stdout
// StartLogging listens to rsyslog and outputs entries to stdout
var StartLogging = func() {
s := syslog.NewServer()
s.AddHandler(newHandler())
Expand Down
36 changes: 20 additions & 16 deletions proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,8 @@ backend dummy-be
}

func (m HaProxy) getConfigData() ConfigData {
certPaths := m.GetCertPaths()
certsString := []string{}
if len(certPaths) > 0 {
certsString = append(certsString, " ssl")
for _, certPath := range certPaths {
certsString = append(certsString, fmt.Sprintf("crt %s", certPath))
}
}
if len(os.Getenv("CA_FILE")) > 0 {
if len(certsString) == 0 {
certsString = append(certsString, " ssl")
}
cf := "ca-file " + os.Getenv("CA_FILE") + " verify optional"
certsString = append(certsString, cf)
}
d := ConfigData{
CertsString: strings.Join(certsString, " "),
CertsString: strings.Join(m.getCerts(), " "),
}
d.ConnectionMode = GetSecretOrEnvVar("CONNECTION_MODE", "http-server-close")
d.SslBindCiphers = GetSecretOrEnvVar("SSL_BIND_CIPHERS", "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS")
Expand Down Expand Up @@ -274,6 +259,25 @@ func (m HaProxy) getConfigData() ConfigData {
return d
}

func (m *HaProxy) getCerts() []string {
certPaths := m.GetCertPaths()
certs := []string{}
if len(certPaths) > 0 {
certs = append(certs, " ssl")
for _, certPath := range certPaths {
certs = append(certs, fmt.Sprintf("crt %s", certPath))
}
}
if len(os.Getenv("CA_FILE")) > 0 {
if len(certs) == 0 {
certs = append(certs, " ssl")
}
cf := "ca-file " + os.Getenv("CA_FILE") + " verify optional"
certs = append(certs, cf)
}
return certs
}

func (m *HaProxy) addCompression(data *ConfigData) {
if len(os.Getenv("COMPRESSION_ALGO")) > 0 {
data.ExtraDefaults += fmt.Sprintf(`
Expand Down
2 changes: 1 addition & 1 deletion proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsCaFile_WhenEnvVarIs
var actualData string
tmpl := strings.Replace(
s.TemplateContent, "bind *:443",
"bind *:443 ssl ca-file " + caFile + " verify optional",
"bind *:443 ssl ca-file "+caFile+" verify optional",
-1)
expectedData := tmpl + s.ServicesContent
writeFile = func(filename string, data []byte, perm os.FileMode) error {
Expand Down
9 changes: 5 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ type runner interface {
Execute(args []string) error
}

type Run struct{}
type run struct{}

var NewRun = func() runner {
return &Run{}
var newRun = func() runner {
return &run{}
}

func (m *Run) Execute(args []string) error {
// Execute runs the proxy
func (m *run) Execute(args []string) error {
return haproxy.HaProxy{}.RunCmd([]string{})
}
2 changes: 1 addition & 1 deletion run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *RunTestSuite) SetupTest() {
// NewRun

func (s RunTestSuite) Test_NewRun_ReturnsNewStruct() {
s.NotNil(NewRun())
s.NotNil(newRun())
}

// Suite
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *Serve) Execute(args []string) error {
}
logPrintf("Starting HAProxy")
m.setConsulAddresses()
NewRun().Execute([]string{})
newRun().Execute([]string{})
address := fmt.Sprintf("%s:%s", m.IP, m.Port)
cert.Init()
var server2 = server.NewServer(
Expand Down
6 changes: 3 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func (s *ServerTestSuite) Test_Execute_ReturnsError_WhenHTTPListenAndServeFails(
}

func (s *ServerTestSuite) Test_Execute_InvokesRunExecute() {
orig := NewRun
orig := newRun
defer func() {
NewRun = orig
newRun = orig
}()
mockObj := getRunMock("")
NewRun = func() runner {
newRun = func() runner {
return mockObj
}

Expand Down

0 comments on commit ef8df13

Please sign in to comment.