From 3b62addc76d847ee8522346a582c162a0e0d9237 Mon Sep 17 00:00:00 2001 From: Ziming Zhang Date: Wed, 7 Aug 2019 15:38:21 +0800 Subject: [PATCH 1/3] check rule conflict Signed-off-by: Ziming Zhang Change-Id: I8af648f22aa3fe76024240d322a7fdae560e3fbd --- src/core/api/retention.go | 51 ++++++++++---------- src/portal/package-lock.json | 93 +++++++++++++----------------------- 2 files changed, 61 insertions(+), 83 deletions(-) diff --git a/src/core/api/retention.go b/src/core/api/retention.go index 7e9e113e7..9899fde00 100644 --- a/src/core/api/retention.go +++ b/src/core/api/retention.go @@ -1,6 +1,8 @@ package api import ( + "crypto/sha256" + "encoding/json" "errors" "fmt" "net/http" @@ -41,30 +43,6 @@ func (r *RetentionAPI) GetMetadatas() { data := ` { "templates": [ - { - "rule_template": "lastXDays", - "display_text": "the images from the last # days", - "action": "retain", - "params": [ - { - "type": "int", - "unit": "DAYS", - "required": true - } - ] - }, - { - "rule_template": "latestActiveK", - "display_text": "the most recent active # images", - "action": "retain", - "params": [ - { - "type": "int", - "unit": "COUNT", - "required": true - } - ] - }, { "rule_template": "latestPushedK", "display_text": "the most recently pushed # images", @@ -194,6 +172,10 @@ func (r *RetentionAPI) CreateRetention() { r.SendBadRequestError(err) return } + if err = r.checkRuleConflict(p); err != nil { + r.SendConflictError(err) + return + } if !r.requireAccess(p, rbac.ActionCreate) { return } @@ -241,6 +223,10 @@ func (r *RetentionAPI) UpdateRetention() { return } p.ID = id + if err = r.checkRuleConflict(p); err != nil { + r.SendConflictError(err) + return + } if !r.requireAccess(p, rbac.ActionUpdate) { return } @@ -250,6 +236,23 @@ func (r *RetentionAPI) UpdateRetention() { } } +func (r *RetentionAPI) checkRuleConflict(p *policy.Metadata) error { + temp := make(map[string]int) + for n, rule := range p.Rules { + tid := rule.ID + rule.ID = 0 + bs, _ := json.Marshal(rule) + s := sha256.New() + h := s.Sum(bs) + if old, exists := temp[string(h)]; exists { + return fmt.Errorf("rule %d is conflict with rule %d", n, old) + } + temp[string(h)] = tid + rule.ID = tid + } + return nil +} + // TriggerRetentionExec Trigger Retention Execution func (r *RetentionAPI) TriggerRetentionExec() { id, err := r.GetIDFromURL() diff --git a/src/portal/package-lock.json b/src/portal/package-lock.json index 31054d2c4..ca4c3d186 100644 --- a/src/portal/package-lock.json +++ b/src/portal/package-lock.json @@ -176,15 +176,13 @@ "version": "1.40.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==", - "dev": true, - "optional": true + "dev": true }, "mime-types": { "version": "2.1.24", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", "dev": true, - "optional": true, "requires": { "mime-db": "1.40.0" } @@ -9254,10 +9252,13 @@ } }, "karma-jasmine": { - "version": "1.1.2", - "resolved": "http://registry.npm.taobao.org/karma-jasmine/download/karma-jasmine-1.1.2.tgz", - "integrity": "sha1-OU8rJf+0pkS5rabyLUQ+L9CIhsM=", - "dev": true + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/karma-jasmine/-/karma-jasmine-2.0.1.tgz", + "integrity": "sha512-iuC0hmr9b+SNn1DaUD2QEYtUxkS1J+bSJSn7ejdEexs7P8EYvA1CWkEdrDQ+8jVH3AgWlCNwjYsT1chjcNW9lA==", + "dev": true, + "requires": { + "jasmine-core": "^3.3" + } }, "karma-jasmine-html-reporter": { "version": "0.2.2", @@ -9266,6 +9267,14 @@ "dev": true, "requires": { "karma-jasmine": "^1.0.2" + }, + "dependencies": { + "karma-jasmine": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/karma-jasmine/-/karma-jasmine-1.1.2.tgz", + "integrity": "sha1-OU8rJf+0pkS5rabyLUQ+L9CIhsM=", + "dev": true + } } }, "karma-mocha-reporter": { @@ -10654,8 +10663,7 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true, - "optional": true + "bundled": true }, "aproba": { "version": "1.2.0", @@ -10679,7 +10687,6 @@ "brace-expansion": { "version": "1.1.11", "bundled": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -10692,8 +10699,7 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "concat-map": { "version": "0.0.1", @@ -10702,8 +10708,7 @@ }, "console-control-strings": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "core-util-is": { "version": "1.0.2", @@ -10806,8 +10811,7 @@ }, "inherits": { "version": "2.0.3", - "bundled": true, - "optional": true + "bundled": true }, "ini": { "version": "1.3.5", @@ -10817,7 +10821,6 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -10830,20 +10833,17 @@ "minimatch": { "version": "3.0.4", "bundled": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true, - "optional": true + "bundled": true }, "minipass": { "version": "2.3.5", "bundled": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -10860,7 +10860,6 @@ "mkdirp": { "version": "0.5.1", "bundled": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -10933,8 +10932,7 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true, - "optional": true + "bundled": true }, "object-assign": { "version": "4.1.1", @@ -10944,7 +10942,6 @@ "once": { "version": "1.4.0", "bundled": true, - "optional": true, "requires": { "wrappy": "1" } @@ -11020,8 +11017,7 @@ }, "safe-buffer": { "version": "5.1.2", - "bundled": true, - "optional": true + "bundled": true }, "safer-buffer": { "version": "2.1.2", @@ -11051,7 +11047,6 @@ "string-width": { "version": "1.0.2", "bundled": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -11069,7 +11064,6 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -11108,13 +11102,11 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true, - "optional": true + "bundled": true }, "yallist": { "version": "3.0.3", - "bundled": true, - "optional": true + "bundled": true } } }, @@ -18778,8 +18770,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -18807,7 +18798,6 @@ "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -18822,8 +18812,7 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", @@ -18834,8 +18823,7 @@ "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -18952,8 +18940,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -18965,7 +18952,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -18980,7 +18966,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -18988,14 +18973,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -19014,7 +18997,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -19095,8 +19077,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -19108,7 +19089,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -19194,8 +19174,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -19231,7 +19210,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -19251,7 +19229,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -19295,14 +19272,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, From 730d95edc864ac7c96c0b4332e3e3ee0bef686bc Mon Sep 17 00:00:00 2001 From: Ziming Zhang Date: Wed, 7 Aug 2019 17:52:47 +0800 Subject: [PATCH 2/3] check rule conflict Signed-off-by: Ziming Zhang Change-Id: I95f7e683b30fa6059b5bb49a2fc8f78dd7276079 --- src/core/api/retention.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/api/retention.go b/src/core/api/retention.go index 9899fde00..885c0de70 100644 --- a/src/core/api/retention.go +++ b/src/core/api/retention.go @@ -1,7 +1,6 @@ package api import ( - "crypto/sha256" "encoding/json" "errors" "fmt" @@ -242,12 +241,10 @@ func (r *RetentionAPI) checkRuleConflict(p *policy.Metadata) error { tid := rule.ID rule.ID = 0 bs, _ := json.Marshal(rule) - s := sha256.New() - h := s.Sum(bs) - if old, exists := temp[string(h)]; exists { + if old, exists := temp[string(bs)]; exists { return fmt.Errorf("rule %d is conflict with rule %d", n, old) } - temp[string(h)] = tid + temp[string(bs)] = tid rule.ID = tid } return nil From fb5acdc64b4989fc0d4c72343d455823b0fc3be4 Mon Sep 17 00:00:00 2001 From: Ziming Zhang Date: Wed, 7 Aug 2019 18:01:42 +0800 Subject: [PATCH 3/3] check rule conflict Signed-off-by: Ziming Zhang Change-Id: Iea8c9ff4702873cb4ab3ebd943deec22bec418e6 Signed-off-by: Ziming Zhang --- src/core/api/retention_test.go | 162 +++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/src/core/api/retention_test.go b/src/core/api/retention_test.go index 0dee6fd13..0fe47f628 100644 --- a/src/core/api/retention_test.go +++ b/src/core/api/retention_test.go @@ -143,6 +143,87 @@ func TestCreatePolicy(t *testing.T) { }, code: http.StatusBadRequest, }, + { + request: &testingRequest{ + method: http.MethodPost, + url: "/api/retentions", + bodyJSON: &policy.Metadata{ + Algorithm: "or", + Rules: []rule.Metadata{ + { + ID: 1, + Priority: 1, + Template: "recentXdays", + Parameters: rule.Parameters{ + "num": 10, + }, + TagSelectors: []*rule.Selector{ + { + Kind: "label", + Decoration: "with", + Pattern: "latest", + }, + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "release-[\\d\\.]+", + }, + }, + ScopeSelectors: map[string][]*rule.Selector{ + "repository": { + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: ".+", + }, + }, + }, + }, + { + ID: 2, + Priority: 1, + Template: "recentXdays", + Parameters: rule.Parameters{ + "num": 10, + }, + TagSelectors: []*rule.Selector{ + { + Kind: "label", + Decoration: "with", + Pattern: "latest", + }, + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "release-[\\d\\.]+", + }, + }, + ScopeSelectors: map[string][]*rule.Selector{ + "repository": { + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: ".+", + }, + }, + }, + }, + }, + Trigger: &policy.Trigger{ + Kind: "Schedule", + Settings: map[string]interface{}{ + "cron": "* 22 11 * * *", + }, + }, + Scope: &policy.Scope{ + Level: "project", + Reference: 1, + }, + }, + credential: sysAdmin, + }, + code: http.StatusConflict, + }, } runCodeCheckingCases(t, cases...) @@ -267,6 +348,87 @@ func TestPolicy(t *testing.T) { }, code: http.StatusOK, }, + { + request: &testingRequest{ + method: http.MethodPut, + url: fmt.Sprintf("/api/retentions/%d", id), + bodyJSON: &policy.Metadata{ + Algorithm: "or", + Rules: []rule.Metadata{ + { + ID: 1, + Priority: 1, + Template: "recentXdays", + Parameters: rule.Parameters{ + "num": 10, + }, + TagSelectors: []*rule.Selector{ + { + Kind: "label", + Decoration: "with", + Pattern: "latest", + }, + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "release-[\\d\\.]+", + }, + }, + ScopeSelectors: map[string][]*rule.Selector{ + "repository": { + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "b.+", + }, + }, + }, + }, + { + ID: 2, + Priority: 1, + Template: "recentXdays", + Parameters: rule.Parameters{ + "num": 10, + }, + TagSelectors: []*rule.Selector{ + { + Kind: "label", + Decoration: "with", + Pattern: "latest", + }, + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "release-[\\d\\.]+", + }, + }, + ScopeSelectors: map[string][]*rule.Selector{ + "repository": { + { + Kind: "regularExpression", + Decoration: "matches", + Pattern: "b.+", + }, + }, + }, + }, + }, + Trigger: &policy.Trigger{ + Kind: "Schedule", + Settings: map[string]interface{}{ + "cron": "* 22 11 * * *", + }, + }, + Scope: &policy.Scope{ + Level: "project", + Reference: 1, + }, + }, + credential: sysAdmin, + }, + code: http.StatusConflict, + }, { request: &testingRequest{ method: http.MethodPost,