sign.py revision 3321
660N/A#!/usr/bin/python2.7
660N/A#
660N/A# CDDL HEADER START
660N/A#
660N/A# The contents of this file are subject to the terms of the
660N/A# Common Development and Distribution License (the "License").
660N/A# You may not use this file except in compliance with the License.
660N/A#
660N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
660N/A# or http://www.opensolaris.org/os/licensing.
660N/A# See the License for the specific language governing permissions
660N/A# and limitations under the License.
660N/A#
660N/A# When distributing Covered Code, include this CDDL HEADER in each
660N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
660N/A# If applicable, add the following below this CDDL HEADER, with the
660N/A# fields enclosed by brackets "[]" replaced with your own identifying
684N/A# information: Portions Copyright [yyyy] [name of copyright owner]
684N/A#
684N/A# CDDL HEADER END
684N/A#
684N/A
684N/A#
684N/A# Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
684N/A#
684N/A
684N/Aimport getopt
684N/Aimport gettext
684N/Aimport hashlib
684N/Aimport locale
684N/Aimport os
684N/Aimport shutil
684N/Aimport sys
684N/Aimport tempfile
684N/Aimport traceback
684N/A
684N/Afrom cryptography import x509
684N/Afrom cryptography.hazmat.backends import default_backend
684N/Afrom cryptography.hazmat.primitives import serialization
684N/Afrom imp import reload
684N/A
684N/Aimport pkg
684N/Aimport pkg.actions as actions
684N/Aimport pkg.client.api_errors as api_errors
684N/Aimport pkg.client.transport.transport as transport
684N/Aimport pkg.digest as digest
684N/Aimport pkg.fmri as fmri
684N/Aimport pkg.manifest as manifest
684N/Aimport pkg.misc as misc
684N/Aimport pkg.publish.transaction as trans
684N/Afrom pkg.client import global_settings
684N/Afrom pkg.client.debugvalues import DebugValues
684N/Afrom pkg.misc import emsg, msg, PipeError
684N/A
684N/APKG_CLIENT_NAME = "pkgsign"
684N/A
684N/A# pkg exit codes
684N/AEXIT_OK = 0
684N/AEXIT_OOPS = 1
684N/AEXIT_BADOPT = 2
684N/AEXIT_PARTIAL = 3
684N/A
684N/Arepo_cache = {}
684N/A
684N/Adef error(text, cmd=None):
684N/A """Emit an error message prefixed by the command name """
684N/A
684N/A if cmd:
684N/A text = "{0}: {1}".format(cmd, text)
684N/A
684N/A else:
684N/A text = "{0}: {1}".format(PKG_CLIENT_NAME, text)
684N/A
684N/A
684N/A # If the message starts with whitespace, assume that it should come
684N/A # *before* the command-name prefix.
684N/A text_nows = text.lstrip()
684N/A ws = text[:len(text) - len(text_nows)]
684N/A
684N/A # This has to be a constant value as we can't reliably get our actual
684N/A # program name on all platforms.
684N/A emsg(ws + text_nows)
684N/A
684N/Adef usage(usage_error=None, cmd=None, retcode=EXIT_BADOPT):
684N/A """Emit a usage message and optionally prefix it with a more specific
684N/A error message. Causes program to exit."""
684N/A
684N/A if usage_error:
684N/A error(usage_error, cmd=cmd)
684N/A emsg (_("""\
684N/AUsage:
684N/A pkgsign -s path_or_uri [-acikn] [--no-index] [--no-catalog]
684N/A (fmri|pattern) ...
684N/A"""))
684N/A
684N/A sys.exit(retcode)
684N/A
684N/Adef fetch_catalog(src_pub, xport, temp_root):
684N/A """Fetch the catalog from src_uri."""
684N/A
684N/A if not src_pub.meta_root:
684N/A # Create a temporary directory for catalog.
684N/A cat_dir = tempfile.mkdtemp(dir=temp_root)
684N/A src_pub.meta_root = cat_dir
684N/A
684N/A src_pub.transport = xport
684N/A src_pub.refresh(True, True)
684N/A
684N/A return src_pub.catalog
684N/A
684N/Adef __make_tmp_cert(d, pth):
684N/A try:
684N/A with open(pth, "rb") as f:
684N/A cert = x509.load_pem_x509_certificate(f.read(),
684N/A default_backend())
684N/A except (ValueError, IOError) as e:
684N/A raise api_errors.BadFileFormat(_("The file {0} was expected to "
684N/A "be a PEM certificate but it could not be read.").format(
684N/A pth))
684N/A fd, fp = tempfile.mkstemp(dir=d)
684N/A with os.fdopen(fd, "wb") as fh:
684N/A fh.write(cert.public_bytes(serialization.Encoding.PEM))
684N/A return fp
684N/A
684N/Adef main_func():
684N/A misc.setlocale(locale.LC_ALL, "", error)
684N/A gettext.install("pkg", "/usr/share/locale",
684N/A codeset=locale.getpreferredencoding())
684N/A global_settings.client_name = "pkgsign"
684N/A
684N/A try:
684N/A opts, pargs = getopt.getopt(sys.argv[1:], "a:c:i:k:ns:D:",
684N/A ["help", "no-index", "no-catalog"])
684N/A except getopt.GetoptError as e:
684N/A usage(_("illegal global option -- {0}").format(e.opt))
684N/A
684N/A show_usage = False
684N/A sig_alg = "rsa-sha256"
684N/A cert_path = None
684N/A key_path = None
679N/A chain_certs = []
679N/A add_to_catalog = True
684N/A set_alg = False
684N/A dry_run = False
684N/A
684N/A repo_uri = os.getenv("PKG_REPO", None)
684N/A for opt, arg in opts:
684N/A if opt == "-a":
684N/A sig_alg = arg
684N/A set_alg = True
684N/A elif opt == "-c":
684N/A cert_path = os.path.abspath(arg)
684N/A if not os.path.isfile(cert_path):
684N/A usage(_("{0} was expected to be a certificate "
684N/A "but isn't a file.").format(cert_path))
684N/A elif opt == "-i":
684N/A p = os.path.abspath(arg)
684N/A if not os.path.isfile(p):
684N/A usage(_("{0} was expected to be a certificate "
684N/A "but isn't a file.").format(p))
684N/A chain_certs.append(p)
684N/A elif opt == "-k":
684N/A key_path = os.path.abspath(arg)
684N/A if not os.path.isfile(key_path):
684N/A usage(_("{0} was expected to be a key file "
684N/A "but isn't a file.").format(key_path))
684N/A elif opt == "-n":
684N/A dry_run = True
684N/A elif opt == "-s":
684N/A repo_uri = misc.parse_uri(arg)
684N/A elif opt == "--help":
684N/A show_usage = True
684N/A elif opt == "--no-catalog":
684N/A add_to_catalog = False
684N/A elif opt == "-D":
684N/A try:
684N/A key, value = arg.split("=", 1)
684N/A DebugValues.set_value(key, value)
684N/A except (AttributeError, ValueError):
684N/A error(_("{opt} takes argument of form "
684N/A "name=value, not {arg}").format(
684N/A opt=opt, arg=arg))
684N/A if show_usage:
684N/A usage(retcode=EXIT_OK)
684N/A
684N/A if not repo_uri:
684N/A usage(_("a repository must be provided"))
684N/A
684N/A if key_path and not cert_path:
684N/A usage(_("If a key is given to sign with, its associated "
684N/A "certificate must be given."))
684N/A
684N/A if cert_path and not key_path:
684N/A usage(_("If a certificate is given, its associated key must be "
684N/A "given."))
679N/A
684N/A if chain_certs and not cert_path:
684N/A usage(_("Intermediate certificates are only valid if a key "
684N/A "and certificate are also provided."))
684N/A
684N/A if not pargs:
684N/A usage(_("At least one fmri or pattern must be provided to "
684N/A "sign."))
684N/A
684N/A if not set_alg and not key_path:
684N/A sig_alg = "sha256"
684N/A
684N/A s, h = actions.signature.SignatureAction.decompose_sig_alg(sig_alg)
684N/A if h is None:
684N/A usage(_("{0} is not a recognized signature algorithm.").format(
684N/A sig_alg))
684N/A if s and not key_path:
684N/A usage(_("Using {0} as the signature algorithm requires that a "
684N/A "key and certificate pair be presented using the -k and -c "
684N/A "options.").format(sig_alg))
684N/A if not s and key_path:
684N/A usage(_("The {0} hash algorithm does not use a key or "
684N/A "certificate. Do not use the -k or -c options with this "
684N/A "algorithm.").format(sig_alg))
684N/A
684N/A if DebugValues:
684N/A reload(digest)
684N/A
684N/A errors = []
684N/A
684N/A t = misc.config_temp_root()
684N/A temp_root = tempfile.mkdtemp(dir=t)
684N/A del t
684N/A
684N/A cache_dir = tempfile.mkdtemp(dir=temp_root)
684N/A incoming_dir = tempfile.mkdtemp(dir=temp_root)
684N/A chash_dir = tempfile.mkdtemp(dir=temp_root)
684N/A cert_dir = tempfile.mkdtemp(dir=temp_root)
684N/A
684N/A try:
684N/A chain_certs = [
684N/A __make_tmp_cert(cert_dir, c) for c in chain_certs
684N/A ]
684N/A if cert_path is not None:
684N/A cert_path = __make_tmp_cert(cert_dir, cert_path)
684N/A
684N/A xport, xport_cfg = transport.setup_transport()
684N/A xport_cfg.add_cache(cache_dir, readonly=False)
684N/A xport_cfg.incoming_root = incoming_dir
684N/A
684N/A # Configure publisher(s)
684N/A transport.setup_publisher(repo_uri, "source", xport,
684N/A xport_cfg, remote_prefix=True)
684N/A pats = pargs
684N/A successful_publish = False
684N/A
684N/A concrete_fmris = []
684N/A unmatched_pats = set(pats)
684N/A all_pats = frozenset(pats)
684N/A get_all_pubs = False
684N/A pub_prefs = set()
684N/A # Gather the publishers whose catalogs will be needed.
684N/A for pat in pats:
684N/A try:
684N/A p_obj = fmri.MatchingPkgFmri(pat)
684N/A except fmri.IllegalMatchingFmri as e:
684N/A errors.append(e)
684N/A continue
684N/A pub_prefix = p_obj.get_publisher()
684N/A if pub_prefix:
684N/A pub_prefs.add(pub_prefix)
684N/A else:
684N/A get_all_pubs = True
684N/A # Check each publisher for matches to our patterns.
684N/A for p in xport_cfg.gen_publishers():
684N/A if not get_all_pubs and p.prefix not in pub_prefs:
684N/A continue
684N/A cat = fetch_catalog(p, xport, temp_root)
684N/A ms, tmp1, u = cat.get_matching_fmris(pats)
684N/A # Find which patterns matched.
684N/A matched_pats = all_pats - u
684N/A # Remove those patterns from the unmatched set.
684N/A unmatched_pats -= matched_pats
679N/A for v_list in ms.values():
660N/A concrete_fmris.extend([(v, p) for v in v_list])
684N/A if unmatched_pats:
684N/A raise api_errors.PackageMatchErrors(
679N/A unmatched_fmris=unmatched_pats)
684N/A
684N/A for pfmri, src_pub in sorted(set(concrete_fmris)):
684N/A try:
684N/A # Get the existing manifest for the package to
684N/A # be signed.
684N/A m_str = xport.get_manifest(pfmri,
684N/A content_only=True, pub=src_pub)
684N/A m = manifest.Manifest()
684N/A m.set_content(content=m_str)
679N/A
684N/A # Construct the base signature action.
684N/A attrs = { "algorithm": sig_alg }
684N/A a = actions.signature.SignatureAction(cert_path,
684N/A **attrs)
684N/A a.hash = cert_path
684N/A
684N/A # Add the action to the manifest to be signed
684N/A # since the action signs itself.
684N/A m.add_action(a, misc.EmptyI)
684N/A
684N/A # Set the signature value and certificate
684N/A # information for the signature action.
684N/A a.set_signature(m.gen_actions(),
684N/A key_path=key_path, chain_paths=chain_certs,
684N/A chash_dir=chash_dir)
684N/A
684N/A # The hash of 'a' is currently a path, we need
684N/A # to find the hash of that file to allow
684N/A # comparison to existing signatures.
684N/A hsh = None
684N/A if cert_path:
684N/A # Action identity still uses the 'hash'
684N/A # member of the action, so we need to
660N/A # stay with the sha1 hash.
684N/A hsh, _dummy = \
684N/A misc.get_data_digest(cert_path,
684N/A hash_func=hashlib.sha1)
684N/A
684N/A # Check whether the signature about to be added
684N/A # is identical, or almost identical, to existing
684N/A # signatures on the package. Because 'a' has
684N/A # already been added to the manifest, it is
684N/A # generated by gen_actions_by_type, so the cnt
684N/A # must be 2 or higher to be an issue.
684N/A cnt = 0
684N/A almost_identical = False
684N/A for a2 in m.gen_actions_by_type("signature"):
684N/A try:
679N/A if a.identical(a2, hsh):
684N/A cnt += 1
684N/A except api_errors.AlmostIdentical as e:
684N/A e.pkg = pfmri
684N/A errors.append(e)
684N/A almost_identical = True
684N/A if almost_identical:
684N/A continue
684N/A if cnt == 2:
684N/A continue
684N/A elif cnt > 2:
684N/A raise api_errors.DuplicateSignaturesAlreadyExist(pfmri)
684N/A assert cnt == 1, "Cnt was:{0}".format(cnt)
679N/A
684N/A if not dry_run:
684N/A # Append the finished signature action
684N/A # to the published manifest.
684N/A t = trans.Transaction(repo_uri,
684N/A pkg_name=str(pfmri), xport=xport,
684N/A pub=src_pub)
684N/A try:
684N/A t.append()
684N/A t.add(a)
679N/A for c in chain_certs:
684N/A t.add_file(c)
684N/A t.close(add_to_catalog=
684N/A add_to_catalog)
684N/A except:
684N/A if t.trans_id:
684N/A t.close(abandon=True)
684N/A raise
684N/A msg(_("Signed {0}").format(pfmri.get_fmri(
684N/A include_build=False)))
679N/A successful_publish = True
679N/A except (api_errors.ApiException, fmri.FmriError,
684N/A trans.TransactionError) as e:
684N/A errors.append(e)
684N/A if errors:
684N/A error("\n".join([str(e) for e in errors]))
684N/A if successful_publish:
684N/A return EXIT_PARTIAL
684N/A else:
684N/A return EXIT_OOPS
684N/A return EXIT_OK
684N/A except api_errors.ApiException as e:
725N/A error(e)
725N/A return EXIT_OOPS
725N/A finally:
725N/A shutil.rmtree(temp_root)
684N/A
684N/A#
684N/A# Establish a specific exit status which means: "python barfed an exception"
684N/A# so that we can more easily detect these in testing of the CLI commands.
684N/A#
684N/Aif __name__ == "__main__":
684N/A try:
684N/A __ret = main_func()
684N/A except (PipeError, KeyboardInterrupt):
684N/A # We don't want to display any messages here to prevent
684N/A # possible further broken pipe (EPIPE) errors.
684N/A __ret = EXIT_OOPS
684N/A except SystemExit as _e:
684N/A raise _e
679N/A except:
684N/A traceback.print_exc()
684N/A error(misc.get_traceback_message())
684N/A __ret = 99
684N/A sys.exit(__ret)
684N/A