Skip to content
Snippets Groups Projects
Unverified Commit 9e28a330 authored by Max Adamo's avatar Max Adamo
Browse files

testing bearer

parent f8f6666e
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,5 @@ module acme-web ...@@ -3,3 +3,5 @@ module acme-web
go 1.17 go 1.17
require github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 require github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
require gopkg.in/ini.v1 v1.67.0 // indirect
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/docopt/docopt-go" "github.com/docopt/docopt-go"
"gopkg.in/ini.v1"
) )
var ( var (
...@@ -17,6 +18,7 @@ var ( ...@@ -17,6 +18,7 @@ var (
buildTime string buildTime string
webDir string webDir string
jsonConverter string jsonConverter string
bearerToken string
WarningLogger *log.Logger WarningLogger *log.Logger
InfoLogger *log.Logger InfoLogger *log.Logger
ErrorLogger *log.Logger ErrorLogger *log.Logger
...@@ -65,6 +67,33 @@ func renderPage(w http.ResponseWriter, req *http.Request) { ...@@ -65,6 +67,33 @@ func renderPage(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, serveFile) http.ServeFile(w, req, serveFile)
} }
// trigger puppet
func triggerPuppet(w http.ResponseWriter, req *http.Request) {
cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1")
authToken := strings.Split(req.Header.Get("Authorization"), "Bearer ")[1]
statusFile := "/tmp/200.json"
// create json files on the fly
okMsg := []byte("{\n \"status\": \"OK\",\n \"response\": 200\n }")
koMsg := []byte("{\n \"status\": \"KO\",\n \"response\": 503\n }")
okErr := os.WriteFile("/tmp/200.json", okMsg, 0644)
koErr := os.WriteFile("/tmp/503.json", koMsg, 0644)
if okErr != nil || koErr != nil {
statusFile = "/tmp/503.json"
w.WriteHeader(http.StatusUnauthorized)
} else if authToken != bearerToken {
statusFile = "/tmp/503.json"
w.WriteHeader(http.StatusUnauthorized)
} else {
cmd.Run()
if verboseBool {
InfoLogger.Printf("HTTP Status %v", http.StatusOK)
}
w.Header().Set("Content-Type", "application/json")
}
http.ServeFile(w, req, statusFile)
}
// function redirect // function redirect
func redirect(w http.ResponseWriter, req *http.Request) { func redirect(w http.ResponseWriter, req *http.Request) {
redirectURL := filepath.Join(req.URL.Path, "/by_name.html") redirectURL := filepath.Join(req.URL.Path, "/by_name.html")
...@@ -76,7 +105,7 @@ func main() { ...@@ -76,7 +105,7 @@ func main() {
progName := filepath.Base(os.Args[0]) progName := filepath.Base(os.Args[0])
usage := fmt.Sprintf(`ACME Web: usage := fmt.Sprintf(`ACME Web:
- serve ACME HTML pages - serve ACME HTML pages, trigger Puppet, expose API
Usage: Usage:
%v [--json-converter=JSONCONVERTER] [--listen-address=LISTENADDRESS] [--listen-port=LISTENPORT] [--verbose] %v [--json-converter=JSONCONVERTER] [--listen-address=LISTENADDRESS] [--listen-port=LISTENPORT] [--verbose]
...@@ -85,13 +114,13 @@ Usage: ...@@ -85,13 +114,13 @@ Usage:
%v -v | --version %v -v | --version
Options: Options:
-h --help Show this screen -h --help Show this screen
-b --build Print version and build information and exit -b --build Print version and build information and exit
-v --version Print version information and exit -v --version Print version information and exit
--json-converter=JSONCONVERTER Path to json converter script [default: /usr/bin/cert2json.py] --json-converter=JSONCONVERTER Path to json converter script [default: /usr/bin/cert2json.py]
--listen-address=LISTENADDRESS Web server address. Check Go net/http documentation [default: any] --listen-address=LISTENADDRESS Web server address. Check Go net/http documentation [default: any]
--listen-port=LISTENPORT Web server port [default: 8000] --listen-port=LISTENPORT Web server port [default: 8000]
--verbose Log also successful connections --verbose Log also successful connections
`, progName, progName, progName, progName) `, progName, progName, progName, progName)
arguments, _ := docopt.ParseArgs(usage, nil, appVersion) arguments, _ := docopt.ParseArgs(usage, nil, appVersion)
...@@ -101,6 +130,13 @@ Options: ...@@ -101,6 +130,13 @@ Options:
os.Exit(0) os.Exit(0)
} }
cfg, err := ini.Load("/root/.acme.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
bearerToken = fmt.Sprintf("Bearer %v", cfg.Section("acme").Key("bearer_token").String())
webDir = "/var/www/acme_web" webDir = "/var/www/acme_web"
jsonConverter = arguments["--json-converter"].(string) jsonConverter = arguments["--json-converter"].(string)
verboseBool = arguments["--verbose"].(bool) verboseBool = arguments["--verbose"].(bool)
...@@ -116,10 +152,13 @@ Options: ...@@ -116,10 +152,13 @@ Options:
"/sectigo_ov/sectigo_ov.json", "/sectigo_ov/sectigo_ov_expired.json", "/sectigo_ov/sectigo_ov.json", "/sectigo_ov/sectigo_ov_expired.json",
"/sectigo_ev/by_name.html", "/sectigo_ev/by_date.html", "/sectigo_ev/by_name.html", "/sectigo_ev/by_date.html",
"/sectigo_ev/sectigo_ev.json", "/sectigo_ev/sectigo_ev_expired.json"} "/sectigo_ev/sectigo_ev.json", "/sectigo_ev/sectigo_ev_expired.json"}
puppetURL := "/puppet"
fs := http.FileServer(http.Dir("/var/www/acme_web/static")) fs := http.FileServer(http.Dir("/var/www/acme_web/static"))
http.Handle("/static/", http.StripPrefix("/static/", fs)) http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc(puppetURL, triggerPuppet)
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, filepath.Join(webDir, "index.html")) http.ServeFile(res, req, filepath.Join(webDir, "index.html"))
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment