Files
Krzysztof Skrzętnicki 8a8537355b Database objects periodic import (#43790)
* Add periodic import of database objects from supported databases.

A single implementation is included: for Postgres, along with necessary changes.

Also refactors SQLAdminClient interface to accept cert expiry instead of pulling that information from `tlsca.Identity`.

* fix logging, imports

* add missing license

* review feedback
- several minor changes
- apply import rules for all registered object fetchers using wrapper type
- refactor DAC to use object fetcher
- adjust tests

* add comment and missing log call

* support config overrides through unstable env vars, including disabling the service entirely

* refactoring and minor changes

- be more specific about required API for ObjectFetcherConfig
- measure scan time
- bump default scan interval
- call StopImporter from stopDatabase to fix issue with dynamic db resource updates
- add comments

* Refactor `connectAsAdmin`, add `withDefaultDatabase()` method for clarity of implementation.

* Reduce surface area for fetcher config

* make the not-implemented case less noisy by skipping the error

* lint fix

* decrease verbosity of reporting for unsupported configurations

* make `reason` field private

* move `unregisterObjectFetcher` to tests

* avoid exceedingly long lines with log calls

* correct formatting leftover from slog conversion

* tighten logic around disabled importer

if the scan interval is zero or less, the importer becomes disabled, which avoids panic from interval.New().

add some tests and comments for clarity.

* drop unused method
2024-07-25 18:47:56 +00:00

165 lines
4.1 KiB
Go

// Teleport
// Copyright (C) 2024 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package objects
import (
"context"
"log/slog"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestLoadEnvVar(t *testing.T) {
testCases := []struct {
name string
envValue string
expectedFound bool
expectedDuration time.Duration
}{
{"empty", "", false, 0},
{"never", "never", true, 0},
{"valid duration", "1h", true, time.Hour},
{"invalid duration", "invalid", true, 0},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("TEST_ENV_VAR", tc.envValue)
c := &Config{Log: slog.Default()}
found, duration := c.loadEnvVar(context.Background(), "TEST_ENV_VAR")
require.Equal(t, tc.expectedFound, found)
require.Equal(t, tc.expectedDuration, duration)
})
}
}
func TestLoadEnvVarOverrides(t *testing.T) {
testCases := []struct {
name string
envVars map[string]string
expectedScanInt time.Duration
expectedObjectTTL time.Duration
expectedRefreshTh time.Duration
}{
{
name: "no overrides",
envVars: map[string]string{},
expectedScanInt: 0,
expectedObjectTTL: 0,
expectedRefreshTh: 0,
},
{
name: "scan interval only",
envVars: map[string]string{
"TELEPORT_UNSTABLE_DB_OBJECTS_SCAN_INTERVAL": "1h",
},
expectedScanInt: time.Hour,
expectedObjectTTL: 12 * time.Hour,
expectedRefreshTh: 3 * time.Hour,
},
{
name: "object ttl only",
envVars: map[string]string{
"TELEPORT_UNSTABLE_DB_OBJECTS_OBJECT_TTL": "2h",
},
expectedScanInt: 0,
expectedObjectTTL: 2 * time.Hour,
expectedRefreshTh: 0,
},
{
name: "refresh threshold only",
envVars: map[string]string{
"TELEPORT_UNSTABLE_DB_OBJECTS_REFRESH_THRESHOLD": "3h",
},
expectedScanInt: 0,
expectedObjectTTL: 0,
expectedRefreshTh: 3 * time.Hour,
},
{
name: "all overrides",
envVars: map[string]string{
"TELEPORT_UNSTABLE_DB_OBJECTS_SCAN_INTERVAL": "1h",
"TELEPORT_UNSTABLE_DB_OBJECTS_OBJECT_TTL": "2h",
"TELEPORT_UNSTABLE_DB_OBJECTS_REFRESH_THRESHOLD": "3h",
},
expectedScanInt: time.Hour,
expectedObjectTTL: 2 * time.Hour,
expectedRefreshTh: 3 * time.Hour,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for k, v := range tc.envVars {
t.Setenv(k, v)
}
c := &Config{Log: slog.Default()}
c.loadEnvVarOverrides(context.Background())
require.Equal(t, tc.expectedScanInt, c.ScanInterval)
require.Equal(t, tc.expectedObjectTTL, c.ObjectTTL)
require.Equal(t, tc.expectedRefreshTh, c.RefreshThreshold)
})
}
}
func TestConfigDisabled(t *testing.T) {
testCases := []struct {
name string
cfg *Config
expected bool
}{
{
name: "default config",
cfg: &Config{
ScanInterval: time.Minute * 15,
ObjectTTL: time.Minute * 180,
RefreshThreshold: time.Minute * 45,
},
expected: false,
},
{
name: "disabled through object TTL",
cfg: &Config{
ScanInterval: time.Minute * 15,
ObjectTTL: 0,
RefreshThreshold: time.Minute * 45,
},
expected: true,
},
{
name: "disabled through scan interval",
cfg: &Config{
ScanInterval: 0,
ObjectTTL: time.Minute * 180,
RefreshThreshold: time.Minute * 45,
},
expected: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, tc.cfg.disabled())
})
}
}