From d18966276a4d4d6b4c99ca552e62bc963fb3e752 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 16 Dec 2022 17:00:20 +0100 Subject: [PATCH] Store the previous applied dns configuration hash (#628) This prevents changing the system DNS config when there is nothing to new It also prevents issues with network change on google chrome --- .../workflows/test-docker-compose-linux.yml | 10 +++-- client/internal/dns/server.go | 43 +++++++++++++------ go.mod | 1 + go.sum | 2 + infrastructure_files/configure.sh | 1 + infrastructure_files/tests/setup.env | 2 +- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test-docker-compose-linux.yml b/.github/workflows/test-docker-compose-linux.yml index 9374650c5..d681dd89c 100644 --- a/.github/workflows/test-docker-compose-linux.yml +++ b/.github/workflows/test-docker-compose-linux.yml @@ -39,7 +39,8 @@ jobs: working-directory: infrastructure_files run: bash -x configure.sh env: - CI_NETBIRD_AUTH_CLIENT_ID: ${{ secrets.CI_NETBIRD_AUTH_CLIENT_ID }} + CI_NETBIRD_DOMAIN: localhost + CI_NETBIRD_AUTH_CLIENT_ID: testing.client.id CI_NETBIRD_AUTH_AUDIENCE: testing.ci CI_NETBIRD_AUTH_OIDC_CONFIGURATION_ENDPOINT: https://example.eu.auth0.com/.well-known/openid-configuration CI_NETBIRD_USE_AUTH0: true @@ -47,7 +48,8 @@ jobs: - name: check values working-directory: infrastructure_files env: - CI_NETBIRD_AUTH_CLIENT_ID: ${{ secrets.CI_NETBIRD_AUTH_CLIENT_ID }} + CI_NETBIRD_DOMAIN: localhost + CI_NETBIRD_AUTH_CLIENT_ID: testing.client.id CI_NETBIRD_AUTH_AUDIENCE: testing.ci CI_NETBIRD_AUTH_OIDC_CONFIGURATION_ENDPOINT: https://example.eu.auth0.com/.well-known/openid-configuration CI_NETBIRD_USE_AUTH0: true @@ -63,7 +65,7 @@ jobs: grep AUTH_AUDIENCE docker-compose.yml | grep $CI_NETBIRD_AUTH_AUDIENCE grep AUTH_SUPPORTED_SCOPES docker-compose.yml | grep "$CI_NETBIRD_AUTH_SUPPORTED_SCOPES" grep USE_AUTH0 docker-compose.yml | grep $CI_NETBIRD_USE_AUTH0 - grep NETBIRD_MGMT_API_ENDPOINT docker-compose.yml | grep "http://localhost:33073" + grep NETBIRD_MGMT_API_ENDPOINT docker-compose.yml | grep "$CI_NETBIRD_DOMAIN:33073" grep AUTH_REDIRECT_URI docker-compose.yml | grep $CI_NETBIRD_AUTH_REDIRECT_URI grep AUTH_SILENT_REDIRECT_URI docker-compose.yml | egrep 'AUTH_SILENT_REDIRECT_URI=$' @@ -72,6 +74,8 @@ jobs: run: | docker-compose up -d sleep 5 + docker-compose ps + docker-compose logs --tail=20 - name: test running containers run: | diff --git a/client/internal/dns/server.go b/client/internal/dns/server.go index 494472cff..4d0761359 100644 --- a/client/internal/dns/server.go +++ b/client/internal/dns/server.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/miekg/dns" + "github.com/mitchellh/hashstructure/v2" nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/iface" log "github.com/sirupsen/logrus" @@ -30,19 +31,20 @@ type Server interface { // DefaultServer dns server object type DefaultServer struct { - ctx context.Context - stop context.CancelFunc - mux sync.Mutex - server *dns.Server - dnsMux *dns.ServeMux - dnsMuxMap registrationMap - localResolver *localResolver - wgInterface *iface.WGIface - hostManager hostManager - updateSerial uint64 - listenerIsRunning bool - runtimePort int - runtimeIP string + ctx context.Context + stop context.CancelFunc + mux sync.Mutex + server *dns.Server + dnsMux *dns.ServeMux + dnsMuxMap registrationMap + localResolver *localResolver + wgInterface *iface.WGIface + hostManager hostManager + updateSerial uint64 + listenerIsRunning bool + runtimePort int + runtimeIP string + previousConfigHash uint64 } type registrationMap map[string]struct{} @@ -184,6 +186,20 @@ func (s *DefaultServer) UpdateDNSServer(serial uint64, update nbdns.Config) erro s.mux.Lock() defer s.mux.Unlock() + hash, err := hashstructure.Hash(update, hashstructure.FormatV2, &hashstructure.HashOptions{ + ZeroNil: true, + IgnoreZeroValue: true, + SlicesAsSets: true, + }) + if err != nil { + log.Errorf("unable to hash the dns configuration update, got error: %s", err) + } + + if s.previousConfigHash == hash { + log.Debugf("not applying the dns configuration update as there is nothing new") + s.updateSerial = serial + return nil + } // is the service should be disabled, we stop the listener // and proceed with a regular update to clean up the handlers and records if !update.ServiceEnable { @@ -215,6 +231,7 @@ func (s *DefaultServer) UpdateDNSServer(serial uint64, update nbdns.Config) erro } s.updateSerial = serial + s.previousConfigHash = hash return nil } diff --git a/go.mod b/go.mod index 2802a369b..9e673f9d2 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/libp2p/go-netroute v0.2.0 github.com/magiconair/properties v1.8.5 github.com/miekg/dns v1.1.41 + github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/prometheus/client_golang v1.13.0 github.com/rs/xid v1.3.0 diff --git a/go.sum b/go.sum index 707d7c808..498b585ee 100644 --- a/go.sum +++ b/go.sum @@ -465,6 +465,8 @@ github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCL github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= +github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= diff --git a/infrastructure_files/configure.sh b/infrastructure_files/configure.sh index 563f88844..ed6367171 100755 --- a/infrastructure_files/configure.sh +++ b/infrastructure_files/configure.sh @@ -49,6 +49,7 @@ fi # local development or tests if [[ $NETBIRD_DOMAIN == "localhost" || $NETBIRD_DOMAIN == "127.0.0.1" ]] then + export NETBIRD_MGMT_SINGLE_ACCOUNT_MODE_DOMAIN="netbird.selfhosted" export NETBIRD_MGMT_API_ENDPOINT=http://$NETBIRD_DOMAIN:$NETBIRD_MGMT_API_PORT unset NETBIRD_MGMT_API_CERT_FILE unset NETBIRD_MGMT_API_CERT_KEY_FILE diff --git a/infrastructure_files/tests/setup.env b/infrastructure_files/tests/setup.env index 59294db02..cdb5e5c6b 100644 --- a/infrastructure_files/tests/setup.env +++ b/infrastructure_files/tests/setup.env @@ -1,7 +1,7 @@ ## example file, you can copy this file to setup.env and update its values ## # Dashboard domain. e.g. app.mydomain.com -NETBIRD_DOMAIN="localhost" +NETBIRD_DOMAIN=$CI_NETBIRD_DOMAIN # e.g. https://dev-24vkclam.us.auth0.com/ or https://YOUR-KEYCLOAK-HOST:8080/realms/netbird NETBIRD_AUTH_OIDC_CONFIGURATION_ENDPOINT="https://example.eu.auth0.com/.well-known/openid-configuration" # e.g. netbird-client