-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This function is relevant also outside of the `ginaudit` package, so let's make it available through a new package. Signed-off-by: Juan Antonio Osorio <[email protected]>
- Loading branch information
Showing
6 changed files
with
143 additions
and
97 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
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,49 @@ | ||
/* | ||
Copyright 2022 Equinix, Inc. | ||
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 helpers | ||
|
||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
const ( | ||
ownerGroupAccess = 0o640 | ||
retryInterval = 100 * time.Millisecond | ||
) | ||
|
||
// OpenAuditLogFileUntilSuccess attempts to open a file for writing audit events until | ||
// it succeeds. | ||
// It assumes that audit events are less than 4096 bytes to ensure atomicity. | ||
// it takes a writer for the audit log. | ||
func OpenAuditLogFileUntilSuccess(path string) (*os.File, error) { | ||
for { | ||
// This is opened with the O_APPEND option to ensure | ||
// atomicity of writes. This is important to ensure | ||
// we can concurrently write to the file and not block | ||
// the server's main loop. | ||
fd, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, ownerGroupAccess) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
time.Sleep(retryInterval) | ||
continue | ||
} | ||
// Not being able to write audit log events is a fatal error | ||
return nil, err | ||
} | ||
return fd, 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,90 @@ | ||
/* | ||
Copyright 2022 Equinix, Inc. | ||
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 helpers_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/metal-toolbox/auditevent/helpers" | ||
) | ||
|
||
func TestOpenAuditLogFileUntilSuccess(t *testing.T) { | ||
t.Parallel() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
tmpdir := t.TempDir() | ||
tmpfile := filepath.Join(tmpdir, "audit.log") | ||
|
||
go func() { | ||
defer wg.Done() | ||
time.Sleep(time.Second) | ||
fd, err := os.OpenFile(tmpfile, os.O_RDONLY|os.O_CREATE, 0o600) | ||
require.NoError(t, err) | ||
err = fd.Close() | ||
require.NoError(t, err) | ||
}() | ||
|
||
fd, err := helpers.OpenAuditLogFileUntilSuccess(tmpfile) | ||
require.NoError(t, err) | ||
require.NotNil(t, fd) | ||
|
||
err = fd.Close() | ||
require.NoError(t, err) | ||
|
||
// We wait so we don't leak file descriptors | ||
wg.Wait() | ||
|
||
err = os.Remove(tmpfile) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestOpenAuditLogFileError(t *testing.T) { | ||
t.Parallel() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
tmpdir := t.TempDir() | ||
tmpfile := filepath.Join(tmpdir, "audit.log") | ||
|
||
go func() { | ||
defer wg.Done() | ||
time.Sleep(time.Second) | ||
// This file is read only | ||
fd, err := os.OpenFile(tmpfile, os.O_RDONLY|os.O_CREATE, 0o500) | ||
require.NoError(t, err) | ||
err = fd.Close() | ||
require.NoError(t, err) | ||
}() | ||
|
||
fd, err := helpers.OpenAuditLogFileUntilSuccess(tmpfile) | ||
require.Error(t, err) | ||
require.Nil(t, fd) | ||
|
||
// We wait so we don't leak file descriptors | ||
wg.Wait() | ||
|
||
err = os.Remove(tmpfile) | ||
require.NoError(t, err) | ||
} |
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