This is an automated email from the ASF dual-hosted git repository.

rawlin pushed a commit to branch 4.1.x
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/4.1.x by this push:
     new cb23467  Fix MaxRevalDurationDays validation for invalidation jobs 
(#4718) (#4733)
cb23467 is described below

commit cb23467cf6e9dbe6734b286b1b3a2c7a7bf97481
Author: Rawlin Peters <[email protected]>
AuthorDate: Thu May 28 10:57:06 2020 -0600

    Fix MaxRevalDurationDays validation for invalidation jobs (#4718) (#4733)
    
    * Fix MaxRevalDurationDays validation for invalidation jobs
    
    * Address review feedback
    
    * unindent
    
    (cherry picked from commit 94e4edef92a4cbd7fd494d6f29811f2d0d7b0cb1)
---
 CHANGELOG.md                  |  1 +
 lib/go-tc/invalidationjobs.go | 26 +++++++++++++++++---------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7d6acf7..d597a3b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -63,6 +63,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - Fixed the `GET /api/x/jobs` and `GET /api/x/jobs/:id` Traffic Ops API routes 
to allow falling back to Perl via the routing blacklist
 - Fixed audit logging from the `/jobs` APIs to bring them back to the same 
level of information provided by TO-Perl
 - Fixed #4541: traffic_ops_server.js searches based on which= and can find 
data for multiple servers
+- Fixed `maxRevalDurationDays` validation for `POST 
/api/1.x/user/current/jobs` and added that validation to the `/api/x/jobs` 
endpoints
 
 ### Deprecated/Removed
 - The Traffic Ops `db/admin.pl` script has now been removed. Please use the 
`db/admin` binary instead.
diff --git a/lib/go-tc/invalidationjobs.go b/lib/go-tc/invalidationjobs.go
index a51574c..7d109cb 100644
--- a/lib/go-tc/invalidationjobs.go
+++ b/lib/go-tc/invalidationjobs.go
@@ -257,9 +257,16 @@ func (job *InvalidationJobInput) Validate(tx *sql.Tx) 
error {
        }
 
        if job.TTL != nil {
-               if _, err := job.TTLHours(); err != nil {
+               hours, err := job.TTLHours()
+               if err != nil {
                        errs = append(errs, "ttl: must be a number of hours, or 
a duration string e.g. '48h'")
                }
+               var maxDays uint
+               err = tx.QueryRow(`SELECT value FROM parameter WHERE 
name='maxRevalDurationDays' AND 
config_file='regex_revalidate.config'`).Scan(&maxDays)
+               maxHours := maxDays * 24
+               if err == nil && hours > maxHours { // silently ignore other 
errors too
+                       errs = append(errs, "ttl: cannot exceed 
"+strconv.FormatUint(uint64(maxHours), 10)+"!")
+               }
        }
 
        if len(errs) > 0 {
@@ -329,7 +336,7 @@ func (job *UserInvalidationJobInput) Validate(tx *sql.Tx) 
error {
        if job.StartTime == nil {
                errs = append(errs, "startTime: cannot be blank")
        } else if job.StartTime.After(time.Now().Add(twoDays)) {
-               errs = append(errs, "startTime: must be within two days!")
+               errs = append(errs, "startTime: must be within two days")
        }
 
        if job.Regex != nil && *(job.Regex) != "" {
@@ -343,20 +350,21 @@ func (job *UserInvalidationJobInput) Validate(tx *sql.Tx) 
error {
                var id uint
                if err := row.Scan(&id); err != nil {
                        log.Errorln(err.Error())
-                       errs = append(errs, "No Delivery Service corresponding 
to 'dsId'!")
+                       errs = append(errs, "no Delivery Service corresponding 
to 'dsId'")
                }
        }
 
        if job.TTL != nil {
                row := tx.QueryRow(`SELECT value FROM parameter WHERE 
name='maxRevalDurationDays' AND config_file='regex_revalidate.config'`)
-               var max uint64
-               err := row.Scan(&max)
+               var maxDays uint64
+               err := row.Scan(&maxDays)
+               maxHours := maxDays * 24
                if err == sql.ErrNoRows && MaxTTL < *(job.TTL) {
-                       errs = append(errs, "ttl: cannot exceed 
"+strconv.FormatUint(MaxTTL, 10)+"!")
-               } else if err == nil && max < *(job.TTL) { //silently ignore 
other errors to
-                       errs = append(errs, "ttl: cannot exceed 
"+strconv.FormatUint(max, 10)+"!")
+                       errs = append(errs, "ttl: cannot exceed 
"+strconv.FormatUint(MaxTTL, 10))
+               } else if err == nil && maxHours < *(job.TTL) { // silently 
ignore other errors
+                       errs = append(errs, "ttl: cannot exceed 
"+strconv.FormatUint(maxHours, 10))
                } else if *(job.TTL) < 1 {
-                       errs = append(errs, "ttl: must be at least 1!")
+                       errs = append(errs, "ttl: must be at least 1")
                }
        }
 

Reply via email to