harborapi.ext.api
The harborapi.ext.api
module contains helper functions for performing common tasks with the Harbor API that are not included in the API spec, such as fetching all vulnerabilities for all artifacts in one or more projects or repositories, fetching all repositories in one or more projects, etc. These functions are implemented using the standard endpoint methods from harborapi.HarborAsyncClient
.
T = TypeVar('T')
module-attribute
ExceptionCallback = Callable[[List[Exception]], None]
module-attribute
get_artifact(client, project, repository, reference, with_report=True)
async
Fetch an artifact, optionally with a report.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
HarborAsyncClient
|
The client to use for the API call. |
required |
project |
str
|
The artifact's project. |
required |
repository |
str
|
The artifact's repository. |
required |
reference |
str
|
The artifact's reference. |
required |
with_report |
bool
|
Whether or not to fetch the artifact's report if it exists. |
True
|
Source code in harborapi/ext/api.py
get_artifacts(client, projects=None, repositories=None, tag=None, query=None, callback=None, max_connections=5, **kwargs)
async
Fetch all artifacts in all repositories. Optionally specify a list of repositories or projects to fetch from.
The Harbor API doesn't support getting all artifacts in all projects at once, so we have to retrieve all artifacts in each repository and then combine them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
HarborAsyncClient
|
The client to use for the API call. |
required |
projects |
Optional[List[str]]
|
List of projects to fetch artifacts from. |
None
|
repositories |
Optional[str]]
|
List of repositories to fetch artifacts from.
Can be either the full name or only the repository name:
|
None
|
tag |
Optional[str]
|
The tag to filter the artifacts by.
A shorthand for |
None
|
query |
Optional[str]
|
The query to filter the artifacts by.
Follows the same format as the Harbor API.
Has no effect if |
None
|
callback |
Optional[Callable[[List[Exception]], None]]
|
A callback function to handle exceptions raised by the API calls. The function takes a list of exceptions as its only argument. If not specified, exceptions are ignored. The function always fires even if there are no exceptions. |
None
|
max_connections |
Optional[int]
|
The maximum number of concurrent connections to open. |
5
|
**kwargs |
Any
|
Additional arguments to pass to the |
{}
|
Returns:
Type | Description |
---|---|
List[ArtifactInfo]
|
A list of ArtifactInfo objects, without the .report field populated. |
Source code in harborapi/ext/api.py
get_repositories(client, projects=None)
async
Fetch all repositories in all projects or a subset of projects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
HarborAsyncClient
|
The client to use for the API call. |
required |
projects |
Optional[List[str]]
|
The list of projects to fetch repositories from. If not specified, fetches all repos in all projects. |
None
|
Returns:
Type | Description |
---|---|
List[Repository]
|
A list of Repository objects. |
Source code in harborapi/ext/api.py
get_artifact_vulnerabilities(client, tags=None, projects=None, repositories=None, max_connections=5, callback=None, **kwargs)
async
Fetch all artifact vulnerability reports in all projects or a subset of projects, optionally filtered by tags.
The Harbor API doesn't support getting all artifacts in all projects at once, so we have to retrieve all artifacts in each repository and then combine them into a single list of ArtifactInfo objects afterwards.
Attempting to fetch all artifact vulnerability reports in all projects
simultaneously will likely DoS your harbor instance, and as such it is not advisable
to set max_connections
to a large value. The default value of 5 is a known safe value,
but you may need to experiment with your own instance to find the optimal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
HarborAsyncClient
|
The client to use for the API call. |
required |
tags |
Optional[List[str]]
|
The tag(s) to filter the artifacts by. |
None
|
projects |
Optional[List[str]]
|
The project(s) to fetch artifacts from. If not specified, all projects will be used. |
None
|
max_connections |
Optional[int]
|
The maximum number of concurrent connections to the Harbor API. If None, the number of connections is unlimited. WARNING: uncapping connections will likely cause a DoS on the Harbor server. |
5
|
callback |
Optional[Callable[[List[Exception]], None]]
|
A callback function to handle exceptions raised by the API calls. The function takes a list of exceptions as its only argument. If not specified, exceptions are ignored. The function always fires even if there are no exceptions. |
None
|
**kwargs |
Any
|
Additional arguments to pass to the |
{}
|
Returns:
Type | Description |
---|---|
List[ArtifactInfo]
|
A list of ArtifactInfo objects, where each object's |
Source code in harborapi/ext/api.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
run_coros(coros, max_connections)
async
Runs an iterable of coroutines concurrently and returns the results.
Given a max_connections
value, the number of concurrent coroutines is limited.
All coroutines are run with asyncio.gather(..., return_exceptions=True)
,
so the list of results can contain exceptions, which must be handled
by the caller.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coros |
Sequence[Awaitable[T]]
|
An iterable of coroutines to run. |
required |
max_connections |
Optional[int]
|
The maximum number of concurrent coroutines to run. |
required |
Returns:
Type | Description |
---|---|
List[T]
|
A list of results from running the coroutines, which may contain exceptions. |
Source code in harborapi/ext/api.py
handle_gather(results, callback=None)
Handles the returned values of an asyncio.gather()
call, handling
any exceptions and returning a list of the results with exceptions removed.
Flattens lists of results. TODO: toggle this?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
(Sequence[Union[T, Sequence[T], Exception]])
|
The results of an |
required |
callback |
Optional[Callable[[List[Exception]], None]]
|
A callback function to handle exceptions raised by the API calls. The function takes a list of exceptions as its only argument. If not specified, exceptions are ignored. The function always fires even if there are no exceptions. |
None
|
Returns:
Type | Description |
---|---|
List[T]
|
The list of results with exceptions removed. |
Source code in harborapi/ext/api.py
get_artifact_owner(client, artifact)
async
Get the full owner information for an artifact.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
HarborAsyncClient
|
The client to use for the API call. |
required |
artifact |
Union[Artifact, ArtifactInfo]
|
The artifact to get the owner for. |
required |
Returns:
Type | Description |
---|---|
UserResp
|
The full owner information for the artifact. |