rs_server_osam/utils/tools.md¶
A collection of varied and versatile utility functions
S3StorageConfigurationSingleton
¶
Singleton class designed to cache the contents of the S3 storage configuration CSV file in memory.
This avoids redundant and potentially expensive I/O operations when accessing bucket expiration and access rules. The configuration is typically loaded from a file ('/app/conf/bucket_expiration.csv') provided from a kubernetes configmap.
The singleton tracks the state of the configuration file using a 'fingerprint' to detect changes without re-reading the entire file if it hasn't been modified.
Attributes:
| Name | Type | Description |
|---|---|---|
instance |
The single instance of this class. |
|
file_lock |
A threading lock to ensure thread-safe access to the configuration. |
|
bucket_configuration_csv |
list[list]
|
The parsed content of the CSV file. |
config_file_path |
str
|
The path to the currently loaded configuration file. |
last_fingerprint |
tuple
|
A tuple containing (inode, mtime, size) of the configuration file used to detect updates. Thus, even if the file is a symlink, the fingerprint will be updated if the target file changes. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
get_file_fingerprint(config_file_path)
classmethod
¶
Returns a fingerprint (inode, mtime, size) for the given file. Using os.stat follows the symlink to the actual data file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file_path
|
str
|
Path to the config file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
(inode, mtime, size) |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
get_s3_bucket_configuration(config_file_path)
classmethod
¶
Returns content of given CSV configuration file as a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file_path
|
str
|
Path to the CSV config file. |
required |
Returns:
| Type | Description |
|---|---|
list[list]
|
list[list]: Content of the CSV file. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
load_csv_file_into_variable(config_file_path)
classmethod
¶
To load a CSV file into the singleton. If the file given is the same one as the one already in the singleton, and if this file hasn't changed since last execution, it will do nothing. In other cases, it will load the content of the file in the singleton and update the file name and modification date values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file_path
|
str
|
Path to the config file. |
required |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
create_description_from_template(keycloak_user, template)
¶
Applies the given Keycloak user name in the description, using the given template. The template must have a '%keycloak-user%' placeholder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keycloak_user
|
str
|
Keycloak user to set in the description. |
required |
template
|
(str, optionnal)
|
Template to use. Default is '## linked to keycloak user %keycloak-user%'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Description with correct user name. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
get_allowed_buckets(user, csv_rows)
¶
Get the allowed buckets for user from the csv configmap
get_configmap_user_values(user)
¶
Retrieves collection, eopf_type, and bucket access values for a given user
based on rules defined in the configmap_data.
The function filters configmap_data entries where the first element
(the user specifier) matches the provided user or the wildcard "*".
It then extracts and groups the second, third, and last values from the matching rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
str
|
The username to look up in the configmap rules. |
required |
Returns:
| Type | Description |
|---|---|
|
tuple[list, list, list]: Three lists corresponding to: - collections (list): Values from the second element in matched rules. - eopf_type (list): Values from the third element in matched rules. - bucket (list): Values from the last element in matched rules. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
get_keycloak_user_from_description(description, template)
¶
Returns the Keycloak user name included in the given description using its template. The template must have a '%keycloak-user%' placeholder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description
|
str
|
Description containing a Keycloak user name. |
required |
template
|
(str, optionnal)
|
Template to use. Default is '## linked to keycloak user %keycloak-user%'. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Keycloak user name or None if the conditions are not fulfilled |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
load_configmap_data()
¶
Loads the configmap data from the CSV file specified in the environment variable or default path.
Returns:
| Type | Description |
|---|---|
|
list[list]: Content of the CSV file as a list of rows. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
match_roles(roles)
¶
Matches parsed roles against a configuration map to determine S3 bucket access paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roles
|
list[tuple[str, str]]
|
List of tuples representing (owner, collection) pairs from parsed user roles. |
required |
Returns:
| Type | Description |
|---|---|
|
set[str]: Set of S3 access paths that match the given roles based on wildcards and configmap entries. |
Source code in docs/rs-server/services/osam/rs_server_osam/utils/tools.py
parse_role(role)
¶
Parses a Keycloak role string into owner, collection, and operation components.
This function expects the role to follow the format: <prefix>_<owner>:<collection>_<operation>.
It extracts and returns the owner, collection name, and operation (e.g., read, write, download).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Role string to be parsed. |
required |
Returns:
| Type | Description |
|---|---|
|
tuple[str, str, str] | None: A tuple (owner, collection, operation) if parsing is successful; otherwise, returns None on format error or exception. |