Skip to content

Commit 6b4d4a5

Browse files
authored
Enhance SecurityTokenAuth initialization with additional arguments.
Enhance SecurityTokenAuth initialization with additional arguments.
2 parents b1e96e7 + 00ca9bd commit 6b4d4a5

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/oci_openai/oci_openai.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,63 @@ class OciSessionAuth(HttpxOciAuth):
182182
"""
183183

184184
def __init__(
185-
self, config_file: str = DEFAULT_LOCATION, profile_name: str = DEFAULT_PROFILE
185+
self,
186+
config_file: str = DEFAULT_LOCATION,
187+
profile_name: str = DEFAULT_PROFILE,
188+
**kwargs: Mapping[str, Any],
186189
):
190+
"""
191+
Initialize a Security Token-based OCI signer.
192+
193+
Parameters
194+
----------
195+
config_file : str, optional
196+
Path to the OCI configuration file. Defaults to `~/.oci/config`.
197+
profile_name : str, optional
198+
Profile name inside the OCI configuration file to use. Defaults to "DEFAULT".
199+
**kwargs : Mapping[str, Any]
200+
Optional keyword arguments:
201+
- `generic_headers`: Optional[Dict[str, str]]
202+
Headers to be used for generic requests.
203+
Default: `["date", "(request-target)", "host"]`
204+
- `body_headers`: Optional[Dict[str, str]]
205+
Headers to be used for signed request bodies.
206+
Default: `["content-length", "content-type", "x-content-sha256"]`
207+
208+
Raises
209+
------
210+
oci.exceptions.ConfigFileNotFound
211+
If the configuration file cannot be found.
212+
KeyError
213+
If a required key such as `"key_file"` is missing in the config.
214+
Exception
215+
For any other initialization errors.
216+
"""
217+
# Load OCI configuration and token
187218
config = oci.config.from_file(config_file, profile_name)
188219
token = self._load_token(config)
189-
private_key = oci.signer.load_private_key_from_file(config["key_file"]) # type: ignore
190-
self.signer = oci.auth.signers.SecurityTokenSigner(token, private_key)
220+
221+
# Load the private key from config
222+
key_path = config.get("key_file")
223+
if not key_path:
224+
raise KeyError(
225+
f"Missing 'key_file' entry in OCI config profile '{profile_name}'."
226+
)
227+
private_key = oci.signer.load_private_key_from_file(key_path)
228+
229+
# Optional signer header customization
230+
generic_headers = kwargs.pop("generic_headers", None)
231+
body_headers = kwargs.pop("body_headers", None)
232+
233+
additional_kwargs = {}
234+
if generic_headers:
235+
additional_kwargs["generic_headers"] = generic_headers
236+
if body_headers:
237+
additional_kwargs["body_headers"] = body_headers
238+
239+
self.signer = oci.auth.signers.SecurityTokenSigner(
240+
token, private_key, **additional_kwargs
241+
)
191242

192243
def _load_token(self, config: Mapping[str, Any]) -> str:
193244
token_file = config["security_token_file"]

0 commit comments

Comments
 (0)