Skip to content

Commit 5b4e674

Browse files
authored
Move github.com/elastic/beats/v7/libbeat/keystore and github.com/elastic/beats/v7/libbeat/common/file.FileInfo (#20)
1 parent 9d07e6f commit 5b4e674

14 files changed

+1407
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Provided packages:
1010
* `github.com/elastic/elastic-agent-libs/cloudid` is used for parsing `cloud.id` and `cloud.auth` when connecting to the Elastic stack.
1111
* `github.com/elastic/elastic-agent-libs/config` the previous `config.go` file from `github.com/elastic/beats/v7/libbeat/common`. A minimal wrapper around `github.com/elastic/go-ucfg`. It contains helpers for merging and accessing configuration objects and flags.
1212
* `github.com/elastic/elastic-agent-libs/file` is responsible for rotating and writing input and output files.
13+
* `github.com/elastic/elastic-agent-libs/keystore` interface for keystores and file keystore implementation.
1314
* `github.com/elastic/elastic-agent-libs/logp` is the well known logger from libbeat.
1415
* `github.com/elastic/elastic-agent-libs/logp/cfgwarn` provides logging utilities for warning users about deprecated settings.
1516
* `github.com/elastic/elastic-agent-libs/mapstr` is the old `github.com/elastic/beats/v7/libbeat/common.MapStr`. It is an extra layer on top of `map[string]interface{}`.

file/fileinfo.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package file
19+
20+
import (
21+
"errors"
22+
"os"
23+
)
24+
25+
// A FileInfo describes a file and is returned by Stat and Lstat.
26+
type FileInfo interface {
27+
os.FileInfo
28+
UID() (int, error) // UID of the file owner. Returns an error on non-POSIX file systems.
29+
GID() (int, error) // GID of the file owner. Returns an error on non-POSIX file systems.
30+
}
31+
32+
// Stat returns a FileInfo describing the named file.
33+
// If there is an error, it will be of type *PathError.
34+
func Stat(name string) (FileInfo, error) {
35+
return stat(name, os.Stat)
36+
}
37+
38+
// Lstat returns a FileInfo describing the named file.
39+
// If the file is a symbolic link, the returned FileInfo
40+
// describes the symbolic link. Lstat makes no attempt to follow the link.
41+
// If there is an error, it will be of type *PathError.
42+
func Lstat(name string) (FileInfo, error) {
43+
return stat(name, os.Lstat)
44+
}
45+
46+
// Wrap wraps the given os.FileInfo and returns a FileInfo in order to expose
47+
// the UID and GID in a uniform manner across operating systems.
48+
func Wrap(info os.FileInfo) (FileInfo, error) {
49+
return wrap(info)
50+
}
51+
52+
type fileInfo struct {
53+
os.FileInfo
54+
uid *int
55+
gid *int
56+
}
57+
58+
func (f fileInfo) UID() (int, error) {
59+
if f.uid == nil {
60+
return -1, errors.New("uid not implemented")
61+
}
62+
63+
return *f.uid, nil
64+
}
65+
66+
func (f fileInfo) GID() (int, error) {
67+
if f.gid == nil {
68+
return -1, errors.New("gid not implemented")
69+
}
70+
71+
return *f.gid, nil
72+
}

file/fileinfo_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build !windows && !openbsd
19+
// +build !windows,!openbsd
20+
21+
// Test for openbsd are excluded here as info.GID() returns 0 instead of the actual value
22+
// As the code does not seem to be used in any of the beats, this should be ok
23+
// Still it would be interesting to know why it returns 0.
24+
25+
package file_test
26+
27+
import (
28+
"io/ioutil"
29+
"os"
30+
"path/filepath"
31+
"testing"
32+
33+
"github.com/stretchr/testify/require"
34+
35+
"github.com/elastic/elastic-agent-libs/file"
36+
)
37+
38+
func TestStat(t *testing.T) {
39+
f, err := ioutil.TempFile("", "teststat")
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
defer os.Remove(f.Name())
44+
45+
link := filepath.Join(os.TempDir(), "teststat-link")
46+
if err := os.Symlink(f.Name(), link); err != nil {
47+
t.Fatal(err)
48+
}
49+
defer os.Remove(link)
50+
51+
info, err := file.Stat(link)
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
require.True(t, info.Mode().IsRegular())
57+
58+
uid, err := info.UID()
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
require.EqualValues(t, os.Geteuid(), uid)
63+
64+
gid, err := info.GID()
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
require.EqualValues(t, os.Getegid(), gid)
69+
}
70+
71+
func TestLstat(t *testing.T) {
72+
link := filepath.Join(os.TempDir(), "link")
73+
if err := os.Symlink("dummy", link); err != nil {
74+
t.Fatal(err)
75+
}
76+
defer os.Remove(link)
77+
78+
info, err := file.Lstat(link)
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
require.True(t, info.Mode()&os.ModeSymlink > 0)
84+
85+
uid, err := info.UID()
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
require.EqualValues(t, os.Geteuid(), uid)
90+
91+
gid, err := info.GID()
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
require.EqualValues(t, os.Getegid(), gid)
96+
}

file/fileinfo_unix.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build !windows
19+
// +build !windows
20+
21+
package file
22+
23+
import (
24+
"errors"
25+
"os"
26+
"syscall"
27+
)
28+
29+
func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) {
30+
info, err := statFunc(name)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return wrap(info)
36+
}
37+
38+
func wrap(info os.FileInfo) (FileInfo, error) {
39+
stat, ok := info.Sys().(*syscall.Stat_t)
40+
if !ok {
41+
return nil, errors.New("failed to get uid/gid")
42+
}
43+
44+
uid := int(stat.Uid)
45+
gid := int(stat.Gid)
46+
return fileInfo{FileInfo: info, uid: &uid, gid: &gid}, nil
47+
}

file/fileinfo_windows.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package file
19+
20+
import (
21+
"os"
22+
)
23+
24+
func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) {
25+
info, err := statFunc(name)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return wrap(info)
31+
}
32+
33+
func wrap(info os.FileInfo) (FileInfo, error) {
34+
return fileInfo{FileInfo: info}, nil
35+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/stretchr/testify v1.7.0
1414
go.elastic.co/ecszap v1.0.0
1515
go.uber.org/zap v1.21.0
16+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
1617
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
1718
)
1819

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U
384384
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
385385
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
386386
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
387+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
387388
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
388389
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
389390
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

keystore/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package keystore
19+
20+
// Config Define keystore configurable options
21+
type Config struct {
22+
Path string `config:"path"`
23+
}
24+
25+
func defaultConfig() Config {
26+
return Config{
27+
Path: "",
28+
}
29+
}

0 commit comments

Comments
 (0)