107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
//"html/template"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
Unauthorized = errors.New("unauthorized")
|
||
|
Unauthenticated = errors.New("unauthenticated")
|
||
|
UnknownAuth = errors.New("unknown authentication method")
|
||
|
USER = "DUMMY_USER"
|
||
|
PASSWORD = "DUMMY_PASSWORD"
|
||
|
//dockerClient =
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
}
|
||
|
|
||
|
func verifyAuthentication(r *http.Request) error {
|
||
|
user, password, ok := r.BasicAuth()
|
||
|
if !ok {
|
||
|
return Unauthenticated
|
||
|
}
|
||
|
if user != USER || password != PASSWORD {
|
||
|
return Unauthorized
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func loginMiddleware(w http.ResponseWriter, r *http.Request) error {
|
||
|
err := verifyAuthentication(r)
|
||
|
switch err {
|
||
|
case Unauthorized:
|
||
|
http.Error(w, fmt.Sprint(err), http.StatusUnauthorized)
|
||
|
ip := r.Header.Get("X-Forwarded-For")
|
||
|
if ip == "" {
|
||
|
ip = r.RemoteAddr
|
||
|
}
|
||
|
log.Print("Failed auth from: ", ip)
|
||
|
return Unauthorized
|
||
|
case Unauthenticated:
|
||
|
w.Header().Set("www-authenticate", "Basic realm=\"OPENPOD\"")
|
||
|
http.Error(w, fmt.Sprint(err), http.StatusUnauthorized)
|
||
|
return Unauthenticated
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func manageHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
err := loginMiddleware(w, r)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
fmt.Fprint(w, `<html>
|
||
|
<head>
|
||
|
<title>OpenPOD Management</title>
|
||
|
</head>
|
||
|
<body>
|
||
|
<h1>OpenPOD Management</h1>
|
||
|
<div>
|
||
|
<form action="/cmd" method="post">
|
||
|
<label for="version_tag">New version:</label>
|
||
|
<input type="text" id="version_tag" name="version_tag">
|
||
|
<input type="submit" value="Submit">
|
||
|
</form>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>`)
|
||
|
}
|
||
|
|
||
|
func cmdHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method != "POST" {
|
||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||
|
return
|
||
|
}
|
||
|
r.ParseForm()
|
||
|
|
||
|
newVersion := r.FormValue("version_tag")
|
||
|
if newVersion == "" {
|
||
|
newVersion = "latest"
|
||
|
}
|
||
|
|
||
|
err := updateOpenPODVersion(newVersion)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
fmt.Fprint(w, "Success")
|
||
|
}
|
||
|
|
||
|
func updateOpenPODVersion(newVersion string) error {
|
||
|
log.Println("New OpenPOD version:", newVersion)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
http.HandleFunc("/", manageHandler)
|
||
|
http.HandleFunc("/cmd", cmdHandler)
|
||
|
|
||
|
log.Println("Starting on :8080")
|
||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||
|
}
|