MCPcopy Create free account
hub / github.com/StackStorm/st2 / put

Method put

st2api/st2api/controllers/v1/auth.py:182–223  ·  view source on GitHub ↗
(self, api_key_api, api_key_id_or_key, requester_user)

Source from the content-addressed store, hash-verified

180 return Response(json=api_key_create_response_api, status=http_client.CREATED)
181
182 def put(self, api_key_api, api_key_id_or_key, requester_user):
183 api_key_db = ApiKey.get_by_key_or_id(api_key_id_or_key)
184
185 permission_type = PermissionType.API_KEY_MODIFY
186 rbac_utils = get_rbac_backend().get_utils_class()
187 rbac_utils.assert_user_has_resource_db_permission(
188 user_db=requester_user,
189 resource_db=api_key_db,
190 permission_type=permission_type,
191 )
192
193 old_api_key_db = api_key_db
194 api_key_db = ApiKeyAPI.to_model(api_key_api)
195
196 try:
197 User.get_by_name(api_key_api.user)
198 except StackStormDBObjectNotFoundError:
199 user_db = UserDB(name=api_key_api.user)
200 User.add_or_update(user_db)
201
202 extra = {"username": api_key_api.user, "user": user_db}
203 LOG.audit('Registered new user "%s".' % (api_key_api.user), extra=extra)
204
205 # Passing in key_hash as MASKED_ATTRIBUTE_VALUE is expected since we do not
206 # leak it out therefore it is expected we get the same value back. Interpret
207 # this special code and empty value as no-change
208 if api_key_db.key_hash == MASKED_ATTRIBUTE_VALUE or not api_key_db.key_hash:
209 api_key_db.key_hash = old_api_key_db.key_hash
210
211 # Rather than silently ignore any update to key_hash it is better to explicitly
212 # disallow and notify user.
213 if old_api_key_db.key_hash != api_key_db.key_hash:
214 raise ValueError("Update of key_hash is not allowed.")
215
216 api_key_db.id = old_api_key_db.id
217 api_key_db = ApiKey.add_or_update(api_key_db)
218
219 extra = {"old_api_key_db": old_api_key_db, "new_api_key_db": api_key_db}
220 LOG.audit("API Key updated. ApiKey.id=%s." % (api_key_db.id), extra=extra)
221 api_key_api = ApiKeyAPI.from_model(api_key_db)
222
223 return api_key_api
224
225 def delete(self, api_key_id_or_key, requester_user):
226 """

Callers

nothing calls this directly

Calls 9

get_rbac_backendFunction · 0.90
UserDBClass · 0.90
get_by_key_or_idMethod · 0.80
get_utils_classMethod · 0.45
to_modelMethod · 0.45
get_by_nameMethod · 0.45
add_or_updateMethod · 0.45
from_modelMethod · 0.45

Tested by

no test coverage detected