Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(log): replace panics with log fatal or log panic functions #1723

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
var internalErr *Error
details := make(map[string]string)

if errors.As(err, &internalErr) {

Check failure on line 41 in errors/errors.go

View workflow job for this annotation

GitHub Actions / coverage

condition "errors.As(err, &internalErr)" was never evaluated

Check failure on line 41 in errors/errors.go

View workflow job for this annotation

GitHub Actions / coverage

condition "errors.As(err, &internalErr)" was never evaluated
details = internalErr.GetDetails()
}

Expand Down Expand Up @@ -164,6 +164,8 @@
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 @@
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 @@
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).
laurentiuNiculae marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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).
laurentiuNiculae marked this conversation as resolved.
Show resolved Hide resolved
Msg("failed to open creds-file")
}
defer credsFile.Close()

Expand All @@ -324,10 +329,10 @@

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 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 @@

relyingParty, err := rp.NewRelyingPartyOAuth(rpConfig, options...)
andaaron marked this conversation as resolved.
Show resolved Hide resolved
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
Loading