Commit Graph

299 Commits

Author SHA1 Message Date
Abhishek Kekane fd222f3128 Sort locations based on store weight
Related to blueprint store-weight

Change-Id: I2383a476cb7e79c7efecdf33203cff0b50ef3bbb
2023-08-01 14:16:08 +00:00
Pierre-Samuel Le Stang 480ea3825f Implement glance-download internal plugin
Add a new import method called glance-download
that implements a glance to glance download in
a multi-region cloud with a federated Keystone.

This method will copy the image data and
selected metadata to the target glance, checking
that the downloaded size match the "size" image
attribute in the source glance.

Implements: blueprint glance-download-import
Co-Authored-By: Victor Coutellier <victor.coutellier@gmail.com>
Change-Id: Ic51c5fd87caf04d38aeaf758ad2d0e2f28098e4d
2022-08-23 08:26:52 -07:00
Pranali Deore 3790cfd4a1 Remove dead code of auth and policy layers
In Xena we have mangaed to move all policy checks to API layer,
now removing the dead code from policy and authorization layer

NOTE: Some of the code is still being used from policy layer,
hence keeping it there only at this moment.

Change-Id: Ibee749cde20687d8c243cf84ae80b4de67d8ef3d
2022-06-14 10:15:55 +00:00
Cyril Roelandt 1962f47ed2 Delay string interpolations at logging calls
This was already fixed in 4889dc1814 but
we did not enforce this rule and reintroduced "bad" string
interpolations. This patch adds a hacking rule to prevent us from doing
this again in the future.

Change-Id: I96d9a157d3887286542859d67138ffdae5a589f1
2022-05-14 03:17:34 +02:00
Stephen Finucane 87ba56161b Remove six.moves.urllib usage
Change-Id: I71be65ef7b8f710e9317419d7b38559b39f461b0
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2022-01-27 14:54:05 +00:00
Stephen Finucane 9679ffc463 Remove six.moves.http_client usage
This is a rather beefy change due to the number of usages of this
import. The changes are trivial though.

Change-Id: I7badeeaca438b0291f4ed86670e7f217e6372c61
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2022-01-27 14:54:05 +00:00
Stephen Finucane 6bd7c188ee Remove six.text_type, six.binary_type usage
Change-Id: I2ed464202f8b645aed11490e111c61d3c7423c11
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2022-01-27 14:54:00 +00:00
Takashi Kajinami 0a5b511729 Use LOG.warning instead of deprecated LOG.warn
The LOG.warn method is deprecated[1] and the LOG.warning method should
be used instead.

[1] https://docs.python.org/3/library/logging.html#logging.warning

Change-Id: Ie0cac63dedf69728392f293f4551e495aebf4d40
2022-01-19 11:36:12 +09:00
Abhishek Kekane ab0c95da68 Check policies for image tasks information in API
This patch enforces policy checks required for fetching task
information for image in API layer.

Partially-Implements: blueprint policy-refactor
Depends-On: https://review.opendev.org/c/openstack/nova/+/688802

Change-Id: I8f59b8405d9c55b5f69294d3f9bd7bcbc064203f
2021-08-30 05:57:15 +00:00
Abhishek Kekane 6c87a18d4c Check policies for image import operation in API
This patch enforces policy checks required for importing/copying image
data to store in API layer.

Partially-Implements: blueprint policy-refactor

Change-Id: I18a5187d80bf76c0dc6f22dd8c96a8ffa0f46dc1
2021-08-24 20:09:24 +00:00
Zuul 646e15da44 Merge "Check add_image policy in the API" 2021-08-24 01:05:48 +00:00
Abhishek Kekane 434f881d8c Check policies for delete image for store in API
These change enforce policies required for deleting image from store
in API layer.

Partially-Implements: blueprint policy-refactor
Closes-Bug: #1939977

Change-Id: If390f04aef00fa4e6dc792658ae5fee9917c7d1f
2021-08-23 15:11:21 +00:00
Dan Smith de560b19e6 Check add_image policy in the API
Change-Id: I6d0b6bdef924a63f61f4a4df32fc2187f84a8c3f
Partially-Implements: blueprint policy-refactor
2021-08-19 11:37:48 -07:00
Abhishek Kekane c87bfbbcd7 Check delete_image policy in the API
Note that this adds a clause to the delete exception handler which
returns 404 if the user can't delete the image via policy, but leaves
the existing internal forbidden->403 handler because of how image
property protections work.

Partially-Implements: blueprint policy-refactor

Change-Id: I5f2a406e31b706906b71ea5ecb4f1a53674c97fa
2021-08-10 09:13:52 -07:00
Dan Smith ba37ea3227 Check get_image(s) in the API
This includes a change to catch Forbidden and convert to NotFound.
The previous Forbidden handler was not only correct (it shoud hide
the permissions error with "not found") but it was actually dead code,
since the DB was performing its own checks and would never raise
Forbidden.

This also includes a change of the default policy for get_images
to include the other states, like get_image does. I think this was
just an oversight in the original RBAC patches, which didn't matter
because they weren't really being honored strictly.

Partially implements: blueprint policy-refactor

Change-Id: I70100cd7f01da803e9740cea1f7ce7ae18ad6919
2021-08-04 07:56:05 -07:00
Dan Smith 3825d2111a Make image update check policy at API layer
This implements the proposal at the PTG that makes the image update
API our first one to do all policy checks at the API layer instead
of the lower ones.

There are still some things that have to be resolved, like the image
repo will still check get_image even though we don't want it to.

However, this adds a new v2/policy module to encapsulate these checks
and adds them into the various places of the update call. It also
makes our test policy for modify_image match that of our actual
default, which is a major step in making sure our tests actually run
with the policy we expect at runtime.

NOTE: db.ImageRepo.save() was raising NotFound in cases where
Forbidden was raised by _check_mutate_authorization(). This means
that we could not tell the difference at the higher layers between
an actual NotFound and the 404 generated by an auth failure.

The ImageAPIPolicy module will raise Forbidden for cases where an
operation is forbidden, but the image would be otherwise visible to
the user, and NotFound otherwise to obscure the existence of images
that the user can not otherwise see.

Partially-Implements: blueprint policy-refactor

Change-Id: I43dbc88a9f3fd4c6b2a10c2534ccee9283663282
2021-08-04 07:52:27 -07:00
Dan Smith 8d6ee6f822 Add image_count_uploading quota enforcement
This makes us enforce a quota on the total number of upload-related
image operations owned by a user.

Partially-implements: blueprint glance-unified-quotas

Change-Id: I2a28750aaf968e6a6324eb194d4280a640bfa5aa
2021-07-02 08:29:07 -07:00
Dan Smith a36666e2fe Add image_count_total quota enforcement
This makes us enforce a quota on the total number of (non-deleted)
images owned by a user.

Partially-implements: blueprint glance-unified-quotas

Change-Id: I8af124d9307263cd8289d0701fb9a745d13b1d56
2021-06-29 08:53:18 -07:00
Dan Smith 76c3011a64 Enforce keystone limits for image upload
This adds enforcement of the image_size_total keystone limit for
image upload and import. We simply check the quota before either of
these operations and refuse to proceed further if the user is over
their quota.

Note that this disables checking of the global size quota if keystone
quotas are enabled.

Note this includes another fix to couple unit tests that do not
properly pass context to the get_flow() method.

Partially-implements: blueprint glance-unified-quotas
Change-Id: Idf5f004b72436df1f9c77bb32d60b9be5ae77a68
2021-06-29 08:53:18 -07:00
Dan Smith 41e1cecbe6 Distributed image import
This implements distributed image import support, which addresses
the problem when one API worker has staged the image and another
receives the import request.

The general approach is that when a worker stages the image, it
records its self-reference URL in the image's extra_properties.  When
the import request comes in, any other host will proxy that HTTP
request direct to the original host instead of trying to do the import
itself.

Implements: blueprint distributed-image-import

Change-Id: I12daccb43c535b579c22f9d0742039b2ab42e929
2021-03-02 11:52:12 -08:00
Abhishek Kekane 281fadc15c New API /v2/images/{id}/tasks
Added new API /v2/images/{id}/tasks to show tasks associated with
image. This API will return list of tasks associated for valid image
else returns 404 not found if image is not present. This API also
initiates task scrubbing before returning tasks to user.

Implements: blueprint messages-api
Change-Id: Ib3cacb4dd4d75de32e539f8a3b48bdaa762e6d8e
2021-02-24 05:19:43 +00:00
Abhishek Kekane d54449af44 Utilize newly added tasks database fields
Made provision to pass image_id, request_id and user_id information
while creating new task.

Partially-Implements: blueprint messages-api
Change-Id: I299a222eeef81431143db3ba7fc08365c924326b
2021-02-24 05:17:43 +00:00
Abhishek Kekane 8f7ad9f45d Extract req.context in a variable
req.context has been used in various places in import_image and create
method, this patch extract that in a variable for furthrer use.

Change-Id: I9d09769f971477b4d9a9a3a7d458deb19d1f3c8c
Related: blueprint messages-api
2021-02-24 05:14:40 +00:00
Dan Smith 314e93abe4 Exclude os_glance namespace from property quota
Now that glance is using properties in the os_glance namespace for
internal purposes, we should exclude the counting of these from the
enforced image property quota.

Change-Id: I5fbe5eb12fd34e054137732a02c4cc5b687e7c77
Related-Bug: #1912001
2021-01-25 12:30:50 -08:00
Dan Smith 0c45de3ed8 Make os_glance namespace reserved
This adds a general mechanism for reserving property names that start
with os_glance. This has been done informally already, but no
enforcement was performed, except for specific keys on update. As a
result, banning these keys from create, for example, was missed and
users are able to set these keys during an POST /images operation.

Depends-On: https://review.opendev.org/c/openstack/nova/+/771234
Change-Id: I31b4dae018d52ead773db25472013d783066ee17
Closes-Bug: #1912001
2021-01-25 12:30:50 -08:00
Zuul 5126ca0242 Merge "[Trivial]Add missing print parameters in log messages" 2020-09-03 07:02:35 +00:00
zhufl 179d111c1f [Trivial]Add missing print parameters in log messages
This is to add missing print parameters in log message.

Change-Id: I300a3f19a0dfacb23903ac3e92571855ed32cd83
2020-08-31 11:25:05 +08:00
Dan Smith 552da84400 Cleanup import status information after busting a lock
When we bust a lock, we now own the image for that time period
and may exclude the other task (if still running) from updating
the import status information. If not still running, we should
take responsibility of that cleanup since we know what task we
stole the lock from. We should, however, only do that if we
succeed in grabbing the lock to avoid racing with another thread
which might be trying to do the same thing.

Change-Id: Iff3dfbfcbfb956a06d77a144e5456bdb556c5a2c
2020-08-24 06:41:13 -07:00
Dan Smith 3f6e349d08 Implement time-limited import locking
This attempts to provide a time-based import lock that is dependent
on the task actually making progress. While the task is copying
data, the task message is updated, which in turn touches the task
updated_at time. The API will break any lock after 30 minutes of
no activity on a stalled or dead task. The import taskflow will
check to see if it has lost the lock at any point, and/or if its
task status has changed and abort if so.

The logic in more detail:

1. API locks the image by task-id before we start the task thread, but
   before we return
2. Import thread will check the task-id lock on the image every time it
   tries to modify the image, and if it has changed, will abort
3. The data pipeline will heartbeat the task every minute by updating
   the task.message (bonus: we get some status)
4. If the data pipeline heartbeat ever finds the task state to be changed
   from the expected 'processing' it will abort
5. On task revert or completion, we drop the task-id lock from the image
6. If something ever gets stuck or dies, the heartbeating will stop
7. If the API gets a request for an import where the lock is held, it
   will grab the task by id (in the lock) and check the state and age.
   If the age is sufficiently old (no heartbeating) and the state is
   either 'processing' or terminal, it will mark the task as failed,
   steal the lock, and proceed.

Lots of logging throughout any time we encounter unexpected situations.

Closes-Bug: #1884596
Change-Id: Icb3c1d27e9a514d96fca7c1d824fd2183f69d8b3
2020-08-24 06:41:13 -07:00
Erno Kuvaja e1f0e94b90 Add "stores" to disallowed properties
Stores is image property which API uses to indicate which
stores (store IDs) contains the image. This also can be
set by user making it very confusing and potentially
catastrophic breaking for consumers.

This patch prevents that to happen.

Depends-on: https://review.opendev.org/#/c/744024/
Change-Id: I4eca092bd0a7cce1d6bbbd30685f4643cb4e7d1c
Closes-Bug: #1889676
2020-07-30 19:30:17 +00:00
Dan Smith 16a5431c66 Make glance-api able to do async tasks in WSGI mode
This teaches glance-api how to do async threading things when it is
running in pure-WSGI mode. In order to do that, a refactoring of things
that currently depend on eventlet is required.

It adds a [wsgi]/task_pool_threads configuration knob, which is used
in the case of pure-WSGI and native threads to constrain the number
of threads in that pool (and thus the task parallelism). This will
allow tuning by the operator, but also lets us default that to just
a single thread in the backport of these fixes so that we can avoid
introducing a new larger footprint in the backport unexpectedly.

Partial-Bug: #1888713
Depends-On: https://review.opendev.org/#/c/742047/
Change-Id: Ie15028b75fb8518ec2b0c0c0386d21782166f759
2020-07-24 11:13:45 -07:00
Dan Smith ee8a69d506 Add a policy knob for allowing non-owned image copying
This adds a copy_image policy knob which can be used to grant users
the ability to copy images other than their own using the new
functionality just added to the api_image_import task. By default,
only admins are allowed to do this.

A functional test modification is included to show that users can
be granted permission to do this based on something like the
"public visibility" attribute of an image.

Change-Id: Idebf66e2944bcddb7a5c76b81e47c654b401c2a8
2020-07-15 12:59:13 -07:00
Zuul 9c66f46a14 Merge "Improve lazy loading mechanism for multiple stores" 2020-07-14 16:17:03 +00:00
Dan Smith 2fd0c25733 Make import task capable of running as admin on behalf of user
This makes the api_image_import task capable of running as an admin on
behalf of a user if so authorized by the API. It includes a new object
called ImportActionWrapper which provides a bundle of utility methods
which can be run either against the user-authorized or admin-authorized
ImageRepo passed in from the API. It encapsulates all the actions
we are able and willing to run as an admin for the user.

This is currently not drivable by the API because the policy check is
still statically defined as "admin or owner" but this change is
offered without any needed modification to the functional tests to
prove that it does not regress existing functionality. The following
patch will introduce a more robust knob for allowing users to do this,
and it brings the functional test changes with it.

Change-Id: Iac75956e314ec6f41db18430486bd8be9754e780
2020-07-10 08:59:54 -07:00
Abhishek Kekane ab0e5268a9 Improve lazy loading mechanism for multiple stores
Glance has a facility lazy loading for legacy images which will be called
on get/list api calls to add store information in image's location metadata
based on location URL of image. Even if admin decides to change the store
names in glance-api.conf same will also be updated in location metadata
for all images related to that particular store. Current implementation of
legacy image performs this operation on each get/list call as location metadata
is not getting updated in database or it doesn't handle to perform store name
check in glance-api.conf.

Improvements done:
1. Save updated location metadata information in database permenantly
2. Add logic to perform lazy loading only if store information is not present
in location metadata or store present in location metadata is not defined in
glance's enbaled_backends configuration option.

Change-Id: I789fa7adfb459e7861c90a51f418a635c0c22244
Closes-Bug: #1886374
2020-07-06 07:49:31 +00:00
Dan Smith c930638fcf Check authorization before import for image
Right now we only check to see if the user can see the image before
we kick off an import operation. However, that will never work unless
the user is the *owner* of the image (or an admin) which means we
return a 202 to the API caller and then the task fails immediately.

This change makes us check that authorization up front and return an
appropriate error to the user so they know it failed, and avoid
starting a task destined for failure.

Note that there was already a check for a Forbidden result when calling
the import API. However, that used a context.owner=None which could never
happen in reality. A more suitable check would have been to use a context
with a different real owner, but it turns out that the task creation
would have succeeded in that case as well. This test is changed to use
an alternate owner and ensure that we get the forbidden result from the
new check immediately.

Change-Id: I385f222c5e3b46978b40bdefdc28fcb20d9c67d3
Closes-Bug: #1884587
2020-06-30 10:24:45 -07:00
Abhishek Kekane 55b7c86ecf Fix multiple image imports if boolean input passed as string
If user passes 'all_stores_must_succeed' as 'false' in copy-image, multiple
image imports request body then it does not work as expected. Expected is
to skip the failure store and continue copying/importing image to other
stores but instead it stops execution of task and revert it and deletes
the image data copied/imported to previois stores.

Raised 400 BadRequest if 'all_stores_must_succeed' and 'all_stores' are
other thatn than boolean values.

NOTE: Documentation clearly suggest that we expect boolean values for
'all_stores_must_succeed' and 'all_stores'

Closes-Bug: #1871588
Change-Id: I5118489284fea007f8f29663581221b07a575cf3
2020-04-08 19:07:58 +00:00
Dirk Mueller 595c1b17ff
Raise hacking to latest 2.0.0 release
We were capped at a very old version of hacking. Hacking itself caps the
various linters it uses to remain consistent, so our pep8 job was not
checking quite a bit that current versions have added.

This raises that limit to the latest to get up to the level of other
projects and addresses the errors the updated linters uncovered.

Change-Id: I89a9d73fbd59606a649e26077acebc5c42873d67
Co-authored-by: Sean McGinnis <sean.mcginnis@gmail.com>
2020-03-27 14:11:08 -05:00
Erno Kuvaja f267bd6cde Add possibility to delete image from single store
This change introduces new 'v2/stores/<store_id>/<image_id>'
endpoint that accepts 'DELETE' method request. Once successful
the request will delete the image <image_id>'s location that
matches the store <store_id>. If the store is not read-only
or return image in use exception the image data will be
deleted. In the case of read-only store, the location will
be removed and if the image in use is raised, the call will
fail.

bp: delete-from-store

Co-authored-by: Brian Rosmaita <rosmaita.fossdev@gmail.com>

Change-Id: I1cb45026489a96a283b82e8e7efc9975c181fceb
2020-03-13 14:46:13 +00:00
Abhishek Kekane 1754c9e2b0 Copy existing image in multiple stores
Added new import method 'copy-image' which will copy existing image into
specified list of stores. Introduced additional task which will serve
as internal plugin which will allow copying existing image into staging
area and then this data will be uploaded to specified stores via regula
import flow.

NOTE: This new import method 'copy-image' is only supported if multiple
stores are enabled in deployment.

APIImpact
Implements: blueprint copy-existing-image
Change-Id: I13eaab7ab013f44ce18465bdbdbe8052942570ff
2020-02-12 05:32:46 +00:00
Grégoire Unbekandt 92492cf504 Add ability to import image into multi-stores
The import image api now supports a list of stores to import data into.
This list can be specified through a new "stores" field that has been
added to the request body.
During import stage, Glance iterates overs this list and send the data
to each store one by one.
If an invalid backend is requested by the user, an exception is raised.
If an errors occurs during verify, already pushed data is removed and
image state is unchanged.

Change-Id: Id3ac19488c0a693d7042be4a3c83f3b9f12313d0
Implements: blueprint import-multi-stores
2020-02-10 09:39:01 +01:00
Abhishek Kekane 5d15f07371 Staging area not cleared if image is deleted while importing
If multiple stores configured in glance and Image is deleted while import
operation is in progress then image data stays in staging area
(filesystem backend) and there is no other way than clearing it
manually.

Modified delete method to delete the data from staging area if image is
deleted while import operation is in progress.

Change-Id: Ib58accd6514e589dccde57fe063815b1ab1ce496
Closes-Bug: #1855417
2020-01-03 05:56:30 +00:00
Abhishek Kekane 6dba83ba3a Rethinking filesystem access
In Rocky multiple backend support is added as experimental feature. In
order to take advantage of this feature it is decided to deprecate
work_dir and node_staging_uri configuration options
and reserve two filesystem stores 'os_glance_tasks_store' and
'os_glance_staging_store', which can be used to get rid of initializing
store via internal functions.

These internal stores are considered "reserved stores" by Glance.
For the time being, these are hard-coded as filesystem stores.  The
store prefix 'os_glance_' is reserved for internal Glance use and
the glance-api service will refuse to start if a store with this
prefix is included in the enabled_backends config option in
glance-api.conf.

NOTE: Because there are no sensible default values for the location
of the datadir for each of these stores, the operator must define
'os_glance_tasks_store' and 'os_glance_staging_store' in
glance-api.conf configuration file as shown below.

[os_glance_tasks_store]
filesystem_store_datadir = /var/lib/glance/tasks_work_dir/

[os_glance_staging_store]
filesystem_store_datadir = /var/lib/glance/staging/

Each filesystem store must have a unique datadir.

Depends-On: https://review.openstack.org/#/c/639765/
Implements: blueprint rethinking-filesystem-access
Change-Id: I86ec513c5fc653dbb97b79d953d8430f014e684f
2019-10-01 09:53:48 +00:00
Zuul d9f4451828 Merge "Make location API compatible with multiple store" 2019-09-25 14:22:28 +00:00
Zuul e475581c72 Merge "Delete secret key on image deletion" 2019-09-06 13:49:25 +00:00
Cyril Roelandt b190a39a28 Delete secret key on image deletion
We add two extra properties for images:
- cinder_encryption_key_id, which stores the encryption key id;
- cinder_encryption_key_deletion_policy, which states whether the secret
  key should be deleted on image deletion.

This feature uses the Castellan key manager, and will therefore work
with all its supported backends.

Implements: blueprint barbican-secret-deletion-support
DocImpact

Change-Id: Iacd0b3785ad4cdd06961e6d11967775806e009ff
2019-09-05 03:16:39 +02:00
Abhishek Kekane 4e070fd6e0 Make location API compatible with multiple store
In case of Multiple store while adding location to image if backend store is
not provided it returns 400 BadRequest saying 'Invalid location'.

Made changes to loaction API to findout the store from location URI
and add it as a backend store to the location metadata.

DocImpact
Closes-Bug: #1802587
Co-Authored-By: Victor Coutellier <victor.coutellier@gmail.com>
Change-Id: If6d0348346d2086a2500b0012a0e81e80cea7395
2019-08-08 04:54:29 +00:00
Abhishek Kekane 0e55ad71a8 Change location metadata key 'backend' to 'store'
As a part of vocabulary correction, changed the location
metadata key name from 'backend' to 'store'. Modified
corresponding tests as well.

bp:multi-store-vocabulary-correction
Change-Id: Iae3503cba6be362b372e1fc3e75c2ddb1e99b763
2019-08-08 04:52:51 +00:00
Abhishek Kekane 7f74a92338 Image deletion returns 500 if 'file' store is not enabled
When image import fails during image is uploading from staging area,
image remains in uploading state and data remains in staging area. In
this scenario if 'file' store is not enabled then while deleting the
image glance-api returns 500 status code with error 'file' scheme is
Unknwon.

Used os module to unlink the file present in staging area explicitly to
delete the data from staging area.

Change-Id: I57dcd6b18b0039e824e8adbe77de59079360a34f
Closes-Bug: #1836140
2019-07-22 05:16:46 +00:00
Thierry Carrez 84c8f9e3ff Quiece 'invalid escape sequence' deprecation msg
Glance tests are currently too verbose for subunit and make
them randomly fail under python 3.

Using raw strings should avoid the DeprecationWarnings around
'invalid escape sequence' being used with the re module.

Change-Id: I14c381ac4b17ca4c5755ed78c55dc44362ab37ef
2019-04-03 17:09:47 +02:00