From cee7ac26e983874315839d161fe5b126cb064a29 Mon Sep 17 00:00:00 2001
From: Matt Hook <hookenz@gmail.com>
Date: Tue, 27 Jul 2021 10:49:28 +1200
Subject: [PATCH] Fix dockerhub pro account rate-limit issue (#5352)

---
 .../handler/endpoints/endpoint_dockerhub_status.go     | 10 ++++++++++
 .../por-image-registry-rate-limits.controller.js       |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/api/http/handler/endpoints/endpoint_dockerhub_status.go b/api/http/handler/endpoints/endpoint_dockerhub_status.go
index 92cfbef9c..7a8cc44a3 100644
--- a/api/http/handler/endpoints/endpoint_dockerhub_status.go
+++ b/api/http/handler/endpoints/endpoint_dockerhub_status.go
@@ -132,8 +132,18 @@ func getDockerHubLimits(httpClient *client.HTTPClient, token string) (*dockerhub
 		return nil, errors.New("failed fetching dockerhub limits")
 	}
 
+	// An error with rateLimit-Limit or RateLimit-Remaining is likely for dockerhub pro accounts where there is no rate limit.
+	// In that specific case the headers will not be present.  Don't bubble up the error as its normal
+	// See: https://docs.docker.com/docker-hub/download-rate-limit/
 	rateLimit, err := parseRateLimitHeader(resp.Header, "RateLimit-Limit")
+	if err != nil {
+		return nil, nil
+	}
+
 	rateLimitRemaining, err := parseRateLimitHeader(resp.Header, "RateLimit-Remaining")
+	if err != nil {
+		return nil, nil
+	}
 
 	return &dockerhubStatusResponse{
 		Limit:     rateLimit,
diff --git a/app/docker/components/imageRegistry/por-image-registry-rate-limits.controller.js b/app/docker/components/imageRegistry/por-image-registry-rate-limits.controller.js
index 0c23eba56..809f1ba52 100644
--- a/app/docker/components/imageRegistry/por-image-registry-rate-limits.controller.js
+++ b/app/docker/components/imageRegistry/por-image-registry-rate-limits.controller.js
@@ -32,7 +32,7 @@ export default class porImageRegistryContainerController {
 
     try {
       this.pullRateLimits = await this.DockerHubService.checkRateLimits(this.endpoint, this.registryId || 0);
-      this.setValidity(this.pullRateLimits.remaining >= 0);
+      this.setValidity(!this.pullRateLimits.limit || (this.pullRateLimits.limit && this.pullRateLimits.remaining >= 0));
     } catch (e) {
       // eslint-disable-next-line no-console
       console.error('Failed loading DockerHub pull rate limits', e);