From 1d68509463124138f04ea736dd3aa75c1e47f574 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 14 Jan 2022 09:19:16 +0100 Subject: [PATCH 01/10] Strip binary, update to go-1.17.6 --- Dockerfile | 3 ++- Dockerfile.alpine | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0d344268..d5fe3421 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Builder image -FROM golang:1.17.1-bullseye AS build +FROM golang:1.17.6-bullseye AS build ENV GOPATH /go WORKDIR /go/src/headscale @@ -9,6 +9,7 @@ RUN go mod download COPY . . RUN go install -a -ldflags="-extldflags=-static" -tags netgo,sqlite_omit_load_extension ./cmd/headscale +RUN strip /go/bin/headscale RUN test -e /go/bin/headscale # Production image diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 7d90e1ec..8185dbe4 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,5 +1,5 @@ # Builder image -FROM golang:1.17.1-alpine AS build +FROM golang:1.17.6-alpine AS build ENV GOPATH /go WORKDIR /go/src/headscale @@ -10,6 +10,7 @@ RUN go mod download COPY . . RUN go install -a -ldflags="-extldflags=-static" -tags netgo,sqlite_omit_load_extension ./cmd/headscale +RUN strip /go/bin/headscale RUN test -e /go/bin/headscale # Production image From febbb6006f040b26bfe30db31ed4dee7ea362540 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 14 Jan 2022 12:59:53 +0100 Subject: [PATCH 02/10] use most recent go 1.17 for tests --- .github/workflows/build.yml | 2 +- .github/workflows/test-integration.yml | 2 +- .github/workflows/test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 963663a7..589d8c58 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,7 +18,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v2 with: - go-version: "1.17.3" + go-version: "1.17" - name: Install dependencies run: | diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index abf46d32..d4179ea2 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v2 with: - go-version: "1.17.3" + go-version: "1.17" - name: Run Integration tests run: go test -tags integration -timeout 30m diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4540482c..651063fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v2 with: - go-version: "1.17.3" # The Go version to download (if necessary) and use. + go-version: "1.17" # The Go version to download (if necessary) and use. # Install all the dependencies - name: Install dependencies From 86f3f26a18f2aaf4de6b73047be1f2cb51b81c4d Mon Sep 17 00:00:00 2001 From: Jim Tittsler Date: Sat, 15 Jan 2022 16:32:52 +0900 Subject: [PATCH 03/10] Fix typos --- README.md | 6 +++--- docs/running-headscale-linux.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 07da4aab..9a599d3d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ To contribute to Headscale you would need the lastest version of [Go](https://go ### Code style -To ensure we have some consistency with a growing number of contributes, this project has adopted linting and style/formatting rules: +To ensure we have some consistency with a growing number of contributions, this project has adopted linting and style/formatting rules: The **Go** code is linted with [`golangci-lint`](https://golangci-lint.run) and formatted with [`golines`](https://github.com/segmentio/golines) (width 88) and @@ -76,7 +76,7 @@ run `make lint` and `make fmt` before committing any code. The **Proto** code is linted with [`buf`](https://docs.buf.build/lint/overview) and formatted with [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html). -The **rest** (markdown, yaml, etc) is formatted with [`prettier`](https://prettier.io). +The **rest** (Markdown, YAML, etc) is formatted with [`prettier`](https://prettier.io). Check out the `.golangci.yaml` and `Makefile` to see the specific configuration. @@ -92,7 +92,7 @@ make install-protobuf-plugins ### Testing and building -Some parts of the project requires the generation of Go code from Protobuf (if changes is made in `proto/`) and it must be (re-)generated with: +Some parts of the project require the generation of Go code from Protobuf (if changes are made in `proto/`) and it must be (re-)generated with: ```shell make generate diff --git a/docs/running-headscale-linux.md b/docs/running-headscale-linux.md index d53b72f8..7a62fcc6 100644 --- a/docs/running-headscale-linux.md +++ b/docs/running-headscale-linux.md @@ -21,7 +21,7 @@ wget --output-document=/usr/local/bin/headscale \ chmod +x /usr/local/bin/headscale ``` -3. Prepare a direction to hold `headscale` configuration and the [SQLite](https://www.sqlite.org/) database: +3. Prepare a directory to hold `headscale` configuration and the [SQLite](https://www.sqlite.org/) database: ```shell # Directory for configuration @@ -32,7 +32,7 @@ mkdir -p /etc/headscale mkdir -p /var/lib/headscale ``` -4. Create an empty SQlite datebase: +4. Create an empty SQLite database: ```shell touch /var/lib/headscale/db.sqlite @@ -106,7 +106,7 @@ tailscale up --login-server --authkey ## Running `headscale` in the background with SystemD -In this section it will be demonstrated how to run `headscale` as a service in the background with [SystemD](https://www.freedesktop.org/wiki/Software/systemd/). +This section demonstrates how to run `headscale` as a service in the background with [SystemD](https://www.freedesktop.org/wiki/Software/systemd/). This should work on most modern Linux distributions. 1. Create a SystemD service configuration at `/etc/systemd/system/headscale.service` containing: From 67d1dd984f575c3b4b73d96ff7ce5b1899b730d3 Mon Sep 17 00:00:00 2001 From: Ryan Fowler Date: Fri, 21 Jan 2022 21:48:58 -0800 Subject: [PATCH 04/10] Fix missing return in PollNetMapHandler --- poll.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/poll.go b/poll.go index 1d2db944..10389038 100644 --- a/poll.go +++ b/poll.go @@ -74,6 +74,8 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { Str("handler", "PollNetMap"). Msgf("Failed to fetch machine from the database with Machine key: %s", machineKey.String()) ctx.String(http.StatusInternalServerError, "") + + return } log.Trace(). Str("handler", "PollNetMap"). From f59071ff1c2da7f03cd58b626f2267ab14905352 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 28 Jan 2022 17:23:01 +0000 Subject: [PATCH 05/10] Trim whitespace from privateKey before parsing --- app.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index b9d570c5..3ba384b9 100644 --- a/app.go +++ b/app.go @@ -724,7 +724,8 @@ func readOrCreatePrivateKey(path string) (*key.MachinePrivate, error) { return nil, fmt.Errorf("failed to read private key file: %w", err) } - privateKeyEnsurePrefix := PrivateKeyEnsurePrefix(string(privateKey)) + trimmedPrivateKey := strings.TrimSpace(string(privateKey)) + privateKeyEnsurePrefix := PrivateKeyEnsurePrefix(trimmedPrivateKey) var machineKey key.MachinePrivate if err = machineKey.UnmarshalText([]byte(privateKeyEnsurePrefix)); err != nil { From ab45cd37f8cf9203fcc6c14865d0e33271036ac9 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 28 Jan 2022 17:40:39 +0000 Subject: [PATCH 06/10] Only golint new problems --- .github/workflows/lint.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b3c64006..6fb985f7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -14,6 +14,12 @@ jobs: with: version: latest + # Only block PRs on new problems. + # If this is not enabled, we will end up having PRs + # blocked because new linters has appared and other + # parts of the code is affected. + only-new-issues: true + prettier-lint: runs-on: ubuntu-latest steps: From 25ccf5ef1808375a17337e9f1a2562858c5edb7e Mon Sep 17 00:00:00 2001 From: Csaba Sarkadi Date: Sun, 16 Jan 2022 11:59:03 +0100 Subject: [PATCH 07/10] PollNetMapStream: do not create any rows during long-poll operation --- machine.go | 8 ++++++ poll.go | 84 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/machine.go b/machine.go index d58c9c5c..de1764e1 100644 --- a/machine.go +++ b/machine.go @@ -319,6 +319,14 @@ func (h *Headscale) DeleteMachine(machine *Machine) error { return h.RequestMapUpdates(namespaceID) } +func (h *Headscale) TouchMachine(machine *Machine) error { + return h.db.Updates(Machine{ + ID: machine.ID, + LastSeen: machine.LastSeen, + LastSuccessfulUpdate: machine.LastSuccessfulUpdate, + }).Error +} + // HardDeleteMachine hard deletes a Machine from the database. func (h *Headscale) HardDeleteMachine(machine *Machine) error { err := h.RemoveSharedMachineFromAllNamespaces(machine) diff --git a/poll.go b/poll.go index 10389038..d7aa12e9 100644 --- a/poll.go +++ b/poll.go @@ -102,7 +102,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { machine.Endpoints = datatypes.JSON(endpoints) machine.LastSeen = &now } - h.db.Save(&machine) + h.db.Updates(machine) data, err := h.getMapResponse(machineKey, req, machine) if err != nil { @@ -291,6 +291,10 @@ func (h *Headscale) PollNetMapStream( Str("channel", "pollData"). Err(err). Msg("Cannot update machine from database") + + // client has been removed from database + // since the stream opened, terminate connection. + return false } now := time.Now().UTC() machine.LastSeen = &now @@ -299,13 +303,22 @@ func (h *Headscale) PollNetMapStream( Set(float64(now.Unix())) machine.LastSuccessfulUpdate = &now - h.db.Save(&machine) - log.Trace(). - Str("handler", "PollNetMapStream"). - Str("machine", machine.Name). - Str("channel", "pollData"). - Int("bytes", len(data)). - Msg("Machine entry in database updated successfully after sending pollData") + err = h.TouchMachine(machine) + if err != nil { + log.Error(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "pollData"). + Err(err). + Msg("Cannot update machine LastSuccessfulUpdate") + } else { + log.Trace(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "pollData"). + Int("bytes", len(data)). + Msg("Machine entry in database updated successfully after sending pollData") + } return true @@ -344,16 +357,29 @@ func (h *Headscale) PollNetMapStream( Str("channel", "keepAlive"). Err(err). Msg("Cannot update machine from database") + + // client has been removed from database + // since the stream opened, terminate connection. + return false } now := time.Now().UTC() machine.LastSeen = &now - h.db.Save(&machine) - log.Trace(). - Str("handler", "PollNetMapStream"). - Str("machine", machine.Name). - Str("channel", "keepAlive"). - Int("bytes", len(data)). - Msg("Machine updated successfully after sending keep alive") + err = h.TouchMachine(machine) + if err != nil { + log.Error(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "keepAlive"). + Err(err). + Msg("Cannot update machine LastSeen") + } else { + log.Trace(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "keepAlive"). + Int("bytes", len(data)). + Msg("Machine updated successfully after sending keep alive") + } return true @@ -417,6 +443,10 @@ func (h *Headscale) PollNetMapStream( Str("channel", "update"). Err(err). Msg("Cannot update machine from database") + + // client has been removed from database + // since the stream opened, terminate connection. + return false } now := time.Now().UTC() @@ -424,7 +454,15 @@ func (h *Headscale) PollNetMapStream( Set(float64(now.Unix())) machine.LastSuccessfulUpdate = &now - h.db.Save(&machine) + err = h.TouchMachine(machine) + if err != nil { + log.Error(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "update"). + Err(err). + Msg("Cannot update machine LastSuccessfulUpdate") + } } else { log.Trace(). Str("handler", "PollNetMapStream"). @@ -452,10 +490,22 @@ func (h *Headscale) PollNetMapStream( Str("channel", "Done"). Err(err). Msg("Cannot update machine from database") + + // client has been removed from database + // since the stream opened, terminate connection. + return false } now := time.Now().UTC() machine.LastSeen = &now - h.db.Save(&machine) + err = h.TouchMachine(machine) + if err != nil { + log.Error(). + Str("handler", "PollNetMapStream"). + Str("machine", machine.Name). + Str("channel", "Done"). + Err(err). + Msg("Cannot update machine LastSeen") + } log.Trace(). Str("handler", "PollNetMapStream"). From 761147ea3be88ea0531f688ed2e89840f124d92d Mon Sep 17 00:00:00 2001 From: Csaba Sarkadi Date: Fri, 28 Jan 2022 21:48:23 +0100 Subject: [PATCH 08/10] update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38904b0a..3d4f4aa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ **TBD (TBD):** +- Fixed issue where hosts deleted from control server may be written back to the database, as long as they are connected to the control server [#278]((https://github.com/juanfont/headscale/pull/278) +) + **0.12.3 (2022-01-13):** **Changes**: From fbe5054a6707ce8a4020fd773061e2284a6ef6d7 Mon Sep 17 00:00:00 2001 From: Csaba Sarkadi Date: Fri, 28 Jan 2022 22:00:13 +0100 Subject: [PATCH 09/10] fixup! update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4f4aa6..6541b638 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ **TBD (TBD):** -- Fixed issue where hosts deleted from control server may be written back to the database, as long as they are connected to the control server [#278]((https://github.com/juanfont/headscale/pull/278) +- Fixed issue where hosts deleted from control server may be written back to the database, as long as they are connected to the control server [#278](https://github.com/juanfont/headscale/pull/278) ) **0.12.3 (2022-01-13):** From 17411b65f34334c18b446126a8f0d01e488026a0 Mon Sep 17 00:00:00 2001 From: Csaba Sarkadi Date: Sat, 29 Jan 2022 13:48:14 +0100 Subject: [PATCH 10/10] fixup! fixup! update CHANGELOG prettier changes --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6541b638..f823f0c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ **TBD (TBD):** - Fixed issue where hosts deleted from control server may be written back to the database, as long as they are connected to the control server [#278](https://github.com/juanfont/headscale/pull/278) -) + ) **0.12.3 (2022-01-13):**