Skip to content

Commit

Permalink
Remove zope dependency.
Browse files Browse the repository at this point in the history
It should work with Certbot 2

Signed-off-by: Pol Henarejos <[email protected]>
  • Loading branch information
polhenarejos committed Apr 18, 2023
1 parent 5d0c01a commit 9429f3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
53 changes: 23 additions & 30 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/env python3

import argparse, logging, sys, getpass, tempfile, os
import zope.component
from certbot._internal.plugins import disco as plugins_disco
from certbot._internal.plugins import selection as plug_sel
from certbot._internal import cli
from certbot._internal import main as certbot_main
from certbot._internal import reporter
from certbot._internal import log
from certbot._internal.display import obj as display_obj
from certbot.display import util as display_util
Expand All @@ -22,30 +20,29 @@
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.serialization import pkcs12, Encoding

logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)

def prepare_cli_args(args):
cli_args = []
command = args.command.lower()
if (args.config_dir): cli_args.extend(['--config-dir',args.config_dir])
if (args.work_dir): cli_args.extend(['--work-dir',args.work_dir])
if (args.logs_dir): cli_args.extend(['--logs-dir',args.logs_dir])

if (command == 'cert'): cli_args.extend(['certonly'])
else: cli_args.extend([command])

if (args.test): cli_args.extend(['--server','https://acme-staging.castle.cloud/acme/directory'])
else: cli_args.extend(['--server','https://acme.castle.cloud/acme/directory'])

if (args.non_interactive): cli_args.extend(['-n'])

return cli_args

def prepare_config(cli_args):
def prepare_config(cli_args):
plugins = plugins_disco.PluginsRegistry.find_all()
cargs = cli.prepare_and_parse_args(plugins, cli_args)
config = configuration.NamespaceConfig(cargs)
zope.component.provideUtility(config, interfaces.IConfig)
return config,plugins

def root_cert_advise():
Expand All @@ -65,7 +62,7 @@ def request_cert(args, config):
key, csr = csr_util.prepare(args.email, config, key_path=args.key_path, usage=args.usage)
## Reparse for including --csr arguments
cli_args = prepare_cli_args(args)
if (args.dry_run):
if (args.dry_run):
cli_args.extend(['--dry-run'])
for email in args.email:
cli_args.extend(['-d',email])
Expand Down Expand Up @@ -107,14 +104,14 @@ def request_cert(args, config):
elif (args.passphrase):
cli_args.extend(['--castle-installer-passphrase',args.passphrase])
cli_args.extend(['-m',args.contact])
if (args.agree_tos):
if (args.agree_tos):
cli_args.extend(['--agree-tos'])
config,plugins = prepare_config(cli_args)

config.cert_path = config.live_dir+'/cert.pem'
config.chain_path = config.live_dir+'/ca.pem'
config.fullchain_path = config.live_dir+'/chain.pem'

config.key_path = key.file
try:
# installers are used in auth mode to determine domain names
Expand All @@ -133,7 +130,7 @@ def request_cert(args, config):
certbot_main._install_cert(config, le_client, args.email)
else:
util.safely_remove(csr.file)

def try_open_p12(file,passphrase=None):
with open(args.cert_path,'rb') as p12:
(private_key, certificate, _) = pkcs12.load_key_and_certificates(p12.read(),passphrase)
Expand All @@ -145,7 +142,7 @@ def try_open_p12(file,passphrase=None):
temp_pkey.close()
return temp_pkey.name,temp_cert.name
return None,None

def revoke_cert(args, config):
cli_args = prepare_cli_args(args)
if (args.reason):
Expand All @@ -156,7 +153,7 @@ def revoke_cert(args, config):
key_path,cert_path = try_open_p12(args.cert_path)
cli_args.extend(['--cert-path',cert_path])
cli_args.extend(['--key-path',key_path])
except ValueError as e:
except ValueError as e:
if ('Invalid password' in str(e)):
passphrase = None
if (args.passphrase):
Expand Down Expand Up @@ -184,7 +181,7 @@ def revoke_cert(args, config):
os.unlink(key_path)
if (cert_path):
os.unlink(cert_path)

def main(args):
## Prepare storage system
command = args.command.lower()
Expand All @@ -198,17 +195,14 @@ def main(args):
certbot_main.make_or_verify_needed_dirs(config)
except errors.Error:
raise
report = reporter.Reporter(config)
zope.component.provideUtility(report, interfaces.IReporter)
util.atexit_register(report.print_messages)
with certbot_main.make_displayer(config) as displayer:
display_obj.set_display(displayer)

if (command == 'cert'):
request_cert(args, config)
elif (command == 'revoke'):
revoke_cert(args, config)

def process_args(args):
if args.email:
for e in args.email:
Expand All @@ -228,28 +222,28 @@ def parse_args():
parser.add_argument('--contact', help='Contact e-mail for important account notifications')
parser.add_argument('--imap', help='Uses IMAP Authenticator for automatic reply', action='store_true')
parser.add_argument('command',choices=['cert','revoke','renew'])

parser.add_argument('--login',help='IMAP login',required='--imap' in sys.argv)
parser.add_argument('--password',help='IMAP password',required='--imap' in sys.argv)
parser.add_argument('--host',help='IMAP server host',required='--imap' in sys.argv)
parser.add_argument('--port',help='IMAP server port. If empty, it will be auto-detected')
parser.add_argument('--ssl',help='IMAP SSL connection',action='store_true')

parser.add_argument('--smtp-method',help='SMTP method {STARTTLS,SSL,plain}',choices= ['STARTTLS','SSL','plain'])
parser.add_argument('--smtp-login',help='SMTP login. If empty, IMAP login will be used')
parser.add_argument('--smtp-password',help='SMTP password. If empty, IMAP password will be used')
parser.add_argument('--smtp-host',help='SMTP server host',required='--imap' in sys.argv)
parser.add_argument('--smtp-port',help='SMTP server port. If empty, it will be auto-detected')

parser.add_argument('--no-passphrase',help='PKCS12 is stored without passphrase. Use with CAUTION: the PKCS12 contains the private key',action='store_true')
parser.add_argument('--passphrase',help='Passphrase to use for the PKCS12 generation. This passpharse will be used for private key encryption')

parser.add_argument('--usage', help='Key usage for certificate. Multiple usages can be specified', choices=['digitalSignature','contentCommitment','keyEncipherment','keyAgreement'], action='append')

parser.add_argument('--cert-path',help='Path where certificate is located',required='revoke' in sys.argv)
parser.add_argument('--reason',help='Reason of revocation',choices=['unspecified','keycompromise','affiliationchanged','superseded','cessationofoperation'])
parser.add_argument('--key-path',help='Path of private key location')

parser.add_argument('--outlook', help='Uses MAPI (Outlook) Authenticator for automatic reply', action='store_true')
parser.add_argument('--outlook-account', help='Outlook account where the challenge is processed', required='--outlook' in sys.argv)

Expand All @@ -260,12 +254,11 @@ def parse_args():

args = parser.parse_args()
process_args(args)

return args


if __name__ == "__main__":

args = parse_args()
main(args)

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
version = mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

INSTALL_REQUIRES = [
'certbot>=1.20.0',
'acme>=1.20.0',
'setuptools',
'zope.interface',
'imapclient',
'dkimpy',
'cryptography>=3.3',
Expand Down

0 comments on commit 9429f3f

Please sign in to comment.