forked from elastic/elastic-agent-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
github.com/elastic/beats/v7/libbeat/keystore
and `github.com/e…
…lastic/beats/v7/libbeat/common/file.FileInfo` (elastic#20)
- Loading branch information
Showing
14 changed files
with
1,407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 file | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
) | ||
|
||
// A FileInfo describes a file and is returned by Stat and Lstat. | ||
type FileInfo interface { | ||
os.FileInfo | ||
UID() (int, error) // UID of the file owner. Returns an error on non-POSIX file systems. | ||
GID() (int, error) // GID of the file owner. Returns an error on non-POSIX file systems. | ||
} | ||
|
||
// Stat returns a FileInfo describing the named file. | ||
// If there is an error, it will be of type *PathError. | ||
func Stat(name string) (FileInfo, error) { | ||
return stat(name, os.Stat) | ||
} | ||
|
||
// Lstat returns a FileInfo describing the named file. | ||
// If the file is a symbolic link, the returned FileInfo | ||
// describes the symbolic link. Lstat makes no attempt to follow the link. | ||
// If there is an error, it will be of type *PathError. | ||
func Lstat(name string) (FileInfo, error) { | ||
return stat(name, os.Lstat) | ||
} | ||
|
||
// Wrap wraps the given os.FileInfo and returns a FileInfo in order to expose | ||
// the UID and GID in a uniform manner across operating systems. | ||
func Wrap(info os.FileInfo) (FileInfo, error) { | ||
return wrap(info) | ||
} | ||
|
||
type fileInfo struct { | ||
os.FileInfo | ||
uid *int | ||
gid *int | ||
} | ||
|
||
func (f fileInfo) UID() (int, error) { | ||
if f.uid == nil { | ||
return -1, errors.New("uid not implemented") | ||
} | ||
|
||
return *f.uid, nil | ||
} | ||
|
||
func (f fileInfo) GID() (int, error) { | ||
if f.gid == nil { | ||
return -1, errors.New("gid not implemented") | ||
} | ||
|
||
return *f.gid, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
//go:build !windows && !openbsd | ||
// +build !windows,!openbsd | ||
|
||
// Test for openbsd are excluded here as info.GID() returns 0 instead of the actual value | ||
// As the code does not seem to be used in any of the beats, this should be ok | ||
// Still it would be interesting to know why it returns 0. | ||
|
||
package file_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/elastic-agent-libs/file" | ||
) | ||
|
||
func TestStat(t *testing.T) { | ||
f, err := ioutil.TempFile("", "teststat") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(f.Name()) | ||
|
||
link := filepath.Join(os.TempDir(), "teststat-link") | ||
if err := os.Symlink(f.Name(), link); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(link) | ||
|
||
info, err := file.Stat(link) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.True(t, info.Mode().IsRegular()) | ||
|
||
uid, err := info.UID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.EqualValues(t, os.Geteuid(), uid) | ||
|
||
gid, err := info.GID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.EqualValues(t, os.Getegid(), gid) | ||
} | ||
|
||
func TestLstat(t *testing.T) { | ||
link := filepath.Join(os.TempDir(), "link") | ||
if err := os.Symlink("dummy", link); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(link) | ||
|
||
info, err := file.Lstat(link) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.True(t, info.Mode()&os.ModeSymlink > 0) | ||
|
||
uid, err := info.UID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.EqualValues(t, os.Geteuid(), uid) | ||
|
||
gid, err := info.GID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.EqualValues(t, os.Getegid(), gid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
//go:build !windows | ||
// +build !windows | ||
|
||
package file | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) { | ||
info, err := statFunc(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return wrap(info) | ||
} | ||
|
||
func wrap(info os.FileInfo) (FileInfo, error) { | ||
stat, ok := info.Sys().(*syscall.Stat_t) | ||
if !ok { | ||
return nil, errors.New("failed to get uid/gid") | ||
} | ||
|
||
uid := int(stat.Uid) | ||
gid := int(stat.Gid) | ||
return fileInfo{FileInfo: info, uid: &uid, gid: &gid}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 file | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) { | ||
info, err := statFunc(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return wrap(info) | ||
} | ||
|
||
func wrap(info os.FileInfo) (FileInfo, error) { | ||
return fileInfo{FileInfo: info}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 keystore | ||
|
||
// Config Define keystore configurable options | ||
type Config struct { | ||
Path string `config:"path"` | ||
} | ||
|
||
func defaultConfig() Config { | ||
return Config{ | ||
Path: "", | ||
} | ||
} |
Oops, something went wrong.