Skip to content

Commit

Permalink
Merge 6262c16 into 1b98e1c
Browse files Browse the repository at this point in the history
  • Loading branch information
ilaner committed Feb 13, 2024
2 parents 1b98e1c + 6262c16 commit f8ac543
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ Note that the following commands may work partially without an internet connecti

## XSOAR CI/CD
For information regarding XSOAR CI/CD, please see [this article](https://xsoar.pan.dev/docs/reference/packs/content-management)

## Custom Container Registry

By default, the `demisto-sdk` will use `dockerhub` as the container registry to pull the integrations and scripts docker image.
In order configure a custom container registry, the following environment variables must be set:

* `DEMISTO_SDK_CONTAINER_REGISTRY`: the url of the container registry.
* `DEMISTO_SDK_CR_USER`: the username to use in the container registry.
* `DEMISTO_SDK_CR_PASSWORD`: the password to use in the container registry.
7 changes: 6 additions & 1 deletion demisto_sdk/commands/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
"CI_PROJECT_ID", "1061"
) # Default value is the ID of the content repo on GitLab
ENV_SDK_WORKING_OFFLINE = "DEMISTO_SDK_OFFLINE_ENV"
DOCKER_REGISTRY_URL = os.getenv("DOCKER_IO", "docker.io")

DEFAULT_DOCKER_REGISTRY_URL = "docker.io"
DOCKER_REGISTRY_URL = os.getenv(
"DEMISTO_SDK_CONTAINER_REGISTRY",
os.getenv("DOCKER_IO", DEFAULT_DOCKER_REGISTRY_URL),
)


# Authentication
Expand Down
64 changes: 45 additions & 19 deletions demisto_sdk/commands/common/docker_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from requests.exceptions import RequestException

from demisto_sdk.commands.common.constants import (
DEFAULT_DOCKER_REGISTRY_URL,
DEFAULT_PYTHON2_VERSION,
DEFAULT_PYTHON_VERSION,
DOCKER_REGISTRY_URL,
Expand Down Expand Up @@ -73,11 +74,7 @@ def init_global_docker_client(timeout: int = 60, log_prompt: str = ""):
if docker_user and docker_pass:
logger.debug(f"{log_prompt} - logging in to docker registry")
try:
DOCKER_CLIENT.login(
username=docker_user,
password=docker_pass,
registry="https://index.docker.io/v1",
)
docker_login(DOCKER_CLIENT)
except Exception:
logger.exception(f"{log_prompt} - failed to login to docker registry")
else:
Expand All @@ -86,6 +83,13 @@ def init_global_docker_client(timeout: int = 60, log_prompt: str = ""):
return DOCKER_CLIENT


def is_custom_registry():
return (
not os.getenv("CONTENT_GITLAB_CI")
and DOCKER_REGISTRY_URL != DEFAULT_DOCKER_REGISTRY_URL
)


@functools.lru_cache
def docker_login(docker_client) -> bool:
"""Login to docker-hub using environment variables:
Expand All @@ -96,23 +100,37 @@ def docker_login(docker_client) -> bool:
Returns:
bool: True if logged in successfully.
"""
docker_user = os.getenv("DOCKERHUB_USER")
docker_pass = os.getenv("DOCKERHUB_PASSWORD")
docker_user = os.getenv("DEMISTO_SDK_CR_USER", os.getenv("DOCKERHUB_USER"))
docker_pass = os.getenv("DEMISTO_SDK_CR_PASSWORD", os.getenv("DOCKERHUB_PASSWORD"))
if docker_user and docker_pass:
try:
docker_client.login(
username=docker_user,
password=docker_pass,
registry="https://index.docker.io/v1",
)
ping = docker_client.ping()
logger.debug(f"Successfully connected to dockerhub, login {ping=}")
return ping
if not is_custom_registry():

docker_client.login(
username=docker_user,
password=docker_pass,
registry="https://index.docker.io/v1",
)
ping = docker_client.ping()
logger.debug(f"Successfully connected to dockerhub, login {ping=}")
return ping
else:
# login to custom docker registry
docker_client.login(
username=docker_user,
password=docker_pass,
registry=DOCKER_REGISTRY_URL,
)
ping = docker_client.ping()
logger.debug(
f"Successfully connected to {DOCKER_REGISTRY_URL}, login {ping=}"
)
return ping
except docker.errors.APIError:
logger.info("Did not successfully log in to dockerhub")
logger.info(f"Did not successfully log in to {DOCKER_REGISTRY_URL}")
return False

logger.debug("Did not log in to dockerhub")
logger.debug(f"Did not log in to {DOCKER_REGISTRY_URL}")
return False


Expand Down Expand Up @@ -322,13 +340,13 @@ def create_image(
tag=tag,
changes=self.changes[container_type],
)
if push:
if push and os.getenv("CONTENT_GITLAB_CI"):
self.push_image(image, log_prompt=log_prompt)
return image

@staticmethod
def get_image_registry(image: str) -> str:
if os.getenv("CONTENT_GITLAB_CI") and DOCKER_REGISTRY_URL not in image:
if DOCKER_REGISTRY_URL not in image:
return f"{DOCKER_REGISTRY_URL}/{image}"
return image

Expand Down Expand Up @@ -380,6 +398,9 @@ def get_or_create_test_image(
test_docker_image = (
f'{base_image.replace("demisto", "devtestdemisto")}-{identifier}'
)
if is_custom_registry():
# if we use a custom registry, we need to have to pull the image and we can't use dockerhub api
should_pull = True
if not should_pull and self.is_image_available(test_docker_image):
return test_docker_image, errors
base_image = self.get_image_registry(base_image)
Expand Down Expand Up @@ -609,6 +630,7 @@ def _get_python_version_from_image_client(image: str) -> Version:
Version: Python version X.Y (3.7, 3.6, ..)
"""
try:
image = DockerBase.get_image_registry(image)
image_model = DockerBase.pull_image(image)
image_env = image_model.attrs["Config"]["Env"]
logger.debug(f"Got {image_env=} from {image=}")
Expand All @@ -628,6 +650,10 @@ def _get_python_version_from_dockerhub_api(image: str) -> Version:
Returns:
Version: Python version X.Y (3.7, 3.6, ..)
"""
if is_custom_registry():
raise RuntimeError(
f"Docker registry is configured to be {DOCKER_REGISTRY_URL}, unable to query the dockerhub api"
)
if ":" not in image:
repo = image
tag = "latest"
Expand Down

0 comments on commit f8ac543

Please sign in to comment.