Add support for basic authentication

Change-Id: I15e8ab531a5e82b211b65da312890579b5498024
This commit is contained in:
Julien Danjou 2016-12-20 15:34:02 +01:00
parent a5ecc4e143
commit a373d4a486
4 changed files with 45 additions and 3 deletions

View File

@ -41,6 +41,16 @@ in your environment::
export GNOCCHI_PROJECT_ID=<yourprojectid>
export GNOCCHI_ENDPOINT=http://urlofgnocchi
Basic authentication
~~~~~~~~~~~~~~~~~~~~
If you're using Gnocchi with basic authentication, export the following variables
in your environment::
export OS_AUTH_TYPE=gnocchi-basic
export GNOCCHI_USER=<youruserid>
export GNOCCHI_ENDPOINT=http://urlofgnocchi
OpenStack Keystone authentication
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -11,6 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import base64
import os
from keystoneauth1 import loading
@ -80,3 +81,32 @@ class GnocchiNoAuthLoader(loading.BaseLoader):
metavar="<gnocchi endpoint>"),
])
return options
class GnocchiBasicPlugin(plugin.BaseAuthPlugin):
"""Basic authentication plugin for Gnocchi."""
def __init__(self, user, endpoint):
self._user = user.encode('utf-8')
self._endpoint = endpoint
def get_headers(self, session, **kwargs):
return {'Authorization':
b"basic " + base64.b64encode(self._user + b":")}
def get_endpoint(self, session, **kwargs):
return self._endpoint
class GnocchiBasicLoader(loading.BaseLoader):
plugin_class = GnocchiBasicPlugin
def get_options(self):
options = super(GnocchiBasicLoader, self).get_options()
options.extend([
GnocchiOpt('user', help='User', required=True,
metavar="<gnocchi user>"),
GnocchiOpt('endpoint', help='Gnocchi endpoint',
dest="endpoint", required=True,
metavar="<gnocchi endpoint>"),
])
return options

View File

@ -23,9 +23,9 @@ from cliff import commandmanager
from keystoneauth1 import exceptions
from keystoneauth1 import loading
from gnocchiclient import auth
from gnocchiclient import benchmark
from gnocchiclient import client
from gnocchiclient import noauth
from gnocchiclient.v1 import archive_policy_cli
from gnocchiclient.v1 import archive_policy_rule_cli as ap_rule_cli
from gnocchiclient.v1 import capabilities_cli
@ -134,7 +134,8 @@ class GnocchiShell(app.App):
plugin = loading.register_auth_argparse_arguments(
parser=parser, argv=sys.argv, default="password")
if not isinstance(plugin, noauth.GnocchiNoAuthLoader):
if not isinstance(plugin, (auth.GnocchiNoAuthLoader,
auth.GnocchiBasicLoader)):
parser.add_argument(
'--endpoint',
default=os.environ.get('GNOCCHI_ENDPOINT'),

View File

@ -28,7 +28,8 @@ console_scripts =
gnocchi = gnocchiclient.shell:main
keystoneauth1.plugin =
gnocchi-noauth = gnocchiclient.noauth:GnocchiNoAuthLoader
gnocchi-noauth = gnocchiclient.auth:GnocchiNoAuthLoader
gnocchi-basic = gnocchiclient.auth:GnocchiBasicLoader
openstack.cli.extension =
metric = gnocchiclient.osc