Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

1.3 KB
Raw Blame
package store

import (
	"context"

	"github.com/pkg/errors"

	"github.com/bytebase/bytebase/backend/common"
	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)

// GetServerConfig returns the global server configuration.
// Stored in the server_config table (single row, not workspace-scoped).
func (s *Store) GetServerConfig(ctx context.Context) (*storepb.ServerConfigPayload, error) {
	var payloadBytes []byte
	if err := s.GetDB().QueryRowContext(ctx,
		`SELECT payload FROM server_config LIMIT 1`,
	).Scan(&payloadBytes); err != nil {
		return nil, errors.Wrap(err, "failed to get server config")
	}
	config := &storepb.ServerConfigPayload{}
	if err := common.ProtojsonUnmarshaler.Unmarshal(payloadBytes, config); err != nil {
		return nil, errors.Wrap(err, "failed to unmarshal server config")
	}
	return config, nil
}

// GetAuthSecret returns the global auth secret used for JWT signing.
// Only migrations write server_config, so the first successful read is kept for
// the life of the Store, even when the cache is disabled.
func (s *Store) GetAuthSecret(ctx context.Context) (string, error) {
	if cached := s.authSecret.Load(); cached != nil {
		return *cached, nil
	}
	config, err := s.GetServerConfig(ctx)
	if err != nil {
		return "", err
	}
	secret := config.AuthSecret
	s.authSecret.Store(&secret)
	return secret, nil
}