summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/api/controllers/v1/test_rules.py
blob: 23808e5955e5cc509a67f0e269f2a58fb473621b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2018 AT&T Intellectual Property
#
#   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 base64

from notario import decorators
from notario.validators import types
from oslo_log import log
import pecan

from conductor.api.controllers import error
from conductor.api.controllers import string_or_dict
from conductor.i18n import _, _LI
from oslo_config import cfg

CONF = cfg.CONF


LOG = log.getLogger(__name__)

class TestRuleBaseController(object):

    def test_rule(self, args):

        ctx = {}
        method = 'test_rule'
        client = pecan.request.controller
        response = client.call(ctx, method, args)

        return response

class TestRuleController(TestRuleBaseController):

    @classmethod
    def allow(cls):
        """Allowed methods"""
        return 'POST'

    @pecan.expose(generic=True, template='json')
    def index(self):
        """Catchall for unallowed methods"""
        message = _('The {} method is not allowed.').format(
            pecan.request.method)
        kwargs = {'allow': self.allow()}
        error('/errors/not_allowed', message, **kwargs)

    @index.when(method='OPTIONS', template='json')
    def index_options(self):
        """Options"""
        pecan.response.headers['Allow'] = self.allow()
        pecan.response.status = 204


    @index.when(method='POST', template='json')
    # @validate(CREATE_SCHEMA, '/errors/schema')
    def index_post(self):

        args = pecan.request.json

        if check_basic_auth():
            response = self.test_rule(args)
        if not response:
            error('/errors/server_error', _('Unable to release orders'))
        else:
            pecan.response.status = 201
            return response


def check_basic_auth():
    '''
    :return: boolean
    '''

    try:
        if pecan.request.headers['Authorization'] and  verify_user(pecan.request.headers['Authorization']):
            LOG.debug("Authorized username and password")
            plan = True
        else:
            plan = False
            auth_str = pecan.request.headers['Authorization']
            user_pw = auth_str.split(' ')[1]
            decode_user_pw = base64.b64decode(user_pw)
            list_id_pw = decode_user_pw.split(':')
            LOG.error("Incorrect username={} / password={}".format(list_id_pw[0],list_id_pw[1]))
    except:
        error('/errors/basic_auth_error', _('Unauthorized: The request does not provide any HTTP authentication (basic authetnication)'))

    if not plan:
        error('/errors/authentication_error', _('Invalid credentials: username or password is incorrect'))
    return plan


def verify_user(authstr):
    """
    authenticate user as per config file
    :param headers:
    :return boolean value
    """
    user_dict = dict()
    auth_str = authstr
    user_pw = auth_str.split(' ')[1]
    decode_user_pw = base64.b64decode(user_pw)
    list_id_pw = decode_user_pw.split(':')
    user_dict['username'] = list_id_pw[0]
    user_dict['password'] = list_id_pw[1]
    password = CONF.conductor_api.password
    username = CONF.conductor_api.username
    if username == user_dict['username'] and password == user_dict['password']:
        return True
    else:
        return False