From f922ff963d37178c9d2adf6972247d5c7c32cbbb Mon Sep 17 00:00:00 2001 From: Piotr Stankiewicz Date: Fri, 8 Jul 2022 10:22:43 +0200 Subject: [PATCH 1/2] Use Docker CLI socket from the User's home directory We should not rely on having a global path for the Docker CLI socket. On macOS this forces Docker Desktop to access directories which require raised privileges. Whereas on Linux we do not create sockets in that location at all, currently. So look for the Docker CLI socket in the User's home directory. Signed-off-by: Piotr Stankiewicz --- cli/metrics/conn_darwin.go | 43 +++++++++++++++++++++++++++++++++++++ cli/metrics/conn_e2e.go | 5 +++-- cli/metrics/conn_note2e.go | 23 ++++++++++++++++++++ cli/metrics/conn_other.go | 22 +++++++++++++++---- cli/metrics/conn_windows.go | 7 +++++- 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 cli/metrics/conn_darwin.go create mode 100644 cli/metrics/conn_note2e.go diff --git a/cli/metrics/conn_darwin.go b/cli/metrics/conn_darwin.go new file mode 100644 index 000000000..234a106be --- /dev/null +++ b/cli/metrics/conn_darwin.go @@ -0,0 +1,43 @@ +//go:build darwin +// +build darwin + +/* + Copyright 2022 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package metrics + +import ( + "net" + "path/filepath" + + "github.com/docker/docker/pkg/homedir" +) + +var ( + socket = "/var/run/docker-cli.sock" +) + +func init() { + // Attempt to retrieve the Docker CLI socket for the current user. + if home := homedir.Get(); home != "" { + socket = filepath.Join(home, "/Library/Containers/com.docker.docker/Data/docker-cli.sock") + } // else: On macOS Docker Desktop creates symlinks in /var/run, so fall back to the old default. + overrideSocket() // nop, unless built for e2e testing +} + +func conn() (net.Conn, error) { + return net.Dial("unix", socket) +} diff --git a/cli/metrics/conn_e2e.go b/cli/metrics/conn_e2e.go index 53e87c9e2..06395fff3 100644 --- a/cli/metrics/conn_e2e.go +++ b/cli/metrics/conn_e2e.go @@ -1,7 +1,8 @@ +//go:build e2e // +build e2e /* - Copyright 2020 Docker Compose CLI authors + Copyright 2020, 2022 Docker Compose CLI authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,7 +23,7 @@ import ( "os" ) -func init() { +func overrideSocket() { testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET") if defined { socket = testSocket diff --git a/cli/metrics/conn_note2e.go b/cli/metrics/conn_note2e.go new file mode 100644 index 000000000..8a080505a --- /dev/null +++ b/cli/metrics/conn_note2e.go @@ -0,0 +1,23 @@ +//go:build !e2e +// +build !e2e + +/* + Copyright 2022 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package metrics + +func overrideSocket() { +} diff --git a/cli/metrics/conn_other.go b/cli/metrics/conn_other.go index d51945f6a..aa346fba9 100644 --- a/cli/metrics/conn_other.go +++ b/cli/metrics/conn_other.go @@ -1,7 +1,8 @@ -// +build !windows +//go:build !windows,!darwin +// +build !windows,!darwin /* - Copyright 2020 Docker Compose CLI authors + Copyright 2020, 2022 Docker Compose CLI authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,12 +19,25 @@ package metrics -import "net" +import ( + "net" + "path/filepath" + + "github.com/docker/docker/pkg/homedir" +) var ( - socket = "/var/run/docker-cli.sock" + socket = "" ) +func init() { + // Attempt to retrieve the Docker CLI socket for the current user. + if home := homedir.Get(); home != "" { + socket = filepath.Join(home, ".docker/desktop/docker-cli.sock") + } // else: On Linux we don't expect to have a global CLI socket, so leave it empty and let connections fail. + overrideSocket() // nop, unless built for e2e testing +} + func conn() (net.Conn, error) { return net.Dial("unix", socket) } diff --git a/cli/metrics/conn_windows.go b/cli/metrics/conn_windows.go index f10a5472c..27c680ad7 100644 --- a/cli/metrics/conn_windows.go +++ b/cli/metrics/conn_windows.go @@ -1,7 +1,8 @@ +//go:build windows // +build windows /* - Copyright 2020 Docker Compose CLI authors + Copyright 2020, 2022 Docker Compose CLI authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -30,6 +31,10 @@ var ( socket = `\\.\pipe\docker_cli` ) +func init() { + overrideSocket() // no-op, unless built for e2e testing +} + func conn() (net.Conn, error) { if strings.HasPrefix(socket, `\\.\pipe\`) { timeout := 200 * time.Millisecond From 19ac0adf624ce860610e997a4550bc9ff9b0087f Mon Sep 17 00:00:00 2001 From: Piotr Stankiewicz Date: Wed, 13 Jul 2022 09:35:31 +0200 Subject: [PATCH 2/2] Switch to using addlicense for copyright header validation Signed-off-by: Piotr Stankiewicz --- Dockerfile | 2 +- scripts/validate/fileheader | 12 +++++------- scripts/validate/template/bash.txt | 13 ------------- scripts/validate/template/dockerfile.txt | 13 ------------- scripts/validate/template/go.txt | 16 ---------------- scripts/validate/template/makefile.txt | 13 ------------- 6 files changed, 6 insertions(+), 63 deletions(-) delete mode 100644 scripts/validate/template/bash.txt delete mode 100644 scripts/validate/template/dockerfile.txt delete mode 100644 scripts/validate/template/go.txt delete mode 100644 scripts/validate/template/makefile.txt diff --git a/Dockerfile b/Dockerfile index f37355cea..d2fcd609f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -108,7 +108,7 @@ RUN --mount=target=. \ make -f builder.Makefile test FROM base AS check-license-headers -RUN go get -u github.com/kunalkushwaha/ltag +RUN go install github.com/google/addlicense@latest RUN --mount=target=. \ make -f builder.Makefile check-license-headers diff --git a/scripts/validate/fileheader b/scripts/validate/fileheader index 8b503df36..4f5dc0270 100755 --- a/scripts/validate/fileheader +++ b/scripts/validate/fileheader @@ -1,6 +1,6 @@ #!/usr/bin/env sh -# Copyright Docker Compose CLI authors +# Copyright 2020, 2022 Docker Compose CLI authors # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,12 +16,10 @@ set -eu -o pipefail -if ! command -v ltag; then - >&2 echo "ERROR: ltag not found. Install with:" - >&2 echo " go get -u github.com/kunalkushwaha/ltag" +if ! command -v addlicense; then + >&2 echo "ERROR: addlicense not found. Install with:" + >&2 echo " go install github.com/google/addlicense@latest" exit 1 fi -BASEPATH="${1-}" - -ltag -t "${BASEPATH}scripts/validate/template" -excludes "validate testdata resolvepath" --check -v \ No newline at end of file +find . -regex '.*\.sh' -o -regex '.*\.go' -o -regex '.*Makefile' -o -regex '.*Dockerfile' | xargs addlicense -check -l apache -c 'Docker Compose CLI authors' -ignore validate -ignore testdata -ignore resolvepath -v 1>&2 diff --git a/scripts/validate/template/bash.txt b/scripts/validate/template/bash.txt deleted file mode 100644 index 31e00001c..000000000 --- a/scripts/validate/template/bash.txt +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2020 Docker Compose CLI authors - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/scripts/validate/template/dockerfile.txt b/scripts/validate/template/dockerfile.txt deleted file mode 100644 index 31e00001c..000000000 --- a/scripts/validate/template/dockerfile.txt +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2020 Docker Compose CLI authors - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/scripts/validate/template/go.txt b/scripts/validate/template/go.txt deleted file mode 100644 index a3ee08615..000000000 --- a/scripts/validate/template/go.txt +++ /dev/null @@ -1,16 +0,0 @@ -/* - Copyright 2020 Docker Compose CLI authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - diff --git a/scripts/validate/template/makefile.txt b/scripts/validate/template/makefile.txt deleted file mode 100644 index 31e00001c..000000000 --- a/scripts/validate/template/makefile.txt +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2020 Docker Compose CLI authors - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License.