@@ -6,6 +6,7 @@ package gss
66import (
77 "encoding/hex"
88 "errors"
9+ "fmt"
910 "math"
1011 "net"
1112 "os"
@@ -17,6 +18,7 @@ import (
1718 "github.com/bodgit/tsig"
1819 "github.com/bodgit/tsig/internal/util"
1920 "github.com/go-logr/logr"
21+ multierror "github.com/hashicorp/go-multierror"
2022 "github.com/jcmturner/gokrb5/v8/client"
2123 "github.com/jcmturner/gokrb5/v8/config"
2224 "github.com/jcmturner/gokrb5/v8/credentials"
@@ -345,25 +347,44 @@ func loadCache() (*credentials.CCache, error) {
345347 return cache , nil
346348}
347349
348- func (c * Client ) loadConfig () (* config.Config , error ) {
349- if c .config != "" {
350- return config .NewFromString (c .config )
350+ func findFile (env string , try []string ) (string , error ) {
351+ path , ok := os .LookupEnv (env )
352+ if ok {
353+ if _ , err := os .Stat (path ); err != nil {
354+ return "" , fmt .Errorf ("%s: %w" , env , err )
355+ }
356+
357+ return path , nil
351358 }
352359
353- path := os .Getenv ("KRB5_CONFIG" )
354- _ , err := os .Stat (path )
355- if err != nil {
360+ errs := fmt .Errorf ("%s: not found" , env )
356361
357- // List of candidates to try
358- try := []string {"/etc/krb5.conf" }
362+ for _ , t := range try {
363+ _ , err := os .Stat (t )
364+ if err != nil {
365+ errs = multierror .Append (errs , err )
359366
360- for _ , t := range try {
361- _ , err := os .Stat (t )
362- if err == nil {
363- path = t
364- break
367+ if os .IsNotExist (err ) {
368+ continue
365369 }
370+
371+ return "" , errs
366372 }
373+
374+ return t , nil
375+ }
376+
377+ return "" , errs
378+ }
379+
380+ func (c * Client ) loadConfig () (* config.Config , error ) {
381+ if c .config != "" {
382+ return config .NewFromString (c .config )
383+ }
384+
385+ path , err := findFile ("KRB5_CONFIG" , []string {"/etc/krb5.conf" })
386+ if err != nil {
387+ return nil , err
367388 }
368389
369390 return config .Load (path )
0 commit comments