diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 000000000..155e4f638 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[report] +omit = etc/*,setup.py,*egg*,.tox/*,barbican/tests/*,barbican/openstack/* diff --git a/.gitignore b/.gitignore index fb8695b52..87a87a1cf 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ pip-log.txt .coverage .tox nosetests.xml +coverage.xml # Translations *.mo diff --git a/barbican/api/app.py b/barbican/api/app.py index 64308ea5b..d8b5c2f72 100644 --- a/barbican/api/app.py +++ b/barbican/api/app.py @@ -24,22 +24,27 @@ from barbican.api.resources import TenantsResource, TenantResource from barbican.api.resources import CSRsResource, CSRResource from barbican.api.resources import CertificatesResource, CertificateResource from barbican.api.resources import SecretsResource, SecretResource - -# Resources -VERSIONS = VersionResource() -TENANTS = TenantsResource() -TENANT = TenantResource() -SECRETS = SecretsResource() -SECRET = SecretResource() -CSRS = CSRsResource() -CSR = CSRResource() -CERTS = CertificatesResource() -CERT = CertificateResource() +from barbican.openstack.common import log +from barbican.common import config def create_main_app(global_config, **local_conf): """uWSGI factory method for the Barbican-API application""" + config.parse_args() + log.setup('barbican') + + # Resources + VERSIONS = VersionResource() + TENANTS = TenantsResource() + TENANT = TenantResource() + SECRETS = SecretsResource() + SECRET = SecretResource() + CSRS = CSRsResource() + CSR = CSRResource() + CERTS = CertificatesResource() + CERT = CertificateResource() + wsgi_app = api = falcon.API() api.add_route('/', VERSIONS) api.add_route('/tenants', TENANTS) @@ -51,5 +56,4 @@ def create_main_app(global_config, **local_conf): api.add_route('/{tenant_id}/certificates', CERTS) api.add_route('/{tenant_id}/certificates/{cert_id}', CERT) - return wsgi_app - \ No newline at end of file + return wsgi_app \ No newline at end of file diff --git a/barbican/api/middleware/simple.py b/barbican/api/middleware/simple.py index ca683db08..ecda07041 100644 --- a/barbican/api/middleware/simple.py +++ b/barbican/api/middleware/simple.py @@ -20,16 +20,12 @@ A filter middleware that just outputs to logs, for instructive/sample purposes o """ from oslo.config import cfg - from barbican.api.middleware import Middleware +from barbican.common import utils -import barbican.openstack.common.log as logging - - +LOG = utils.getLogger(__name__) CONF = cfg.CONF -LOG = logging.getLogger(__name__) - class SimpleFilter(Middleware): diff --git a/barbican/api/resources.py b/barbican/api/resources.py index 33a6f58b5..5555a60b6 100644 --- a/barbican/api/resources.py +++ b/barbican/api/resources.py @@ -27,12 +27,11 @@ from barbican.model.repositories import TenantRepo, SecretRepo from barbican.model.repositories import CSRRepo, CertificateRepo from barbican.queue.resources import QueueResource, StartCSRMessage from barbican.crypto.fields import encrypt, decrypt -from barbican.common import config from barbican.openstack.common.gettextutils import _ -import barbican.openstack.common.log as logging from barbican.openstack.common import jsonutils as json +from barbican.common import utils -LOG = logging.getLogger(__name__) +LOG = utils.getLogger(__name__) def _tenant_not_found(): diff --git a/barbican/common/config.py b/barbican/common/config.py index 7ad24198d..59a6e3f09 100644 --- a/barbican/common/config.py +++ b/barbican/common/config.py @@ -17,18 +17,76 @@ Configuration setup for Barbican. """ - +import logging +import logging.config +import logging.handlers import os +import sys + +from barbican.version import __version__ from oslo.config import cfg -# Ensure the local python config path is on the list to pull config info from -CONF_FILES = cfg.find_config_files(prog='barbican-api') - -#CONF_FILES = cfg.find_config_files(project='barbican', prog='barbican-api') -CONF_FILES.append('./etc/barbican/barbican-api.conf') -CONF_FILES.append('../etc/barbican/barbican-api.conf') -CONF_FILES = [cfile for cfile in CONF_FILES if os.path.isfile(cfile)] - -# Set configuration files CONF = cfg.CONF -CONF(prog='barbican-api', default_config_files=CONF_FILES) +CONF.import_opt('verbose', 'barbican.openstack.common.log') +CONF.import_opt('debug', 'barbican.openstack.common.log') +CONF.import_opt('log_dir', 'barbican.openstack.common.log') +CONF.import_opt('log_file', 'barbican.openstack.common.log') +CONF.import_opt('log_config', 'barbican.openstack.common.log') +CONF.import_opt('log_format', 'barbican.openstack.common.log') +CONF.import_opt('log_date_format', 'barbican.openstack.common.log') +CONF.import_opt('use_syslog', 'barbican.openstack.common.log') +CONF.import_opt('syslog_log_facility', 'barbican.openstack.common.log') + + +def parse_args(args=None, usage=None, default_config_files=None): + CONF(args=args, + project='barbican', + prog='barbican-api', + version=__version__, + usage=usage, + default_config_files=default_config_files) + + +def setup_logging(): + """ + Sets up the logging options + """ + + if CONF.log_config: + # Use a logging configuration file for all settings... + if os.path.exists(CONF.log_config): + logging.config.fileConfig(CONF.log_config) + return + else: + raise RuntimeError("Unable to locate specified logging " + "config file: %s" % CONF.log_config) + + root_logger = logging.root + if CONF.debug: + root_logger.setLevel(logging.DEBUG) + elif CONF.verbose: + root_logger.setLevel(logging.INFO) + else: + root_logger.setLevel(logging.WARNING) + + formatter = logging.Formatter(CONF.log_format, CONF.log_date_format) + + if CONF.use_syslog: + try: + facility = getattr(logging.handlers.SysLogHandler, + CONF.syslog_log_facility) + except AttributeError: + raise ValueError(_("Invalid syslog facility")) + + handler = logging.handlers.SysLogHandler(address='/dev/log', + facility=facility) + elif CONF.log_file: + logfile = CONF.log_file + if CONF.log_dir: + logfile = os.path.join(CONF.log_dir, logfile) + handler = logging.handlers.WatchedFileHandler(logfile) + else: + handler = logging.StreamHandler(sys.stdout) + + handler.setFormatter(formatter) + root_logger.addHandler(handler) diff --git a/barbican/common/utils.py b/barbican/common/utils.py new file mode 100644 index 000000000..6a475eb1e --- /dev/null +++ b/barbican/common/utils.py @@ -0,0 +1,29 @@ +# Copyright (c) 2013 Rackspace, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Common utilities for Barbican. +""" + +import barbican.openstack.common.log as logging + + +# Return a logger instance. +# Note: Centralize access to the logger to avoid the dreaded +# 'ArgsAlreadyParsedError: arguments already parsed: cannot register CLI option' +# error. +def getLogger(name): + return logging.getLogger(name) + diff --git a/barbican/model/models.py b/barbican/model/models.py index d9dcc1330..6d8424b75 100644 --- a/barbican/model/models.py +++ b/barbican/model/models.py @@ -17,7 +17,6 @@ Defines database models for Barbican """ - from sqlalchemy import Column, Integer, String, BigInteger from sqlalchemy.ext.compiler import compiles from sqlalchemy.ext.declarative import declarative_base @@ -28,9 +27,9 @@ from sqlalchemy import Index, UniqueConstraint from barbican.openstack.common import timeutils from barbican.openstack.common import uuidutils from barbican.openstack.common import jsonutils as json -import barbican.openstack.common.log as logging +from barbican.common import utils -LOG = logging.getLogger(__name__) +LOG = utils.getLogger(__name__) BASE = declarative_base() diff --git a/barbican/model/repositories.py b/barbican/model/repositories.py index 846cff122..0db4872dd 100644 --- a/barbican/model/repositories.py +++ b/barbican/model/repositories.py @@ -30,14 +30,14 @@ import sqlalchemy.orm as sa_orm import sqlalchemy.sql as sa_sql from barbican.common import exception -from barbican.common import config # TBD: from barbican.db.sqlalchemy import migration from barbican.model import models from barbican.openstack.common import timeutils from barbican.openstack.common.gettextutils import _ -import barbican.openstack.common.log as os_logging +from barbican.common import utils + +LOG = utils.getLogger(__name__) -LOG = os_logging.getLogger(__name__) _ENGINE = None _MAKER = None diff --git a/barbican/tests/api/resources_test.py b/barbican/tests/api/resources_test.py index a31be6550..4460076d1 100644 --- a/barbican/tests/api/resources_test.py +++ b/barbican/tests/api/resources_test.py @@ -13,16 +13,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime -from barbican.api.resources import * -from barbican.model.models import * -from barbican.crypto.fields import * -from barbican.common import config - from mock import MagicMock - import falcon +import json import unittest + +from datetime import datetime +from barbican.api.resources import VersionResource +from barbican.api.resources import TenantsResource, TenantResource +from barbican.api.resources import CSRsResource, CSRResource +from barbican.api.resources import CertificatesResource, CertificateResource +from barbican.api.resources import SecretsResource, SecretResource +from barbican.model.models import Certificate, CSR, Secret, Tenant +from barbican.model.repositories import CSRRepo, CertificateRepo +from barbican.model.repositories import TenantRepo, SecretRepo +from barbican.crypto.fields import decrypt_value, encrypt_value +from barbican.common import config from barbican.common import exception diff --git a/barbican/tests/config_test.py b/barbican/tests/config_test.py deleted file mode 100644 index 5ae5a97b2..000000000 --- a/barbican/tests/config_test.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2013 Rackspace, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -import os -import unittest - -from oslo.config import cfg -from barbican.common import config -import logging - -# Configuration test configuration options -test_group = cfg.OptGroup(name='test', title='Configuration Test') - -CFG_TEST_OPTIONS = [ - cfg.BoolOpt('should_pass', - default=False, - help="""Example option to make sure configuration - loading passes test. - """ - ) -] - -CONF = cfg.CONF - -LOG = logging.getLogger(__name__) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(WhenConfiguring()) - - return suite - - -class WhenConfiguring(unittest.TestCase): - - def test_loading(self): - - LOG.debug("In test 'test_loading'") - - CONF.register_group(test_group) - CONF.register_opts(CFG_TEST_OPTIONS, group=test_group) - - self.assertTrue(CONF.test.should_pass) - - -if __name__ == '__main__': - unittest.main() diff --git a/barbican/worker/simple.py b/barbican/worker/simple.py index fe44ed2b5..1a65b9199 100644 --- a/barbican/worker/simple.py +++ b/barbican/worker/simple.py @@ -17,9 +17,9 @@ Simple Worker API implementation. """ from barbican.queue.resources import StartCSRMessage -import barbican.openstack.common.log as logging +from barbican.common import utils -LOG = logging.getLogger(__name__) +LOG = utils.getLogger(__name__) class StartCSRProcessor(object): diff --git a/bin/barbican-api b/bin/barbican-api index 517986d38..d2e1e41f6 100755 --- a/bin/barbican-api +++ b/bin/barbican-api @@ -5,7 +5,7 @@ PKG=barbican # For local development, set VENV_PYTHON equal to the path to your virtual environment's site-packages location -VENV=.venv_barbican +VENV=.venv VENV_PYTHON=./$VENV/lib/python2.7/site-packages PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/etc/$PKG:$PATH diff --git a/etc/barbican/barbican-api.conf b/etc/barbican/barbican-api.conf index 8980f001a..0c7989810 100644 --- a/etc/barbican/barbican-api.conf +++ b/etc/barbican/barbican-api.conf @@ -13,7 +13,7 @@ bind_port = 9292 # Log to this file. Make sure you do not set the same log # file for both the API and registry servers! -log_file = /var/log/barbican/api.log +#log_file = /var/log/barbican/api.log # Backlog requests when creating socket backlog = 4096 @@ -87,4 +87,5 @@ scrub_time = 43200 scrubber_datadir = /var/lib/barbican/scrubber [test] -should_pass = true \ No newline at end of file +should_pass = True +yada = foo diff --git a/setup.cfg b/setup.cfg index 07e69300c..41d62b4da 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,12 +27,12 @@ tag_svn_revision = 0 where=barbican nocapture=1 cover-erase=1 -with-xunit=1 -all-modules=1 -traverse-namespace=1 +#-with-xunit=1 +#-all-modules=1 +#-traverse-namespace=1 #with-coverage=1 cover-package=barbican -cover-inclusive=1 +#-cover-inclusive=1 # TBD: OpenStack stuff... # NOTE(jkoelker) To run the test suite under nose install the following