Skip to content

Commit

Permalink
Merge pull request #92 from bodgit/issue91
Browse files Browse the repository at this point in the history
Error if no `krb5.conf` is found.
  • Loading branch information
bodgit authored Feb 13, 2023
2 parents 10b91d4 + 45db94c commit e486e05
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions gss/gokrb5.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gss
import (
"encoding/hex"
"errors"
"fmt"
"math"
"net"
"os"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/bodgit/tsig"
"github.com/bodgit/tsig/internal/util"
"github.com/go-logr/logr"
multierror "github.com/hashicorp/go-multierror"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
Expand Down Expand Up @@ -345,25 +347,44 @@ func loadCache() (*credentials.CCache, error) {
return cache, nil
}

func (c *Client) loadConfig() (*config.Config, error) {
if c.config != "" {
return config.NewFromString(c.config)
func findFile(env string, try []string) (string, error) {
path, ok := os.LookupEnv(env)
if ok {
if _, err := os.Stat(path); err != nil {
return "", fmt.Errorf("%s: %w", env, err)
}

return path, nil
}

path := os.Getenv("KRB5_CONFIG")
_, err := os.Stat(path)
if err != nil {
errs := fmt.Errorf("%s: not found", env)

// List of candidates to try
try := []string{"/etc/krb5.conf"}
for _, t := range try {
_, err := os.Stat(t)
if err != nil {
errs = multierror.Append(errs, err)

for _, t := range try {
_, err := os.Stat(t)
if err == nil {
path = t
break
if os.IsNotExist(err) {
continue
}

return "", errs
}

return t, nil
}

return "", errs
}

func (c *Client) loadConfig() (*config.Config, error) {
if c.config != "" {
return config.NewFromString(c.config)
}

path, err := findFile("KRB5_CONFIG", []string{"/etc/krb5.conf"})
if err != nil {
return nil, err
}

return config.Load(path)
Expand Down

0 comments on commit e486e05

Please sign in to comment.