Skip to content

Commit c155df6

Browse files
committed
Implemented basic auth for remote archives
1 parent f31958a commit c155df6

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

RunSettings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type RunSettings struct {
1717
skeletonPath string
1818
targetPath string
1919

20+
basicAuthUser string
21+
basicAuthPass string
22+
2023
inspectionRun bool
2124
addRegistry bool
2225
showRegistry bool
@@ -40,6 +43,8 @@ var FLAGS = []string{
4043
"-l",
4144
"-f",
4245
"-w",
46+
"-bu",
47+
"-bp",
4348
"-flags",
4449
}
4550

@@ -54,6 +59,8 @@ func FindRunSettings() (RunSettings, error) {
5459
var err error
5560

5661
flag.StringVar(&parameterGroup, "p", "", "Semicolon-separated list of parameters in k=v form.")
62+
flag.StringVar(&ret.basicAuthUser, "bu", "", "The 'user' to use when a remote archive requests Basic Authentication")
63+
flag.StringVar(&ret.basicAuthPass, "bp", "", "The 'password' to use when a remote archive requests Basic Authentication")
5764
flag.BoolVar(&ret.inspectionRun, "i", false, "Whether or not to show a list of available parameters for the skeleton")
5865
flag.BoolVar(&ret.addRegistry, "a", false, "Whether or not to register the template at the given path (can be http/https URLs)")
5966
flag.BoolVar(&ret.showRegistry, "l", false, "Whether or not to show a list of all available registered templates")

TemplateRegistry.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ func (this TemplateRegistry) GetTemplatePath(name string) (string, error) {
109109
Registers the given [path] in the registry, by copying the archive file to it.
110110
If the file is not an archive, or it cannot be read, or the registry cannot be written,
111111
an error is returned.
112+
If the given [path] is remote (such as http), and if the server requires authentication,
113+
the given [username] and [password] will be used.
114+
If the path is local, username/password are inconsequential.
112115
*/
113-
func (this *TemplateRegistry) RegisterTemplate(path string) (string, error) {
116+
func (this *TemplateRegistry) RegisterTemplate(path string, username string, password string) (string, error) {
114117

115118
var targetPath, name string
116119
var err error
117120

118121
// if this is a remote path, download as a zip to a temporary directory before trying to register.
119122
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
120123

121-
path, err = downloadArchive(path)
124+
path, err = downloadArchive(path, username, password)
122125
if(err != nil) {
123126
return "", err
124127
}
@@ -145,9 +148,11 @@ func (this *TemplateRegistry) RegisterTemplate(path string) (string, error) {
145148
Downloads the given [url] to a temporary directory (as a .zip).
146149
Returns the temporary path to the downloaded archive, or an error if it encountered one.
147150
*/
148-
func downloadArchive(targetURL string) (string, error) {
151+
func downloadArchive(targetURL string, username string, password string) (string, error) {
149152

153+
var request *http.Request
150154
var response *http.Response
155+
var client http.Client
151156
var outputFile *os.File
152157
var parsedURL *url.URL
153158
var outputPath string
@@ -164,6 +169,12 @@ func downloadArchive(targetURL string) (string, error) {
164169
return "", err
165170
}
166171

172+
request, err = http.NewRequest("GET", targetURL, nil)
173+
if(err != nil) {
174+
return "", err
175+
}
176+
request.SetBasicAuth(username, password)
177+
167178
baseName = filepath.Base(parsedURL.Path)
168179
baseName = strings.TrimSuffix(baseName, filepath.Ext(baseName))
169180
outputPath = filepath.Join(outputPath, baseName + ".zip")
@@ -175,12 +186,12 @@ func downloadArchive(targetURL string) (string, error) {
175186
}
176187
defer outputFile.Close()
177188

178-
response, err = http.Get(targetURL)
189+
response, err = client.Do(request)
179190
if(err != nil) {
180191
return "", err
181192
}
182193

183-
if(response.Status != "200") {
194+
if(!strings.Contains(response.Status, "200")) {
184195
errorMsg := fmt.Sprintf("Unable to download remote archive: HTTP %s\n", response.Status)
185196
return "", errors.New(errorMsg)
186197
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func addRegistry(settings RunSettings, registry *TemplateRegistry) {
5656
var name string
5757
var err error
5858

59-
name, err = registry.RegisterTemplate(settings.skeletonPath)
59+
name, err = registry.RegisterTemplate(settings.skeletonPath, settings.basicAuthUser, settings.basicAuthPass)
6060
if err != nil {
6161

6262
// TODO: I'm deeply uncomfortable with using "exitWith" outside of the actual

0 commit comments

Comments
 (0)