Skip to content

Commit 20fe837

Browse files
authored
feat: sync tls config (#651)
* feat: implement tls config sync * feat: implement workaround for #633 * extend feature test * add tests
1 parent 961cf4d commit 20fe837

File tree

17 files changed

+203
-59
lines changed

17 files changed

+203
-59
lines changed

.golangci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ linters:
164164
disabled: true
165165
- name: var-naming
166166
disabled: true
167+
- name: enforce-switch-style
168+
disabled: true
167169
staticcheck:
168170
checks:
169171
- 'all'

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ deepcopy-gen: tb.controller-gen
1515
@touch ./tmp/deepcopy-gen-boilerplate.go.txt
1616
$(TB_CONTROLLER_GEN) paths=./pkg/types object
1717

18+
.PHONY: docs
19+
docs:
20+
go run docs/main.go
21+
1822
# Run tests
1923
test: generate lint test-ci
2024

README.md

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -185,58 +185,60 @@ services:
185185
186186
## Config via environment variables
187187
188-
For Replicas replace `#` with the index number for the replica. E.g: `REPLICA#_URL` -> `REPLICA1_URL`
189-
190-
| Name | Type | Description |
191-
|:-------------------------------------|--------|:----------------------------------------------------------|
192-
| ORIGIN_URL (string) | string | URL of adguardhome instance |
193-
| ORIGIN_WEB_URL (string) | string | Web URL of adguardhome instance |
194-
| ORIGIN_API_PATH (string) | string | API Path |
195-
| ORIGIN_USERNAME (string) | string | Adguardhome username |
196-
| ORIGIN_PASSWORD (string) | string | Adguardhome password |
197-
| ORIGIN_COOKIE (string) | string | Adguardhome cookie |
198-
| ORIGIN_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
199-
| ORIGIN_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
200-
| ORIGIN_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
201-
| ORIGIN_INTERFACE_NAME (string) | string | Network interface name |
202-
| ORIGIN_DHCP_SERVER_ENABLED (bool) | bool | Enable DHCP server |
203-
| REPLICA#_URL (string) | string | URL of adguardhome instance |
204-
| REPLICA#_WEB_URL (string) | string | Web URL of adguardhome instance |
205-
| REPLICA#_API_PATH (string) | string | API Path |
206-
| REPLICA#_USERNAME (string) | string | Adguardhome username |
207-
| REPLICA#_PASSWORD (string) | string | Adguardhome password |
208-
| REPLICA#_COOKIE (string) | string | Adguardhome cookie |
209-
| REPLICA#_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
210-
| REPLICA#_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
211-
| REPLICA#_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
212-
| REPLICA#_INTERFACE_NAME (string) | string | Network interface name |
213-
| REPLICA#_DHCP_SERVER_ENABLED (bool) | bool | Enable DHCP server |
214-
| CRON (string) | string | Cron expression for the sync interval |
215-
| RUN_ON_START (bool) | bool | Run the sung on startup |
216-
| PRINT_CONFIG_ONLY (bool) | bool | Print current config only and stop the application |
217-
| CONTINUE_ON_ERROR (bool) | bool | Continue sync on errors |
218-
| API_PORT (int) | int | API port |
219-
| API_USERNAME (string) | string | API username |
220-
| API_PASSWORD (string) | string | API password |
221-
| API_DARK_MODE (bool) | bool | API dark mode |
222-
| API_METRICS_ENABLED (bool) | bool | Enable metrics |
223-
| API_METRICS_SCRAPE_INTERVAL (int64) | int64 | Interval for metrics scraping |
224-
| API_METRICS_QUERY_LOG_LIMIT (int) | int | Metrics log query limit |
225-
| API_TLS_CERT_DIR (string) | string | API TLS certificate directory |
226-
| API_TLS_CERT_NAME (string) | string | API TLS certificate file name |
227-
| API_TLS_KEY_NAME (string) | string | API TLS key file name |
228-
| FEATURES_DNS_ACCESS_LISTS (bool) | bool | Sync DNS access lists |
229-
| FEATURES_DNS_SERVER_CONFIG (bool) | bool | Sync DNS server config |
230-
| FEATURES_DNS_REWRITES (bool) | bool | Sync DNS rewrites |
231-
| FEATURES_DHCP_SERVER_CONFIG (bool) | bool | Sync DHCP server config |
232-
| FEATURES_DHCP_STATIC_LEASES (bool) | bool | Sync DHCP static leases |
233-
| FEATURES_GENERAL_SETTINGS (bool) | bool | Sync general settings |
234-
| FEATURES_QUERY_LOG_CONFIG (bool) | bool | Sync query log config |
235-
| FEATURES_STATS_CONFIG (bool) | bool | Sync stats config |
236-
| FEATURES_CLIENT_SETTINGS (bool) | bool | Sync client settings |
237-
| FEATURES_SERVICES (bool) | bool | Sync services |
238-
| FEATURES_FILTERS (bool) | bool | Sync filters |
239-
| FEATURES_THEME (bool) | bool | Sync the weg UI theme |
188+
For Replicas replace `#` with the index number for the replica. E.g.: `REPLICA#_URL` -> `REPLICA1_URL`
189+
<!-- env-doc-start -->
190+
| Name | Type | Description |
191+
| :--- | ---- |:----------- |
192+
| ORIGIN_URL (string) | string | URL of adguardhome instance |
193+
| ORIGIN_WEB_URL (string) | string | Web URL of adguardhome instance |
194+
| ORIGIN_API_PATH (string) | string | API Path |
195+
| ORIGIN_USERNAME (string) | string | Adguardhome username |
196+
| ORIGIN_PASSWORD (string) | string | Adguardhome password |
197+
| ORIGIN_COOKIE (string) | string | Adguardhome cookie |
198+
| ORIGIN_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
199+
| ORIGIN_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
200+
| ORIGIN_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
201+
| ORIGIN_INTERFACE_NAME (string) | string | Network interface name |
202+
| ORIGIN_DHCP_SERVER_ENABLED (bool) | bool | Enable DHCP server |
203+
| REPLICA#_URL (string) | string | URL of adguardhome instance |
204+
| REPLICA#_WEB_URL (string) | string | Web URL of adguardhome instance |
205+
| REPLICA#_API_PATH (string) | string | API Path |
206+
| REPLICA#_USERNAME (string) | string | Adguardhome username |
207+
| REPLICA#_PASSWORD (string) | string | Adguardhome password |
208+
| REPLICA#_COOKIE (string) | string | Adguardhome cookie |
209+
| REPLICA#_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
210+
| REPLICA#_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
211+
| REPLICA#_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
212+
| REPLICA#_INTERFACE_NAME (string) | string | Network interface name |
213+
| REPLICA#_DHCP_SERVER_ENABLED (bool) | bool | Enable DHCP server |
214+
| CRON (string) | string | Cron expression for the sync interval |
215+
| RUN_ON_START (bool) | bool | Run the sung on startup |
216+
| PRINT_CONFIG_ONLY (bool) | bool | Print current config only and stop the application |
217+
| CONTINUE_ON_ERROR (bool) | bool | Continue sync on errors |
218+
| API_PORT (int) | int | API port |
219+
| API_USERNAME (string) | string | API username |
220+
| API_PASSWORD (string) | string | API password |
221+
| API_DARK_MODE (bool) | bool | API dark mode |
222+
| API_METRICS_ENABLED (bool) | bool | Enable metrics |
223+
| API_METRICS_SCRAPE_INTERVAL (int64) | int64 | Interval for metrics scraping |
224+
| API_METRICS_QUERY_LOG_LIMIT (int) | int | Metrics log query limit |
225+
| API_TLS_CERT_DIR (string) | string | API TLS certificate directory |
226+
| API_TLS_CERT_NAME (string) | string | API TLS certificate file name |
227+
| API_TLS_KEY_NAME (string) | string | API TLS key file name |
228+
| FEATURES_DNS_ACCESS_LISTS (bool) | bool | Sync DNS access lists |
229+
| FEATURES_DNS_SERVER_CONFIG (bool) | bool | Sync DNS server config |
230+
| FEATURES_DNS_REWRITES (bool) | bool | Sync DNS rewrites |
231+
| FEATURES_DHCP_SERVER_CONFIG (bool) | bool | Sync DHCP server config |
232+
| FEATURES_DHCP_STATIC_LEASES (bool) | bool | Sync DHCP static leases |
233+
| FEATURES_GENERAL_SETTINGS (bool) | bool | Sync general settings |
234+
| FEATURES_QUERY_LOG_CONFIG (bool) | bool | Sync query log config |
235+
| FEATURES_STATS_CONFIG (bool) | bool | Sync stats config |
236+
| FEATURES_CLIENT_SETTINGS (bool) | bool | Sync client settings |
237+
| FEATURES_SERVICES (bool) | bool | Sync services |
238+
| FEATURES_FILTERS (bool) | bool | Sync filters |
239+
| FEATURES_THEME (bool) | bool | Sync the web UI theme |
240+
| FEATURES_TLS_CONFIG (bool) | bool | Sync the TLS config |
241+
<!-- env-doc-end -->
240242

241243
### Unraid
242244

cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func init() {
6767
doCmd.PersistentFlags().Bool(config.FlagFeatureClient, true, "Enable client settings feature")
6868
doCmd.PersistentFlags().Bool(config.FlagFeatureServices, true, "Enable services sync feature")
6969
doCmd.PersistentFlags().Bool(config.FlagFeatureFilters, true, "Enable filters sync feature")
70+
doCmd.PersistentFlags().Bool(config.FlagFeatureTLSConfig, true, "Enable TLS config sync feature")
7071

7172
doCmd.PersistentFlags().String(config.FlagOriginURL, "", "Origin instance url")
7273
doCmd.PersistentFlags().

docs/main.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,57 @@ package main
33

44
import (
55
"fmt"
6+
"io"
7+
"log"
8+
"os"
69
"reflect"
710
"strings"
811

912
"github.com/bakito/adguardhome-sync/pkg/types"
1013
)
1114

1215
func main() {
13-
_, _ = fmt.Println("| Name | Type | Description |")
14-
_, _ = fmt.Println("| :--- | ---- |:----------- |")
16+
// Read the README.md file
17+
content, err := os.ReadFile("README.md")
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
// Convert to string for easier manipulation
23+
fileContent := string(content)
24+
25+
// Generate the environment variables documentation
26+
var buf strings.Builder
27+
_, _ = buf.WriteString("| Name | Type | Description |\n")
28+
_, _ = buf.WriteString("| :--- | ---- |:----------- |\n")
29+
oldStdout := os.Stdout
30+
r, w, _ := os.Pipe()
31+
os.Stdout = w
1532
printEnvTags(reflect.TypeOf(types.Config{}), "")
33+
_ = w.Close()
34+
envDoc, _ := io.ReadAll(r)
35+
os.Stdout = oldStdout
36+
_, _ = buf.Write(envDoc)
37+
38+
// Find the markers and replace content between them
39+
startMarker := "<!-- env-doc-start -->"
40+
endMarker := "<!-- env-doc-end -->"
41+
42+
start := strings.Index(fileContent, startMarker)
43+
end := strings.Index(fileContent, endMarker)
44+
45+
if start == -1 || end == -1 {
46+
log.Fatal("Could not find markers in README.md")
47+
}
48+
49+
// Construct new content
50+
newContent := fileContent[:start+len(startMarker)] + "\n" + buf.String() + fileContent[end:]
51+
52+
// Write back to README.md
53+
err = os.WriteFile("README.md", []byte(newContent), 0o644)
54+
if err != nil {
55+
log.Fatal(err)
56+
}
1657
}
1758

1859
// printEnvTags recursively prints all fields with `env` tags.
@@ -36,8 +77,6 @@ func printEnvTags(t reflect.Type, prefix string) {
3677
envTag = "ORIGIN"
3778
case "Replica":
3879
envTag = "REPLICA#"
39-
default:
40-
continue
4180
}
4281
}
4382
combinedTag := envTag

pkg/client/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ type Client interface {
147147
SetDhcpConfig(status *model.DhcpStatus) error
148148
AddDHCPStaticLease(lease model.DhcpStaticLease) error
149149
DeleteDHCPStaticLease(lease model.DhcpStaticLease) error
150+
TLSConfig() (*model.TlsConfig, error)
151+
SetTLSConfig(tls *model.TlsConfig) error
150152
}
151153

152154
type client struct {
@@ -466,3 +468,14 @@ func (cl *client) SetProfileInfo(profile *model.ProfileInfo) error {
466468
cl.log.With("language", profile.Language, "theme", profile.Theme).Info("Set profile")
467469
return cl.doPut(cl.client.R().EnableTrace().SetBody(profile), "/profile/update")
468470
}
471+
472+
func (cl *client) TLSConfig() (*model.TlsConfig, error) {
473+
tlsc := &model.TlsConfig{}
474+
err := cl.doGet(cl.client.R().EnableTrace().SetResult(tlsc), "/tls/status")
475+
return tlsc, err
476+
}
477+
478+
func (cl *client) SetTLSConfig(tlsc *model.TlsConfig) error {
479+
cl.log.With("enabled", tlsc.Enabled).Info("Set TLS config")
480+
return cl.doPost(cl.client.R().EnableTrace().SetBody(tlsc), "/tls/configure")
481+
}

pkg/client/model/model-functions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,7 @@ func sumUp(t, o *[]int) *[]int {
490490
}
491491
return t
492492
}
493+
494+
func (c *TlsConfig) Equals(config *TlsConfig) bool {
495+
return utils.JSONEquals(c, config)
496+
}

pkg/config/config-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
},
151151
"theme": {
152152
"type": "boolean"
153+
},
154+
"tlsConfig": {
155+
"type": "boolean"
153156
}
154157
},
155158
"type": "object"

pkg/config/flag-names.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
FlagFeatureClient = "feature-client-settings"
2323
FlagFeatureServices = "feature-services"
2424
FlagFeatureFilters = "feature-filters"
25+
FlagFeatureTLSConfig = "feature-tls-config"
2526

2627
FlagOriginURL = "origin-url"
2728
FlagOriginWebURL = "origin-web-url"

pkg/config/flags.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,13 @@ func (fr *flagReader) readFeatureFlags() error {
173173
}); err != nil {
174174
return err
175175
}
176-
return fr.setBoolFlag(FlagFeatureFilters, func(cgf *types.Config, value bool) {
176+
if err := fr.setBoolFlag(FlagFeatureFilters, func(cgf *types.Config, value bool) {
177177
fr.cfg.Features.Filters = value
178+
}); err != nil {
179+
return err
180+
}
181+
return fr.setBoolFlag(FlagFeatureTLSConfig, func(cgf *types.Config, value bool) {
182+
fr.cfg.Features.TLSConfig = value
178183
})
179184
}
180185

0 commit comments

Comments
 (0)