added updated code for core wiring

1. Removed tracing
2. Skipped Registration
This commit is contained in:
MohitKatare-protean
2025-03-25 21:06:34 +05:30
parent 519cca19af
commit ec558558c5
87 changed files with 9279 additions and 711 deletions

322
pkg/log/log.go Normal file
View File

@@ -0,0 +1,322 @@
package log
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/rs/zerolog"
"gopkg.in/natefinch/lumberjack.v2"
)
// Error definitions for logging configuration.
var (
ErrInvalidLogLevel = errors.New("invalid log level")
ErrLogDestinationNil = errors.New("log Destinations cant be empty")
ErrMissingFilePath = errors.New("file path missing in destination config for file logging")
)
// DestinationType represents the type of logging destination.
type DestinationType string
// Supported logging destinations.
const (
Stdout DestinationType = "stdout"
File DestinationType = "file"
)
// Destination defines a log output destination.
type Destination struct {
Type DestinationType `yaml:"type"` // Specifies destination type
Config map[string]string `yaml:"config"` // holds destination-specific configuration.
}
// Level represents logging levels.
type Level string
// Supported log levels.
const (
DebugLevel Level = "debug"
InfoLevel Level = "info"
WarnLevel Level = "warn"
ErrorLevel Level = "error"
FatalLevel Level = "fatal"
PanicLevel Level = "panic"
)
// Mapping of Level to zerolog.Level.
var logLevels = map[Level]zerolog.Level{
DebugLevel: zerolog.DebugLevel,
InfoLevel: zerolog.InfoLevel,
WarnLevel: zerolog.WarnLevel,
ErrorLevel: zerolog.ErrorLevel,
FatalLevel: zerolog.FatalLevel,
PanicLevel: zerolog.PanicLevel,
}
// Config represents the logging configuration.
type Config struct {
Level Level `yaml:"level"` //Logging Level
Destinations []Destination `yaml:"destinations"` // List of log destinations
ContextKeys []string `yaml:"contextKeys"` // List of context keys to extract
}
// Logger Instance
var (
logger zerolog.Logger
once sync.Once
cfg Config
)
// init initializes the logger with default configuration.
func init() {
logger, _ = getLogger(defaultConfig)
}
// InitLogger initializes the logger with the provided configuration.
//
// It ensures that the logger is initialized only once using sync.Once.
// Returns an error if the configuration is invalid.
func InitLogger(c Config) error {
var err error
once.Do(func() { // makes it singleton
err = c.validate()
if err != nil {
return
}
logger, err = getLogger(c)
if err != nil {
return
}
})
return err
}
// getLogger creates and configures a new logger based on the given configuration.
// Returns an initialized zerolog.Logger or an error if configuration is invalid.
func getLogger(config Config) (zerolog.Logger, error) {
var newLogger zerolog.Logger
// Multiwriter for multiple log destinations
var writers []io.Writer
for _, dest := range config.Destinations {
switch dest.Type {
case Stdout:
writers = append(writers, os.Stdout)
case File:
filePath := dest.Config["path"]
// File rotation
lumberjackLogger := &lumberjack.Logger{
Filename: filePath,
}
setConfigValue := func(key string, target *int) {
if valStr, ok := dest.Config[key]; ok {
if val, err := strconv.Atoi(valStr); err == nil {
*target = val
}
}
}
setConfigValue("maxSize", &lumberjackLogger.MaxSize)
setConfigValue("maxBackups", &lumberjackLogger.MaxBackups)
setConfigValue("maxAge", &lumberjackLogger.MaxAge)
if compress, ok := dest.Config["compress"]; ok {
lumberjackLogger.Compress = compress == "true"
}
writers = append(writers, lumberjackLogger)
}
}
multiwriter := io.MultiWriter(writers...)
newLogger = zerolog.New(multiwriter).
Level(logLevels[config.Level]).
With().
Timestamp().
Caller().
Logger()
// Replace the cfg with given config
cfg = config
return newLogger, nil
}
// validate checks if the provided logging configuration is valid.
// It ensures that a valid log level is provided and that at least one
// destination is specified. Returns an error if validation fails
func (config *Config) validate() error {
// Log Level is valid
if _, exists := logLevels[config.Level]; !exists {
return ErrInvalidLogLevel
}
// Log Destinations is not empty
if len(config.Destinations) == 0 {
return ErrLogDestinationNil
}
// File path exists in destination config for File type destination
for _, dest := range config.Destinations {
switch dest.Type {
case Stdout:
case File:
if _, exists := dest.Config["path"]; !exists {
return ErrMissingFilePath
}
// Validate lumberjack config if present
for _, key := range []string{"maxSize", "maxBackups", "maxAge"} {
if valStr, ok := dest.Config[key]; ok {
if _, err := strconv.Atoi(valStr); err != nil {
return fmt.Errorf("invalid %s: %w", key, err)
}
}
}
default:
return fmt.Errorf("Invalid destination type '%s'", dest.Type)
}
}
return nil
}
// Default Config
var defaultConfig = Config{
Level: InfoLevel,
Destinations: []Destination{
{Type: Stdout},
},
ContextKeys: []string{},
}
// Debug logs a debug-level message.
func Debug(ctx context.Context, msg string) {
logEvent(ctx, zerolog.DebugLevel, msg, nil)
}
// Debugf logs a formatted debug-level message.
func Debugf(ctx context.Context, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.DebugLevel, msg, nil)
}
// Info logs an info-level message.
func Info(ctx context.Context, msg string) {
logEvent(ctx, zerolog.InfoLevel, msg, nil)
}
// Infof logs a formatted info-level message.
func Infof(ctx context.Context, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.InfoLevel, msg, nil)
}
// Warn logs a warning-level message.
func Warn(ctx context.Context, msg string) {
logEvent(ctx, zerolog.WarnLevel, msg, nil)
}
// Warnf logs a formatted warning-level message.
func Warnf(ctx context.Context, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.WarnLevel, msg, nil)
}
// Error logs an error-level message.
func Error(ctx context.Context, err error, msg string) {
logEvent(ctx, zerolog.ErrorLevel, msg, err)
}
// Errorf logs a formatted error-level message.
func Errorf(ctx context.Context, err error, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.ErrorLevel, msg, err)
}
// Fatal logs a fatal-level message and exits the application.
func Fatal(ctx context.Context, err error, msg string) {
logEvent(ctx, zerolog.FatalLevel, msg, err)
}
// Fatalf logs a formatted fatal-level message and exits the application.
func Fatalf(ctx context.Context, err error, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.FatalLevel, msg, err)
}
// Panic logs a panic-level message.
func Panic(ctx context.Context, err error, msg string) {
logEvent(ctx, zerolog.PanicLevel, msg, err)
}
// Panicf logs a formatted panic-level message.
func Panicf(ctx context.Context, err error, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
logEvent(ctx, zerolog.PanicLevel, msg, err)
}
// Request logs an HTTP request.
func Request(ctx context.Context, r *http.Request, body []byte) {
event := logger.Info()
// Iterate through headers and log them
for name, values := range r.Header {
for _, value := range values {
event = event.Str(name, value)
}
}
addCtx(ctx, event)
event.Str("method", r.Method).
Str("url", r.URL.String()).
Str("body", string(body)).
Str("remoteAddr", r.RemoteAddr).
Msg("HTTP Request")
}
// Response logs an HTTP response.
func Response(ctx context.Context, r *http.Request, statusCode int, responseTime time.Duration) {
event := logger.Info()
addCtx(ctx, event)
event.Str("method", r.Method).
Str("url", r.URL.String()).
Int("statusCode", statusCode).
Dur("responseTime", responseTime).
Msg("HTTP Response")
}
// logEvent logs messages at the specified level with optional error details.
func logEvent(ctx context.Context, level zerolog.Level, msg string, err error) {
event := logger.WithLevel(level)
// Attach error if provided
if err != nil {
event = event.Err(err)
}
// Add context fields
addCtx(ctx, event)
event.Msg(msg)
}
// addCtx adds context-specific fields to log events.
func addCtx(ctx context.Context, event *zerolog.Event) {
for _, key := range cfg.ContextKeys {
if val, ok := ctx.Value(key).(string); ok {
event.Any(key, val)
}
}
}

128
pkg/model/error.go Normal file
View File

@@ -0,0 +1,128 @@
package model
import (
"fmt"
"net/http"
"strings"
)
// Error represents an error response.
type Error struct {
Code string `json:"code"`
Paths string `json:"paths,omitempty"`
Message string `json:"message"`
}
// Error implements the error interface for the Error struct.
func (e *Error) Error() string {
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
}
// SchemaValidationErr represents a collection of schema validation failures.
type SchemaValidationErr struct {
Errors []Error
}
// Error implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string {
var errorMessages []string
for _, err := range e.Errors {
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Paths, err.Message))
}
return strings.Join(errorMessages, "; ")
}
// BecknError converts SchemaValidationErr into a Beckn-compliant Error response.
func (e *SchemaValidationErr) BecknError() *Error {
if len(e.Errors) == 0 {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "Schema validation error.",
}
}
// Collect all error paths and messages
var paths []string
var messages []string
for _, err := range e.Errors {
if err.Paths != "" {
paths = append(paths, err.Paths)
}
messages = append(messages, err.Message)
}
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Paths: strings.Join(paths, ";"),
Message: strings.Join(messages, "; "),
}
}
// SignValidationErr represents an error that occurs during signature validation.
type SignValidationErr struct {
error
}
// NewSignValidationErrf creates a new SignValidationErr with a formatted message.
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
return &SignValidationErr{fmt.Errorf(format, a...)}
}
// NewSignValidationErr creates a new SignValidationErr from an existing error.
func NewSignValidationErr(e error) *SignValidationErr {
return &SignValidationErr{e}
}
// BecknError converts SignValidationErr into a Beckn-compliant Error response.
func (e *SignValidationErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusUnauthorized),
Message: "Signature Validation Error: " + e.Error(),
}
}
// BadReqErr represents an error related to a bad request.
type BadReqErr struct {
error
}
// NewBadReqErr creates a new BadReqErr from an existing error.
func NewBadReqErr(err error) *BadReqErr {
return &BadReqErr{err}
}
// NewBadReqErrf creates a new BadReqErr with a formatted message.
func NewBadReqErrf(format string, a ...any) *BadReqErr {
return &BadReqErr{fmt.Errorf(format, a...)}
}
// BecknError converts BadReqErr into a Beckn-compliant Error response.
func (e *BadReqErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "BAD Request: " + e.Error(),
}
}
// NotFoundErr represents an error for a missing resource or endpoint.
type NotFoundErr struct {
error
}
// NewNotFoundErr creates a new NotFoundErr from an existing error.
func NewNotFoundErr(err error) *NotFoundErr {
return &NotFoundErr{err}
}
// NewNotFoundErrf creates a new NotFoundErr with a formatted message.
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
return &NotFoundErr{fmt.Errorf(format, a...)}
}
// BecknError converts NotFoundErr into a Beckn-compliant Error response.
func (e *NotFoundErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusNotFound),
Message: "Endpoint not found: " + e.Error(),
}
}

132
pkg/model/model.go Normal file
View File

@@ -0,0 +1,132 @@
package model
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Subscriber represents a unique operational configuration of a trusted platform on a network.
type Subscriber struct {
SubscriberID string `json:"subscriber_id"`
URL string `json:"url" format:"uri"`
Type string `json:"type" enum:"BAP,BPP,BG"`
Domain string `json:"domain"`
}
// Subscription represents subscription details of a network participant.
type Subscription struct {
Subscriber `json:",inline"`
KeyID string `json:"key_id" format:"uuid"`
SigningPublicKey string `json:"signing_public_key"`
EncrPublicKey string `json:"encr_public_key"`
ValidFrom time.Time `json:"valid_from" format:"date-time"`
ValidUntil time.Time `json:"valid_until" format:"date-time"`
Status string `json:"status" enum:"INITIATED,UNDER_SUBSCRIPTION,SUBSCRIBED,EXPIRED,UNSUBSCRIBED,INVALID_SSL"`
Created time.Time `json:"created" format:"date-time"`
Updated time.Time `json:"updated" format:"date-time"`
Nonce string
}
// Authorization-related constants for headers.
const (
AuthHeaderSubscriber string = "Authorization"
AuthHeaderGateway string = "X-Gateway-Authorization"
UnaAuthorizedHeaderSubscriber string = "WWW-Authenticate"
UnaAuthorizedHeaderGateway string = "Proxy-Authenticate"
)
// MsgIDKey represents the key for the message ID.
const MsgIDKey = "message_id"
// Role defines different roles in the network.
type Role string
const (
// RoleBAP represents a Buyer App Participant.
RoleBAP Role = "bap"
// RoleBPP represents a Buyer Platform Participant.
RoleBPP Role = "bpp"
// RoleGateway represents a Network Gateway.
RoleGateway Role = "gateway"
// RoleRegistery represents a Registry Service.
RoleRegistery Role = "registery"
)
// validRoles ensures only allowed values are accepted
var validRoles = map[Role]bool{
RoleBAP: true,
RoleBPP: true,
RoleGateway: true,
RoleRegistery: true,
}
// UnmarshalYAML implements custom YAML unmarshalling for Role to ensure only valid values are accepted.
func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
var roleName string
if err := unmarshal(&roleName); err != nil {
return err
}
role := Role(roleName)
if !validRoles[role] {
return fmt.Errorf("invalid Role: %s", roleName)
}
*r = role
return nil
}
// Route represents a network route for message processing.
type Route struct {
Type string
URL *url.URL
Publisher string
}
// StepContext holds context information for a request processing step.
type StepContext struct {
context.Context
Request *http.Request
Body []byte
Route *Route
SubID string
Role Role
RespHeader http.Header
}
// WithContext updates the context in StepContext while keeping other fields unchanged.
func (ctx *StepContext) WithContext(newCtx context.Context) {
ctx.Context = newCtx // Update the existing context, keeping all other fields unchanged.
}
// Status represents the status of an acknowledgment.
type Status string
const (
// StatusACK indicates a successful acknowledgment.
StatusACK Status = "ACK"
// StatusNACK indicates a negative acknowledgment.
StatusNACK Status = "NACK"
)
// Ack represents an acknowledgment response.
type Ack struct {
Status Status `json:"status"` // ACK or NACK
}
// Message represents the message object in the response.
type Message struct {
Ack Ack `json:"ack"`
Error *Error `json:"error,omitempty"`
}
// Response represents the main response structure.
type Response struct {
Message Message `json:"message"`
}

21
pkg/plugin/config.go Normal file
View File

@@ -0,0 +1,21 @@
package plugin
type PublisherCfg struct {
ID string `yaml:"id"`
Config map[string]string `yaml:"config"`
}
type ValidatorCfg struct {
ID string `yaml:"id"`
Config map[string]string `yaml:"config"`
}
type Config struct {
ID string `yaml:"id"`
Config map[string]string `yaml:"config"`
}
type ManagerConfig struct {
Root string `yaml:"root"`
RemoteRoot string `yaml:"remoteRoot"`
}

View File

@@ -0,0 +1,27 @@
package definition
import (
"context"
"time"
)
// Cache defines the general cache interface for caching plugins.
type Cache interface {
// Get retrieves a value from the cache based on the given key.
Get(ctx context.Context, key string) (string, error)
// Set stores a value in the cache with the given key and TTL (time-to-live) in seconds.
Set(ctx context.Context, key, value string, ttl time.Duration) error
// Delete removes a value from the cache based on the given key.
Delete(ctx context.Context, key string) error
// Clear removes all values from the cache.
Clear(ctx context.Context) error
}
// CacheProvider interface defines the contract for managing cache instances.
type CacheProvider interface {
// New initializes a new cache instance with the given configuration.
New(ctx context.Context, config map[string]string) (Cache, func() error, error)
}

View File

@@ -0,0 +1,35 @@
package definition
import (
"context"
"github.com/beckn/beckn-onix/pkg/model"
)
type Keyset struct {
UniqueKeyID string
SigningPrivate string
SigningPublic string
EncrPrivate string
EncrPublic string
}
// KeyManager defines the interface for key management operations/methods.
type KeyManager interface {
GenerateKeyPairs() (*Keyset, error)
StorePrivateKeys(ctx context.Context, keyID string, keys *Keyset) error
SigningPrivateKey(ctx context.Context, keyID string) (string, string, error)
EncrPrivateKey(ctx context.Context, keyID string) (string, string, error)
SigningPublicKey(ctx context.Context, subscriberID, uniqueKeyID string) (string, error)
EncrPublicKey(ctx context.Context, subscriberID, uniqueKeyID string) (string, error)
DeletePrivateKeys(ctx context.Context, keyID string) error
}
// KeyManagerProvider initializes a new signer instance.
type KeyManagerProvider interface {
New(context.Context, Cache, RegistryLookup, map[string]string) (KeyManager, func() error, error)
}
type RegistryLookup interface {
Lookup(ctx context.Context, req *model.Subscription) ([]model.Subscription, error)
}

View File

@@ -0,0 +1,10 @@
package definition
import (
"context"
"net/http"
)
type MiddlewareProvider interface {
New(ctx context.Context, cfg map[string]string) (func(http.Handler) http.Handler, error)
}

View File

@@ -0,0 +1,16 @@
package definition
import (
"context"
"net/url"
"github.com/beckn/beckn-onix/pkg/model"
)
type Router interface {
Route(ctx context.Context, url *url.URL, body []byte) (*model.Route, error)
}
type RouterProvider interface {
New(ctx context.Context, cfg map[string]string) (Router, error)
}

View File

@@ -0,0 +1,16 @@
package definition
import (
"context"
"net/url"
)
// SchemaValidator interface for schema validation.
type SchemaValidator interface {
Validate(ctx context.Context, url *url.URL, reqBody []byte) error
}
// SchemaValidatorProvider interface for creating validators.
type SchemaValidatorProvider interface {
New(ctx context.Context, config map[string]string) (SchemaValidator, func() error, error)
}

View File

@@ -0,0 +1,15 @@
package definition
import (
"context"
"github.com/beckn/beckn-onix/pkg/model"
)
type Step interface {
Run(ctx *model.StepContext) error
}
type StepProvider interface {
New(context.Context, map[string]string) (Step, func(), error)
}

View File

@@ -1,171 +1,338 @@
package plugin
import (
"archive/zip"
"context"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"plugin"
"strings"
"time"
"github.com/beckn/beckn-onix/pkg/log"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
)
// Config represents the plugin manager configuration.
type Config struct {
Root string `yaml:"root"`
Signer PluginConfig `yaml:"signer"`
Verifier PluginConfig `yaml:"verifier"`
Decrypter PluginConfig `yaml:"decrypter"`
Encrypter PluginConfig `yaml:"encrypter"`
Publisher PluginConfig `yaml:"publisher"`
}
// PluginConfig represents configuration details for a plugin.
type PluginConfig struct {
ID string `yaml:"id"`
Config map[string]string `yaml:"config"`
}
// Manager handles dynamic plugin loading and management.
type Manager struct {
sp definition.SignerProvider
vp definition.VerifierProvider
dp definition.DecrypterProvider
ep definition.EncrypterProvider
pb definition.PublisherProvider
cfg *Config
plugins map[string]*plugin.Plugin
closers []func()
}
// NewManager initializes a new Manager with the given configuration file.
func NewManager(ctx context.Context, cfg *Config) (*Manager, error) {
if cfg == nil {
return nil, fmt.Errorf("configuration cannot be nil")
}
// Load signer plugin.
sp, err := provider[definition.SignerProvider](cfg.Root, cfg.Signer.ID)
if err != nil {
return nil, fmt.Errorf("failed to load signer plugin: %w", err)
}
// Load publisher plugin.
pb, err := provider[definition.PublisherProvider](cfg.Root, cfg.Publisher.ID)
if err != nil {
return nil, fmt.Errorf("failed to load publisher plugin: %w", err)
}
// Load verifier plugin.
vp, err := provider[definition.VerifierProvider](cfg.Root, cfg.Verifier.ID)
if err != nil {
return nil, fmt.Errorf("failed to load Verifier plugin: %w", err)
}
// Load decrypter plugin.
dp, err := provider[definition.DecrypterProvider](cfg.Root, cfg.Decrypter.ID)
if err != nil {
return nil, fmt.Errorf("failed to load Decrypter plugin: %w", err)
}
// Load encryption plugin.
ep, err := provider[definition.EncrypterProvider](cfg.Root, cfg.Encrypter.ID)
if err != nil {
return nil, fmt.Errorf("failed to load encryption plugin: %w", err)
}
return &Manager{sp: sp, vp: vp, pb: pb, ep: ep, dp: dp, cfg: cfg}, nil
func validateMgrCfg(cfg *ManagerConfig) error {
return nil
}
// provider loads a plugin dynamically and retrieves its provider instance.
func provider[T any](root, id string) (T, error) {
func NewManager(ctx context.Context, cfg *ManagerConfig) (*Manager, func(), error) {
if err := validateMgrCfg(cfg); err != nil {
return nil, nil, fmt.Errorf("Invalid config: %w", err)
}
log.Debugf(ctx, "RemoteRoot : %s", cfg.RemoteRoot)
if len(cfg.RemoteRoot) != 0 {
log.Debugf(ctx, "Unzipping files from : %s to : %s", cfg.RemoteRoot, cfg.Root)
if err := unzip(cfg.RemoteRoot, cfg.Root); err != nil {
return nil, nil, err
}
}
plugins, err := plugins(ctx, cfg)
if err != nil {
return nil, nil, err
}
closers := []func(){}
return &Manager{plugins: plugins, closers: closers}, func() {
for _, closer := range closers {
closer()
}
}, nil
}
func plugins(ctx context.Context, cfg *ManagerConfig) (map[string]*plugin.Plugin, error) {
plugins := make(map[string]*plugin.Plugin)
err := filepath.WalkDir(cfg.Root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // Skip directories
}
if strings.HasSuffix(d.Name(), ".so") {
id := strings.TrimSuffix(d.Name(), ".so") // Extract plugin ID
log.Debugf(ctx, "Loading plugin: %s", id)
start := time.Now()
p, err := plugin.Open(path) // Use the full path
if err != nil {
return fmt.Errorf("failed to open plugin %s: %w", id, err)
}
elapsed := time.Since(start)
plugins[id] = p
log.Debugf(ctx, "Loaded plugin: %s in %s", id, elapsed)
}
return nil
})
if err != nil {
return nil, err
}
return plugins, nil
}
func provider[T any](plugins map[string]*plugin.Plugin, id string) (T, error) {
var zero T
if len(strings.TrimSpace(id)) == 0 {
return zero, nil
pgn, ok := plugins[id]
if !ok {
return zero, fmt.Errorf("plugin %s not found", id)
}
p, err := plugin.Open(pluginPath(root, id))
provider, err := pgn.Lookup("Provider")
if err != nil {
return zero, fmt.Errorf("failed to open plugin %s: %w", id, err)
return zero, fmt.Errorf("failed to lookup Provider for %s: %w", id, err)
}
log.Debugf(context.Background(), "Provider type: %T\n", provider)
symbol, err := p.Lookup("Provider")
if err != nil {
return zero, fmt.Errorf("failed to find Provider symbol in plugin %s: %w", id, err)
}
prov, ok := symbol.(*T)
pp, ok := provider.(T)
if !ok {
return zero, fmt.Errorf("failed to cast Provider for %s", id)
}
return *prov, nil
log.Debugf(context.Background(), "Casting successful for: %s", provider)
return pp, nil
}
// pluginPath constructs the path to the plugin shared object file.
func pluginPath(root, id string) string {
return filepath.Join(root, id+".so")
}
// Signer retrieves the signing plugin instance.
func (m *Manager) Signer(ctx context.Context) (definition.Signer, func() error, error) {
if m.sp == nil {
return nil, nil, fmt.Errorf("signing plugin provider not loaded")
}
signer, close, err := m.sp.New(ctx, m.cfg.Signer.Config)
// GetPublisher returns a Publisher instance based on the provided configuration.
// It reuses the loaded provider.
func (m *Manager) Publisher(ctx context.Context, cfg *Config) (definition.Publisher, error) {
pp, err := provider[definition.PublisherProvider](m.plugins, cfg.ID)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize signer: %w", err)
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
return signer, close, nil
}
// Verifier retrieves the verification plugin instance.
func (m *Manager) Verifier(ctx context.Context) (definition.Verifier, func() error, error) {
if m.vp == nil {
return nil, nil, fmt.Errorf("Verifier plugin provider not loaded")
}
Verifier, close, err := m.vp.New(ctx, m.cfg.Verifier.Config)
p, err := pp.New(ctx, cfg.Config)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize Verifier: %w", err)
return nil, err
}
return Verifier, close, nil
return p, nil
}
// Decrypter retrieves the decryption plugin instance.
func (m *Manager) Decrypter(ctx context.Context) (definition.Decrypter, func() error, error) {
if m.dp == nil {
return nil, nil, fmt.Errorf("decrypter plugin provider not loaded")
func (m *Manager) addCloser(closer func()) {
if closer != nil {
m.closers = append(m.closers, closer)
}
}
decrypter, close, err := m.dp.New(ctx, m.cfg.Decrypter.Config)
func (m *Manager) SchemaValidator(ctx context.Context, cfg *Config) (definition.SchemaValidator, error) {
vp, err := provider[definition.SchemaValidatorProvider](m.plugins, cfg.ID)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize Decrypter: %w", err)
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
return decrypter, close, nil
}
// Encrypter retrieves the encryption plugin instance.
func (m *Manager) Encrypter(ctx context.Context) (definition.Encrypter, func() error, error) {
if m.ep == nil {
return nil, nil, fmt.Errorf("encryption plugin provider not loaded")
}
encrypter, close, err := m.ep.New(ctx, m.cfg.Encrypter.Config)
v, closer, err := vp.New(ctx, cfg.Config)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize encrypter: %w", err)
return nil, err
}
return encrypter, close, nil
if closer != nil {
m.addCloser(func() {
if err := closer(); err != nil {
panic(err)
}
})
}
return v, nil
}
// Publisher retrieves the publisher plugin instance.
func (m *Manager) Publisher(ctx context.Context) (definition.Publisher, error) {
if m.pb == nil {
return nil, fmt.Errorf("publisher plugin provider not loaded")
}
publisher, err := m.pb.New(ctx, m.cfg.Publisher.Config)
func (m *Manager) Router(ctx context.Context, cfg *Config) (definition.Router, error) {
rp, err := provider[definition.RouterProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to initialize publisher: %w", err)
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
return publisher, nil
return rp.New(ctx, cfg.Config)
}
func (m *Manager) Middleware(ctx context.Context, cfg *Config) (func(http.Handler) http.Handler, error) {
mwp, err := provider[definition.MiddlewareProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
return mwp.New(ctx, cfg.Config)
}
func (m *Manager) Step(ctx context.Context, cfg *Config) (definition.Step, error) {
sp, err := provider[definition.StepProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
step, closer, error := sp.New(ctx, cfg.Config)
if closer != nil {
m.closers = append(m.closers, closer)
}
return step, error
}
func (m *Manager) Cache(ctx context.Context, cfg *Config) (definition.Cache, error) {
cp, err := provider[definition.CacheProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
c, close, err := cp.New(ctx, cfg.Config)
if err != nil {
return nil, err
}
m.addCloser(func() {
if err := close(); err != nil {
panic(err)
}
})
return c, nil
}
func (m *Manager) Signer(ctx context.Context, cfg *Config) (definition.Signer, error) {
sp, err := provider[definition.SignerProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
s, closer, err := sp.New(ctx, cfg.Config)
if err != nil {
return nil, err
}
if closer != nil {
m.addCloser(func() {
if err := closer(); err != nil {
panic(err)
}
})
}
return s, nil
}
func (m *Manager) Encryptor(ctx context.Context, cfg *Config) (definition.Encrypter, error) {
ep, err := provider[definition.EncrypterProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
encrypter, closer, err := ep.New(ctx, cfg.Config)
if err != nil {
return nil, err
}
if closer != nil {
m.addCloser(func() {
if err := closer(); err != nil {
panic(err)
}
})
}
return encrypter, nil
}
func (m *Manager) Decryptor(ctx context.Context, cfg *Config) (definition.Decrypter, error) {
dp, err := provider[definition.DecrypterProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
decrypter, closer, err := dp.New(ctx, cfg.Config)
if err != nil {
return nil, err
}
if closer != nil {
m.addCloser(func() {
if err := closer(); err != nil {
panic(err)
}
})
}
return decrypter, nil
}
func (m *Manager) SignValidator(ctx context.Context, cfg *Config) (definition.Verifier, error) {
svp, err := provider[definition.VerifierProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
v, closer, err := svp.New(ctx, cfg.Config)
if err != nil {
return nil, err
}
if closer != nil {
m.addCloser(func() {
if err := closer(); err != nil {
panic(err)
}
})
}
return v, nil
}
// KeyManager returns a KeyManager instance based on the provided configuration.
// It reuses the loaded provider.
func (m *Manager) KeyManager(ctx context.Context, cache definition.Cache, rClient definition.RegistryLookup, cfg *Config) (definition.KeyManager, error) {
kmp, err := provider[definition.KeyManagerProvider](m.plugins, cfg.ID)
if err != nil {
return nil, fmt.Errorf("failed to load provider for %s: %w", cfg.ID, err)
}
km, close, err := kmp.New(ctx, cache, rClient, cfg.Config)
if err != nil {
return nil, err
}
m.addCloser(func() {
if err := close(); err != nil {
panic(err)
}
})
return km, nil
}
// Validator implements handler.PluginManager.
func (m *Manager) Validator(ctx context.Context, cfg *Config) (definition.SchemaValidator, error) {
panic("unimplemented")
}
// Unzip extracts a ZIP file to the specified destination
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
// Ensure the destination directory exists
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
for _, f := range r.File {
fpath := filepath.Join(dest, f.Name)
// Ensure directory exists
log.Debugf(context.Background(), "Pain : fpath: %s,filepath.Dir(fpath): %s", fpath, filepath.Dir(fpath))
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
// Open the file inside the zip
srcFile, err := f.Open()
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
dstFile, err := os.Create(fpath)
if err != nil {
return err
}
defer dstFile.Close()
// Copy file contents
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
}
return nil
}

View File

@@ -3,141 +3,121 @@ package response
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/beckn/beckn-onix/pkg/model"
)
// ErrorType represents different types of errors in the Beckn protocol.
type ErrorType string
const (
// SchemaValidationErrorType represents an error due to schema validation failure.
SchemaValidationErrorType ErrorType = "SCHEMA_VALIDATION_ERROR"
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
// InvalidRequestErrorType represents an error due to an invalid request.
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
)
// BecknRequest represents a generic Beckn request with an optional context.
type BecknRequest struct {
Context map[string]interface{} `json:"context,omitempty"`
}
type Error struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Paths string `json:"paths,omitempty"`
}
type Message struct {
Ack struct {
Status string `json:"status,omitempty"`
} `json:"ack,omitempty"`
Error *Error `json:"error,omitempty"`
}
type BecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Message Message `json:"message,omitempty"`
}
type ClientFailureBecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Error *Error `json:"error,omitempty"`
}
var errorMap = map[ErrorType]Error{
SchemaValidationErrorType: {
Code: "400",
Message: "Schema validation failed",
},
InvalidRequestErrorType: {
Code: "401",
Message: "Invalid request format",
},
}
var DefaultError = Error{
Code: "500",
Message: "Internal server error",
}
func Nack(ctx context.Context, tp ErrorType, paths string, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
errorObj, ok := errorMap[tp]
if paths != "" {
errorObj.Paths = paths
}
var response BecknResponse
if !ok {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &DefaultError,
},
}
} else {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &errorObj,
},
}
}
return json.Marshal(response)
}
func Ack(ctx context.Context, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
response := BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "ACK",
// SendAck sends an acknowledgment (ACK) response indicating a successful request processing.
func SendAck(w http.ResponseWriter) {
// Create the response object
resp := &model.Response{
Message: model.Message{
Ack: model.Ack{
Status: model.StatusACK,
},
},
}
return json.Marshal(response)
}
func HandleClientFailure(ctx context.Context, tp ErrorType, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
// Marshal to JSON
data, err := json.Marshal(resp)
if err != nil {
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
return
}
errorObj, ok := errorMap[tp]
var response ClientFailureBecknResponse
// Set headers and write response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
if !ok {
response = ClientFailureBecknResponse{
Context: req.Context,
Error: &DefaultError,
}
} else {
response = ClientFailureBecknResponse{
Context: req.Context,
Error: &errorObj,
}
// nack sends a negative acknowledgment (NACK) response with an error message.
func nack(w http.ResponseWriter, err *model.Error, status int) {
// Create the NACK response object
resp := &model.Response{
Message: model.Message{
Ack: model.Ack{
Status: model.StatusNACK,
},
Error: err,
},
}
return json.Marshal(response)
// Marshal the response to JSON
data, jsonErr := json.Marshal(resp)
if jsonErr != nil {
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
return
}
// Set headers and write response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status) // Assuming NACK means a bad request
w.Write(data)
}
func internalServerError(ctx context.Context) *model.Error {
return &model.Error{
Message: fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)),
}
}
// SendNack sends a negative acknowledgment (NACK) response with an error message.
func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
var schemaErr *model.SchemaValidationErr
var signErr *model.SignValidationErr
var badReqErr *model.BadReqErr
var notFoundErr *model.NotFoundErr
switch {
case errors.As(err, &schemaErr): // Custom application error
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &signErr):
nack(w, signErr.BecknError(), http.StatusUnauthorized)
return
case errors.As(err, &badReqErr):
nack(w, badReqErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &notFoundErr):
nack(w, notFoundErr.BecknError(), http.StatusNotFound)
return
default:
nack(w, internalServerError(ctx), http.StatusInternalServerError)
return
}
}
// BecknError generates a standardized Beckn error response.
func BecknError(ctx context.Context, err error, status int) *model.Error {
msg := err.Error()
msgID := ctx.Value(model.MsgIDKey)
if status == http.StatusInternalServerError {
msg = "Internal server error"
}
return &model.Error{
Message: fmt.Sprintf("%s. MessageID: %s.", msg, msgID),
Code: strconv.Itoa(status),
}
}

View File

@@ -1,303 +0,0 @@
package response
import (
"context"
"encoding/json"
"reflect"
"testing"
)
func TestNack(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
errorType ErrorType
requestBody string
wantStatus string
wantErrCode string
wantErrMsg string
wantErr bool
path string
}{
{
name: "Schema validation error",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
{
name: "Invalid request error",
errorType: InvalidRequestErrorType,
requestBody: `{"context": {"domain": "test-domain"}}`,
wantStatus: "NACK",
wantErrCode: "401",
wantErrMsg: "Invalid request format",
wantErr: false,
path: "test",
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantStatus: "NACK",
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
path: "test",
},
{
name: "Empty request body",
errorType: SchemaValidationErrorType,
requestBody: `{}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
path: "test",
},
{
name: "Complex nested context",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123}}}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := Nack(ctx, tt.errorType, tt.path, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var becknResp BecknResponse
if err := json.Unmarshal(resp, &becknResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus {
t.Errorf("Nack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus)
}
if becknResp.Message.Error.Code != tt.wantErrCode {
t.Errorf("Nack() error code = %v, want %v", becknResp.Message.Error.Code, tt.wantErrCode)
}
if becknResp.Message.Error.Message != tt.wantErrMsg {
t.Errorf("Nack() error message = %v, want %v", becknResp.Message.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Nack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
}
})
}
}
func TestAck(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
requestBody string
wantStatus string
wantErr bool
}{
{
name: "Valid request",
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantStatus: "ACK",
wantErr: false,
},
{
name: "Empty context",
requestBody: `{"context": {}}`,
wantStatus: "ACK",
wantErr: false,
},
{
name: "Invalid JSON",
requestBody: `{invalid json}`,
wantErr: true,
},
{
name: "Complex nested context",
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123, "array": [1,2,3]}}}`,
wantStatus: "ACK",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := Ack(ctx, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("Ack() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var becknResp BecknResponse
if err := json.Unmarshal(resp, &becknResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus {
t.Errorf("Ack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus)
}
if becknResp.Message.Error != nil {
t.Errorf("Ack() should not have error, got %v", becknResp.Message.Error)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Ack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
}
})
}
}
func TestHandleClientFailure(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
errorType ErrorType
requestBody string
wantErrCode string
wantErrMsg string
wantErr bool
}{
{
name: "Schema validation error",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
},
{
name: "Invalid request error",
errorType: InvalidRequestErrorType,
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "401",
wantErrMsg: "Invalid request format",
wantErr: false,
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := HandleClientFailure(ctx, tt.errorType, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("HandleClientFailure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var failureResp ClientFailureBecknResponse
if err := json.Unmarshal(resp, &failureResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if failureResp.Error.Code != tt.wantErrCode {
t.Errorf("HandleClientFailure() error code = %v, want %v", failureResp.Error.Code, tt.wantErrCode)
}
if failureResp.Error.Message != tt.wantErrMsg {
t.Errorf("HandleClientFailure() error message = %v, want %v", failureResp.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(failureResp.Context, origReq.Context) {
t.Errorf("HandleClientFailure() context not preserved, got = %v, want %v", failureResp.Context, origReq.Context)
}
}
})
}
}
func TestErrorMap(t *testing.T) {
expectedTypes := []ErrorType{
SchemaValidationErrorType,
InvalidRequestErrorType,
}
for _, tp := range expectedTypes {
if _, exists := errorMap[tp]; !exists {
t.Errorf("ErrorType %v not found in errorMap", tp)
}
}
if DefaultError.Code != "500" || DefaultError.Message != "Internal server error" {
t.Errorf("DefaultError not set correctly, got code=%v, message=%v", DefaultError.Code, DefaultError.Message)
}
}
func compareContexts(c1, c2 map[string]interface{}) bool {
if c1 == nil && c2 == nil {
return true
}
if c1 == nil && len(c2) == 0 || c2 == nil && len(c1) == 0 {
return true
}
return reflect.DeepEqual(c1, c2)
}