Skip to content

Commit

Permalink
fix:remove dots from the name in query parameter of request for badge
Browse files Browse the repository at this point in the history
to avoid RFC 1123 validate

Signed-off-by: KouWakai <[email protected]>
  • Loading branch information
KouWakai authored and KouWakai committed Dec 10, 2023
1 parent 21672a2 commit ee513cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/badge/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

//Sample url: http://localhost:8080/api/badge?name=123
if name, ok := r.URL.Query()["name"]; ok && enabled && !notFound {
if errs := validation.NameIsDNSLabel(strings.ToLower(name[0]), false); len(errs) == 0 {
if errs := validation.NameIsDNSLabel(strings.Replace(strings.ToLower(name[0]), ".", "", -1), false); len(errs) == 0 {
if app, err := h.appClientset.ArgoprojV1alpha1().Applications(reqNs).Get(context.Background(), name[0], v1.GetOptions{}); err == nil {
health = app.Status.Health.Status
status = app.Status.Sync.Status
Expand Down
31 changes: 31 additions & 0 deletions server/badge/badge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ var (
},
},
}
testAppDots = v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{Name: "test.app", Namespace: "default"},
Status: v1alpha1.ApplicationStatus{
Sync: v1alpha1.SyncStatus{Status: v1alpha1.SyncStatusCodeSynced},
Health: v1alpha1.HealthStatus{Status: health.HealthStatusHealthy},
OperationState: &v1alpha1.OperationState{
SyncResult: &v1alpha1.SyncOperationResult{
Revision: "aa29b85",
},
},
},
}
testProject = v1alpha1.AppProject{
ObjectMeta: v1.ObjectMeta{Name: "test-project", Namespace: "default"},
Spec: v1alpha1.AppProjectSpec{},
Expand Down Expand Up @@ -332,3 +344,22 @@ func TestHandlerFeatureIsDisabled(t *testing.T) {
assert.Equal(t, "Unknown", leftTextPattern.FindStringSubmatch(response)[1])
assert.Equal(t, "Unknown", rightTextPattern.FindStringSubmatch(response)[1])
}

func TestHandlerNameDotsContained(t *testing.T) {
settingsMgr := settings.NewSettingsManager(context.Background(), fake.NewSimpleClientset(&argoCDCm, &argoCDSecret), "default")
handler := NewHandler(appclientset.NewSimpleClientset(&testAppDots), settingsMgr, "default", []string{})
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test.app", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))

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])
}

0 comments on commit ee513cf

Please sign in to comment.