Permalink
Browse files

Add basic auth for http resource (#327)

* Add basic auth for http resource

This patch will enable optional basic auth for the http resource. Therefore
it introduces two new parameters: username and password. If any of both is
set, the request will be performed using HTTP Basic Auth.

* Add integration test for http-basic-auth check

* Remove print statement

* Remove invalid import

* Update test count

* Fix test count

Got wrong how tests are counted.
  • Loading branch information...
Ntr0 authored and aelsabbahy committed May 20, 2018
1 parent df20bc4 commit cc29f25d853fd61f2a951f2bf80e54fd00b9a7cb
Showing with 45 additions and 5 deletions.
  1. +2 −0 add.go
  2. +8 −0 cmd/goss/goss.go
  3. +2 −0 docs/manual.md
  4. +9 −0 integration-tests/goss/goss-shared.yaml
  5. +2 −2 integration-tests/test.sh
  6. +7 −1 resource/http.go
  7. +13 −2 system/http.go
  8. +2 −0 util/config.go
2 add.go
@@ -21,6 +21,8 @@ func AddResources(fileName, resourceName string, keys []string, c *cli.Context)
AllowInsecure: c.Bool("insecure"),
NoFollowRedirects: c.Bool("no-follow-redirects"),
Server: c.String("server"),
Username: c.String("username"),
Password: c.String("password"),
}
var gossConfig GossConfig
@@ -285,6 +285,14 @@ func main() {
Name: "timeout",
Value: 5 * time.Second,
},
cli.StringFlag{
Name: "username, u",
Usage: "Username for basic auth",
},
cli.StringFlag{
Name: "password, p",
Usage: "Password for basic auth",
},
},
Action: func(c *cli.Context) error {
goss.AddResources(c.GlobalString("gossfile"), "HTTP", c.Args(), c)
@@ -575,6 +575,8 @@ http:
no-follow-redirects: false # Setting this to true will NOT follow redirects
timeout: 1000
body: [] # Check http response content for these patterns
username: "" # username for basic auth
password: "" # password for basic auth
```
@@ -164,6 +164,15 @@ http:
allow-insecure: false
timeout: 5000
body: []
https://httpbin.org/basic-auth/username/secret:
status: 200
username: username
password: secret
https://httpbin.org/basic-auth/username/secret:
status: 401
username: username
password: wrong
matching:
has_substr:
content: some string
@@ -44,9 +44,9 @@ out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" -g "/gos
echo "$out"
if [[ $os == "arch" ]]; then
egrep -q 'Count: 73, Failed: 0' <<<"$out"
egrep -q 'Count: 74, Failed: 0' <<<"$out"
else
egrep -q 'Count: 87, Failed: 0' <<<"$out"
egrep -q 'Count: 88, Failed: 0' <<<"$out"
fi
if [[ ! $os == "arch" ]]; then
@@ -14,6 +14,8 @@ type HTTP struct {
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout" yaml:"timeout"`
Body []string `json:"body" yaml:"body"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
}
func (u *HTTP) ID() string { return u.HTTP }
@@ -28,7 +30,9 @@ func (u *HTTP) Validate(sys *system.System) []TestResult {
if u.Timeout == 0 {
u.Timeout = 5000
}
sysHTTP := sys.NewHTTP(u.HTTP, sys, util.Config{AllowInsecure: u.AllowInsecure, NoFollowRedirects: u.NoFollowRedirects, Timeout: u.Timeout})
sysHTTP := sys.NewHTTP(u.HTTP, sys, util.Config{
AllowInsecure: u.AllowInsecure, NoFollowRedirects: u.NoFollowRedirects,
Timeout: u.Timeout, Username: u.Username, Password: u.Password})
sysHTTP.SetAllowInsecure(u.AllowInsecure)
sysHTTP.SetNoFollowRedirects(u.NoFollowRedirects)
@@ -54,6 +58,8 @@ func NewHTTP(sysHTTP system.HTTP, config util.Config) (*HTTP, error) {
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Timeout: config.Timeout,
Username: config.Username,
Password: config.Password,
}
return u, err
}
@@ -5,7 +5,6 @@ import (
"io"
"net/http"
"time"
"github.com/aelsabbahy/goss/util"
)
@@ -26,6 +25,8 @@ type DefHTTP struct {
Timeout int
loaded bool
err error
Username string
Password string
}
func NewDefHTTP(http string, system *System, config util.Config) HTTP {
@@ -34,6 +35,8 @@ func NewDefHTTP(http string, system *System, config util.Config) HTTP {
allowInsecure: config.AllowInsecure,
noFollowRedirects: config.NoFollowRedirects,
Timeout: config.Timeout,
Username: config.Username,
Password: config.Password,
}
}
@@ -57,7 +60,15 @@ func (u *DefHTTP) setup() error {
return http.ErrUseLastResponse
}
}
u.resp, u.err = client.Get(u.http)
req, err := http.NewRequest("GET", u.http, nil)
if err != nil {
return u.err
}
if u.Username != "" || u.Password != "" {
req.SetBasicAuth(u.Username, u.Password)
}
u.resp, u.err = client.Do(req)
return u.err
}
@@ -14,6 +14,8 @@ type Config struct {
AllowInsecure bool
NoFollowRedirects bool
Server string
Username string
Password string
}
type OutputConfig struct {

0 comments on commit cc29f25

Please sign in to comment.