|
- package api
-
- import (
- "crypto/tls"
- "crypto/x509"
- "fmt"
- "os"
- )
-
- type ConfigJSON struct {
- EndPoint string `json:"endpoint"`
- RootCA string `json:"rootCA"`
- ClientCert string `json:"clientCert"`
- ClientKey string `json:"clientKey"`
- }
-
- func (c *ConfigJSON) Build() (Config, error) {
- rootCAPool := x509.NewCertPool()
-
- rootCAPem, err := os.ReadFile(c.RootCA)
- if err != nil {
- return Config{}, fmt.Errorf("reading root CA: %w", err)
- }
-
- if !rootCAPool.AppendCertsFromPEM(rootCAPem) {
- return Config{}, fmt.Errorf("parsing root CA failed")
- }
-
- cliCert, err := tls.LoadX509KeyPair(c.ClientCert, c.ClientKey)
- if err != nil {
- return Config{}, fmt.Errorf("loading client cert: %w", err)
- }
-
- return Config{
- EndPoint: c.EndPoint,
- RootCA: rootCAPool,
- Cert: cliCert,
- }, nil
- }
-
- type Config struct {
- EndPoint string
- RootCA *x509.CertPool
- Cert tls.Certificate
- }
|