Skip to content

Commit

Permalink
refactor(log): replace panics with log fatal or log panic functions
Browse files Browse the repository at this point in the history
Signed-off-by: Laurentiu Niculae <[email protected]>
  • Loading branch information
laurentiuNiculae committed Nov 22, 2023
1 parent 83f287d commit 2869ccc
Show file tree
Hide file tree
Showing 22 changed files with 969 additions and 302 deletions.
2 changes: 2 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ var (
ErrFileAlreadyClosed = errors.New("storageDriver: file already closed")
ErrFileAlreadyCommitted = errors.New("storageDriver: file already committed")
ErrInvalidOutputFormat = errors.New("cli: invalid output format")
ErrServerIsRunning = errors.New("server is running")
ErrDatabaseFileAlreadyInUse = errors.New("boltdb file is already in use")
ErrFlagValueUnsupported = errors.New("supported values ")
ErrUnknownSubcommand = errors.New("cli: unknown subcommand")
ErrMultipleReposSameName = errors.New("test: can't have multiple repos with the same name")
Expand Down
37 changes: 22 additions & 15 deletions pkg/api/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ const (
type AuthnMiddleware struct {
credMap map[string]string
ldapClient *LDAPClient
log log.Logger
}

func AuthHandler(ctlr *Controller) mux.MiddlewareFunc {
authnMiddleware := &AuthnMiddleware{}
authnMiddleware := &AuthnMiddleware{log: ctlr.Log}

if ctlr.Config.IsBearerAuthEnabled() {
return bearerAuthHandler(ctlr)
Expand Down Expand Up @@ -279,21 +280,24 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
if ctlr.Config.HTTP.Auth.LDAP.CACert != "" {
caCert, err := os.ReadFile(ctlr.Config.HTTP.Auth.LDAP.CACert)
if err != nil {
panic(err)
amw.log.Panic().Err(err).Str("caCert", ctlr.Config.HTTP.Auth.LDAP.CACert).
Msg("failed to read caCert")
}

caCertPool := x509.NewCertPool()

if !caCertPool.AppendCertsFromPEM(caCert) {
panic(zerr.ErrBadCACert)
amw.log.Panic().Err(zerr.ErrBadCACert).Str("caCert", ctlr.Config.HTTP.Auth.LDAP.CACert).
Msg("failed to read caCert")
}

amw.ldapClient.ClientCAs = caCertPool
} else {
// default to system cert pool
caCertPool, err := x509.SystemCertPool()
if err != nil {
panic(zerr.ErrBadCACert)
amw.log.Panic().Err(zerr.ErrBadCACert).Str("caCert", ctlr.Config.HTTP.Auth.LDAP.CACert).
Msg("failed to get system cert pool")

Check warning on line 300 in pkg/api/authn.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/authn.go#L299-L300

Added lines #L299 - L300 were not covered by tests
}

amw.ldapClient.ClientCAs = caCertPool
Expand All @@ -303,7 +307,8 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
if ctlr.Config.IsHtpasswdAuthEnabled() {
credsFile, err := os.Open(ctlr.Config.HTTP.Auth.HTPasswd.Path)
if err != nil {
panic(err)
amw.log.Panic().Err(err).Str("credsFile", ctlr.Config.HTTP.Auth.HTPasswd.Path).
Msg("failed to open creds-file")
}
defer credsFile.Close()

Expand All @@ -324,10 +329,10 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun

for provider := range ctlr.Config.HTTP.Auth.OpenID.Providers {
if config.IsOpenIDSupported(provider) {
rp := NewRelyingPartyOIDC(ctlr.Config, provider)
rp := NewRelyingPartyOIDC(ctlr.Config, provider, ctlr.Log)
ctlr.RelyingParties[provider] = rp
} else if config.IsOauth2Supported(provider) {
rp := NewRelyingPartyGithub(ctlr.Config, provider)
rp := NewRelyingPartyGithub(ctlr.Config, provider, ctlr.Log)
ctlr.RelyingParties[provider] = rp
}
}
Expand Down Expand Up @@ -557,19 +562,20 @@ func (rh *RouteHandler) AuthURLHandler() http.HandlerFunc {
}
}

func NewRelyingPartyOIDC(config *config.Config, provider string) rp.RelyingParty {
issuer, clientID, clientSecret, redirectURI, scopes, options := getRelyingPartyArgs(config, provider)
func NewRelyingPartyOIDC(config *config.Config, provider string, log log.Logger) rp.RelyingParty {
issuer, clientID, clientSecret, redirectURI, scopes, options := getRelyingPartyArgs(config, provider, log)

relyingParty, err := rp.NewRelyingPartyOIDC(issuer, clientID, clientSecret, redirectURI, scopes, options...)
if err != nil {
panic(err)
log.Panic().Err(err).Str("issuer", issuer).Str("redirectURI", redirectURI).Strs("scopes", scopes).
Msg("failed to get new relying party oicd")
}

return relyingParty
}

func NewRelyingPartyGithub(config *config.Config, provider string) rp.RelyingParty {
_, clientID, clientSecret, redirectURI, scopes, options := getRelyingPartyArgs(config, provider)
func NewRelyingPartyGithub(config *config.Config, provider string, log log.Logger) rp.RelyingParty {
_, clientID, clientSecret, redirectURI, scopes, options := getRelyingPartyArgs(config, provider, log)

rpConfig := &oauth2.Config{
ClientID: clientID,
Expand All @@ -581,17 +587,18 @@ func NewRelyingPartyGithub(config *config.Config, provider string) rp.RelyingPar

relyingParty, err := rp.NewRelyingPartyOAuth(rpConfig, options...)
if err != nil {
panic(err)
log.Panic().Err(err).Str("redirectURI", redirectURI).Strs("scopes", scopes).
Msg("failed to get new relying party oauth")

Check warning on line 591 in pkg/api/authn.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/authn.go#L590-L591

Added lines #L590 - L591 were not covered by tests
}

return relyingParty
}

func getRelyingPartyArgs(cfg *config.Config, provider string) (
func getRelyingPartyArgs(cfg *config.Config, provider string, log log.Logger) (
string, string, string, string, []string, []rp.Option,
) {
if _, ok := cfg.HTTP.Auth.OpenID.Providers[provider]; !ok {
panic(zerr.ErrOpenIDProviderDoesNotExist)
log.Panic().Err(zerr.ErrOpenIDProviderDoesNotExist).Str("provider", provider).Msg("")
}

clientID := cfg.HTTP.Auth.OpenID.Providers[provider].ClientID
Expand Down
8 changes: 6 additions & 2 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ func (c *Controller) Run(reloadCtx context.Context) error {

caCert, err := os.ReadFile(c.Config.HTTP.TLS.CACert)
if err != nil {
panic(err)
c.Log.Error().Err(err).Str("caCert", c.Config.HTTP.TLS.CACert).Msg("failed to read file")

return err
}

caCertPool := x509.NewCertPool()

if !caCertPool.AppendCertsFromPEM(caCert) {
panic(errors.ErrBadCACert)
c.Log.Error().Err(errors.ErrBadCACert).Msg("failed to append certs from pem")

return errors.ErrBadCACert
}

server.TLSConfig.ClientAuth = clientAuth
Expand Down
Loading

0 comments on commit 2869ccc

Please sign in to comment.