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

Cherry-pick #19793 to 7.x: [Elastic Agent] Fix saving of agent configuration on Windows to have proper ACLs #19806

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10641,6 +10641,25 @@ Exhibit B - “Incompatible With Secondary Licenses” Notice
the Mozilla Public License, v. 2.0.


--------------------------------------------------------------------------------
Dependency : github.com/hectane/go-acl
Version: v0.0.0-20190604041725-da78bae5fc95
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/hectane/[email protected]/LICENSE.txt:

The MIT License (MIT)

Copyright (c) 2015 Nathan Osman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/elastic/dhcp
Version: v0.0.0-20200227161230-57ec251c7eb3
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/h2non/filetype v1.0.12
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/golang-lru v0.5.2-0.20190520140433-59383c442f7d // indirect
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95
github.com/insomniacslk/dhcp v0.0.0-20180716145214-633285ba52b2
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ github.com/haya14busa/go-actions-toolkit v0.0.0-20200105081403-ca0307860f01 h1:H
github.com/haya14busa/go-actions-toolkit v0.0.0-20200105081403-ca0307860f01/go.mod h1:1DWDZmeYf0LX30zscWb7K9rUMeirNeBMd5Dum+seUhc=
github.com/haya14busa/go-checkstyle v0.0.0-20170303121022-5e9d09f51fa1/go.mod h1:RsN5RGgVYeXpcXNtWyztD5VIe7VNSEqpJvF2iEH7QvI=
github.com/haya14busa/secretbox v0.0.0-20180525171038-07c7ecf409f5/go.mod h1:FGO/dXIFZnan7KvvUSFk1hYMnoVNzB6NTMPrmke8SSI=
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 h1:S4qyfL2sEm5Budr4KVMyEniCy+PbS55651I/a+Kn/NQ=
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95/go.mod h1:QiyDdbZLaJ/mZP4Zwc9g2QsfaEA4o7XvvgZegSci5/E=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -811,6 +813,7 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190514135907-3a4b5fb9f71f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190529164535-6a60838ec259/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
16 changes: 16 additions & 0 deletions x-pack/elastic-agent/pkg/agent/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
"time"

"github.com/hectane/go-acl"

"github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
)
Expand Down Expand Up @@ -129,6 +131,13 @@ func (r *ReplaceOnSuccessStore) Save(in io.Reader) error {
}
}

if err := acl.Chmod(r.target, perms); err != nil {
return errors.New(err,
fmt.Sprintf("could not set permissions target file %s", r.target),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, r.target))
}

return nil
}

Expand Down Expand Up @@ -178,6 +187,13 @@ func (d *DiskStore) Save(in io.Reader) error {
errors.M(errors.MetaKeyPath, d.target))
}

if err := acl.Chmod(d.target, perms); err != nil {
return errors.New(err,
fmt.Sprintf("could not set permissions target file %s", d.target),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, d.target))
}

return nil
}

Expand Down
40 changes: 16 additions & 24 deletions x-pack/elastic-agent/pkg/agent/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -49,11 +50,7 @@ func TestReplaceOrRollbackStore(t *testing.T) {

require.True(t, bytes.Equal(writtenContent, replaceWith))
requireFilesCount(t, dir, 2)

info, err := os.Stat(target)
require.NoError(t, err)

require.Equal(t, perms, info.Mode())
checkPerms(t, target, perms)
})

t.Run("when save is not successful", func(t *testing.T) {
Expand Down Expand Up @@ -103,10 +100,6 @@ func TestReplaceOrRollbackStore(t *testing.T) {
require.True(t, bytes.Equal(writtenContent, replaceWith))
requireFilesCount(t, dir, 1)

info, err := os.Stat(target)
require.NoError(t, err)

require.Equal(t, perms, info.Mode())
})

t.Run("when target file do not exist", func(t *testing.T) {
Expand Down Expand Up @@ -135,11 +128,7 @@ func TestDiskStore(t *testing.T) {
require.NoError(t, err)

require.Equal(t, msg, content)

info, err := os.Stat(target)
require.NoError(t, err)

require.Equal(t, perms, info.Mode())
checkPerms(t, target, perms)
})

t.Run("when the target do no exist", func(t *testing.T) {
Expand All @@ -158,11 +147,7 @@ func TestDiskStore(t *testing.T) {
require.NoError(t, err)

require.Equal(t, msg, content)

info, err := os.Stat(target)
require.NoError(t, err)

require.Equal(t, perms, info.Mode())
checkPerms(t, target, perms)
})

t.Run("return an io.ReadCloser to the target file", func(t *testing.T) {
Expand All @@ -178,11 +163,7 @@ func TestDiskStore(t *testing.T) {
content, err := ioutil.ReadAll(r)
require.NoError(t, err)
require.Equal(t, msg, content)

info, err := os.Stat(target)
require.NoError(t, err)

require.Equal(t, perms, info.Mode())
checkPerms(t, target, perms)
})
}

Expand Down Expand Up @@ -210,3 +191,14 @@ func requireFilesCount(t *testing.T, dir string, l int) {
require.NoError(t, err)
require.Equal(t, l, len(files))
}

func checkPerms(t *testing.T, target string, expected os.FileMode) {
t.Helper()
if runtime.GOOS == "windows" {
// Windows API validation of ACL is skipped, as its very complicated.
return
}
info, err := os.Stat(target)
require.NoError(t, err)
require.Equal(t, expected, info.Mode())
}