Skip to content

Commit 1e7d917

Browse files
authored
feat: Add option to use custom value when creating API key (#270)
* Support user-defined API keys * Add tests for user-defined API key
1 parent 60ee277 commit 1e7d917

4 files changed

Lines changed: 11 additions & 7 deletions

File tree

alertaclient/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def delete_heartbeat(self, id):
265265
return self.http.delete('/heartbeat/%s' % id)
266266

267267
# API Keys
268-
def create_key(self, username, scopes=None, expires=None, text='', customer=None):
268+
def create_key(self, username, scopes=None, expires=None, text='', customer=None, **kwargs):
269269
data = {
270270
'user': username,
271271
'scopes': scopes or list(),
@@ -274,6 +274,8 @@ def create_key(self, username, scopes=None, expires=None, text='', customer=None
274274
}
275275
if expires:
276276
data['expireTime'] = DateTime.iso8601(expires)
277+
if kwargs.get('key'):
278+
data['key'] = kwargs['key']
277279
r = self.http.post('/key', data)
278280
return ApiKey.parse(r['data'])
279281

alertaclient/commands/cmd_key.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55

66

77
@click.command('key', short_help='Create API key')
8+
@click.option('--api-key', '-K', help='User-defined API Key. [default: random string]')
89
@click.option('--username', '-u', help='User (Admin only)')
910
@click.option('--scope', 'scopes', multiple=True, help='List of permissions eg. admin:keys, write:alerts')
1011
@click.option('--duration', metavar='SECONDS', type=int, help='Duration API key is valid')
1112
@click.option('--text', help='Description of API key use')
1213
@click.option('--customer', metavar='STRING', help='Customer')
1314
@click.option('--delete', '-D', metavar='ID', help='Delete API key using ID or KEY')
1415
@click.pass_obj
15-
def cli(obj, username, scopes, duration, text, customer, delete):
16+
def cli(obj, api_key, username, scopes, duration, text, customer, delete):
1617
"""Create or delete an API key."""
1718
client = obj['client']
1819
if delete:
1920
client.delete_key(delete)
2021
else:
2122
try:
2223
expires = datetime.utcnow() + timedelta(seconds=duration) if duration else None
23-
key = client.create_key(username, scopes, expires, text, customer)
24+
key = client.create_key(username, scopes, expires, text, customer, key=api_key)
2425
except Exception as e:
2526
click.echo('ERROR: {}'.format(e), err=True)
2627
sys.exit(1)

tests/integration/test_keys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def test_key(self):
2323
self.assertEqual(api_key.text, 'Updated Ops API Key')
2424

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

2930
api_keys = self.client.get_keys(query=[('user', 'key@alerta.io')])
3031
self.assertEqual(len(api_keys), 2)

tests/unit/test_keys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def setUp(self):
2828
"type": "read-write",
2929
"user": "johndoe@example.com"
3030
},
31-
"key": "BpSG0Ck5JCqk5TJiuBSLAWuTs03QKc_527T5cDtw",
31+
"key": "demo-key",
3232
"status": "ok"
3333
}
3434
"""
3535

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

0 commit comments

Comments
 (0)