Debian Codegraph @

internal/config/validate.go

raw
1 package config
2
3 import (
4 "errors"
5 "fmt"
6 "io"
7 "net/netip"
8 "slices"
9 "strings"
10
11 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
12 )
13
14 // Validate validates the config.
15 func Validate(mode int, conf *Config) error {
16 if err := validateOAuth2Config(conf); err != nil {
17 return err
18 }
19
20 if err := validateHTTPConfig(conf); err != nil {
21 return err
22 }
23
24 return validateOpenVPNConfig(mode, conf)
25 }
26
27 func validateOpenVPNConfig(mode int, conf *Config) error {
28 if conf.OpenVPN.EnforceUniqueUser && mode != ManagementClient {
29 return errors.New("openvpn.enforce-unique-user requires the OpenVPN management interface")
30 }
31
32 if mode != ManagementClient {
33 return nil
34 }
35
36 if conf.OpenVPN.Addr.IsEmpty() {
37 return fmt.Errorf("openvpn.addr is %w", ErrRequired)
38 }
39
40 if !slices.Contains([]string{SchemeTCP, SchemeUNIX}, conf.OpenVPN.Addr.Scheme) {
41 return errors.New("openvpn.addr: invalid URL. only tcp://addr or unix://addr scheme supported")
42 }
43
44 return nil
45 }
46
47 // validateHTTPConfig validates the HTTP configuration.
48 func validateHTTPConfig(conf *Config) error {
49 if conf.HTTP.Secret.String() == "" {
50 return fmt.Errorf("http.secret is %w", ErrRequired)
51 }
52
53 if err := validateEncryptionSecret(conf.HTTP.Secret); err != nil {
54 return fmt.Errorf("http.secret %w", err)
55 }
56
57 if conf.HTTP.BaseURL.IsEmpty() {
58 return fmt.Errorf("http.baseurl is %w", ErrRequired)
59 }
60
61 if err := validateURL(conf.HTTP.BaseURL); err != nil {
62 return fmt.Errorf("http.baseurl: %w", err)
63 }
64
65 if conf.HTTP.EnableProxyHeaders {
66 if len(conf.HTTP.TrustedProxies) == 0 {
67 return fmt.Errorf("http.trusted-proxies is %w when http.enable-proxy-headers is true", ErrRequired)
68 }
69
70 for _, trustedProxy := range conf.HTTP.TrustedProxies {
71 trustedProxy = strings.TrimSpace(trustedProxy)
72 if _, err := netip.ParsePrefix(trustedProxy); err != nil {
73 return fmt.Errorf("http.trusted-proxies: invalid CIDR %q: %w", trustedProxy, err)
74 }
75 }
76 }
77
78 if err := conf.HTTP.Template.Execute(io.Discard, map[string]string{
79 "title": "",
80 "message": "",
81 "errorID": "",
82 }); err != nil {
83 return fmt.Errorf("invalid rendering http.template: %w", err)
84 }
85
86 return nil
87 }
88
89 // validateOAuth2Config validates the OAuth2 configuration.
90 //
91 //nolint:cyclop
92 func validateOAuth2Config(conf *Config) error {
93 if conf.OAuth2.Issuer.IsEmpty() {
94 return fmt.Errorf("oauth2.issuer is %w", ErrRequired)
95 }
96
97 if err := validateURL(conf.OAuth2.Issuer); err != nil {
98 return fmt.Errorf("oauth2.issuer: %w", err)
99 }
100
101 if conf.OAuth2.Client.ID == "" {
102 return fmt.Errorf("oauth2.client.id is %w", ErrRequired)
103 }
104
105 if conf.OAuth2.Client.Secret.String() == "" && conf.OAuth2.Client.PrivateKey.String() == "" {
106 return fmt.Errorf("one of oauth2.client.private-key or oauth2.client.secret is %w", ErrRequired)
107 }
108
109 if conf.OAuth2.Client.Secret.String() != "" && conf.OAuth2.Client.PrivateKey.String() != "" {
110 return errors.New("only one of oauth2.client.private-key or oauth2.client.secret is allowed")
111 }
112
113 if err := validateURL(conf.OAuth2.Endpoints.Discovery); err != nil {
114 return fmt.Errorf("oauth2.endpoint.discovery: %w", err)
115 }
116
117 if err := validateURL(conf.OAuth2.Endpoints.Token); err != nil {
118 return fmt.Errorf("oauth2.endpoint.token: %w", err)
119 }
120
121 if err := validateURL(conf.OAuth2.Endpoints.Auth); err != nil {
122 return fmt.Errorf("oauth2.endpoint.auth: %w", err)
123 }
124
125 if conf.OAuth2.Refresh.Enabled {
126 if err := validateEncryptionSecret(conf.OAuth2.Refresh.Secret); err != nil {
127 return fmt.Errorf("oauth2.refresh.secret %w", err)
128 }
129 }
130
131 if conf.OpenVPN.ClientConfig.Enabled {
132 if conf.OpenVPN.ClientConfig.Expression == "" {
133 return errors.New("openvpn.client-config.expression is required when openvpn.client-config.enabled is true")
134 }
135
136 file, err := conf.OpenVPN.ClientConfig.Path.Open(".")
137 if err != nil {
138 return fmt.Errorf("openvpn.client-config.path: %w", err)
139 }
140
141 _ = file.Close()
142 }
143
144 if !conf.OAuth2.Endpoints.Auth.IsEmpty() && !conf.OAuth2.Endpoints.Token.IsEmpty() {
145 if conf.OAuth2.UserInfo {
146 return errors.New("oauth2.userinfo: cannot be used if oauth2.endpoint.auth and oauth2.endpoint.token is set")
147 }
148 }
149
150 return nil
151 }
152
153 func validateURL(uri types.URL) error {
154 if uri.IsEmpty() {
155 return nil
156 }
157
158 if !slices.Contains([]string{SchemeHTTP, SchemeHTTPS}, uri.Scheme) {
159 return errors.New("invalid URL. only http:// or https:// scheme supported")
160 }
161
162 return nil
163 }
164
165 func validateEncryptionSecret(secret types.Secret) error {
166 if !slices.Contains([]int{16, 24, 32}, len(secret.String())) {
167 return errors.New("requires a length of 16, 24 or 32")
168 }
169
170 return nil
171 }