From cc3f5236a6c18d9d16c8c6faa1e2044d0610f490 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Fri, 23 Sep 2022 14:30:06 +0300 Subject: [PATCH 2/2] fix: darwin: ARM CPU darwin krb5 path fix --- src/test/kerberos/t/001_auth.pl | 89 ++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 24 deletions(-) diff --git a/src/test/kerberos/t/001_auth.pl b/src/test/kerberos/t/001_auth.pl index 47169a1d1e..c1211b80dd 100644 --- a/src/test/kerberos/t/001_auth.pl +++ b/src/test/kerberos/t/001_auth.pl @@ -30,39 +30,80 @@ elsif ($ENV{PG_TEST_EXTRA} !~ /\bkerberos\b/) plan skip_all => 'Potentially unsafe test GSSAPI/Kerberos not enabled in PG_TEST_EXTRA'; } -my ($krb5_bin_dir, $krb5_sbin_dir); - -if ($^O eq 'darwin') -{ - $krb5_bin_dir = '/usr/local/opt/krb5/bin'; - $krb5_sbin_dir = '/usr/local/opt/krb5/sbin'; -} -elsif ($^O eq 'freebsd') -{ - $krb5_bin_dir = '/usr/local/bin'; - $krb5_sbin_dir = '/usr/local/sbin'; -} -elsif ($^O eq 'linux') -{ - $krb5_sbin_dir = '/usr/sbin'; -} - my $krb5_config = 'krb5-config'; my $kinit = 'kinit'; my $kdb5_util = 'kdb5_util'; my $kadmin_local = 'kadmin.local'; my $krb5kdc = 'krb5kdc'; -if ($krb5_bin_dir && -d $krb5_bin_dir) +# get prefix for kerberos executables and try to find them at this path +sub test_krb5_paths { - $krb5_config = $krb5_bin_dir . '/' . $krb5_config; - $kinit = $krb5_bin_dir . '/' . $kinit; + my ($is_krb5_found, $krb5_path) = (@_); + + # if it is alredy found return + if ($is_krb5_found) + { + return 1; + } + + my ($kdb5_util_path, $kadmin_local_path, $krb5kdc_path, $krb5_config_path, $kinit_path, @krb5_file_paths, $bin, $sbin); + + # remove '\n' since 'krb5-config --prefix' returns path ends with '\n' + $krb5_path =~ s/\n//g; + # remove trailing slashes + $krb5_path =~ s{(.+)/\z}{$1}; + $bin = '/bin/'; + $sbin = '/sbin/'; + $kdb5_util_path = $krb5_path . $sbin . $kdb5_util; + $kadmin_local_path = $krb5_path . $sbin . $kadmin_local; + $krb5kdc_path = $krb5_path . $sbin . $krb5kdc; + $krb5_config_path = $krb5_path . $bin . $krb5_config; + $kinit_path = $krb5_path . $bin . $kinit; + @krb5_file_paths = ($kdb5_util_path, $kadmin_local_path, $krb5kdc_path, $krb5_config_path, $kinit_path); + + # check if all of the files exist + foreach (@krb5_file_paths) + { + if(! -e $_) + { + return 0; + } + } + # all files are found + $krb5_config = $krb5_config_path; + $kinit = $kinit_path; + $kdb5_util = $kdb5_util_path; + $kadmin_local = $kadmin_local_path; + $krb5kdc = $krb5kdc_path; + return 1; } -if ($krb5_sbin_dir && -d $krb5_sbin_dir) + +my $is_krb5_found = 0; +# first try to use 'krb5-config --prefix', then try hardcoded paths. +# if executables are still not found after trying these paths, +# try to use them from PATH. +my $krb5_config_prefix_cmd = $krb5_config . ' --prefix'; +$is_krb5_found = test_krb5_paths($is_krb5_found, `$krb5_config_prefix_cmd`); +if (! $is_krb5_found) { - $kdb5_util = $krb5_sbin_dir . '/' . $kdb5_util; - $kadmin_local = $krb5_sbin_dir . '/' . $kadmin_local; - $krb5kdc = $krb5_sbin_dir . '/' . $krb5kdc; + if ($^O eq 'darwin') + { + # krb5 is placed at /usr/local/opt for Intel CPU darwin + $is_krb5_found = test_krb5_paths($is_krb5_found, '/usr/local/opt/krb5/'); + # krb5 is placed at /opt/homebrew/opt/ for arm CPU darwin + $is_krb5_found = test_krb5_paths($is_krb5_found, '/opt/homebrew/opt/krb5/'); + # krb5 is placed at /opt/local/ for MacPorts + $is_krb5_found = test_krb5_paths($is_krb5_found, '/opt/local/'); + } + elsif ($^O eq 'freebsd') + { + $is_krb5_found = test_krb5_paths($is_krb5_found, '/usr/local/'); + } + elsif ($^O eq 'linux') + { + $is_krb5_found = test_krb5_paths($is_krb5_found, '/usr/sbin/'); + } } my $host = 'auth-test-localhost.postgresql.example.com'; -- 2.25.1