Skip to content

Commit

Permalink
gh issue #18: fix unsupported method due to ssl redirect must be GET,…
Browse files Browse the repository at this point in the history
… but initial request was POST. solution is to enforce SSL for ssl targets
  • Loading branch information
Vincinator committed Aug 5, 2024
1 parent c47353a commit 7f88f23
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PYTHON := python3
#EXAMPLECONTAINERNAME := localhost:8081/examplecontainer2
#EXAMPLECONTAINERNAME := ghcr.io/gardenlinux/gl-oci
CONTAINER_NAME_example-zot := localhost:8081/examplecontainer2
CONTAINER_NAME_example-ghcr := ghcr.io/gardenlinux/gl-oci
CONTAINER_NAME_example-ghcr := https://ghcr.io/gardenlinux/gl-oci
ifneq (,$(wildcard ./.env))
include .env
export
Expand All @@ -30,6 +30,8 @@ install_deps: ## Install dependencies.

example-%:
@echo "==== DEMO ===="
@echo "=== Inspect oci-index"
$(PYTHON) -m gloci.cli image inspect-index --container $(CONTAINER_NAME_$@):latest
@echo "=== Push dummy container 1 arm64"
$(PYTHON) -m gloci.cli image push --container $(CONTAINER_NAME_$@):latest --architecture arm64 --cname yolo-example_dev --info_yaml example-data/info_1.yaml
@echo "=== Push dummy container 1 amd64"
Expand Down
56 changes: 30 additions & 26 deletions src/gloci/commands/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ def image():
pass


def setup_registry(container, container_name):
container = oras.container.Container(container_name)
username = os.getenv("GLOCI_REGISTRY_USERNAME")
token = os.getenv("GLOCI_REGISTRY_TOKEN")
if username is None:
click.echo("No username")
exit(-1)
if token is None:
click.echo("No token")
exit(-1)
return GlociRegistry(
container.registry,
username,
token,
)

@image.command()
@click.option(
"--container",
Expand All @@ -39,19 +55,7 @@ def image():
)
def push(container_name, architecture, cname, info_yaml):
container = oras.container.Container(container_name)
username = os.getenv("GLOCI_REGISTRY_USERNAME")
token = os.getenv("GLOCI_REGISTRY_TOKEN")
if username is None:
click.echo("No username")
exit(-1)
if token is None:
click.echo("No token")
exit(-1)
registry = GlociRegistry(
container.registry,
username,
token,
)
registry = setup_registry(container, container_name)
registry.push_image_manifest(container_name, architecture, cname, info_yaml)
click.echo(f"Pushed {container_name}")

Expand Down Expand Up @@ -80,7 +84,7 @@ def push(container_name, architecture, cname, info_yaml):
def attach(container_name, cname, architecture, file_path, media_type):
"""Attach data to an existing image manifest"""
container = oras.container.Container(container_name)
registry = GlociRegistry(container.registry)
registry = setup_registry(container, container_name)

registry.attach_layer(container_name, cname, architecture, file_path, media_type)

Expand All @@ -93,22 +97,22 @@ def remove():


@image.command()
@click.option("--container", required=True, help="oci image reference")
def status(container):
@click.option("--container","container_name", required=True, help="oci image reference")
def status(container_name):
"""Get status of image"""
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
container = oras.container.Container(container_name)
registry = setup_registry(container, container_name)
registry.status_all(container)


@image.command()
@click.option("--container", required=True, help="oci image reference")
@click.option("--container", "container_name", required=True, help="oci image reference")
@click.option("--cname", required=True, help="cname of image")
@click.option("--architecture", required=True, help="architecture of image")
def inspect(container, cname, architecture):
def inspect(container_name, cname, architecture):
"""inspect container"""
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
container = oras.container.Container(container_name)
registry = setup_registry(container, container_name)
print(
json.dumps(
registry.get_manifest_by_cname(container, cname, architecture), indent=4
Expand All @@ -117,11 +121,11 @@ def inspect(container, cname, architecture):


@image.command()
@click.option("--container", required=True, help="oci image reference")
def inspect_index(container):
@click.option("--container", "container_name", required=True, help="oci image reference")
def inspect_index(container_name):
"""inspects complete index"""
container = oras.container.Container(container)
registry = GlociRegistry(container.registry)
container = oras.container.Container(container_name)
registry = setup_registry(container, container_name)
print(json.dumps(registry.get_index(container), indent=4))


Expand Down
15 changes: 10 additions & 5 deletions src/gloci/oras/registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import base64

import traceback

import oras.oci
import oras.defaults
import oras.auth
Expand Down Expand Up @@ -114,7 +116,7 @@ def create_config_from_dict(conf: dict, annotations: dict):

class GlociRegistry(Registry):
def __init__(self, registry_url, username=None, token=None, config_path=None):
super().__init__(insecure=True)
super().__init__(insecure=False)
self.registry_url = registry_url
self.config_path = config_path
if not token:
Expand Down Expand Up @@ -388,9 +390,9 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):
info_data = yaml.safe_load(f)
base_path = os.path.join(os.path.dirname(info_yaml))

logger.debug("initilializing index..")
#self.init_index(container)
logger.debug("done initializing index.")
# logger.debug("initilializing index..")
# self.init_index(container)
# logger.debug("done initializing index.")

manifest_image = oras.oci.NewManifest()
total_size = 0
Expand Down Expand Up @@ -429,9 +431,11 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):

manifest_image["layers"].append(layer)

logger.debug("Uploading blob..")
logger.debug(layer)
response = self.upload_blob(file_path, container, layer)
logger.debug("Checking response after uploading blob..")
self._check_200_response(response)

if cleanup_blob and os.path.exists(file_path):
os.remove(file_path)

Expand All @@ -452,6 +456,7 @@ def push_image_manifest(self, container_name, architecture, cname, info_yaml):
conf, config_file = create_config_from_dict(dict(), config_annotations)

response = self.upload_blob(config_file, container, conf)

os.remove(config_file)
self._check_200_response(response)

Expand Down

0 comments on commit 7f88f23

Please sign in to comment.