First implementation of new config format

- Functionally equivalent to old version at present
- Temperature and Humidity sensors not handled yet
- Moved HTTP handlers around
This commit is contained in:
Bendodroid 2024-01-13 23:26:04 +01:00
parent 7bb676887f
commit b2f62c7bb0
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
7 changed files with 125 additions and 121 deletions

View file

@ -1,11 +1,54 @@
package config package config
// Configuration represents the settings needed to configure spaceapid import (
type Configuration struct { "bytes"
// The HTTP BasicAuth username for door status updates "encoding/json"
BAUsername string "log"
// The HTTP BasicAuth password for door status updates "os"
BAPassword string "path/filepath"
// The path to the JSON with initial values "slices"
TemplatePath string )
const (
envConfigPath = "CONFIG_PATH"
)
// getConfigPath gets the spaceapid configuration from the respective environment variables
func getConfigPath() string {
// JSON template path
configPath, ok := os.LookupEnv(envConfigPath)
if !ok || configPath == "" {
log.Fatalln("Could not retrieve", envConfigPath, "env variable or variable is empty")
}
// Save as absolute path
absConfigPath, err := filepath.Abs(configPath)
if err != nil {
log.Fatalln("Failed converting", configPath, "to absolute path:", err)
}
return absConfigPath
}
// ParseConfiguration returns the config from file
func ParseConfiguration() (conf SpaceapidConfig) {
log.Println("Parsing configuration file")
// Read file
file, err := os.ReadFile(getConfigPath())
if err != nil {
log.Fatalln("Failed reading file:", err)
}
// Parse JSON
dec := json.NewDecoder(bytes.NewReader(file))
dec.DisallowUnknownFields()
err = dec.Decode(&conf)
if err != nil {
log.Fatalln("Could not parse spaceapid config file:", err)
}
// Check if compatible with v14
if !slices.Contains(conf.Response.APICompatibility, "14") {
log.Fatalln("Provided file doesn't specify compatibility with API version 14")
}
return
} }

35
handlers/root.go Normal file
View file

@ -0,0 +1,35 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Check if GET method
if r.Method != http.MethodGet {
log.Println("Wrong METHOD from", r.RemoteAddr)
w.Header().Set("Allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Serialize response
response, err := json.Marshal(resp)
if err != nil {
log.Println("Failed to serialize JSON response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// Respond with OK
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}
}

View file

@ -1,49 +1,25 @@
package handlers package handlers
import ( import (
"encoding/json"
"io" "io"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types" "gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
) )
func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Check if GET method
if r.Method != http.MethodGet {
log.Println("Wrong METHOD from", r.RemoteAddr)
w.Header().Set("Allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Serialize response
response, err := json.Marshal(resp)
if err != nil {
log.Println("Failed to serialize JSON response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// Respond with OK
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}
}
func StateOpen( func StateOpen(
validUsername, validPassword string, resp *types.SpaceAPIResponseV14, authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceAPIResponseV14,
) func(http.ResponseWriter, *http.Request) { ) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// Check BasicAuth credentials // Check BasicAuth credentials
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !ok || username != validUsername || password != validPassword { if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
log.Println("Unauthorized request from", r.RemoteAddr) log.Println("Unauthorized request from", r.RemoteAddr)
w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"") w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"")
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)

20
main.go
View file

@ -7,20 +7,18 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers" "gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types" "gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util" "gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
) )
func main() { func main() {
log.Println("Reading configuration values") // Get spaceapid configuration
config := util.GetConfiguration() conf := config.ParseConfiguration()
log.Println("Reading initial SpaceAPI response from", config.TemplatePath)
spaceApiResponse := util.ParseTemplate(config.TemplatePath)
// Merge old state if present // Merge old state if present
util.MergeOldState(&spaceApiResponse) util.MergeOldState(&conf.Response)
// Register signal handler // Register signal handler
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
@ -30,11 +28,15 @@ func main() {
log.Println("Saving state and shutting down...") log.Println("Saving state and shutting down...")
util.SaveCurrentState(*resp) util.SaveCurrentState(*resp)
os.Exit(0) os.Exit(0)
}(&spaceApiResponse) }(&conf.Response)
// Register HTTP handlers // Register HTTP handlers
http.HandleFunc("/", handlers.Root(&spaceApiResponse)) http.HandleFunc("/",
http.HandleFunc("/state/open", handlers.StateOpen(config.BAUsername, config.BAPassword, &spaceApiResponse)) handlers.Root(&conf.Response),
)
http.HandleFunc("/state/open",
handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response),
)
// Start webserver // Start webserver
log.Println("Starting HTTP server...") log.Println("Starting HTTP server...")

View file

@ -1,48 +0,0 @@
package util
import (
"log"
"os"
"path/filepath"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
)
const (
envBAUsername = "BA_USERNAME"
envBAPassword = "BA_PASSWORD"
envJSONTemplatePath = "JSON_TEMPLATE_PATH"
)
// GetConfiguration gets the spaceapid configuration from the respective environment variables
func GetConfiguration() (c config.Configuration) {
var (
success bool
err error
)
// HTTP BasicAuth username
c.BAUsername, success = os.LookupEnv(envBAUsername)
if !success || c.BAUsername == "" {
log.Fatalln("Could not retrieve env variable", envBAUsername, "or variable is empty")
}
// HTTP BasicAuth password
c.BAPassword, success = os.LookupEnv(envBAPassword)
if !success || c.BAPassword == "" {
log.Fatalln("Could not retrieve", envBAPassword, "env variable or variable is empty")
}
// JSON template path
templatePath, success := os.LookupEnv(envJSONTemplatePath)
if !success || templatePath == "" {
log.Fatalln("Could not retrieve", envJSONTemplatePath, "env variable or variable is empty")
}
// Save as absolute path
c.TemplatePath, err = filepath.Abs(templatePath)
if err != nil {
log.Fatalln("Failed converting", templatePath, "to absolute path:", err)
}
return
}

20
util/credentials.go Normal file
View file

@ -0,0 +1,20 @@
package util
import (
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
)
// CheckCredentials validates whether a given username/password pair matches an
// entry in the authDB whose id is present in the validCredentials list
func CheckCredentials(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, username, password string,
) bool {
for _, id := range validCredentials {
if cred, present := authDB[id]; present {
if cred.Username == username && cred.Password == password {
return true
}
}
}
return false
}

View file

@ -1,42 +1,18 @@
package util package util
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"log" "log"
"os" "os"
"path" "path"
"slices"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types" "gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
) )
const savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json" const (
savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
// ParseTemplate parses the given file and )
func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) {
// Read template file
template, err := os.ReadFile(file)
if err != nil {
log.Fatalln("Failed reading file:", err)
}
// Parse JSON
dec := json.NewDecoder(bytes.NewReader(template))
dec.DisallowUnknownFields()
err = dec.Decode(&resp)
if err != nil {
log.Fatalln("Could not parse SpaceAPI response template:", err)
}
// Check if compatible with v14
if !slices.Contains(resp.APICompatibility, "14") {
log.Fatalln("Provided template doesn't specify compatibility with API version 14")
}
return
}
// MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes // MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes
// the file containing the old state. // the file containing the old state.