Skip to content

Commit f0fa263

Browse files
committed
Add support for Application Keys.
CC-5
1 parent fbfe50c commit f0fa263

10 files changed

+242
-24
lines changed

comodit_client/api/__init__.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Client(object):
9595
access to ComodIT entities.
9696
"""
9797

98-
def __init__(self, endpoint, username, password, insecure_upload = False):
98+
def __init__(self, endpoint, username, password, token, insecure_upload = False):
9999
"""
100100
Client constructor. In order to create a client, the URL to a ComodIT
101101
server's REST API (e.g. http://my.comodit.com/api) must be provided, in
@@ -107,9 +107,11 @@ def __init__(self, endpoint, username, password, insecure_upload = False):
107107
@type username: String
108108
@param password: A password
109109
@type password: String
110+
@param token: A token
111+
@type token: String
110112
"""
111113

112-
self._http_client = HttpClient(endpoint, username, password, insecure_upload)
114+
self._http_client = HttpClient(endpoint, username, password, token, insecure_upload)
113115
self._organizations = OrganizationCollection(self)
114116
self._flavors = FlavorCollection(self)
115117
self._app_store = AppStoreCollection(self)
@@ -375,3 +377,35 @@ def get_host(self, org_name, env_name, name):
375377
"""
376378

377379
return self.hosts(org_name, env_name).get(name)
380+
381+
382+
# Application Keys helpers
383+
384+
def application_keys(self, org_name):
385+
"""
386+
Instantiates the collection of environments associated to named
387+
organization.
388+
389+
@param org_name: The name of the organization owning requested collection.
390+
@type org_name: string
391+
@rtype: L{EnvironmentCollection}
392+
"""
393+
394+
return self.__get_unresolved_org(org_name).application_keys()
395+
396+
def __get_unresolved_application_key(self, org_name, token):
397+
return self.application_keys(org_name).new(token)
398+
399+
def get_application_key(self, org_name, token):
400+
"""
401+
Fetches an environment given the name of owning organization and its
402+
name.
403+
404+
@param org_name: The name of the organization owning requested platform.
405+
@type org_name: string
406+
@param name: The name of the environment.
407+
@type name: string
408+
@rtype: L{Environment}
409+
"""
410+
411+
return self.application_keys(org_name).get(token)

comodit_client/api/application_key.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# coding: utf-8
2+
"""
3+
Provides the classes related to application key entity: L{ApplicationKey}
4+
and L{ApplicationKeyCollection}.
5+
"""
6+
7+
from collection import Collection
8+
from comodit_client.api.entity import Entity
9+
10+
11+
class ApplicationKeyCollection(Collection):
12+
"""
13+
Collection of application keys. An application key collection is owned by an
14+
L{Organization}.
15+
"""
16+
17+
def _new(self, json_data = None):
18+
return ApplicationKey(self, json_data)
19+
20+
def new(self, token):
21+
"""
22+
Instantiates a new application key object.
23+
24+
@param name: The name of new application key.
25+
@type name: string
26+
@rtype: L{ApplicationKey}
27+
"""
28+
29+
key = self._new()
30+
key.token = token
31+
return key
32+
33+
def create(self, token):
34+
"""
35+
Creates a remote application key entity and returns associated local
36+
object.
37+
38+
@param name: The name of new application key.
39+
@type name: string
40+
@rtype: L{ApplicationKey}
41+
"""
42+
43+
key = self.new(token)
44+
key.create()
45+
return key
46+
47+
48+
class ApplicationKey(Entity):
49+
"""
50+
Application Key entity representation. An application key defines a token that can be used to authenticate
51+
and perform some action in a particular organization.
52+
"""
53+
54+
@property
55+
def organization(self):
56+
"""
57+
The name of the organization owning this environment.
58+
59+
@rtype: string
60+
"""
61+
62+
return self._get_field("organization")
63+
64+
@property
65+
def token(self):
66+
"""
67+
The token associated with this application key.
68+
69+
@rtype: string
70+
"""
71+
72+
return self._get_field("token")
73+
74+
@property
75+
def expiration_date(self):
76+
"""
77+
The expiration date associated with this application key.
78+
79+
@rtype: string
80+
"""
81+
82+
return self._get_field("expirationDate")
83+
84+
@property
85+
def group(self):
86+
"""
87+
The group associated with this application key.
88+
89+
@rtype: string
90+
"""
91+
92+
return self._get_field("group")
93+
94+
@property
95+
def creator(self):
96+
"""
97+
The creator associated with this application key. Its username is actually returned.
98+
99+
@rtype: string
100+
"""
101+
102+
return self._get_field("creator")
103+
104+
def _show(self, indent = 0):
105+
super(ApplicationKey, self)._show(indent)
106+
print " "*indent, "Token:", self.token
107+
print " "*indent, "Expiration date:", self.expiration_date
108+
print " "*indent, "Group:", self.group
109+
print " "*indent, "Creator:", self.creator

comodit_client/api/organization.py

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from comodit_client.api.audit import AuditLogCollection
1717
from comodit_client.api.purchased import PurchasedCollection
1818
from comodit_client.util.json_wrapper import JsonWrapper
19+
from comodit_client.api.application_key import ApplicationKeyCollection
1920

2021

2122
class User(JsonWrapper):
@@ -468,3 +469,13 @@ def get_purchased_dist(self, uuid):
468469

469470
return self.purchased_dists().get(uuid)
470471

472+
def application_keys(self):
473+
"""
474+
Instantiates the collection of application keys associated to this organization.
475+
476+
@return: The collection of application keys associated to this organization.
477+
@rtype: L{ApplicationKeyCollection}
478+
"""
479+
480+
return ApplicationKeyCollection(self.client, self.url + "applicationkeys/")
481+

comodit_client/config.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, message):
3636
@singleton
3737
class Config(object):
3838
"""
39-
Cortex client's configuration.
39+
ComodIT client's configuration.
4040
"""
4141

4242
_default_config = {
@@ -47,6 +47,7 @@ class Config(object):
4747
"api": "http://localhost:8000/api",
4848
"username": "admin",
4949
"password": "secret",
50+
"token": "",
5051
"vnc_viewer_call": "vinagre %h:%p"
5152
}
5253
}
@@ -98,6 +99,9 @@ def get_username(self, profile_name):
9899

99100
def get_password(self, profile_name):
100101
return self._get_value(profile_name, "password")
102+
103+
def get_token(self, profile_name):
104+
return self._get_value(profile_name, "token")
101105

102106
def get_vnc_viewer_call(self, profile_name):
103107
return self._get_value(profile_name, "vnc_viewer_call")
@@ -174,6 +178,7 @@ def _get_config_dict(self, config_path):
174178
[default]
175179
username = foologin
176180
password = foopass
181+
token = token
177182
api = url
178183
179184
becomes :
@@ -185,7 +190,8 @@ def _get_config_dict(self, config_path):
185190
"default": {
186191
"api": "http://localhost:8000/api",
187192
"username": "admin",
188-
"password": "secret"
193+
"password": "secret",
194+
"token": "token"
189195
}
190196
}
191197

comodit_client/control/abstract.py

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
# This software cannot be used and/or distributed without prior
88
# authorization from Guardis.
99

10-
import re
11-
1210
from comodit_client.util import globals
1311
from comodit_client.control.exceptions import ControllerException
1412
from comodit_client.control.doc import ActionDoc
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# control.environments - Controller for comodit Environments entities.
2+
# coding: utf-8
3+
#
4+
# Copyright 2010 Guardis SPRL, Liège, Belgium.
5+
# Authors: Laurent Eschenauer <[email protected]>
6+
#
7+
# This software cannot be used and/or distributed without prior
8+
# authorization from Guardis.
9+
10+
from comodit_client.control.organization_entity import OrganizationEntityController
11+
12+
class ApplicationKeysController(OrganizationEntityController):
13+
14+
_template = "application_key.json"
15+
16+
def __init__(self):
17+
super(ApplicationKeysController, self).__init__()
18+
self._doc = "Application keys handling."
19+
self._unregister("clone")
20+
21+
def _get_collection(self, org_name):
22+
return self._client.application_keys(org_name)
23+
24+
def _prune_json_update(self, json_wrapper):
25+
super(ApplicationKeysController, self)._prune_json_update(json_wrapper)
26+
json_wrapper._del_field("token")
27+
json_wrapper._del_field("creator")
28+
json_wrapper._del_field("organization")

comodit_client/dispatch.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
from rest.exceptions import ApiException
2020
from util import globals
2121
from util.editor import NotModifiedException
22-
import os
2322
import argparse
2423
import traceback
2524
import sys
26-
import textwrap
2725
import control.router
2826
from config import Config, ConfigException
2927
from api import Client
3028
from api.exceptions import PythonApiException
3129
from comodit_client.api.importer import ImportException
3230
from comodit_client.api.exporter import ExportException
3331
from comodit_client.control.store import AppStoreController, DistStoreController
32+
from comodit_client.control.application_keys import ApplicationKeysController
3433

3534
def run(argv):
3635
# entities
@@ -43,6 +42,7 @@ def run(argv):
4342
control.router.register(["hosts"], HostsController())
4443
control.router.register(["app-store"], AppStoreController())
4544
control.router.register(["dist-store"], DistStoreController())
45+
control.router.register(["application_keys"], ApplicationKeysController())
4646

4747
_parse(argv)
4848

@@ -60,6 +60,7 @@ def _get_value_options(parser):
6060
"--api",
6161
"--user",
6262
"--pass",
63+
"--token",
6364
"--templates",
6465
"--profile",
6566
"--completions",
@@ -86,6 +87,7 @@ def __get_parser(config):
8687
environments Environment defined within an organization
8788
hosts Host defined within an environment
8889
flavors Available flavors when creating a distribution
90+
application_keys Temporary access tokens related to an organization
8991
""")
9092

9193
parser.add_argument("entity", help = "An entity")
@@ -115,6 +117,7 @@ def __get_parser(config):
115117
parser.add_argument("--api", dest = "api", help = "endpoint for the API", default = None)
116118
parser.add_argument("--user", dest = "username", help = "username on comodit server", default = None)
117119
parser.add_argument("--pass", dest = "password", help = "password on comodit server", default = None)
120+
parser.add_argument("--token", dest = "token", help = "application key token", default = None)
118121
parser.add_argument("--templates", dest = "templates_path", help = "directory containing JSON templates", default = config.templates_path)
119122
parser.add_argument("--profile", dest = "profile_name", help = "Name of profile to use", default = None)
120123

@@ -200,7 +203,13 @@ def _parse(argv):
200203
password = config.get_password(globals.options.profile_name)
201204
globals.options.password = password
202205

203-
client = Client(api, username, password, globals.options.insecure)
206+
if globals.options.token:
207+
token = globals.options.token
208+
else:
209+
token = config.get_token(globals.options.profile_name)
210+
globals.options.token = token
211+
212+
client = Client(api, username, password, token, globals.options.insecure)
204213

205214
entity_args = [] + globals.options.subentities
206215

0 commit comments

Comments
 (0)