-
Notifications
You must be signed in to change notification settings - Fork 126
/
loader.go
113 lines (85 loc) · 2.9 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package loader
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/hatchet-dev/hatchet/internal/config/client"
"github.com/hatchet-dev/hatchet/internal/config/loader/loaderutils"
)
type ConfigLoader struct {
directory string
}
// LoadClientConfig loads the client configuration
func (c *ConfigLoader) LoadClientConfig() (res *client.ClientConfig, err error) {
sharedFilePath := filepath.Join(c.directory, "client.yaml")
configFileBytes, err := loaderutils.GetConfigBytes(sharedFilePath)
if err != nil {
return nil, err
}
cf, err := LoadClientConfigFile(configFileBytes...)
if err != nil {
return nil, err
}
return GetClientConfigFromConfigFile(cf)
}
// LoadClientConfigFile loads the worker config file via viper
func LoadClientConfigFile(files ...[]byte) (*client.ClientConfigFile, error) {
configFile := &client.ClientConfigFile{}
f := client.BindAllEnv
_, err := loaderutils.LoadConfigFromViper(f, configFile, files...)
return configFile, err
}
func GetClientConfigFromConfigFile(cf *client.ClientConfigFile) (res *client.ClientConfig, err error) {
f := client.BindAllEnv
_, err = loaderutils.LoadConfigFromViper(f, cf)
if err != nil {
return nil, fmt.Errorf("could not load config from viper: %w", err)
}
// if token is empty, throw an error
if cf.Token == "" {
return nil, fmt.Errorf("API token is required. Set it via the HATCHET_CLIENT_TOKEN environment variable.")
}
grpcBroadcastAddress := cf.HostPort
serverURL := cf.HostPort
tokenAddresses, err := getAddressesFromJWT(cf.Token)
if err == nil {
if grpcBroadcastAddress == "" && tokenAddresses.grpcBroadcastAddress != "" {
grpcBroadcastAddress = tokenAddresses.grpcBroadcastAddress
}
if tokenAddresses.serverURL != "" {
serverURL = tokenAddresses.serverURL
}
}
// if there's no broadcast address at this point, throw an error
if grpcBroadcastAddress == "" {
return nil, fmt.Errorf("GRPC broadcast address is required. Set it via the HATCHET_CLIENT_HOST_PORT environment variable.")
}
tlsServerName := cf.TLS.TLSServerName
// if the tls server name is empty, parse the domain from the host:port
if tlsServerName == "" {
// parse the domain from the host:port
domain, err := parseDomain(grpcBroadcastAddress)
if err != nil {
return nil, fmt.Errorf("could not parse domain: %w", err)
}
tlsServerName = domain.Hostname()
}
tlsConf, err := loaderutils.LoadClientTLSConfig(&cf.TLS, tlsServerName)
if err != nil {
return nil, fmt.Errorf("could not load TLS config: %w", err)
}
return &client.ClientConfig{
TenantId: cf.TenantId,
TLSConfig: tlsConf,
Token: cf.Token,
ServerURL: serverURL,
GRPCBroadcastAddress: grpcBroadcastAddress,
}, nil
}
func parseDomain(domain string) (*url.URL, error) {
if !strings.HasPrefix(domain, "http://") && !strings.HasPrefix(domain, "https://") {
domain = "https://" + domain
}
return url.Parse(domain)
}