Skip to content

Add TLS Validation #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pip. Only major versions will be released as tags on Github.

## [0.0.x](https://github.com/oras-project/oras-py/tree/main) (0.0.x)
- allow specification of server and client certificates (0.3.0)
- initialize headers variable in do_request (0.2.31)
- Make reproducible targz without mimetype (0.2.30)
- don't include Content-Length header in upload_manifest (0.2.29)
Expand Down
5 changes: 1 addition & 4 deletions oras/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ class AuthenticationException(Exception):
pass


def get_auth_backend(
name="token", session=None, insecure=False, tls_verify=True, **kwargs
):
def get_auth_backend(name="token", session=None, insecure=False, **kwargs):
backend = auth_backends.get(name)
if not backend:
raise ValueError(f"Authentication backend {backend} is not known.")
backend = backend(**kwargs)
backend.session = session or requests.Session()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kinda is a problem if someone does not pass in the session... Is this something that needs to be taken into account or can we not just make session a required argument here. Having ONE session which is pre-configured to use for all requests has the advantage that we can pass less arguments around settings like

self.session.cookies.set_policy(DefaultCookiePolicy(allowed_domains=[]))

cannot be forgotten

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one session and passing it forward from the client was the idea - see how it is created and passed forward here:

self.session: requests.Session = requests.Session()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the function get_auth_backend stay like this since it is correctly used for should session be made a required parameter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand - do you mean or should session be made required?

Copy link
Contributor Author

@Sojamann Sojamann May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either we leave it like it is now since it works fine the way it is currently used or we change it to

def get_auth_backend(session: requests.Session, name="token", , insecure=False, **kwargs):

so that no that no mistakes be made where in the future somebody does not pass in the session and just does:

get_auth_backend(name="token", insecure=False)

backend.prefix = "http" if insecure else "https"
backend._tls_verify = tls_verify
return backend
1 change: 0 additions & 1 deletion oras/auth/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class AuthBackend:
"""

session: requests.Session
_tls_verify: bool

def __init__(self, *args, **kwargs):
self._auths: dict = {}
Expand Down
6 changes: 2 additions & 4 deletions oras/auth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def request_token(self, h: auth_utils.authHeader) -> bool:
headers["Authorization"] = "Basic %s" % self._basic_auth

logger.debug(f"Requesting auth token for: {h}")
authResponse = self.session.get(h.realm, headers=headers, params=params, verify=self._tls_verify) # type: ignore
authResponse = self.session.get(h.realm, headers=headers, params=params) # type: ignore

if authResponse.status_code != 200:
logger.debug(f"Auth response was not successful: {authResponse.text}")
Expand All @@ -155,9 +155,7 @@ def request_anonymous_token(self, h: auth_utils.authHeader) -> bool:
params["scope"] = h.scope

logger.debug(f"Requesting anon token with params: {params}")
response = self.session.request(
"GET", h.realm, params=params, verify=self._tls_verify
)
response = self.session.request("GET", h.realm, params=params)
if response.status_code != 200:
logger.debug(f"Response for anon token failed: {response.text}")
return
Expand Down
29 changes: 20 additions & 9 deletions oras/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __init__(
insecure: bool = False,
tls_verify: bool = True,
auth_backend: str = "token",
tls_cert: Optional[str] = None,
tls_key: Optional[str] = None,
ca_bundle: Optional[str] = None,
):
"""
Create an ORAS client.
Expand All @@ -58,16 +61,23 @@ def __init__(

:param hostname: the hostname of the registry to ping
:type hostname: str
:param registry: if provided, use this custom provider instead of default
:type registry: oras.provider.Registry or None
:param insecure: use http instead of https
:type insecure: bool
:param tls_verify: perform tls verification
:type tls_verify: bool
:param auth_backend: name of the auth backend to use
:type auth_backend: str
:param tls_cert: path to the client certificate to use
:type tls_cert: str
:param tls_key: path to the client key to use
:type tls_key: str
:param ca_bundle: path to the CA bundle to use (enables tls verification)
:type ca_bundle: str
"""
self.hostname: Optional[str] = hostname
self.headers: dict = {}
self.session: requests.Session = requests.Session()
self.prefix: str = "http" if insecure else "https"
self._tls_verify = tls_verify

if not tls_verify:
requests.packages.urllib3.disable_warnings() # type: ignore
Expand All @@ -77,10 +87,14 @@ def __init__(
# trying to set further CSRF cookies (Harbor is such a case)
self.session.cookies.set_policy(DefaultCookiePolicy(allowed_domains=[]))

self.session.verify = ca_bundle or tls_verify
if tls_cert and tls_key:
self.session.cert = (tls_cert, tls_key)
elif tls_cert:
self.session.cert = tls_cert

# Get custom backend, pass on session to share
self.auth = oras.auth.get_auth_backend(
auth_backend, self.session, insecure, tls_verify=tls_verify
)
self.auth = oras.auth.get_auth_backend(auth_backend, self.session, insecure)

def __repr__(self) -> str:
return str(self)
Expand Down Expand Up @@ -994,7 +1008,6 @@ def do_request(
json=json,
headers=headers,
stream=stream,
verify=self._tls_verify,
)

# A 401 response is a request for authentication, 404 is not found
Expand All @@ -1012,7 +1025,6 @@ def do_request(
json=json,
headers=headers,
stream=stream,
verify=self._tls_verify,
)

# One retry if 403 denied (need new token?)
Expand All @@ -1027,7 +1039,6 @@ def do_request(
json=json,
headers=headers,
stream=stream,
verify=self._tls_verify,
)

return response
2 changes: 1 addition & 1 deletion oras/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__copyright__ = "Copyright The ORAS Authors."
__license__ = "Apache-2.0"

__version__ = "0.2.31"
__version__ = "0.3.0"
AUTHOR = "Vanessa Sochat"
EMAIL = "vsoch@users.noreply.github.com"
NAME = "oras"
Expand Down
Loading