mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
client: moved cacert logic from 'serve' cmd to client
This commit is contained in:
parent
043f8ec120
commit
dac6c2306f
3 changed files with 70 additions and 59 deletions
28
cmd/serve.go
28
cmd/serve.go
|
|
@ -4,15 +4,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/OpenCHAMI/configurator/pkg/generator"
|
"github.com/OpenCHAMI/configurator/pkg/generator"
|
||||||
"github.com/OpenCHAMI/configurator/pkg/server"
|
"github.com/OpenCHAMI/configurator/pkg/server"
|
||||||
|
|
@ -48,7 +44,7 @@ var serveCmd = &cobra.Command{
|
||||||
fmt.Printf("%v\n", string(b))
|
fmt.Printf("%v\n", string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
// set up the routes and start the server
|
// set up the routes and start the serve
|
||||||
server := server.Server{
|
server := server.Server{
|
||||||
Config: &config,
|
Config: &config,
|
||||||
Server: &http.Server{
|
Server: &http.Server{
|
||||||
|
|
@ -66,28 +62,8 @@ var serveCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// add cert to client if `--cacert` flag is passed
|
|
||||||
if cacertPath != "" {
|
|
||||||
cacert, _ := os.ReadFile(cacertPath)
|
|
||||||
certPool := x509.NewCertPool()
|
|
||||||
certPool.AppendCertsFromPEM(cacert)
|
|
||||||
server.Transport = &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{
|
|
||||||
RootCAs: certPool,
|
|
||||||
InsecureSkipVerify: true,
|
|
||||||
},
|
|
||||||
DisableKeepAlives: true,
|
|
||||||
Dial: (&net.Dialer{
|
|
||||||
Timeout: 120 * time.Second,
|
|
||||||
KeepAlive: 120 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 120 * time.Second,
|
|
||||||
ResponseHeaderTimeout: 120 * time.Second,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// start listening with the server
|
// start listening with the server
|
||||||
err := server.Serve()
|
err := server.Serve(cacertPath)
|
||||||
if errors.Is(err, http.ErrServerClosed) {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Printf("Server closed.")
|
fmt.Printf("Server closed.")
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ type (
|
||||||
TemplatePaths []string
|
TemplatePaths []string
|
||||||
PluginPath string
|
PluginPath string
|
||||||
Target string
|
Target string
|
||||||
|
Client *configurator.SmdClient
|
||||||
Verbose bool
|
Verbose bool
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -409,17 +410,24 @@ func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
|
func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
// load generator plugins to generate configs or to print
|
// load generator plugins to generate configs or to print
|
||||||
var (
|
var (
|
||||||
|
client configurator.SmdClient
|
||||||
|
target configurator.Target
|
||||||
|
generator Generator
|
||||||
|
err error
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
// check if we have a client from params first and create new one if not
|
||||||
|
if params.Client == nil {
|
||||||
client = configurator.NewSmdClient(
|
client = configurator.NewSmdClient(
|
||||||
configurator.WithHost(config.SmdClient.Host),
|
configurator.WithHost(config.SmdClient.Host),
|
||||||
configurator.WithPort(config.SmdClient.Port),
|
configurator.WithPort(config.SmdClient.Port),
|
||||||
configurator.WithAccessToken(config.AccessToken),
|
configurator.WithAccessToken(config.AccessToken),
|
||||||
configurator.WithCertPoolFile(config.CertPath),
|
configurator.WithCertPoolFile(config.CertPath),
|
||||||
)
|
)
|
||||||
target configurator.Target
|
} else {
|
||||||
generator Generator
|
client = *params.Client
|
||||||
err error
|
}
|
||||||
ok bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// check if a target is supplied
|
// check if a target is supplied
|
||||||
if len(params.Args) == 0 && params.Target == "" {
|
if len(params.Args) == 0 && params.Target == "" {
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,11 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -60,13 +63,33 @@ func New(config *configurator.Config) *Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main function to start up configurator as a service.
|
// Main function to start up configurator as a service.
|
||||||
func (s *Server) Serve() error {
|
func (s *Server) Serve(cacertPath string) error {
|
||||||
// create client just for the server to use to fetch data from SMD
|
// create client just for the server to use to fetch data from SMD
|
||||||
_ = &configurator.SmdClient{
|
client := &configurator.SmdClient{
|
||||||
Host: s.Config.SmdClient.Host,
|
Host: s.Config.SmdClient.Host,
|
||||||
Port: s.Config.SmdClient.Port,
|
Port: s.Config.SmdClient.Port,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add cert to client if `--cacert` flag is passed
|
||||||
|
if cacertPath != "" {
|
||||||
|
cacert, _ := os.ReadFile(cacertPath)
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
certPool.AppendCertsFromPEM(cacert)
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
RootCAs: certPool,
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
DisableKeepAlives: true,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 120 * time.Second,
|
||||||
|
KeepAlive: 120 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 120 * time.Second,
|
||||||
|
ResponseHeaderTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set the server address with config values
|
// set the server address with config values
|
||||||
s.Server.Addr = fmt.Sprintf("%s:%d", s.Config.Server.Host, s.Config.Server.Port)
|
s.Server.Addr = fmt.Sprintf("%s:%d", s.Config.Server.Host, s.Config.Server.Port)
|
||||||
|
|
||||||
|
|
@ -104,12 +127,12 @@ func (s *Server) Serve() error {
|
||||||
)
|
)
|
||||||
|
|
||||||
// protected routes if using auth
|
// protected routes if using auth
|
||||||
r.HandleFunc("/generate", s.Generate)
|
r.HandleFunc("/generate", s.Generate(client))
|
||||||
r.HandleFunc("/templates", s.ManageTemplates)
|
r.HandleFunc("/templates", s.ManageTemplates)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// public routes without auth
|
// public routes without auth
|
||||||
router.HandleFunc("/generate", s.Generate)
|
router.HandleFunc("/generate", s.Generate(client))
|
||||||
router.HandleFunc("/templates", s.ManageTemplates)
|
router.HandleFunc("/templates", s.ManageTemplates)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,9 +150,12 @@ func (s *Server) Close() {
|
||||||
// This is the corresponding service function to generate templated files, that
|
// This is the corresponding service function to generate templated files, that
|
||||||
// works similarly to the CLI variant. This function takes similiar arguments as
|
// works similarly to the CLI variant. This function takes similiar arguments as
|
||||||
// query parameters that are included in the HTTP request URL.
|
// query parameters that are included in the HTTP request URL.
|
||||||
func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) Generate(client *configurator.SmdClient) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// get all of the expect query URL params and validate
|
// get all of the expect query URL params and validate
|
||||||
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
||||||
|
s.GeneratorParams.Client = client
|
||||||
if s.GeneratorParams.Target == "" {
|
if s.GeneratorParams.Target == "" {
|
||||||
writeErrorResponse(w, "must specify a target")
|
writeErrorResponse(w, "must specify a target")
|
||||||
return
|
return
|
||||||
|
|
@ -154,6 +180,7 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
writeErrorResponse(w, "failed to write response: %v", err)
|
writeErrorResponse(w, "failed to write response: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incomplete WIP function for managing templates remotely. There is currently no
|
// Incomplete WIP function for managing templates remotely. There is currently no
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue