Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion alertaclient/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def delete_heartbeat(self, id):
return self.http.delete('/heartbeat/%s' % id)

# API Keys
def create_key(self, username, scopes=None, expires=None, text='', customer=None):
def create_key(self, username, scopes=None, expires=None, text='', customer=None, **kwargs):
data = {
'user': username,
'scopes': scopes or list(),
Expand All @@ -274,6 +274,8 @@ def create_key(self, username, scopes=None, expires=None, text='', customer=None
}
if expires:
data['expireTime'] = DateTime.iso8601(expires)
if kwargs.get('key'):
data['key'] = kwargs['key']
r = self.http.post('/key', data)
return ApiKey.parse(r['data'])

Expand Down
5 changes: 3 additions & 2 deletions alertaclient/commands/cmd_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@


@click.command('key', short_help='Create API key')
@click.option('--api-key', '-K', help='User-defined API Key. [default: random string]')
@click.option('--username', '-u', help='User (Admin only)')
@click.option('--scope', 'scopes', multiple=True, help='List of permissions eg. admin:keys, write:alerts')
@click.option('--duration', metavar='SECONDS', type=int, help='Duration API key is valid')
@click.option('--text', help='Description of API key use')
@click.option('--customer', metavar='STRING', help='Customer')
@click.option('--delete', '-D', metavar='ID', help='Delete API key using ID or KEY')
@click.pass_obj
def cli(obj, username, scopes, duration, text, customer, delete):
def cli(obj, api_key, username, scopes, duration, text, customer, delete):
"""Create or delete an API key."""
client = obj['client']
if delete:
client.delete_key(delete)
else:
try:
expires = datetime.utcnow() + timedelta(seconds=duration) if duration else None
key = client.create_key(username, scopes, expires, text, customer)
key = client.create_key(username, scopes, expires, text, customer, key=api_key)
except Exception as e:
click.echo('ERROR: {}'.format(e), err=True)
sys.exit(1)
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def test_key(self):
self.assertEqual(api_key.text, 'Updated Ops API Key')

api_key = self.client.create_key(
username='key@alerta.io', scopes=[Scope.admin], text='Admin API Key'
username='key@alerta.io', scopes=[Scope.admin], text='Admin API Key', key='admin-key'
)
self.assertEqual(api_key.key, 'admin-key')

api_keys = self.client.get_keys(query=[('user', 'key@alerta.io')])
self.assertEqual(len(api_keys), 2)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def setUp(self):
"type": "read-write",
"user": "johndoe@example.com"
},
"key": "BpSG0Ck5JCqk5TJiuBSLAWuTs03QKc_527T5cDtw",
"key": "demo-key",
"status": "ok"
}
"""

@requests_mock.mock()
def test_key(self, m):
m.post('http://localhost:8080/key', text=self.key)
api_key = self.client.create_key(username='johndoe@example.com', scopes=[
'write:alerts', 'admin:keys'], text='Ops API Key')
api_key = self.client.create_key(username='johndoe@example.com', scopes=['write:alerts', 'admin:keys'],
text='Ops API Key', key='demo-key')
self.assertEqual(api_key.user, 'johndoe@example.com')
self.assertEqual(sorted(api_key.scopes), sorted(['write:alerts', 'admin:keys']))