Пример #1
0
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import json
import datetime

from .blueprint import api

from ruggedpod_api import config
from ruggedpod_api.common import exception
from ruggedpod_api.services import auth

from flask import request, make_response
from flask import g as request_context

auth_enabled = config.get_attr('authentication')['enabled']


@api.before_request
def check_authentication():
    if not auth_enabled:
        return
    if request.path.endswith('/tokens') and request.method == 'POST':
        return
    token_key = 'X-Auth-Token'
    if token_key in request.cookies:
        token = request.cookies[token_key]
    else:
        if token_key in request.headers:
            token = request.headers[token_key]
        else:
Пример #2
0
    @staticmethod
    def list_bus():
        rc, stdout, stderr = utils.cmd(i2c['bus_lookup_command'])
        if rc != 0:
            raise exception.NoContent(reason='Cannot find any I2C bus')
        return stdout

    @staticmethod
    def detect(bus):
        rc, stdout, stderr = utils.cmd('i2cdetect -y %s' % bus)
        if rc != 0:
            raise exception.NoContent(reason='Cannot find any I2C bus')
        return stdout


i2c = config.get_attr('i2c')
i2c_discovery = dependency.lookup('i2c_bus_discovery')
ADCHelpers = dependency.lookup('adc_helpers')
ADCPi = dependency.lookup('adc')


def _read_config(session=None):
    if not session:
        session = db.session()
    config = {}
    for c in session.query(Config).filter(Config.category == 'i2c'):
        config[c.key] = c
    return config


def read_power_consumption(blade_id):
Пример #3
0
import datetime
import threading

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String, Boolean, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, sessionmaker

from ruggedpod_api import config
from ruggedpod_api.common import ssh
from ruggedpod_api.common.security import hash_password, generate_uuid

storage = config.get_attr('storage')

DBObject = declarative_base()


class User(DBObject):
    """
    Describe a user for the RuggedPOD API
    """

    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    firstname = Column(String)
    lastname = Column(String)
    username = Column(String(20), nullable=False, unique=True)
    password = Column(String)
    enabled = Column(Boolean)
Пример #4
0
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from lxml import etree
import time

from ruggedpod_api import config
from ruggedpod_api.common import dependency

reset_dict = config.get_attr('reset')
onoff_dict = config.get_attr('onoff')
short_press = config.get_attr('short_press')
long_press = config.get_attr('long_press')
serial_select_dict = config.get_attr('serial_select')
oil_pump_dict = config.get_attr('oil_pump')
i2c = config.get_attr('i2c')
consumption_dict = i2c['consumption']

ADCHelpers = dependency.lookup('adc_helpers')
ADCPi = dependency.lookup('adc')
adc = ADCPi(ADCHelpers().get_smbus(i2c['bus']),
            i2c['dac_power_consumption_addr'], None, 12)

GPIO = dependency.lookup('gpio')
Пример #5
0
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import datetime

from flask import Flask, request, make_response

from ruggedpod_api import config
from ruggedpod_api.common import exception
import ruggedpod_api.service_gpio as service
import ruggedpod_api.service_auth as auth


auth_enabled = config.get_attr('authentication')['enabled']

app = Flask(__name__)


@app.errorhandler(auth.AuthenticationFailed)
def handle_invalid_usage(error):
    return '', error.status_code


@app.before_request
def check_authentication():
    if not auth_enabled:
        return
    if request.path == '/token' and request.method == 'POST':
        return
Пример #6
0
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import json
import time
import base64
import hashlib

from Crypto import Random
from Crypto.Cipher import AES

from ruggedpod_api import config
from ruggedpod_api.common import exception


auth = config.get_attr('authentication')
users = auth['users']


class AuthenticationFailed(exception.RuggedpodException):
    msg_fmt = "Authentication failed"
    status_code = 401


class Singleton(type):
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args,
                                                                 **kwargs)
Пример #7
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
dependency injection utilities
"""

import sys
import os

from ruggedpod_api import config

di = config.get_attr('dependency_injection')
profile = di['profile']
dependencies = di['dependencies']


def import_module(import_str):
    """Import a module."""
    __import__(import_str)
    return sys.modules[import_str]


def lookup(name):
    """Get an object from its logical name"""
    dependency = dependencies[name][profile]
    import_str = dependency['import']
Пример #8
0
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from lxml import etree
import time
import mock

from ruggedpod_api import config
from ruggedpod_api.common import importutils


GPIO = importutils.try_import(
    "RPi.GPIO", default=mock.Mock(), warn="WARNING: RPi.GPIO could not be imported," " you are in MOCK MODE!"
)

attention_led_dict = config.get_attr("attention_led")
power_dict = config.get_attr("power")
consumption_dict = config.get_attr("consumption")
reset_dict = config.get_attr("reset")
onoff_dict = config.get_attr("onoff")
short_press = config.get_attr("short_press")
long_press = config.get_attr("long_press")
serial_select_dict = config.get_attr("serial_select")
oil_pump_dict = config.get_attr("oil_pump")


def init():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    # Set all led in Output
    for blade_id in attention_led_dict:
Пример #9
0
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import json
import time

from Crypto import Random

from ruggedpod_api import config
from ruggedpod_api.common import exception, security
from ruggedpod_api.services import users

auth = config.get_attr('authentication')


class AuthenticationFailed(exception.RuggedpodException):
    msg_fmt = "Authentication failed"
    status_code = 401


def get_token(username, password):

    try:
        user = users.find(username=username)
    except exception.NotFound:
        raise AuthenticationFailed()

    if not security.check_password(user.password, password):