From cf3d63de321ae4a18860c4be8a968eb906ddd2fe Mon Sep 17 00:00:00 2001 From: "Fred T. Hamster" Date: Thu, 2 Nov 2023 15:07:08 -0400 Subject: [PATCH 1/1] added python as possible script type for aliases in course of adding new script for splitting up awful one line certs --- scripts/core/generate_aliases.pl | 19 ++++++- scripts/security/one_line_cert_to_pem.py | 72 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100755 scripts/security/one_line_cert_to_pem.py diff --git a/scripts/core/generate_aliases.pl b/scripts/core/generate_aliases.pl index faf9e827..b590ca3d 100644 --- a/scripts/core/generate_aliases.pl +++ b/scripts/core/generate_aliases.pl @@ -30,12 +30,15 @@ use Env qw(FEISTY_MEOW_BINARIES BUILD_TOP FEISTY_MEOW_APEX FEISTY_MEOW_LOADING_D # directory where that file resides. sub make_alias { local($file, $dir) = @_; - if ($file =~ /\.[pP][lL]$/) { - local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//; - &make_perl_alias($aliasname, "$dir"); + if ($file =~ /\.[pP][yY]$/) { + local($aliasname) = $file; $aliasname =~ s/\.[Pp][yY]$//; + &make_python_alias($aliasname, "$dir"); } elsif ($file =~ /\.[sS][hH]$/) { local($aliasname) = $file; $aliasname =~ s/\.[Ss][Hh]$//; &make_bash_alias($aliasname, "$dir"); + } elsif ($file =~ /\.[pP][lL]$/) { + local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//; + &make_perl_alias($aliasname, "$dir"); } } @@ -49,6 +52,16 @@ sub make_bash_alias { print she "define_yeti_alias $aliasname=\"bash $full_alias.sh\"\n"; } +# makes an alias for a python script given the alias name. +#hmmm: don't love that we're hardcoding python3 in here, but apparently some systems don't have a 'python' command despite having python installed. +sub make_python_alias { + local($aliasname) = shift(@_); + local($full_alias) = $aliasname; + $aliasname =~ s/^.*\/([^\/]*)/\1/; +#print "alias became $aliasname\n"; + print she "define_yeti_alias $aliasname=\"python3 $full_alias.py\"\n"; +} + # makes an alias for a perl script given the alias name. sub make_perl_alias { local($aliasname) = shift(@_); diff --git a/scripts/security/one_line_cert_to_pem.py b/scripts/security/one_line_cert_to_pem.py new file mode 100755 index 00000000..22d4e97f --- /dev/null +++ b/scripts/security/one_line_cert_to_pem.py @@ -0,0 +1,72 @@ +#! /usr/bin/env python3 +""" + +Takes a certificate from the awful, often used form of a single line of text and turns +it into a real PEM file. +This assumes the input file has one single line of text that needs to be formatted +appropriately. + +author: chris koeritz + +""" + +import os +import sys + +#### + +def split_lines(unsplit_line: str) -> None: + """ + Takes text as input and breaks it up at the prescribed 64 character boundaries + used by PEM files. + """ + + size = 64 + splits = [(unsplit_line[i : i + size]) for i in range(0, len(unsplit_line), size)] + + #print("splits:", splits) + for split_line in splits: + if len(split_line) > 0: + print(split_line) + +#### + +def main() -> None: + """Main function""" + + # make sure they gave us a filename. + args = len(sys.argv) + if args < 2: + print("\ +This script needs a filename to operate on. The file is expected to contain\n\ +one line of certificate data, which this script will reformat into a standard\n\ +PEM file format. The PEM file will be output on the console.") + exit(1) + + filename = sys.argv[1] + + # make sure the filename is valid. + if not os.path.isfile(filename): + print("The filename provided does not seem to be a readable file:", filename) + exit(1) + + file = open(filename, "r") + + cert_line = file.readline() + cert_line = cert_line.strip('\r\n') + + #ugh, no extra noise needed. + #print() + #print("below is the properly formatted output sourced from:", filename) + #print() + + print("-----BEGIN CERTIFICATE-----") + split_lines(cert_line) + print("-----END CERTIFICATE-----") + +#### + +if __name__ == "__main__": + main() + + -- 2.34.1