Skip to content

Commit 456b3e0

Browse files
authored
Merge pull request #4542 from StackStorm/fix/st2-apikey-load-dup
Fix CLI 'st2 apikey load' idempotence and API '/api/v1/apikeys' not honoring 'ID' for the new record creation
2 parents f45200c + 097fc0d commit 456b3e0

File tree

5 files changed

+11
-2
lines changed

5 files changed

+11
-2
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Fixed
5959

6060
Reported by @johandahlberg (bug fix) #4533 4534
6161

62+
* Fix CLI ``st2 apikey load`` not being idempotent and API endpoint ``/api/v1/apikeys`` not
63+
honoring desired ``ID`` for the new record creation. #4542
64+
6265
2.10.0 - December 13, 2018
6366
--------------------------
6467

st2api/tests/unit/controllers/v1/test_auth_api_keys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def test_post_delete_key(self):
197197

198198
def test_post_delete_same_key_hash(self):
199199
api_key = {
200+
'id': '5c5dbb576cb8de06a2d79a4d',
200201
'user': 'herge',
201202
'key_hash': 'ABCDE'
202203
}
@@ -207,8 +208,9 @@ def test_post_delete_same_key_hash(self):
207208
# drop into the DB since API will be masking this value.
208209
api_key_db = ApiKey.get_by_id(resp1.json['id'])
209210

211+
self.assertEqual(resp1.json['id'], api_key['id'], 'PK ID of created API should match.')
210212
self.assertEqual(api_key_db.key_hash, api_key['key_hash'], 'Key_hash should match.')
211-
self.assertEqual(api_key_db.user, api_key['user'], 'Key_hash should match.')
213+
self.assertEqual(api_key_db.user, api_key['user'], 'User should match.')
212214

213215
resp = self.app.delete('/v1/apikeys/%s' % resp1.json['id'])
214216
self.assertEqual(resp.status_int, 204)

st2common/st2common/models/api/auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ def from_model(cls, model, mask_secrets=False):
143143

144144
@classmethod
145145
def to_model(cls, instance):
146+
# If PrimaryKey ID is provided, - we want to work with existing ST2 API key
147+
id = getattr(instance, 'id', None)
146148
user = str(instance.user) if instance.user else None
147149
key_hash = getattr(instance, 'key_hash', None)
148150
metadata = getattr(instance, 'metadata', {})
149151
enabled = bool(getattr(instance, 'enabled', True))
150-
model = cls.model(user=user, key_hash=key_hash, metadata=metadata, enabled=enabled)
152+
model = cls.model(id=id, user=user, key_hash=key_hash, metadata=metadata, enabled=enabled)
151153
return model
152154

153155

st2tests/st2tests/fixtures/generic/apikeys/apikey1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
id: 58e3f3330c0517062a3fda43
23
user: bill
34
key_hash: "ec81d4a56f5987b0ae1cff6e152459986e873d6604637fc70d85c0a0daf131b0a830ccd5b6454cc0c95c0ba6e6655933c993325eb3a28bc43af6c1d801a7c1e8" # 1234
45
metadata:

st2tests/st2tests/fixtures/generic/apikeys/apikey2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
id: 5c5ddd776cb8de530e0a1391
23
user: dilbert
34
key_hash: "17f858ea0bb108feaa91b8eee524c7382e0218ff541783d45996a1149d50dfde4bc19f2e6a591028a2ea08de4211893b246d4eda61dd3c9cf294a2405184ac4b" # 5678
45
metadata:

0 commit comments

Comments
 (0)