The console previously dispatched per-provider logic through a fan of init()-side-effect maps (driver names, OAuth2 metadata, probe URLs, display names, settings switches), spread across pkg/connector, pkg/accessreview/drivers and the console v1 resolvers. Adding a new provider required edits in every one of those places and a corresponding switch arm in CreateConnectorRequest. The same per-provider knowledge also leaked into Helm templates as hand-rolled environment-variable blocks per connector. This commit collapses the dispatch surface into a single typed *provider.Registry. The registry is constructed once by NewBuiltinRegistry at probod startup and threaded as an explicit dependency into every consumer (accessreview service, console v1 resolver, OAuth2 wiring). There is no package-level state. Each provider lives in one file under pkg/connector/provider/ that exposes a private xxxRegistration() *Registration constructor; NewBuiltinRegistry enumerates them. CreateConnectorRequest loses its per-provider settings fields and takes a single RawSettings json.RawMessage produced by the per-provider MarshalSettings closure. The 1Password SCIM bridge URL is validated at create time (http(s) scheme + non-empty host) so a malformed value fails fast at the resolver boundary. The Helm chart gains probo.connectorEnv and probo.connectorSecretEntries templates so adding a connector requires zero Helm changes. Access-review name resolution moves into the same Registration value to keep one authoritative dispatch table. Tests cover every Registration (DisplayName, NewDriver wired), Register error paths (nil, empty Provider, empty DisplayName, duplicate), All / ProviderDisplayName / ProviderOAuth2Scopes / ProbeURL hit and miss paths, the ApplyOAuth2Defaults templating and PKCE branches, and ConnectorSettings[T] round-trip plus malformed-JSON error path. The pre-refactor ApplyProviderDefaults test in pkg/connector is replaced by the equivalent in pkg/connector/provider. Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
264 lines
7.1 KiB
Go
264 lines
7.1 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package accessreview
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.gearno.de/kit/log"
|
|
"go.gearno.de/kit/pg"
|
|
"go.gearno.de/kit/worker"
|
|
"go.probo.inc/probo/pkg/accessreview/drivers"
|
|
"go.probo.inc/probo/pkg/connector"
|
|
"go.probo.inc/probo/pkg/connector/provider"
|
|
"go.probo.inc/probo/pkg/coredata"
|
|
"go.probo.inc/probo/pkg/crypto/cipher"
|
|
)
|
|
|
|
// sourceNameHandler polls for access sources that have a connector but no
|
|
// synced name, resolves the provider instance name, and updates the source.
|
|
type sourceNameHandler struct {
|
|
pg *pg.Client
|
|
encryptionKey cipher.EncryptionKey
|
|
connectorRegistry *connector.ConnectorRegistry
|
|
providerRegistry *provider.Registry
|
|
logger *log.Logger
|
|
}
|
|
|
|
func NewSourceNameWorker(
|
|
pgClient *pg.Client,
|
|
encryptionKey cipher.EncryptionKey,
|
|
connectorRegistry *connector.ConnectorRegistry,
|
|
providerRegistry *provider.Registry,
|
|
logger *log.Logger,
|
|
opts ...worker.Option,
|
|
) *worker.Worker[coredata.AccessSource] {
|
|
h := &sourceNameHandler{
|
|
pg: pgClient,
|
|
encryptionKey: encryptionKey,
|
|
connectorRegistry: connectorRegistry,
|
|
providerRegistry: providerRegistry,
|
|
logger: logger,
|
|
}
|
|
|
|
defaultOpts := []worker.Option{
|
|
worker.WithInterval(10 * time.Second),
|
|
worker.WithMaxConcurrency(1),
|
|
}
|
|
|
|
return worker.New(
|
|
"source-name-worker",
|
|
h,
|
|
logger,
|
|
append(defaultOpts, opts...)...,
|
|
)
|
|
}
|
|
|
|
func (h *sourceNameHandler) Claim(ctx context.Context) (coredata.AccessSource, error) {
|
|
var source coredata.AccessSource
|
|
|
|
err := h.pg.WithTx(
|
|
ctx,
|
|
func(ctx context.Context, tx pg.Tx) error {
|
|
return source.LoadNextUnsyncedNameForUpdateSkipLocked(ctx, tx)
|
|
},
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, coredata.ErrNoAccessSourceNameSyncAvailable) {
|
|
return coredata.AccessSource{}, worker.ErrNoTask
|
|
}
|
|
|
|
return coredata.AccessSource{}, err
|
|
}
|
|
|
|
return source, nil
|
|
}
|
|
|
|
func (h *sourceNameHandler) Process(ctx context.Context, source coredata.AccessSource) error {
|
|
h.logger.InfoCtx(
|
|
ctx,
|
|
"syncing source name",
|
|
log.String("source_id", source.ID.String()),
|
|
log.String("current_name", source.Name),
|
|
)
|
|
|
|
var (
|
|
dbConnector coredata.Connector
|
|
resolver drivers.NameResolver
|
|
)
|
|
|
|
err := h.pg.WithTx(
|
|
ctx,
|
|
func(ctx context.Context, tx pg.Tx) error {
|
|
scope := coredata.NewScopeFromObjectID(source.ID)
|
|
if source.ConnectorID == nil {
|
|
return fmt.Errorf("source %s has no connector", source.ID)
|
|
}
|
|
|
|
if err := dbConnector.LoadByID(ctx, tx, scope, *source.ConnectorID, h.encryptionKey); err != nil {
|
|
return fmt.Errorf("cannot load connector %s: %w", *source.ConnectorID, err)
|
|
}
|
|
|
|
var tokenBefore string
|
|
if oauth2Conn, ok := dbConnector.Connection.(*connector.OAuth2Connection); ok {
|
|
tokenBefore = oauth2Conn.AccessToken
|
|
}
|
|
|
|
httpClient, err := h.connectorHTTPClient(ctx, &dbConnector)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot create HTTP client for connector: %w", err)
|
|
}
|
|
|
|
if oauth2Conn, ok := dbConnector.Connection.(*connector.OAuth2Connection); ok {
|
|
if oauth2Conn.AccessToken != tokenBefore {
|
|
dbConnector.UpdatedAt = time.Now()
|
|
if err := dbConnector.Update(ctx, tx, scope, h.encryptionKey); err != nil {
|
|
return fmt.Errorf("cannot persist refreshed token for connector %s: %w", *source.ConnectorID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
resolver = h.buildResolver(ctx, &dbConnector, httpClient)
|
|
|
|
return nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
h.logger.ErrorCtx(
|
|
ctx,
|
|
"cannot load connector for source name sync",
|
|
log.String("source_id", source.ID.String()),
|
|
log.Error(err),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
if resolver == nil {
|
|
h.logger.InfoCtx(
|
|
ctx,
|
|
"no name resolver for provider, keeping generic name",
|
|
log.String("source_id", source.ID.String()),
|
|
log.String("provider", dbConnector.Provider.String()),
|
|
)
|
|
|
|
return h.markNameSynced(ctx, &source)
|
|
}
|
|
|
|
resolveCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
instanceName, err := resolver.ResolveInstanceName(resolveCtx)
|
|
if err != nil {
|
|
h.logger.ErrorCtx(
|
|
ctx,
|
|
"cannot resolve instance name",
|
|
log.String("source_id", source.ID.String()),
|
|
log.String("provider", dbConnector.Provider.String()),
|
|
log.Error(err),
|
|
)
|
|
|
|
return fmt.Errorf("cannot resolve instance name for source %s: %w", source.ID, err)
|
|
}
|
|
|
|
if instanceName == "" {
|
|
h.logger.InfoCtx(
|
|
ctx,
|
|
"instance name is empty, keeping generic name",
|
|
log.String("source_id", source.ID.String()),
|
|
log.String("provider", dbConnector.Provider.String()),
|
|
)
|
|
|
|
return h.markNameSynced(ctx, &source)
|
|
}
|
|
|
|
displayName := h.providerRegistry.ProviderDisplayName(dbConnector.Provider)
|
|
newName := displayName + " " + instanceName
|
|
|
|
h.logger.InfoCtx(
|
|
ctx,
|
|
"resolved source name",
|
|
log.String("source_id", source.ID.String()),
|
|
log.String("old_name", source.Name),
|
|
log.String("new_name", newName),
|
|
)
|
|
|
|
source.Name = newName
|
|
|
|
return h.markNameSynced(ctx, &source)
|
|
}
|
|
|
|
func (h *sourceNameHandler) markNameSynced(
|
|
ctx context.Context,
|
|
source *coredata.AccessSource,
|
|
) error {
|
|
return h.pg.WithTx(
|
|
ctx,
|
|
func(ctx context.Context, tx pg.Tx) error {
|
|
scope := coredata.NewScopeFromObjectID(source.ID)
|
|
now := time.Now()
|
|
|
|
source.NameSyncedAt = new(now)
|
|
source.UpdatedAt = now
|
|
|
|
if err := source.Update(ctx, tx, scope); err != nil {
|
|
return fmt.Errorf("cannot update access source: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
)
|
|
}
|
|
|
|
// connectorHTTPClient returns an HTTP client for the given connector.
|
|
// For OAuth2 connections it uses RefreshableClient when a refresh config
|
|
// is registered for the provider, so that short-lived tokens are
|
|
// transparently refreshed.
|
|
func (h *sourceNameHandler) connectorHTTPClient(
|
|
ctx context.Context,
|
|
dbConnector *coredata.Connector,
|
|
) (*http.Client, error) {
|
|
oauth2Conn, ok := dbConnector.Connection.(*connector.OAuth2Connection)
|
|
if !ok {
|
|
return dbConnector.Connection.Client(ctx)
|
|
}
|
|
|
|
if h.connectorRegistry != nil {
|
|
refreshCfg := h.connectorRegistry.GetOAuth2RefreshConfig(string(dbConnector.Provider))
|
|
if refreshCfg != nil {
|
|
return oauth2Conn.RefreshableClient(ctx, *refreshCfg)
|
|
}
|
|
}
|
|
|
|
return oauth2Conn.Client(ctx)
|
|
}
|
|
|
|
func (h *sourceNameHandler) buildResolver(
|
|
ctx context.Context,
|
|
dbConnector *coredata.Connector,
|
|
httpClient *http.Client,
|
|
) drivers.NameResolver {
|
|
reg, ok := h.providerRegistry.Get(dbConnector.Provider)
|
|
if !ok || reg.NewNameResolver == nil {
|
|
return nil
|
|
}
|
|
|
|
return reg.NewNameResolver(ctx, httpClient, dbConnector, h.logger)
|
|
}
|