diff --git a/configuration/postgres/metadata.go b/configuration/postgres/metadata.go index 9ec52a0908..f30939067a 100644 --- a/configuration/postgres/metadata.go +++ b/configuration/postgres/metadata.go @@ -63,6 +63,11 @@ func (m *metadata) InitWithMetadata(meta map[string]string) error { return fmt.Errorf("invalid table name '%s'. non-alphanumerics or upper cased table names are not supported", m.ConfigTable) } + // Timeout + if m.Timeout < 1*time.Second { + return errors.New("invalid value for 'timeout': must be greater than 1s") + } + opts := pgauth.InitWithMetadataOpts{ AzureADEnabled: true, AWSIAMEnabled: true, diff --git a/configuration/postgres/postgres.go b/configuration/postgres/postgres.go index 734a199f0d..2a130c70df 100644 --- a/configuration/postgres/postgres.go +++ b/configuration/postgres/postgres.go @@ -76,9 +76,24 @@ var ( allowedTableNameChars = regexp.MustCompile(`^[a-z0-9./_]*$`) ) +type Options struct { + // Disables support for authenticating with Azure AD + NoAzureAD bool + + // Disables support for authenticating with AWS IAM + NoAWSIAM bool +} + func NewPostgresConfigurationStore(logger logger.Logger) configuration.Store { + return NewPostgresConfigurationStoreWithOptions(logger, Options{}) +} + +// NewPostgresConfigurationStoreWithOptions creates a new instance of PostgreSQL store with options. +func NewPostgresConfigurationStoreWithOptions(logger logger.Logger, opts Options) configuration.Store { return &ConfigurationStore{ logger: logger, + enableAzureAD: !opts.NoAzureAD, + enableAWSIAM: !opts.NoAWSIAM, } } @@ -114,21 +129,20 @@ func (p *ConfigurationStore) Init(ctx context.Context, metadata configuration.Me p.awsAuthProvider.UpdatePostgres(ctx, config) } - pool, err := pgxpool.NewWithConfig(ctx, config) + connCtx, connCancel := context.WithTimeout(ctx, p.metadata.Timeout) + defer connCancel() + p.client, err = pgxpool.NewWithConfig(connCtx, config) if err != nil { return fmt.Errorf("PostgreSQL configuration store connection error: %w", err) } - err = pool.Ping(ctx) + pingCtx, pingCancel := context.WithTimeout(ctx, p.metadata.Timeout) + defer pingCancel() + err = p.client.Ping(pingCtx) if err != nil { return fmt.Errorf("PostgreSQL configuration store ping error: %w", err) } - p.client = pool - err = p.client.Ping(ctx) - if err != nil { - return fmt.Errorf("unable to connect to configuration store: '%w'", err) - } // check if table exists exists := false err = p.client.QueryRow(ctx, QueryTableExists, p.metadata.ConfigTable).Scan(&exists) diff --git a/state/postgresql/v2/metadata.go b/state/postgresql/v2/metadata.go index 2268b3c9a3..ce3fa7f30e 100644 --- a/state/postgresql/v2/metadata.go +++ b/state/postgresql/v2/metadata.go @@ -60,7 +60,7 @@ func (m *pgMetadata) InitWithMetadata(meta state.Metadata, opts pgauth.InitWithM return err } - // Validate and sanitize inputq + // Validate and sanitize input err = m.PostgresAuthMetadata.InitWithMetadata(meta.Properties, opts) if err != nil { return err