|
43 | 43 | options.Kerb5ConfigFile ??= Path.Combine(userKerbDir, "krb5.conf"); |
44 | 44 | options.KeytabFile ??= Path.Combine(userKerbDir, "krb5.keytab"); |
45 | 45 | options.CacheFile ??= Path.Combine(userKerbDir, "krb5cc"); |
| 46 | + options.GenerateKrb5 = options.Kerb5ConfigFile != null! ? !File.Exists(options.Kerb5ConfigFile) : true; |
| 47 | + |
46 | 48 | Directory.CreateDirectory(Path.GetDirectoryName(options.Kerb5ConfigFile)!); |
47 | 49 | Directory.CreateDirectory(Path.GetDirectoryName(options.KeytabFile)!); |
48 | 50 | Directory.CreateDirectory(Path.GetDirectoryName(options.CacheFile)!); |
49 | 51 |
|
50 | 52 | // var config = File.Exists(options.Kerb5ConfigFile) ? Krb5Config.Parse(File.ReadAllText(options.Kerb5ConfigFile)) : Krb5Config.Default(); |
51 | | - var config = Krb5Config.Default(); |
52 | | - config.Defaults.DefaultCCacheName = options.CacheFile; |
53 | | - string realm; |
54 | | - try |
| 53 | + Krb5Config config; |
| 54 | + if (options.GenerateKrb5) |
55 | 55 | { |
56 | | - realm = new KerberosPasswordCredential(options.ServiceAccount, options.Password).Domain; |
57 | | - } |
58 | | - catch (Exception) |
59 | | - { |
60 | | - return; // we're gonna handle this case during validation |
| 56 | + log.LogInformation("No krb5.conf exists - generating"); |
| 57 | + config = Krb5Config.Default(); |
| 58 | + string realm; |
| 59 | + try |
| 60 | + { |
| 61 | + realm = new KerberosPasswordCredential(options.ServiceAccount, options.Password).Domain; |
| 62 | + } |
| 63 | + catch (Exception) |
| 64 | + { |
| 65 | + return; // we're gonna handle this case during validation |
| 66 | + } |
| 67 | + |
| 68 | + options.Kdc ??= realm; |
| 69 | + if (realm != null) |
| 70 | + { |
| 71 | + config.Defaults.DefaultRealm = realm; |
| 72 | + config.Realms[realm].Kdc.Add(options.Kdc); |
| 73 | + config.Realms[realm].DefaultDomain = realm.ToLower(); |
| 74 | + config.DomainRealm.Add(realm.ToLower(), realm.ToUpper()); |
| 75 | + config.DomainRealm.Add($".{realm.ToLower()}", realm.ToUpper()); |
| 76 | + } |
| 77 | + config.Defaults.DefaultCCacheName = options.CacheFile; |
| 78 | + config.Defaults.DefaultKeytabName = options.KeytabFile; |
| 79 | + config.Defaults.DefaultClientKeytabName = options.KeytabFile; |
61 | 80 | } |
62 | | - options.Kdc ??= realm; |
63 | | - if (realm != null) |
| 81 | + else |
64 | 82 | { |
65 | | - config.Realms[realm].Kdc.Add(options.Kdc); |
| 83 | + log.LogInformation("Existing krb5.conf was detected"); |
| 84 | + config = Krb5Config.Parse(File.ReadAllText(options.Kerb5ConfigFile!)); |
66 | 85 | } |
67 | 86 |
|
68 | 87 | var client = new KerberosClient(config, loggerFactory); |
|
0 commit comments