Skip to content

Commit

Permalink
Merge pull request project-kessel#218 from tylercreller/RHCLOUD-35835…
Browse files Browse the repository at this point in the history
…-error-wrapping

RHCLOUD-35835: Adjust errors for wrapping
  • Loading branch information
tylercreller authored Nov 5, 2024
2 parents 27391d4 + 877afc6 commit a85a24d
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion internal/authn/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/authn/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 == "" {
Expand Down
7 changes: 4 additions & 3 deletions internal/authn/psk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package psk

import (
"fmt"
"gopkg.in/yaml.v3"
"io"
"os"

"gopkg.in/yaml.v3"
)

type Config struct {
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions internal/authz/kessel/kessel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions internal/authz/kessel/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,22 @@ 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")
req.Header.Set("Accept", "application/json")

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 {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/data/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/server/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a85a24d

Please sign in to comment.