From 877afc6395b8d83167728371473ea2c2590a9eb5 Mon Sep 17 00:00:00 2001 From: Tyler Creller Date: Mon, 4 Nov 2024 10:18:10 -0500 Subject: [PATCH] RHCLOUD-35835: Adjust errors for wrapping --- internal/authn/authn.go | 3 ++- internal/authn/oidc/oidc.go | 3 ++- internal/authn/psk/config.go | 7 ++++--- internal/authz/kessel/kessel.go | 6 +++--- internal/authz/kessel/token.go | 8 ++++---- internal/data/migrate.go | 6 +++--- internal/server/grpc/config.go | 4 ++-- internal/server/http/config.go | 4 ++-- internal/server/otel.go | 2 +- internal/server/server.go | 8 ++++---- 10 files changed, 27 insertions(+), 24 deletions(-) diff --git a/internal/authn/authn.go b/internal/authn/authn.go index 5138edc3..6fa98cb1 100644 --- a/internal/authn/authn.go +++ b/internal/authn/authn.go @@ -2,6 +2,7 @@ package authn import ( "fmt" + "github.com/go-kratos/kratos/v2/log" "github.com/project-kessel/inventory-api/internal/authn/api" "github.com/project-kessel/inventory-api/internal/authn/clientcert" @@ -32,7 +33,7 @@ func New(config CompletedConfig, logger *log.Helper) (api.Authenticator, error) if a, err := oidc.New(*config.Oidc); err == nil { d.Add(a) } else { - return nil, fmt.Errorf("failed to load OIDC info: %v", err) + return nil, fmt.Errorf("failed to load OIDC info: %w", err) } } diff --git a/internal/authn/oidc/oidc.go b/internal/authn/oidc/oidc.go index c99639ff..99c92ff4 100644 --- a/internal/authn/oidc/oidc.go +++ b/internal/authn/oidc/oidc.go @@ -4,6 +4,7 @@ package oidc import ( "context" "fmt" + coreosoidc "github.com/coreos/go-oidc/v3/oidc" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/transport" @@ -25,7 +26,7 @@ func New(c CompletedConfig) (*OAuth2Authenticator, error) { ctx := coreosoidc.ClientContext(context.Background(), c.Client) provider, err := coreosoidc.NewProvider(ctx, c.AuthorizationServerURL) if err != nil { - return nil, fmt.Errorf("failed to create provider: %v", err) + return nil, fmt.Errorf("failed to create provider: %w", err) } if c.PrincipalUserDomain == "" { diff --git a/internal/authn/psk/config.go b/internal/authn/psk/config.go index 596a0ba6..10f00ea1 100644 --- a/internal/authn/psk/config.go +++ b/internal/authn/psk/config.go @@ -2,9 +2,10 @@ package psk import ( "fmt" - "gopkg.in/yaml.v3" "io" "os" + + "gopkg.in/yaml.v3" ) type Config struct { @@ -48,10 +49,10 @@ func (c *Config) loadPreSharedKeys() error { data, err := io.ReadAll(file) if err == nil { if err := yaml.Unmarshal(data, &c.Keys); err != nil { - return fmt.Errorf("failed to unmarshall preshared key: %v", err) + return fmt.Errorf("failed to unmarshal preshared key: %w", err) } } else { - return fmt.Errorf("failed to read preshared key file: %v", err) + return fmt.Errorf("failed to read preshared key file: %w", err) } } else { return fmt.Errorf("Error opening preshared key file: %s [%s]", c.PreSharedKeyFile, err.Error()) diff --git a/internal/authz/kessel/kessel.go b/internal/authz/kessel/kessel.go index 3d90ae43..93e9e21e 100644 --- a/internal/authz/kessel/kessel.go +++ b/internal/authz/kessel/kessel.go @@ -36,12 +36,12 @@ func New(ctx context.Context, config CompletedConfig, logger *log.Helper) (*Kess successCounter, err := meter.Int64Counter("inventory_relations_api_success") if err != nil { - return nil, fmt.Errorf("failed to create success counter: %v", err) + return nil, fmt.Errorf("failed to create success counter: %w", err) } failureCounter, err := meter.Int64Counter("inventory_relations_api_failure") if err != nil { - return nil, fmt.Errorf("failed to create failure counter: %v", err) + return nil, fmt.Errorf("failed to create failure counter: %w", err) } return &KesselAuthz{ @@ -105,7 +105,7 @@ func (a *KesselAuthz) getCallOptions() ([]grpc.CallOption, error) { if a.tokenClient.EnableOIDCAuth { token, err := a.tokenClient.getToken() if err != nil { - return nil, fmt.Errorf("failed to request token: %v", err) + return nil, fmt.Errorf("failed to request token: %w", err) } if a.tokenClient.Insecure { opts = append(opts, WithInsecureBearerToken(token.AccessToken)) diff --git a/internal/authz/kessel/token.go b/internal/authz/kessel/token.go index fdb6f693..6f4f1168 100644 --- a/internal/authz/kessel/token.go +++ b/internal/authz/kessel/token.go @@ -112,7 +112,7 @@ func (a *tokenClient) getToken() (*TokenResponse, error) { data.Set("grant_type", client_credentials_granttype) req, err := http.NewRequest("POST", a.URL, bytes.NewBufferString(data.Encode())) if err != nil { - return nil, fmt.Errorf("failed to create token request: %v", err) + return nil, fmt.Errorf("failed to create token request: %w", err) } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") @@ -120,14 +120,14 @@ func (a *tokenClient) getToken() (*TokenResponse, error) { resp, err := client.Do(req) if err != nil { - return nil, fmt.Errorf("token request failed: %v", err) + return nil, fmt.Errorf("token request failed: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("failed to parse token response: %v", err) + return nil, fmt.Errorf("failed to parse token response: %w", err) } if resp.StatusCode != http.StatusOK { @@ -136,7 +136,7 @@ func (a *tokenClient) getToken() (*TokenResponse, error) { var tokenResponse TokenResponse if err := json.Unmarshal(body, &tokenResponse); err != nil { - return nil, fmt.Errorf("failed to unmarshal token response: %v", err) + return nil, fmt.Errorf("failed to unmarshal token response: %w", err) } a.cache.Set(cachedTokenKey, tokenResponse.AccessToken, cacheCleanupInterval) return &tokenResponse, nil diff --git a/internal/data/migrate.go b/internal/data/migrate.go index 18dc6cd8..fa1fcb7b 100644 --- a/internal/data/migrate.go +++ b/internal/data/migrate.go @@ -21,7 +21,7 @@ func Migrate(db *gorm.DB, logger *log.Helper) error { } if err := db.AutoMigrate(models...); err != nil { - return fmt.Errorf("auto migration has failed: %v", err) + return fmt.Errorf("auto migration has failed: %w", err) } if db.Dialector.Name() == "sqlite" { @@ -37,12 +37,12 @@ func Migrate(db *gorm.DB, logger *log.Helper) error { statement := &gorm.Statement{DB: db} err := statement.Parse(m) if err != nil { - return fmt.Errorf("statement parsing has failed: %v", err) + return fmt.Errorf("statement parsing has failed: %w", err) } err = gormDbIndexStatement.GormDbAfterMigration(db, statement.Schema) if err != nil { - return fmt.Errorf("migration failure: %v", err) + return fmt.Errorf("migration failure: %w", err) } } } diff --git a/internal/server/grpc/config.go b/internal/server/grpc/config.go index b626f557..d90c9c76 100644 --- a/internal/server/grpc/config.go +++ b/internal/server/grpc/config.go @@ -46,7 +46,7 @@ func (c *Config) getTSLConfig() (*tls.Config, error) { var err error config.Certificates = make([]tls.Certificate, 1) if config.Certificates[0], err = tls.LoadX509KeyPair(c.Options.ServingCertFile, c.Options.PrivateKeyFile); err != nil { - return nil, fmt.Errorf("failed to load X509 key pair: %v", err) + return nil, fmt.Errorf("failed to load X509 key pair: %w", err) } if c.Options.CertOpt > int(tls.NoClientCert) && c.Options.ClientCAFile != "" { @@ -56,7 +56,7 @@ func (c *Config) getTSLConfig() (*tls.Config, error) { caCertPool = x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) } else { - return nil, fmt.Errorf("failed to load CA certificate: %v", err) + return nil, fmt.Errorf("failed to load CA certificate: %w", err) } } diff --git a/internal/server/http/config.go b/internal/server/http/config.go index 45e53c3a..5a4241e5 100644 --- a/internal/server/http/config.go +++ b/internal/server/http/config.go @@ -46,7 +46,7 @@ func (c *Config) getTSLConfig() (*tls.Config, error) { var err error config.Certificates = make([]tls.Certificate, 1) if config.Certificates[0], err = tls.LoadX509KeyPair(c.Options.ServingCertFile, c.Options.PrivateKeyFile); err != nil { - return nil, fmt.Errorf("failed to load X509 key pair: %v", err) + return nil, fmt.Errorf("failed to load X509 key pair: %w", err) } if c.Options.CertOpt > int(tls.NoClientCert) && c.Options.ClientCAFile != "" { @@ -56,7 +56,7 @@ func (c *Config) getTSLConfig() (*tls.Config, error) { caCertPool = x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) } else { - return nil, fmt.Errorf("failed to load CA certificate: %v", err) + return nil, fmt.Errorf("failed to load CA certificate: %w", err) } } diff --git a/internal/server/otel.go b/internal/server/otel.go index f23a2210..795f2d9c 100644 --- a/internal/server/otel.go +++ b/internal/server/otel.go @@ -21,7 +21,7 @@ func NewMeter(provider metric.MeterProvider) (metric.Meter, error) { func NewMeterProvider(s *Server) (metric.MeterProvider, error) { exporter, err := prometheus.New() if err != nil { - return nil, fmt.Errorf("failed to setup exporter for meter provider: %v", err) + return nil, fmt.Errorf("failed to setup exporter for meter provider: %w", err) } provider := sdkmetric.NewMeterProvider( diff --git a/internal/server/server.go b/internal/server/server.go index 0b035000..9c0c94a3 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -34,22 +34,22 @@ func New(c CompletedConfig, authn middleware.Middleware, logger log.Logger) (*Se meterProvider, err := NewMeterProvider(s) if err != nil { - return nil, fmt.Errorf("init meter provider failed: %v", err) + return nil, fmt.Errorf("init meter provider failed: %w", err) } meter, err := NewMeter(meterProvider) if err != nil { - return nil, fmt.Errorf("init meter failed: %v", err) + return nil, fmt.Errorf("init meter failed: %w", err) } httpServer, err := http.New(c.HttpConfig, authn, meter, logger) if err != nil { - return nil, fmt.Errorf("init http server failed: %v", err) + return nil, fmt.Errorf("init http server failed: %w", err) } grpcServer, err := grpc.New(c.GrpcConfig, authn, meter, logger) if err != nil { - return nil, fmt.Errorf("init grpc server failed: %v", err) + return nil, fmt.Errorf("init grpc server failed: %w", err) } s.HttpServer = httpServer