diff --git a/README.md b/README.md index 67db553..04ad35e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ This action makes HEAD requests to a given URL until the required response code The URL to poll. Default `"http://localhost/"` - ### `responseCode` Response code to wait for. Default `"200"` @@ -21,14 +20,21 @@ Timeout before giving up in milliseconds. Default `"30000"` Interval between polling in ms. Default `"1000"` +### `username` / `password` + +For HTTP Basic Auth. Default: _empty_ + ## Example usage -``` -uses: nev7n/wait_for_response@v1 -with: - url: 'http://localhost:8081/' - responseCode: 200 - timeout: 2000 - interval: 500 + +```yaml +- uses: nev7n/wait_for_response@v1 + with: + url: 'http://localhost:8081/' + responseCode: 200 + timeout: 2000 + interval: 500 + username: ${{ secrets.BA_USERNAME }} + password: ${{ secrets.BA_PASSWORD }} ``` ## Development diff --git a/action.yml b/action.yml index bd7b5ee..be44a86 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,14 @@ inputs: description: 'Interval between polling in ms' required: false default: '1000' + username: + description: 'HTTP Basic Auth username' + required: false + default: '' + password: + description: 'HTTP Basic Auth password' + required: false + default: '' outputs: result: description: '0 if response code returned within timeout, otherwise 1' @@ -33,3 +41,5 @@ runs: - -code=${{ inputs.responseCode }} - -timeout=${{ inputs.timeout }} - -interval=${{ inputs.interval }} + - -username=${{ inputs.username }} + - -password=${{ inputs.password }} diff --git a/main/main.go b/main/main.go index 96ce4de..c383140 100644 --- a/main/main.go +++ b/main/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/base64" "flag" "fmt" "net/http" @@ -16,29 +17,52 @@ var ( func main() { var ( + // command flags url = flag.String("url", "http://localhost/", "URL to poll") responseCode = flag.Int("code", 200, "Response code to wait for") timeout = flag.Int("timeout", 30000, "Timeout before giving up in ms") interval = flag.Int("interval", 1000, "Interval between polling in ms") localhost = flag.String("localhost", "", "Ip address to use for localhost") + username = flag.String("username", "", "HTTP Basic Auth: username") + password = flag.String("password", "", "HTTP Basic Auth: password") ) flag.Parse() + var ( + // runtime + userAgent = fmt.Sprintf( + "nev7n/wait_for_response/%s (+https://github.com/nev7n/wait_for_response)", + version) + + startTime = time.Now() + timeoutDuration = time.Duration(*timeout) * time.Millisecond + intervalDuration = time.Duration(*interval) * time.Millisecond + basicAuth = "" + ) + fmt.Printf("Polling URL `%s` for response code %d for up to %d ms at %d ms intervals\n", *url, *responseCode, *timeout, *interval) - startTime := time.Now() - timeoutDuration := time.Duration(*timeout) * time.Millisecond - intervalDuration := time.Duration(*interval) * time.Millisecond - if *localhost != "" && strings.Contains(*url, "localhost") { *url = strings.ReplaceAll(*url, "localhost", *localhost) } - userAgent := fmt.Sprintf( - "nev7n/wait_for_response/%s (+https://github.com/nev7n/wait_for_response)", - version) + // validate both are set + if username != nil || password != nil { + if username == nil { + fmt.Printf("Missing username, password was set") + os.Exit(1) + } + + if password == nil { + fmt.Printf("Missing password, username was set (%s)", *username) + os.Exit(1) + } + + basicAuth = base64.StdEncoding.EncodeToString( + fmt.Appendf(nil, "%s:%s", *username, *password)) + } for { ctx, cancel := context.WithTimeout(context.Background(), intervalDuration) @@ -50,6 +74,10 @@ func main() { } req.Header.Set("User-Agent", userAgent) + if basicAuth != "" { + req.Header.Set("Authorization", "Basic "+basicAuth) + } + client := http.Client{} res, err := client.Do(req) cancel()