Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove file if it exists before creating new one #15186

Merged
merged 7 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/13260-deb-installer
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Bug fix: creating 2 .deb installers one after the other with different enrollment keys no longer
results in the last installer failing at install time.
45 changes: 41 additions & 4 deletions cmd/fleetctl/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
"github.com/fleetdm/fleet/v4/pkg/nettest"
"github.com/stretchr/testify/require"
"pault.ag/go/debian/deb"
)

func TestPackage(t *testing.T) {
Expand Down Expand Up @@ -43,10 +45,45 @@ func TestPackage(t *testing.T) {
}

t.Run("deb", func(t *testing.T) {
lucasmrod marked this conversation as resolved.
Show resolved Hide resolved
runAppForTest(t, []string{"package", "--type=deb", "--insecure", "--disable-open-folder"})
info, err := os.Stat(fmt.Sprintf("fleet-osquery_%s_amd64.deb", updatesData.OrbitVersion))
require.NoError(t, err)
require.Greater(t, info.Size(), int64(0)) // TODO verify contents
shorterEnrollSecret := "aa"
longerEnrollSecret := "aaaaaa"

checkEnrollSecret := func(t *testing.T, expectedEnrollSecret string, updatesData *packaging.UpdatesData) {
runAppForTest(t, []string{"package", "--type=deb", "--insecure", "--disable-open-folder", "--enroll-secret=" + expectedEnrollSecret, "--fleet-url=https://localhost:8080"})
info, err := os.Stat(fmt.Sprintf("fleet-osquery_%s_amd64.deb", updatesData.OrbitVersion))
require.NoError(t, err)
require.Greater(t, info.Size(), int64(0))

fd, err := os.Open(fmt.Sprintf("fleet-osquery_%s_amd64.deb", updatesData.OrbitVersion))
require.NoError(t, err)

defer fd.Close()

debFile, err := deb.Load(fd, fmt.Sprintf("fleet-osquery_%s_amd64.deb", updatesData.OrbitVersion))
require.NoError(t, err)

for {
hdr, err := debFile.Data.Next()
if err == io.EOF {
break
}
require.NoError(t, err)

if hdr.Name != "./etc/default/orbit" {
continue
}

data, err := io.ReadAll(debFile.Data)
require.NoError(t, err)

s := string(data)
require.Contains(t, s, fmt.Sprintf("ORBIT_ENROLL_SECRET=%s\n", expectedEnrollSecret))
t.Log("we did it")
jahzielv marked this conversation as resolved.
Show resolved Hide resolved
}
}

checkEnrollSecret(t, shorterEnrollSecret, updatesData)
checkEnrollSecret(t, longerEnrollSecret, updatesData)
})
jahzielv marked this conversation as resolved.
Show resolved Hide resolved

t.Run("--use-sytem-configuration can't be used on installers that aren't pkg", func(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ require (
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/kjk/lzma v0.0.0-20161016003348-3fd93898850d // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down Expand Up @@ -293,6 +294,7 @@ require (
github.com/xanzy/ssh-agent v0.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yashtewari/glob-intersection v0.1.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
Expand All @@ -319,6 +321,8 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.0.3 // indirect
pault.ag/go/debian v0.16.0 // indirect
pault.ag/go/topsort v0.1.1 // indirect
)

replace github.com/kolide/kit => github.com/fleetdm/kolide-kit v0.0.0-20230519160117-86cc9441f9c1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,15 @@ github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kjk/lzma v0.0.0-20161016003348-3fd93898850d h1:RnWZeH8N8KXfbwMTex/KKMYMj0FJRCF6tQubUuQ02GM=
github.com/kjk/lzma v0.0.0-20161016003348-3fd93898850d/go.mod h1:phT/jsRPBAEqjAibu1BurrabCBNTYiVI+zbmyCZJY6Q=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kolide/launcher v1.0.12 h1:f2uT1kKYGIbj/WVsHDc10f7MIiwu8MpmgwaGaT7D09k=
github.com/kolide/launcher v1.0.12/go.mod h1:j854Q4LqMXi3DQ+fnDy8Ij4uuKRG707ulWOcIz7BCz4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -1910,6 +1914,10 @@ howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pault.ag/go/debian v0.16.0 h1:fivXn/IO9rn2nzTGndflDhOkNU703Axs/StWihOeU2g=
pault.ag/go/debian v0.16.0/go.mod h1:JFl0XWRCv9hWBrB5MDDZjA5GSEs1X3zcFK/9kCNIUmE=
pault.ag/go/topsort v0.1.1 h1:L0QnhUly6LmTv0e3DEzbN2q6/FGgAcQvaEw65S53Bg4=
pault.ag/go/topsort v0.1.1/go.mod h1:r1kc/L0/FZ3HhjezBIPaNVhkqv8L0UJ9bxRuHRVZ0q4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
5 changes: 5 additions & 0 deletions orbit/pkg/packaging/linux_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package packaging
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -217,6 +218,10 @@ func buildNFPM(opt Options, pkger nfpm.Packager) (string, error) {
filename = filepath.Join("build", filename)
}

if err := os.Remove(filename); err != nil && !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("removing existing file: %w", err)
}
lucasmrod marked this conversation as resolved.
Show resolved Hide resolved

out, err := secure.OpenFile(filename, os.O_CREATE|os.O_RDWR, constant.DefaultFileMode)
if err != nil {
return "", fmt.Errorf("open output file: %w", err)
Expand Down
Loading