예제 #1
0
def get_stackn_config():
    # if not os.path.exists(os.path.expanduser('~/.scaleout/stackn.json')):
    #     print('You need to setup STACKn first.')
    #     login()

    stackn_config, load_status = load_from_file('stackn', os.path.expanduser('~/.scaleout'))
    if not load_status:
        print('Failed to load stackn config file.')
        print('You need to setup STACKn first.')
        login()
        stackn_config, load_status = load_from_file('stackn', os.path.expanduser('~/.scaleout'))
        # return None

    return stackn_config, load_status
예제 #2
0
파일: auth.py 프로젝트: hamzaimran08/stackn
def get_token(client_id='studio-api', realm='STACKn', secure=True):
    # stackn_config, load_status = load_from_file('stackn', os.path.expanduser('~/.scaleout/'))
    # if not load_status:
    #     print('Failed to load stackn config file.')
    #     return None
    stackn_config, load_status = get_stackn_config()

    active_dir = os.path.expanduser('~/.scaleout/') + stackn_config['active']
    token_config, load_status = load_from_file('user', active_dir)
    if not load_status:
        print('Failed to load user config file.')
        return None

    access_token = token_config['access_token']

    try:
        public_key_full = '-----BEGIN PUBLIC KEY-----\n' + token_config[
            'public_key'] + '\n-----END PUBLIC KEY-----'
        access_token_json = jwt.decode(access_token,
                                       public_key_full,
                                       algorithms='RS256',
                                       audience='studio-api')
        # print('Token valid for user '+access_token_json['preferred_username'])
    except:
        # Try to refresh token
        token_url = os.path.join(
            token_config['keycloak_url'],
            'auth/realms/{}/protocol/openid-connect/token'.format(realm))
        req = {
            'grant_type': 'refresh_token',
            'client_id': client_id,
            'refresh_token': token_config['refresh_token']
        }
        res = requests.post(token_url, data=req, verify=secure)
        resp = res.json()
        if 'access_token' in resp:
            access_token = resp['access_token']
            refresh_token = resp['refresh_token']
            write_tokens(stackn_config['active'], access_token, refresh_token,
                         token_config['public_key'],
                         token_config['keycloak_url'],
                         token_config['studio_url'])
            # return res['access_token'], res['refresh_token'], public_key
        else:
            print('Failed to authenticate with token, please login again.')
            print(res.text)
            access_token = login(deployment=stackn_config['active'],
                                 keycloak_host=token_config['keycloak_url'],
                                 studio_host=token_config['studio_url'],
                                 secure=secure)

    return access_token, token_config
예제 #3
0
    def __init__(self, config=None):
        self.found_project = False

        self.stackn_config, load_status = sauth.get_stackn_config()
        if not load_status:
            print('Failed to load stackn config')

        self.project = []
        self.project_slug = []
        active_dir = self.stackn_config['active']
        if 'active_project' in self.stackn_config:
            project_dir = os.path.expanduser('~/.scaleout/' + active_dir +
                                             '/projects')
            self.project, load_status = load_from_file(
                self.stackn_config['active_project'], project_dir)
            if load_status:
                self.found_project = True
                self.project_slug = self.project['slug']
                # else:
                #     print('Could not load project config for '+self.stackn_config['active_project'])
            else:
                print('You must set an active valid project.')

        self.secure_mode = True
        if 'secure' in self.stackn_config:
            self.secure_mode = self.stackn_config['secure']
        if self.secure_mode == False:
            print("WARNING: Running in insecure mode.")
            import urllib3
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # self.project = self.get_projects({'slug': self.project_slug})
        if not self.project:
            print('Did not find active project in config')
            self.project_id = -1
        else:
            self.project_id = self.project['id']
            self.project_slug = self.project['slug']

        self.access_token, self.token_config = sauth.get_token(
            secure=self.secure_mode)
        if self.token_config['studio_url'][-1] == '/':
            self.token_config['studio_url'] = self.token_config[
                'studio_url'][:-1]
        self.api_url = urljoin(self.token_config['studio_url'], '/api')
        self.auth_headers = {
            'Authorization': 'Token {}'.format(self.access_token)
        }
        # Fetch and set all active API endpoints
        self.get_endpoints()
예제 #4
0
def get_remote_config():
    stackn_config, load_status = get_stackn_config()
    if not load_status:
        print('Failed to load STACKn config.')
        return [], False

    active_dir = stackn_config['active']
    if 'active_project' in stackn_config:
        active_path = os.path.expanduser('~/.scaleout/'+active_dir)
        remote_config, load_status = load_from_file('user', active_path)
        return remote_config, load_status
    else:
        print('No active project: Create a new project or set an existing project.')
        return [], False
예제 #5
0
def write_stackn_config(updated_values):
    dirpath = os.path.expanduser('~/.scaleout/')
    if os.path.exists(dirpath+'stackn.json'):
        stackn_config, load_status = load_from_file('stackn', dirpath)
        if not load_status:
            print('Failed to load stackn config (~/.scaleout/stackn.json)')
            return False
    else:
        stackn_config = dict()

    for key, value in updated_values.items():
        stackn_config[key] = value
    
    status = dump_to_file(stackn_config, 'stackn', dirpath)
    if not status:
        logger.info('Failed to update config -- could not write to file.')
예제 #6
0
    def __init__(self, config=None):
        self.found_project = False
        self.access_token, self.token_config = sauth.get_token()
        self.api_url = urljoin(self.token_config['studio_url'], '/api')
        self.auth_headers = {
            'Authorization': 'Token {}'.format(self.access_token)
        }

        # Fetch and set all active API endpoints
        self.get_endpoints()

        self.stackn_config, load_status = sauth.get_stackn_config()
        if not load_status:
            print('Failed to load stackn config')

        self.project = []
        self.project_slug = []
        active_dir = self.stackn_config['active']
        if 'active_project' in self.stackn_config:
            project_dir = os.path.expanduser('~/.scaleout/' + active_dir +
                                             '/projects')
            self.project, load_status = load_from_file(
                self.stackn_config['active_project'], project_dir)
            if load_status:
                self.found_project = True
                self.project_slug = self.project['slug']
                # else:
                #     print('Could not load project config for '+self.stackn_config['active_project'])
            else:
                print('You must set an active valid project.')
        # self.project = self.get_projects({'slug': self.project_slug})
        if not self.project:
            print('Did not find existing config')
            self.project_id = -1
        else:
            self.project_id = self.project['id']
            self.project_slug = self.project['slug']