Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HTTPS server skeleton #6

Merged
merged 19 commits into from
Apr 18, 2024
87 changes: 87 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Package server provides a server object that represents the GoCert backend
package server

import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"time"

"gopkg.in/yaml.v3"
)

type ConfigYAML struct {
KeyPath string
CertPath string
DBPath string
Port int
}

type Config struct {
Key []byte
Cert []byte
DBPath string
Port int
}

func ValidateConfigFile(filePath string) (Config, error) {
validationErr := errors.New("config file validation failed: ")
config := Config{}
configYaml, err := os.ReadFile(filePath)
if err != nil {
return config, errors.Join(validationErr, err)
}
c := ConfigYAML{}
if err := yaml.Unmarshal(configYaml, &c); err != nil {
return config, errors.Join(validationErr, err)
}
cert, err := os.ReadFile(c.CertPath)
if err != nil {
return config, errors.Join(validationErr, err)
}
key, err := os.ReadFile(c.KeyPath)
if err != nil {
return config, errors.Join(validationErr, err)
}
if _, err := os.OpenFile(c.DBPath, os.O_CREATE|os.O_RDONLY, 0644); err != nil {
ghislainbourgeois marked this conversation as resolved.
Show resolved Hide resolved
return config, errors.Join(validationErr, err)
}
config.Cert = cert
config.Key = key
config.DBPath = c.DBPath
config.Port = c.Port
return config, nil
}

// NewServer creates a new http server with handlers that Go can start listening to
func NewServer(certificate, key []byte, port int) (*http.Server, error) {
serverCerts, err := tls.X509KeyPair(certificate, key)
if err != nil {
return nil, err
}
router := http.NewServeMux()
router.HandleFunc("GET /", HealthCheck)

v1 := http.NewServeMux()
v1.Handle("/v1/", http.StripPrefix("/v1", router))

s := &http.Server{
gruyaume marked this conversation as resolved.
Show resolved Hide resolved
Addr: fmt.Sprintf(":%d", port),

ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: v1,
MaxHeaderBytes: 1 << 20,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{serverCerts},
},
}

return s, nil
}

func HealthCheck(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Server Alive")) //nolint:errcheck
}
186 changes: 186 additions & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package server_test

import (
"log"
"os"
"testing"

server "github.com/canonical/gocert/api"
)

const (
validCert = `-----BEGIN CERTIFICATE-----
MIIELjCCAxagAwIBAgICBnowDQYJKoZIhvcNAQELBQAwJzELMAkGA1UEBhMCVVMx
GDAWBgNVBAoTD0Nhbm9uaWNhbCwgSU5DLjAeFw0yNDA0MDUxMDAzMjhaFw0zNDA0
MDUxMDAzMjhaMCcxCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9DYW5vbmljYWwsIElO
Qy4wggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDAP98jcfNw40HbS1xR
6UpSQTp4AGldFWQZBOFaVzD+eh7sYM/BFdT0dZRHGjXxL77ewDbwdwAFJ5zuxo+u
8/VgKGRpK6KCnKailmVrdRDhA45airMRQN6QXurN4NZgXcCHJWGAQKA9XJzcwGJF
l5LxoFY58wCv0d1JP8fgmbcgIRQTCIvhrlgrJ5Acz9QP6BuaxEHKbYYvWyTWtAhi
HS/w51yEbh6959ceJGBDZPyEVd9sfGipvHrA73+33+XBluRcUuWV4dCecyP/m+8C
jTBmW5s8gS6JUDE8yl99qm7CnXTkNDqPXThrorcKRwcHrw3ZEOm5rUPLuyzGBx/C
DZUbY9bsvHJMHOHlbwiY+M2MFIO+3H6qyfPfcHs8NFkrZh/as+9hrEzSYcz+tGBi
NynkSmNPQi4yzT00ilKYgcBhPdDDlBbdhcmdeFA3XE880VkQdJgefsYpCgYRdILm
DDd6ZMfZsQOJjuRC8rQKLO+z1X5JhiOlkNxZaOkq9b9eu7230rxTFCGocn0l9oKw
0q8OIDOTb7UKdIaGq/y++uRxe0hhNoijN1OJ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c51Q91JTVptv4OpDBcUswLZjAf94nCO5NS4Usy/1OVC5sa7M
K9tDCdREllkTk5xNfeYpoj1ZKF6HFt+/ZiiCbTqtK6M8V8uwFVQzYHdGiLqRywc+
1Ke4JG0rvqu0a8Srkgp/iKlswCKOUB6zi75wAI7BAEYEUkIL3/K74/c1AAkZs4L2
vXYKrlR+FIfcdUjvKESLBIFDL29D9qKHj+4pQ22F+suK6f87qrtKXchIwQ4gIr8w
umjCv8WtINco0VbqeLlUJCAk4FYTuH0xAoIBAQCA+A2l7DCMCb7MjkjdyNFqkzpg
2ou3WkCf3j7txqg8oGxQ5eCg45BU1zTOW35YVCtP/PMU0tLo7iPudL79jArv+GfS
6SbLz3OEzQb6HU9/4JA5fldHv+6XJLZA27b8LnfhL1Iz6dS+MgH53+OJdkQBc+Dm
Q53tuiWQeoxNOjHiWstBPELxGbW6447JyVVbNYGUk+VFU7okzA6sRTJ/5Ysda4Sf
auNQc2hruhr/2plhFUYoZHPzGz7d5zUGKymhCoS8BsFVtD0WDL4srdtY/W2Us7TD
D7DC34n8CH9+avz9sCRwxpjxKnYW/BeyK0c4n9uZpjI8N4sOVqy6yWBUseww
-----END RSA PRIVATE KEY-----`
validConfig = `keypath: "./key_test.pem"
certpath: "./cert_test.pem"
dbpath: "./certs.db"
port: 8000`
invalidYAMLConfig = `wrong: fields
every: where`
invalidFileConfig = `keypath: "./nokeyfile.pem"
certpath: "./nocertfile.pem"
dbpath: "./certs.db"
port: 8000`
)

func TestMain(m *testing.M) {
testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
}
writeConfigErr := os.WriteFile(testfolder+"/config.yaml", []byte(validConfig), 0644)
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0644)
if writeConfigErr != nil || writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
}
os.Exit(exitval)
}

func TestNewServerSuccess(t *testing.T) {
s, err := server.NewServer([]byte(validCert), []byte(validPK), 8000)
if err != nil {
t.Errorf("Error occured: %s", err)
}
if s.TLSConfig.Certificates == nil {
t.Errorf("No certificates were configured for server")
}
}

func TestNewServerFail(t *testing.T) {
testCases := []struct {
desc string
cert string
key string
}{
{
desc: "wrong certificate",
cert: "some cert",
key: validPK,
},
{
desc: "wrong key",
cert: validCert,
key: "some pk",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
_, err := server.NewServer([]byte(tC.cert), []byte(tC.key), 8000)
if err == nil {
t.Errorf("Expected error")
}
})
}
}

func TestConfigFileSuccess(t *testing.T) {
config, err := server.ValidateConfigFile("./config.yaml")
if err != nil {
t.Errorf("Error occured: %s", err)
}
if config.Cert == nil || config.Key == nil || config.DBPath == "" {
t.Errorf("Expected values were not read: %s", err)
}
}

func TestConfigFileFail(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}
33 changes: 32 additions & 1 deletion cmd/gocert/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package main

import (
"flag"
"log"
"os"

server "github.com/canonical/gocert/api"
"github.com/canonical/gocert/internal/certdb"
)

func main() {
// ListenAndServe
log.SetOutput(os.Stderr)
configFilePtr := flag.String("config", "", "The config file to be provided to the server")
flag.Parse()

if *configFilePtr == "" {
log.Fatalf("Providing a valid config file is required.")
}
config, err := server.ValidateConfigFile(*configFilePtr)
if err != nil {
log.Fatalf("Config file validation failed: %s.", err)
}
_, err = certdb.NewCertificateRequestsRepository(config.DBPath, "CertificateRequests")
if err != nil {
log.Fatalf("Couldn't connect to database: %s", err)
}
srv, err := server.NewServer(config.Cert, config.Key, config.Port)
if err != nil {
log.Fatalf("Couldn't create server: %s", err)
}
log.Printf("Starting server at %s", srv.Addr)
if err := srv.ListenAndServeTLS("", ""); err != nil {
log.Fatalf("Server ran into error: %s", err)
}
}
Loading
Loading