Skip to content

Commit 0fa53bb

Browse files
Merge pull request #65 from CheckmarxDev/health_1.2
Health 1.2
2 parents 8e4dc9f + fecfa92 commit 0fa53bb

File tree

17 files changed

+721
-736
lines changed

17 files changed

+721
-736
lines changed

cmd/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ func main() {
8383
exitIfError(err)
8484
healthcheckLoggingPath := viper.GetString(params.HealthcheckLoggingPathKey)
8585

86+
err = bindKeyToEnvAndDefault(params.HealthcheckScanFlowPathKey, params.HealthcheckScanFlowPathEnv,
87+
"scan-flow")
88+
exitIfError(err)
89+
healthcheckScanFlowPath := viper.GetString(params.HealthcheckScanFlowPathKey)
90+
8691
err = bindKeyToEnvAndDefault(params.HealthcheckGetAstRolePathKey, params.HealthcheckGetAstRolePathEnv,
8792
"ast-role")
8893
exitIfError(err)
@@ -120,6 +125,7 @@ func main() {
120125
fmt.Sprintf("%s/%s", healthcheck, healthcheckObjectStorePath),
121126
fmt.Sprintf("%s/%s", healthcheck, healthcheckInMemoryDBPath),
122127
fmt.Sprintf("%s/%s", healthcheck, healthcheckLoggingPath),
128+
fmt.Sprintf("%s/%s", healthcheck, healthcheckScanFlowPath),
123129
fmt.Sprintf("%s/%s", healthcheck, getRolePath),
124130
)
125131
defaultConfigFileLocation := "/etc/conf/cx/config.yml"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/checkmarxDev/ast-cli
33
go 1.13
44

55
require (
6-
github.com/checkmarxDev/healthcheck v1.1.0
6+
github.com/checkmarxDev/healthcheck v1.2.0
77
github.com/checkmarxDev/sast-rm v1.1.1
88
github.com/checkmarxDev/scans v1.3.0
99
github.com/checkmarxDev/uploads v1.0.1

go.sum

Lines changed: 619 additions & 650 deletions
Large diffs are not rendered by default.

internal/commands/health.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"fmt"
54
"sync"
65

76
"github.com/pkg/errors"
@@ -25,9 +24,13 @@ func runHealthCheck(c *wrappers.HealthCheck) *healthView {
2524
status, err := c.Handler()
2625
v := &healthView{Name: c.Name}
2726
if err != nil {
28-
v.Status = fmt.Sprintf("Error %s", err)
27+
v.Status = "Error"
28+
v.Errors = []string{err.Error()}
29+
} else if !status.Success {
30+
v.Status = "Failure"
31+
v.Errors = status.Errors
2932
} else {
30-
v.Status = status.String()
33+
v.Status = "Success"
3134
}
3235

3336
return v
@@ -50,12 +53,13 @@ func runChecksConcurrently(checks []*wrappers.HealthCheck) []*healthView {
5053
}
5154

5255
func newHealthChecksByRole(h wrappers.HealthCheckWrapper, role string) (checksByRole []*wrappers.HealthCheck) {
53-
sastRoles := [...]string{commonParams.SastALlInOne, commonParams.SastEngine, commonParams.SastManager}
54-
sastAndScaRoles := append(sastRoles[:], commonParams.ScaAgent)
56+
sastRoles := [...]string{commonParams.SastALlInOne, commonParams.SastEngine, commonParams.SastManager, "SAST"}
57+
sastAndScaRoles := append(sastRoles[:], commonParams.ScaAgent, "SCA")
5558
healthChecks := []*wrappers.HealthCheck{
5659
wrappers.NewHealthCheck("DB", h.RunDBCheck, sastRoles[:]),
5760
wrappers.NewHealthCheck("Web App", h.RunWebAppCheck, sastRoles[:]),
5861
wrappers.NewHealthCheck("Keycloak Web App", h.RunKeycloakWebAppCheck, sastRoles[:]),
62+
wrappers.NewHealthCheck("Scan-Flow", h.RunScanFlowCheck, sastRoles[:]),
5963
wrappers.NewHealthCheck("In-memory DB", h.RunInMemoryDBCheck, sastAndScaRoles),
6064
wrappers.NewHealthCheck("Object Store", h.RunObjectStoreCheck, sastAndScaRoles),
6165
wrappers.NewHealthCheck("Message Queue", h.RunMessageQueueCheck, sastAndScaRoles),
@@ -73,6 +77,7 @@ func newHealthChecksByRole(h wrappers.HealthCheckWrapper, role string) (checksBy
7377

7478
func runAllHealthChecks(healthCheckWrapper wrappers.HealthCheckWrapper) func(cmd *cobra.Command, args []string) error {
7579
return func(cmd *cobra.Command, args []string) error {
80+
writeToStandardOutput("Performing health checks...")
7681
role := viper.GetString(commonParams.AstRoleKey)
7782
if role == "" {
7883
var err error
@@ -93,4 +98,5 @@ func runAllHealthChecks(healthCheckWrapper wrappers.HealthCheckWrapper) func(cmd
9398
type healthView struct {
9499
Name string
95100
Status string
101+
Errors []string
96102
}

internal/params/envs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ const (
2222
HealthcheckObjectStorePathEnv = "HEALTHCHECK_OBJECT_STORE_PATH"
2323
HealthcheckInMemoryDBPathEnv = "HEALTHCHECK_IN_MEMORY_DB_PATH"
2424
HealthcheckLoggingPathEnv = "HEALTHCHECK_LOGGING_PATH"
25+
HealthcheckScanFlowPathEnv = "HEALTHCHECK_SCAN_FLOW_PATH"
2526
HealthcheckGetAstRolePathEnv = "HEALTHCHECK_GET_AST_ROLE_PATH"
2627
)

internal/params/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ var (
2323
HealthcheckObjectStorePathKey = strings.ToLower(HealthcheckObjectStorePathEnv)
2424
HealthcheckInMemoryDBPathKey = strings.ToLower(HealthcheckInMemoryDBPathEnv)
2525
HealthcheckLoggingPathKey = strings.ToLower(HealthcheckDBPathEnv)
26+
HealthcheckScanFlowPathKey = strings.ToLower(HealthcheckScanFlowPathEnv)
2627
HealthcheckGetAstRolePathKey = strings.ToLower(HealthcheckGetAstRolePathEnv)
2728
)

internal/wrappers/client.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
)
2020

2121
const (
22-
expiryGraceSeconds = 10
22+
expiryGraceSeconds = 10
23+
DefaultTimeoutSeconds = 5
2324
)
2425

2526
type ClientCredentialsInfo struct {
@@ -37,16 +38,16 @@ type ClientCredentialsError struct {
3738
Description string `json:"error_description"`
3839
}
3940

40-
func getClient() *http.Client {
41+
func getClient(timeout uint) *http.Client {
4142
insecure := viper.GetBool("insecure")
4243
tr := &http.Transport{
4344
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
4445
}
45-
return &http.Client{Transport: tr, Timeout: 5 * time.Second}
46+
return &http.Client{Transport: tr, Timeout: time.Duration(timeout) * time.Second}
4647
}
4748

48-
func SendHTTPRequest(method, path string, body io.Reader, auth bool) (*http.Response, error) {
49-
client := getClient()
49+
func SendHTTPRequest(method, path string, body io.Reader, auth bool, timeout uint) (*http.Response, error) {
50+
client := getClient(timeout)
5051
url := GetURL(path)
5152
req, err := http.NewRequest(method, url, body)
5253
if err != nil {
@@ -74,7 +75,7 @@ func GetURL(path string) string {
7475

7576
func SendHTTPRequestWithQueryParams(method, path string, params map[string]string,
7677
body io.Reader) (*http.Response, error) {
77-
client := getClient()
78+
client := getClient(DefaultTimeoutSeconds)
7879
url := GetURL(path)
7980
req, err := http.NewRequest(method, url, body)
8081
if err != nil {

internal/wrappers/health-http.go

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ import (
77
"io/ioutil"
88
"net/http"
99

10-
healthcheckApi "github.com/checkmarxDev/healthcheck/api/rest/v1"
11-
1210
errors "github.com/pkg/errors"
1311
)
1412

1513
type healthCheckHTTPWrapper struct {
16-
WebAppHealthCheckPath string
17-
KeycloakHealthCheckPath string
14+
WebAppHealthcheckPath string
15+
KeycloakHealthcheckPath string
1816
DBHealthcheckPath string
1917
MessageQueueHealthcheckPath string
2018
ObjectStoreHealthcheckPath string
2119
InMemoryDBHealthcheckPath string
2220
LoggingHealthcheckPath string
21+
ScanFlowHealthcheckPath string
2322
GetAstRolePath string
2423
}
2524

25+
const scanFlowTimeoutSecs uint = 60
26+
2627
func parseHealthcheckResponse(body io.ReadCloser) (*HealthStatus, error) {
2728
status := &HealthStatus{}
2829
if err := json.NewDecoder(body).Decode(status); err != nil {
@@ -32,36 +33,43 @@ func parseHealthcheckResponse(body io.ReadCloser) (*HealthStatus, error) {
3233
return status, nil
3334
}
3435

35-
func runHealthCheckRequest(path string,
36-
parser func(body io.ReadCloser) (*HealthStatus, error)) (*HealthStatus, error) {
37-
resp, err := SendHTTPRequest(http.MethodGet, path, nil, false)
36+
func runHealthCheckRequest(path string, timeout uint, parser func(body io.ReadCloser) (*HealthStatus, error),
37+
) (*HealthStatus, error) {
38+
resp, err := SendHTTPRequest(http.MethodGet, path, nil, false, timeout)
3839
if err != nil {
3940
return nil, errors.Wrapf(err, "Http request %v failed", GetURL(path))
4041
}
4142

4243
defer resp.Body.Close()
4344
if resp.StatusCode != http.StatusOK {
4445
body, _ := ioutil.ReadAll(resp.Body)
45-
return &HealthStatus{
46-
&healthcheckApi.HealthcheckModel{
47-
Success: false,
48-
Message: fmt.Sprintf("Http request %v responded with status code %v and body %v",
49-
resp.Request.URL, resp.StatusCode, func() string {
50-
if err != nil {
51-
return ""
52-
}
53-
54-
return string(body)
55-
}()),
56-
},
57-
}, nil
46+
return NewHealthStatus(
47+
false,
48+
fmt.Sprintf("Http request %v responded with status code %v and body %v",
49+
resp.Request.URL, resp.StatusCode, func() string {
50+
if err != nil {
51+
return ""
52+
}
53+
54+
return string(body)
55+
}()),
56+
), nil
5857
}
5958

6059
return parser(resp.Body)
6160
}
6261

63-
func NewHealthCheckHTTPWrapper(astWebAppPath, astKeycloakWebAppPath, healthDBPath, healthcheckNatsPath,
64-
healthcheckMinioPath, healthCheckRedisPath, healthcheckLoggingPath, getAstRolePath string) HealthCheckWrapper {
62+
func NewHealthCheckHTTPWrapper(
63+
astWebAppPath,
64+
astKeycloakWebAppPath,
65+
healthDBPath,
66+
healthcheckNatsPath,
67+
healthcheckMinioPath,
68+
healthCheckRedisPath,
69+
healthcheckLoggingPath,
70+
healthcheckScanFlowPath,
71+
getAstRolePath string,
72+
) HealthCheckWrapper {
6573
return &healthCheckHTTPWrapper{
6674
astWebAppPath,
6775
astKeycloakWebAppPath,
@@ -70,54 +78,49 @@ func NewHealthCheckHTTPWrapper(astWebAppPath, astKeycloakWebAppPath, healthDBPat
7078
healthcheckMinioPath,
7179
healthCheckRedisPath,
7280
healthcheckLoggingPath,
81+
healthcheckScanFlowPath,
7382
getAstRolePath,
7483
}
7584
}
7685

7786
func (h *healthCheckHTTPWrapper) RunWebAppCheck() (*HealthStatus, error) {
78-
return runHealthCheckRequest(h.WebAppHealthCheckPath, func(body io.ReadCloser) (*HealthStatus, error) {
79-
return &HealthStatus{
80-
&healthcheckApi.HealthcheckModel{
81-
Success: true,
82-
Message: "",
83-
},
84-
}, nil
87+
return runHealthCheckRequest(h.WebAppHealthcheckPath, DefaultTimeoutSeconds, func(body io.ReadCloser) (*HealthStatus, error) {
88+
return NewHealthStatus(true), nil
8589
})
8690
}
8791

8892
func (h *healthCheckHTTPWrapper) RunKeycloakWebAppCheck() (*HealthStatus, error) {
89-
return runHealthCheckRequest(h.KeycloakHealthCheckPath, func(body io.ReadCloser) (*HealthStatus, error) {
90-
return &HealthStatus{
91-
&healthcheckApi.HealthcheckModel{
92-
Success: true,
93-
Message: "",
94-
},
95-
}, nil
93+
return runHealthCheckRequest(h.KeycloakHealthcheckPath, DefaultTimeoutSeconds, func(body io.ReadCloser) (*HealthStatus, error) {
94+
return NewHealthStatus(true), nil
9695
})
9796
}
9897

9998
func (h *healthCheckHTTPWrapper) RunDBCheck() (*HealthStatus, error) {
100-
return runHealthCheckRequest(h.DBHealthcheckPath, parseHealthcheckResponse)
99+
return runHealthCheckRequest(h.DBHealthcheckPath, DefaultTimeoutSeconds, parseHealthcheckResponse)
101100
}
102101

103102
func (h *healthCheckHTTPWrapper) RunMessageQueueCheck() (*HealthStatus, error) {
104-
return runHealthCheckRequest(h.MessageQueueHealthcheckPath, parseHealthcheckResponse)
103+
return runHealthCheckRequest(h.MessageQueueHealthcheckPath, DefaultTimeoutSeconds, parseHealthcheckResponse)
105104
}
106105

107106
func (h *healthCheckHTTPWrapper) RunObjectStoreCheck() (*HealthStatus, error) {
108-
return runHealthCheckRequest(h.ObjectStoreHealthcheckPath, parseHealthcheckResponse)
107+
return runHealthCheckRequest(h.ObjectStoreHealthcheckPath, DefaultTimeoutSeconds, parseHealthcheckResponse)
109108
}
110109

111110
func (h *healthCheckHTTPWrapper) RunInMemoryDBCheck() (*HealthStatus, error) {
112-
return runHealthCheckRequest(h.InMemoryDBHealthcheckPath, parseHealthcheckResponse)
111+
return runHealthCheckRequest(h.InMemoryDBHealthcheckPath, DefaultTimeoutSeconds, parseHealthcheckResponse)
113112
}
114113

115114
func (h *healthCheckHTTPWrapper) RunLoggingCheck() (*HealthStatus, error) {
116-
return runHealthCheckRequest(h.LoggingHealthcheckPath, parseHealthcheckResponse)
115+
return runHealthCheckRequest(h.LoggingHealthcheckPath, DefaultTimeoutSeconds, parseHealthcheckResponse)
116+
}
117+
118+
func (h *healthCheckHTTPWrapper) RunScanFlowCheck() (*HealthStatus, error) {
119+
return runHealthCheckRequest(h.ScanFlowHealthcheckPath, scanFlowTimeoutSecs, parseHealthcheckResponse)
117120
}
118121

119122
func (h *healthCheckHTTPWrapper) GetAstRole() (string, error) {
120-
resp, err := SendHTTPRequest(http.MethodGet, h.GetAstRolePath, nil, false)
123+
resp, err := SendHTTPRequest(http.MethodGet, h.GetAstRolePath, nil, false, DefaultTimeoutSeconds)
121124
if err != nil {
122125
return "", errors.Wrapf(err, "Http request %v failed", GetURL(h.GetAstRolePath))
123126
}

internal/wrappers/health-mock.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ package wrappers
22

33
import (
44
"github.com/checkmarxDev/ast-cli/internal/params"
5-
healthcheckApi "github.com/checkmarxDev/healthcheck/api/rest/v1"
65
)
76

87
func mockRun() (*HealthStatus, error) {
9-
return &HealthStatus{
10-
&healthcheckApi.HealthcheckModel{
11-
Success: true,
12-
Message: "",
13-
},
14-
}, nil
8+
return NewHealthStatus(true), nil
159
}
1610

1711
type HealthCheckMockWrapper struct {
@@ -45,6 +39,10 @@ func (h *HealthCheckMockWrapper) RunLoggingCheck() (*HealthStatus, error) {
4539
return mockRun()
4640
}
4741

42+
func (h *HealthCheckMockWrapper) RunScanFlowCheck() (*HealthStatus, error) {
43+
return mockRun()
44+
}
45+
4846
func (h *HealthCheckMockWrapper) GetAstRole() (string, error) {
4947
return params.ScaAgent, nil
5048
}

internal/wrappers/health.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package wrappers
22

33
import (
4-
"fmt"
5-
6-
healthcheckApi "github.com/checkmarxDev/healthcheck/api/rest/v1"
4+
healthcheckApi "github.com/checkmarxDev/healthcheck/pkg/api/rest/v1"
75
)
86

97
type HealthCheckWrapper interface {
@@ -14,19 +12,21 @@ type HealthCheckWrapper interface {
1412
RunObjectStoreCheck() (*HealthStatus, error)
1513
RunInMemoryDBCheck() (*HealthStatus, error)
1614
RunLoggingCheck() (*HealthStatus, error)
15+
RunScanFlowCheck() (*HealthStatus, error)
1716
GetAstRole() (string, error)
1817
}
1918

2019
type HealthStatus struct {
2120
*healthcheckApi.HealthcheckModel
2221
}
2322

24-
func (h *HealthStatus) String() string {
25-
if h.Success {
26-
return "Success"
23+
func NewHealthStatus(success bool, errs ...string) *HealthStatus {
24+
return &HealthStatus{
25+
&healthcheckApi.HealthcheckModel{
26+
Success: success,
27+
Errors: errs,
28+
},
2729
}
28-
29-
return fmt.Sprintf("Failure, due to %v", h.Message)
3030
}
3131

3232
type HealthCheck struct {

0 commit comments

Comments
 (0)