Skip to content

Commit

Permalink
Add readme changes for basic auth (#3)
Browse files Browse the repository at this point in the history
* Add readme changes for basic auth

* Add Basic Auth Flag
- Uses environment variables since you could just add the credentials to the endpoint
- Updated the way we pass info to the Checkup Function, makes it more flexible

* Update Readme for more 'flair'
  • Loading branch information
JoseThen authored Jan 18, 2021
1 parent 8c758c5 commit f95ad44
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# checkup
CLI to run simple health checks against endpoints
<h1 align="center">Checkup</h1>
<p>CLI to run simple health checks against endpoints</p>
<p>
<a href="https://opensource.org/licenses/MIT">
<img alt="License: MIT" src="https://img.shields.io/github/license/JoseThen/checkup" target="_blank" />
</a>
</p>

<p align="center">
<img align="center" width="160px" src="./assets/gopher.png">
</p>

---
## Example Usage :
Expand All @@ -11,6 +20,7 @@ checkup listen -c 200 -e https://google.com
checkup listen --code 302 --endpoint https://potatoe.com/farm
checkup exam -f list.json
checkup exam --file list.yml
CU_USER=admin CU_PASS=pass go run main.go listen -e http://localhost:8080 -a # with basic auth
```

## Exam File Example :
Expand Down
Binary file added assets/gopher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions cmd/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ var examCmd = &cobra.Command{
Short: "Run a checkup against a file.",
Long: `Runs checkup against a file`,
Run: func(cmd *cobra.Command, args []string) {
// Flag Parsing
file, _ := cmd.Flags().GetString("file")
exitCode := 0
auth, _ := cmd.Flags().GetBool("auth")
exam := utils.ReadExam(file)
exitCode := 0
// Setup http Client
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
Expand All @@ -31,7 +34,14 @@ var examCmd = &cobra.Command{
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "--------", "----", "------", "----")
for _, test := range exam.Tests {
for _, path := range test.Paths {
checkup := utils.Checkup(httpClient, test.Code, exam.Endpoint+path)
// Setup check Request with above variables
checkRequest := &utils.CheckupRequest{
Client: httpClient,
Code: test.Code,
Endpoint: exam.Endpoint + path,
Auth: auth,
}
checkup := utils.Checkup(*checkRequest)
fmt.Fprintf(w, "\n %s\t%d\t%d\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Pass)
if checkup.Pass == false {
exitCode = 1
Expand Down
13 changes: 12 additions & 1 deletion cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ var listenCmd = &cobra.Command{
Short: "Ensure endpoint resolves with correct status code",
Long: `Send a request to a given endpoint and assert that the status code matches your expected status code.`,
Run: func(cmd *cobra.Command, args []string) {
// Flag parsing
code, _ := cmd.Flags().GetInt("code")
auth, _ := cmd.Flags().GetBool("auth")
endpoint, _ := cmd.Flags().GetString("endpoint")
// Setting up http Client
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
checkup := utils.Checkup(httpClient, code, endpoint)
// Setup check Request with above variables
checkRequest := &utils.CheckupRequest{
Client: httpClient,
Code: code,
Endpoint: endpoint,
Auth: auth,
}
// checkup := utils.Checkup(httpClient, code, endpoint, auth)
checkup := utils.Checkup(*checkRequest)
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "Endpoint", "Code", "Result", "Pass")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "--------", "----", "------", "----")
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.checkup.yaml)")
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.checkup.yaml)")
rootCmd.PersistentFlags().BoolP("auth", "a", false, "Toggle to look at env car CU_USER and CU_PASS for basic auth")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build run compile build-linux build-windows build-darwin test

VERSION ?= 0.1.0
VERSION ?= 0.2.0
NAME ?= "checkup"

test:
Expand Down
39 changes: 32 additions & 7 deletions utils/checkup.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package utils

import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
)

// CheckupRequest ... Struct holding all data needed for a checkup
type CheckupRequest struct {
Client *http.Client
Code int
Endpoint string
Auth bool
}

// CheckupResults ... Struct which holds a Checkup results
type CheckupResults struct {
Endpoint string
Expand All @@ -15,22 +25,37 @@ type CheckupResults struct {
}

// Checkup ... utility function to run one test
func Checkup(client *http.Client, code int, endpoint string) CheckupResults {
request, err := http.NewRequest("GET", endpoint, nil)
request.Header.Set("User-Agent", "CheckupCli/1.0")
func Checkup(healthForm CheckupRequest) CheckupResults {
request, err := http.NewRequest("GET", healthForm.Endpoint, nil)
if err != nil {
log.Fatalln(err)
}
resp, err := client.Do(request)
request.Header.Set("User-Agent", "CheckupCli/1.0")
if healthForm.Auth {
request.Header.Add("Authorization", "Basic "+addBasicAuth(os.Getenv("CU_USER"), os.Getenv("CU_PASS")))
healthForm.Client.CheckRedirect = addAuthOnRedirect
}
resp, err := healthForm.Client.Do(request)
if err != nil {
fmt.Printf("error in checkup function: %v", err)
}
defer resp.Body.Close()
results := CheckupResults{
Endpoint: endpoint,
Code: code,
Endpoint: healthForm.Endpoint,
Code: healthForm.Code,
Result: resp.StatusCode,
Pass: resp.StatusCode == code,
Pass: resp.StatusCode == healthForm.Code,
}
return results
}

func addBasicAuth(user, pass string) string {
auth := user + ":" + pass
return base64.StdEncoding.EncodeToString([]byte(auth))
}

// We need this function because on a redirect with net/http you lose your items in your header
func addAuthOnRedirect(req *http.Request, via []*http.Request) error {
req.Header.Add("Authorization", "Basic "+addBasicAuth(os.Getenv("CU_USER"), os.Getenv("CU_PASS")))
return nil
}

0 comments on commit f95ad44

Please sign in to comment.