fix: avoid nil pointer dereference in badge handler (#3316)

This commit is contained in:
Alexander Matyushentsev 2020-03-30 14:27:43 -07:00 committed by GitHub
parent 6411958be5
commit 7ae204d426
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View file

@ -76,7 +76,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if app, err := h.appClientset.ArgoprojV1alpha1().Applications(h.namespace).Get(name[0], v1.GetOptions{}); err == nil {
health = app.Status.Health.Status
status = app.Status.Sync.Status
revision = app.Status.OperationState.SyncResult.Revision
if app.Status.OperationState != nil && app.Status.OperationState.SyncResult != nil {
revision = app.Status.OperationState.SyncResult.Revision
}
} else if errors.IsNotFound(err) {
notFound = true
}
@ -115,7 +117,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
badge = replaceFirstGroupSubMatch(leftTextPattern, badge, leftText)
badge = replaceFirstGroupSubMatch(rightTextPattern, badge, rightText)
if !notFound && revisionEnabled {
if !notFound && revisionEnabled && revision != "" {
// Increase width of SVG and enable display of revision components
badge = svgWidthPattern.ReplaceAllString(badge, fmt.Sprintf(`<svg width="%d" $2`, svgWidthWithRevision))
badge = displayNonePattern.ReplaceAllString(badge, `display="inline"`)

View file

@ -88,6 +88,28 @@ func TestHandlerFeatureIsEnabledRevisionIsEnabled(t *testing.T) {
assert.Contains(t, response, "(aa29b85)")
}
func TestHandlerRevisionIsEnabledNoOperationState(t *testing.T) {
app := testApp.DeepCopy()
app.Status.OperationState = nil
settingsMgr := settings.NewSettingsManager(context.Background(), fake.NewSimpleClientset(&argoCDCm, &argoCDSecret), "default")
handler := NewHandler(appclientset.NewSimpleClientset(app), settingsMgr, "default")
req, err := http.NewRequest("GET", "/api/badge?name=testApp&revision=true", nil)
assert.NoError(t, err)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
response := rr.Body.String()
assert.Equal(t, toRGBString(Green), leftRectColorPattern.FindStringSubmatch(response)[1])
assert.Equal(t, toRGBString(Green), rightRectColorPattern.FindStringSubmatch(response)[1])
assert.Equal(t, "Healthy", leftTextPattern.FindStringSubmatch(response)[1])
assert.Equal(t, "Synced", rightTextPattern.FindStringSubmatch(response)[1])
assert.NotContains(t, response, "(aa29b85)")
}
func TestHandlerFeatureIsDisabled(t *testing.T) {
argoCDCmDisabled := argoCDCm.DeepCopy()