Skip to content

Commit

Permalink
Oracle (#91)
Browse files Browse the repository at this point in the history
* Exposed oAuth scope configuration

* Integrated into ConfigFactory

* Adjusted the chart

* Integrated new attributes

* Reformatted
  • Loading branch information
dalazx authored Jul 29, 2019
1 parent 33e692b commit 3d8c744
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
8 changes: 8 additions & 0 deletions deploy/platformregistryapi/templates/platformregistryapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ spec:
secretKeyRef:
name: gcr-secret
key: password
{{- if .Values.NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE }}
- name: NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE
value: {{ .Values.NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE }}
{{- end }}
{{- if .Values.NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS }}
- name: NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS
value: {{ .Values.NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS }}
{{- end }}
{{- end }}
{{- if eq .Values.NP_REGISTRY_UPSTREAM_TYPE "aws_ecr" }}
- name: AWS_DEFAULT_REGION
Expand Down
4 changes: 3 additions & 1 deletion platform_registry_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ async def create_oauth_upstream(
service=config.token_service,
username=config.token_endpoint_username,
password=config.token_endpoint_password,
)
),
registry_catalog_scope=config.token_registry_catalog_scope,
repository_scope_actions=config.token_repository_scope_actions,
)


Expand Down
10 changes: 10 additions & 0 deletions platform_registry_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class UpstreamRegistryConfig:
token_service: str = ""
token_endpoint_username: str = field(repr=False, default="")
token_endpoint_password: str = field(repr=False, default="")
token_registry_catalog_scope: str = "registry:catalog:*"
token_repository_scope_actions: str = "*"

sock_connect_timeout_s: Optional[float] = 30.0
sock_read_timeout_s: Optional[float] = 30.0
Expand Down Expand Up @@ -97,6 +99,14 @@ def create_upstream_registry(self) -> UpstreamRegistryConfig:
],
)
)
if "NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE" in self._environ:
upstream["token_registry_catalog_scope"] = self._environ[
"NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE"
]
if "NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS" in self._environ:
upstream["token_repository_scope_actions"] = self._environ[
"NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS"
]
return UpstreamRegistryConfig(**upstream) # type: ignore

def create_auth(self) -> AuthConfig:
Expand Down
21 changes: 18 additions & 3 deletions platform_registry_api/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from yarl import URL

from .cache import ExpiringCache
from .config import UpstreamRegistryConfig
from .typedefs import TimeFactory
from .upstream import Upstream

Expand Down Expand Up @@ -91,9 +92,22 @@ async def get_token(self, scope: Optional[str] = None) -> OAuthToken:

class OAuthUpstream(Upstream):
def __init__(
self, *, client: OAuthClient, time_factory: TimeFactory = time.time
self,
*,
client: OAuthClient,
registry_catalog_scope: str = (
UpstreamRegistryConfig.token_registry_catalog_scope
),
repository_scope_actions: str = (
UpstreamRegistryConfig.token_repository_scope_actions
),
time_factory: TimeFactory = time.time,
) -> None:
self._client = client
self._registry_catalog_scope = registry_catalog_scope
self._repository_scope_template = (
"repository:{repo}:" + repository_scope_actions
)
self._cache = ExpiringCache[Dict[str, str]](time_factory=time_factory)

async def _get_headers(self, scope: Optional[str] = None) -> Dict[str, str]:
Expand All @@ -108,7 +122,8 @@ async def get_headers_for_version(self) -> Dict[str, str]:
return await self._get_headers()

async def get_headers_for_catalog(self) -> Dict[str, str]:
return await self._get_headers("registry:catalog:*")
return await self._get_headers(self._registry_catalog_scope)

async def get_headers_for_repo(self, repo: str) -> Dict[str, str]:
return await self._get_headers(f"repository:{repo}:*")
scope = self._repository_scope_template.format(repo=repo)
return await self._get_headers(scope)
6 changes: 6 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def test_defaults_oauth(self) -> None:
token_service="test_host",
token_endpoint_username="test_username",
token_endpoint_password="test_password",
token_registry_catalog_scope="registry:catalog:*",
token_repository_scope_actions="*",
max_catalog_entries=100,
),
auth=AuthConfig(
Expand All @@ -55,6 +57,8 @@ def test_oauth(self) -> None:
"NP_REGISTRY_UPSTREAM_TOKEN_PASSWORD": "test_password",
"NP_REGISTRY_AUTH_URL": "https://test_auth",
"NP_REGISTRY_AUTH_TOKEN": "test_auth_token",
"NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE": "",
"NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS": "push,pull",
}
config = EnvironConfigFactory(environ=environ).create()
assert config == Config(
Expand All @@ -67,6 +71,8 @@ def test_oauth(self) -> None:
token_service="test_host",
token_endpoint_username="test_username",
token_endpoint_password="test_password",
token_registry_catalog_scope="",
token_repository_scope_actions="push,pull",
max_catalog_entries=10000,
),
auth=AuthConfig(
Expand Down

0 comments on commit 3d8c744

Please sign in to comment.