[XAPID-1070] add auth to requests sent by bundle downloader. Reuse http clients
diff --git a/bundle.go b/bundle.go
index b2206ea..622c8ed 100644
--- a/bundle.go
+++ b/bundle.go
@@ -48,12 +48,12 @@
apiMan apiManagerInterface
concurrentDownloads int
markDeploymentFailedAfter time.Duration
- bundleDownloadConnTimeout time.Duration
bundleRetryDelay time.Duration
bundleCleanupDelay time.Duration
downloadQueue chan *DownloadRequest
isClosed *int32
workers []*BundleDownloader
+ client *http.Client
}
type blobServerResponse struct {
@@ -103,7 +103,7 @@
blobId: id,
backoffFunc: createBackoff(retryIn, maxBackOff),
markFailedAt: markFailedAt,
- connTimeout: bm.bundleDownloadConnTimeout,
+ client: bm.client,
}
}
@@ -158,8 +158,8 @@
blobId string
backoffFunc func()
markFailedAt time.Time
- connTimeout time.Duration
blobServerURL string
+ client *http.Client
}
func (r *DownloadRequest) downloadBundle() error {
@@ -172,7 +172,7 @@
}
}
- downloadedFile, err := downloadFromURI(r.blobServerURL, r.blobId, r.connTimeout)
+ downloadedFile, err := downloadFromURI(r.client, r.blobServerURL, r.blobId)
if err != nil {
log.Errorf("Unable to download blob file blobId=%s err:%v", r.blobId, err)
@@ -210,7 +210,7 @@
return path.Join(bundlePath, base64.StdEncoding.EncodeToString([]byte(blobId)))
}
-func getSignedURL(blobServerURL string, blobId string, bundleDownloadConnTimeout time.Duration) (string, error) {
+func getSignedURL(client *http.Client, blobServerURL string, blobId string) (string, error) {
blobUri, err := url.Parse(blobServerURL)
if err != nil {
@@ -224,7 +224,7 @@
uri := blobUri.String()
- surl, err := getURIReader(uri, bundleDownloadConnTimeout)
+ surl, err := getUriReaderWithAuth(client, uri)
if err != nil {
log.Errorf("Unable to get signed URL from BlobServer %s: %v", uri, err)
return "", err
@@ -248,12 +248,12 @@
// downloadFromURI involves retrieving the signed URL for the blob, and storing the resource locally
// after downloading the resource from GCS (via the signed URL)
-func downloadFromURI(blobServerURL string, blobId string, bundleDownloadConnTimeout time.Duration) (tempFileName string, err error) {
+func downloadFromURI(client *http.Client, blobServerURL string, blobId string) (tempFileName string, err error) {
var tempFile *os.File
log.Debugf("Downloading bundle: %s", blobId)
- uri, err := getSignedURL(blobServerURL, blobId, bundleDownloadConnTimeout)
+ uri, err := getSignedURL(client, blobServerURL, blobId)
if err != nil {
log.Errorf("Unable to get signed URL for blobId {%s}, error : {%v}", blobId, err)
return
@@ -268,7 +268,7 @@
tempFileName = tempFile.Name()
var confReader io.ReadCloser
- confReader, err = getURIReader(uri, bundleDownloadConnTimeout)
+ confReader, err = getUriReaderWithAuth(client, uri)
if err != nil {
log.Errorf("Unable to retrieve bundle %s: %v", uri, err)
return
@@ -286,12 +286,14 @@
}
// retrieveBundle retrieves bundle data from a URI
-func getURIReader(uriString string, bundleDownloadConnTimeout time.Duration) (io.ReadCloser, error) {
-
- client := http.Client{
- Timeout: bundleDownloadConnTimeout,
+func getUriReaderWithAuth(client *http.Client, uriString string) (io.ReadCloser, error) {
+ req, err := http.NewRequest("GET", uriString, nil)
+ if err != nil {
+ return nil, err
}
- res, err := client.Get(uriString)
+ // add Auth
+ req.Header.Add("Authorization", getBearerToken())
+ res, err := client.Do(req)
if err != nil {
return nil, err
}
diff --git a/bundle_test.go b/bundle_test.go
index 0b8b535..30626f9 100644
--- a/bundle_test.go
+++ b/bundle_test.go
@@ -72,11 +72,13 @@
apiMan: dummyApiMan,
concurrentDownloads: concurrentDownloads,
markDeploymentFailedAfter: 5 * time.Second,
- bundleDownloadConnTimeout: time.Second,
bundleRetryDelay: time.Second,
bundleCleanupDelay: 5 * time.Second,
downloadQueue: make(chan *DownloadRequest, downloadQueueSize),
isClosed: new(int32),
+ client: &http.Client{
+ Timeout: time.Second,
+ },
}
testBundleMan.initializeBundleDownloading()
time.Sleep(100 * time.Millisecond)
@@ -101,7 +103,7 @@
// setup timeout
atomic.StoreInt32(blobServer.signedTimeout, 1)
atomic.StoreInt32(blobServer.blobTimeout, 1)
- testBundleMan.bundleDownloadConnTimeout = 500 * time.Millisecond
+ testBundleMan.client.Timeout = 500 * time.Millisecond
testBundleMan.bundleRetryDelay = 50 * time.Millisecond
// download blobs
@@ -116,7 +118,7 @@
// setup timeout
atomic.StoreInt32(blobServer.signedTimeout, 1)
atomic.StoreInt32(blobServer.blobTimeout, 1)
- testBundleMan.bundleDownloadConnTimeout = 100 * time.Millisecond
+ testBundleMan.client.Timeout = 100 * time.Millisecond
testBundleMan.bundleRetryDelay = 100 * time.Millisecond
testBundleMan.markDeploymentFailedAfter = 200 * time.Millisecond
diff --git a/init.go b/init.go
index d27b566..6384939 100644
--- a/init.go
+++ b/init.go
@@ -145,11 +145,13 @@
apiMan: apiMan,
concurrentDownloads: concurrentDownloads,
markDeploymentFailedAfter: markDeploymentFailedAfter,
- bundleDownloadConnTimeout: bundleDownloadConnTimeout,
bundleRetryDelay: time.Second,
bundleCleanupDelay: bundleCleanupDelay,
downloadQueue: make(chan *DownloadRequest, downloadQueueSize),
isClosed: new(int32),
+ client: &http.Client{
+ Timeout: bundleDownloadConnTimeout,
+ },
}
bundleMan.initializeBundleDownloading()