Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth header support. #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions http_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (

type httpDownloader struct {
downloader
username string
password string
username string
password string
headerName string
headerValue string
}

func (downloader httpDownloader) Download(remotePath, outputPath string) error {
Expand All @@ -39,6 +41,9 @@ func (downloader httpDownloader) Download(remotePath, outputPath string) error {
if downloader.username != "" && downloader.password != "" {
req.SetBasicAuth(downloader.username, downloader.password)
}
if downloader.headerName != "" && downloader.headerValue != "" {
req.Header.Set(downloader.headerName, downloader.headerValue)
}
resp, err := client.Do(req)
if err != nil {
return err
Expand Down Expand Up @@ -74,7 +79,9 @@ func (downloader httpDownloader) RemoteChecksum(checksumURL string) (string, err
if downloader.username != "" && downloader.password != "" {
req.SetBasicAuth(downloader.username, downloader.password)
}

if downloader.headerName != "" && downloader.headerValue != "" {
req.Header.Set(downloader.headerName, downloader.headerValue)
}
resp, err := client.Do(req)
if err != nil {
return "", errors.Wrap(err, "failed to get remote md5sum")
Expand Down
6 changes: 3 additions & 3 deletions http_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var (
testFilenameHash = "testfile.txt.md5"
testMD5 = "7b20fda6af27c1b59ebdd8c09a93e770"

testEmptyChecksumUrl = ""
testChecksumUrlPath = "custom.txt.md5"
testEmptyChecksumUrl = ""
testChecksumUrlPath = "custom.txt.md5"

testHashlessFilename = "nohash.txt"
testHashlessText = []byte("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include the new fields in the test?

Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *HttpDownloaderTestSuite) SetupTest() {
rw.Write(testText)
case "/" + testFilenameHash:
rw.Write([]byte(testMD5))
case "/" + testChecksumUrlPath:
case "/" + testChecksumUrlPath:
rw.Write([]byte(testMD5))
case "/" + testHashlessFilename:
rw.Write(testHashlessText)
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func init() {
pflag.String("http-proto", "https", "Set to 'http' if necessary")
pflag.String("http-user", "", "HTTP username for pulling the remote file")
pflag.String("http-pass", "", "HTTP password for pulling the remote file")

pflag.String("http-header-name", "", "HTTP header name")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's fill it with context. "HTTP header name for pulling the remote file"

pflag.String("http-header-value", "", "HTTP header value")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's fill it with context. "HTTP header value for pulling the remote file"

pflag.String("http-url", "", "Remote endpoint to retrieve the file from")
pflag.String("http-checksum-url", "", "Remote endpoint to retrieve the checksum from")
pflag.String("s3-arn", "", "Remote object ARN in S3 to retrieve")
Expand Down Expand Up @@ -168,6 +169,8 @@ func getAnsibleRepository(runDir string) error {
downloader := httpDownloader{
username: viper.GetString("http-user"),
password: viper.GetString("http-pass"),
headerName: viper.GetString("http-header-name"),
headerValue: viper.GetString("http-header-value"),
}
err = idempotentFileDownload(downloader, remoteHttpURL, checksumURL, localCacheFile)
} else if s3Obj != "" {
Expand Down