Add hclog adapter and gating to eventer (#1915)
This contains to related things: 1. An adapter allowing an hclog interface to take in information and send them to the eventer 2. A gate on the eventer, which is the analogue to the behavior prior to eventing where we'd ensure logs were not printed prior to the startup information. Without this, plugin logs using the hclog adapter will start showing up too early.
This commit is contained in:
1 parent
7eb29261b2
commit
e596daf34d
17 files changed
+879
-53
No files matched your search
@@ -39,6 +39,7 @@ type Options struct {
|
||||
withAttributeFieldPrefix string
|
||||
withStatusCode int
|
||||
withHostPlugin func() (string, plugin.HostPluginServiceClient)
|
||||
withEventGating bool
|
||||
}
|
||||
|
||||
func getDefaultOptions() Options {
|
||||
@@ -180,3 +181,10 @@ func WithHostPlugin(pluginId string, plg plugin.HostPluginServiceClient) Option
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithEventGating starts the eventer in gated mode
|
||||
func WithEventGating(with bool) Option {
|
||||
return func(o *Options) {
|
||||
o.withEventGating = with
|
||||
}
|
||||
}
|
||||
@@ -117,4 +117,11 @@ func Test_GetOpts(t *testing.T) {
|
||||
testOpts.withDialect = "test-dialect"
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
t.Run("withEventGating", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
testOpts := getDefaultOptions()
|
||||
assert.False(testOpts.withEventGating)
|
||||
opts := getOpts(WithEventGating(true))
|
||||
assert.True(opts.withEventGating)
|
||||
})
|
||||
}
|
||||
@@ -59,6 +59,8 @@ func TestServer_SetupKMSes(t *testing.T) {
|
||||
purposes: []string{globals.KmsPurposeRoot, globals.KmsPurposeRecovery, globals.KmsPurposeWorkerAuth, globals.KmsPurposeConfig},
|
||||
},
|
||||
}
|
||||
logger := hclog.Default()
|
||||
serLock := new(sync.Mutex)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
@@ -70,8 +72,9 @@ func TestServer_SetupKMSes(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
s := NewServer(&Command{})
|
||||
err := s.SetupKMSes(context.Background(), cli.NewMockUi(), &config.Config{SharedConfig: conf})
|
||||
s := NewServer(&Command{Context: context.Background()})
|
||||
require.NoError(s.SetupEventing(logger, serLock, "setup-kms-testing"))
|
||||
err := s.SetupKMSes(s.Context, cli.NewMockUi(), &config.Config{SharedConfig: conf})
|
||||
|
||||
if tt.wantErrContains != "" {
|
||||
require.Error(err)
|
||||
|
||||
@@ -205,7 +205,8 @@ func (b *Server) SetupEventing(logger hclog.Logger, serializationLock *sync.Mute
|
||||
serializationLock,
|
||||
serverName,
|
||||
*opts.withEventerConfig,
|
||||
event.WithAuditWrapper(opts.withEventWrapper))
|
||||
event.WithAuditWrapper(opts.withEventWrapper),
|
||||
event.WithGating(opts.withEventGating))
|
||||
if err != nil {
|
||||
return berrors.WrapDeprecated(err, op, berrors.WithMsg("unable to create eventer"))
|
||||
}
|
||||
@@ -283,11 +284,15 @@ func (b *Server) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, confi
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Server) ReleaseLogGate() {
|
||||
func (b *Server) ReleaseLogGate() error {
|
||||
// Release the log gate.
|
||||
b.Logger.(hclog.OutputResettable).ResetOutputWithFlush(&hclog.LoggerOptions{
|
||||
Output: b.logOutput,
|
||||
}, b.GatedWriter)
|
||||
if b.Eventer != nil {
|
||||
return b.Eventer.ReleaseGate()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Server) StorePidFile(pidPath string) error {
|
||||
@@ -482,6 +487,7 @@ func (b *Server) SetupListeners(ui cli.Ui, config *configutil.SharedConfig, allo
|
||||
// and sends each off to configutil to instantiate a wrapper.
|
||||
func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Config) error {
|
||||
sharedConfig := config.SharedConfig
|
||||
var pluginLogger hclog.Logger
|
||||
var err error
|
||||
for _, kms := range sharedConfig.Seals {
|
||||
for _, purpose := range kms.Purpose {
|
||||
@@ -498,6 +504,13 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
return fmt.Errorf("Unknown KMS purpose %q", kms.Purpose)
|
||||
}
|
||||
|
||||
if pluginLogger == nil {
|
||||
pluginLogger, err = event.NewHclogLogger(b.Context, b.Eventer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating KMS plugin logger: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// This can be modified by configutil so store the original value
|
||||
origPurpose := kms.Purpose
|
||||
kms.Purpose = []string{purpose}
|
||||
@@ -509,7 +522,9 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
&b.Info,
|
||||
configutil.WithPluginOptions(
|
||||
pluginutil.WithPluginsMap(kms_plugin_assets.BuiltinKmsPlugins()),
|
||||
pluginutil.WithPluginsFilesystem(kms_plugin_assets.KmsPluginPrefix, kms_plugin_assets.FileSystem())),
|
||||
pluginutil.WithPluginsFilesystem(kms_plugin_assets.KmsPluginPrefix, kms_plugin_assets.FileSystem()),
|
||||
),
|
||||
configutil.WithLogger(pluginLogger.Named(kms.Type).With("purpose", purpose)),
|
||||
)
|
||||
if wrapperConfigError != nil {
|
||||
return fmt.Errorf(
|
||||
@@ -551,7 +566,6 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
default:
|
||||
return fmt.Errorf("KMS purpose of %q is unknown", purpose)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user