Added possibilities to change user password via fuel-cli

Change-Id: I05dc09951dcf4be6d35c4f339f61091dcf56d55f
Implements: blueprint access-control-master-node
This commit is contained in:
Kamil Sambor
2014-07-09 17:17:27 +02:00
parent 86c2e5b2f0
commit 46b46b1659
4 changed files with 81 additions and 11 deletions

View File

@@ -30,6 +30,7 @@ from fuelclient.cli.actions.role import RoleAction
from fuelclient.cli.actions.settings import SettingsAction
from fuelclient.cli.actions.snapshot import SnapshotAction
from fuelclient.cli.actions.task import TaskAction
from fuelclient.cli.actions.user import UserAction
actions_tuple = (
ReleaseAction,
@@ -45,7 +46,8 @@ actions_tuple = (
NetworkAction,
TaskAction,
SnapshotAction,
HealthCheckAction
HealthCheckAction,
UserAction
)
actions = dict(

View File

@@ -0,0 +1,40 @@
# Copyright 2014 Mirantis, 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.
from fuelclient.cli.actions.base import Action
import fuelclient.cli.arguments as Args
from fuelclient.client import APIClient
class UserAction(Action):
"""Change password for user
"""
action_name = "user"
def __init__(self):
super(UserAction, self).__init__()
self.args = (
Args.get_new_password_arg(),
Args.get_change_password_arg("Change user password")
)
self.flag_func_map = (
("change-password", self.change_password),
)
def change_password(self, params):
"""To change user password:
fuel user change-password
"""
APIClient.update_own_password(params.newpass)

View File

@@ -181,6 +181,15 @@ def get_env_arg(required=False):
)
def get_new_password_arg():
return get_str_arg(
"newpass",
flags=("--new-pass",),
help="new_password",
required=False
)
def get_str_arg(name, **kwargs):
default_kwargs = {
"action": "store",
@@ -239,6 +248,10 @@ def get_check_arg(help_msg):
return get_set_type_arg("check", help=help_msg)
def get_change_password_arg(help_msg):
return get_boolean_arg("change-password", help=help_msg)
def get_name_arg(help_msg):
return get_str_arg("name", flags=("--env-name",), help=help_msg)

View File

@@ -36,7 +36,6 @@ class Client(object):
"LISTEN_PORT": "8000",
"KEYSTONE_USER": "admin",
"KEYSTONE_PASS": "admin",
"KEYSTONE_PORT": "5000",
}
if os.path.exists(path_to_config):
with open(path_to_config, "r") as fh:
@@ -57,6 +56,21 @@ class Client(object):
self.keystone_client = None
self.initialize_keystone_client()
@property
def auth_token(self):
if self.keystone_client:
if not self.keystone_client.auth_token:
self.keystone_client.authenticate()
return self.keystone_client.auth_token
return ''
@property
def user_id(self):
if self.keystone_client and not self.keystone_client.auth_token:
self.keystone_client.authenticate()
return self.keystone_client.user_id
return ''
def auth_status(self):
self.auth_required = False
if not self.test_mod:
@@ -64,6 +78,11 @@ class Client(object):
self.auth_required = json.loads(
request.read()).get('auth_required', False)
def update_own_password(self, new_pass):
if self.auth_token:
self.keystone_client.users.update_own_password(
self.password, new_pass)
def initialize_keystone_client(self):
if not self.test_mod and self.auth_required:
self.keystone_client = auth_client.Client(
@@ -71,7 +90,7 @@ class Client(object):
password=self.password,
auth_url=self.keystone_base,
tenant_name="admin")
self.keystone_client.authenticate()
self.keystone_client.session.auth = self.keystone_client
def debug_mode(self, debug=False):
self.debug = debug
@@ -84,14 +103,13 @@ class Client(object):
def delete_request(self, api):
"""Make DELETE request to specific API with some data
"""
token = self.keystone_client.auth_token if self.keystone_client else ''
self.print_debug(
"DELETE {0}".format(self.api_root + api)
)
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(self.api_root + api)
request.add_header('Content-Type', 'application/json')
request.add_header('X-Auth-Token', token)
request.add_header('X-Auth-Token', self.auth_token)
request.get_method = lambda: 'DELETE'
opener.open(request)
return {}
@@ -99,7 +117,6 @@ class Client(object):
def put_request(self, api, data):
"""Make PUT request to specific API with some data
"""
token = self.keystone_client.auth_token if self.keystone_client else ''
data_json = json.dumps(data)
self.print_debug(
"PUT {0} data={1}"
@@ -108,7 +125,7 @@ class Client(object):
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(self.api_root + api, data=data_json)
request.add_header('Content-Type', 'application/json')
request.add_header('X-Auth-Token', token)
request.add_header('X-Auth-Token', self.auth_token)
request.get_method = lambda: 'PUT'
return json.loads(
opener.open(request).read()
@@ -117,7 +134,6 @@ class Client(object):
def get_request(self, api, ostf=False):
"""Make GET request to specific API
"""
token = self.keystone_client.auth_token if self.keystone_client else ''
url = (self.ostf_root if ostf else self.api_root) + api
self.print_debug(
"GET {0}"
@@ -125,7 +141,7 @@ class Client(object):
)
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(url)
request.add_header('X-Auth-Token', token)
request.add_header('X-Auth-Token', self.auth_token)
return json.loads(
opener.open(request).read()
)
@@ -133,7 +149,6 @@ class Client(object):
def post_request(self, api, data, ostf=False):
"""Make POST request to specific API with some data
"""
token = self.keystone_client.auth_token if self.keystone_client else ''
url = (self.ostf_root if ostf else self.api_root) + api
data_json = json.dumps(data)
self.print_debug(
@@ -147,7 +162,7 @@ class Client(object):
'Content-Type': 'application/json'
}
)
request.add_header('X-Auth-Token', token)
request.add_header('X-Auth-Token', self.auth_token)
try:
response = json.loads(
urllib2.urlopen(request)