Skip to content

Commit

Permalink
Remove creating auth json file
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh committed Mar 1, 2024
1 parent 0830ae6 commit ab8ec1b
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 68 deletions.
44 changes: 0 additions & 44 deletions src/aap_eda/services/activation/engine/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import json
import logging
import os

Expand All @@ -31,7 +29,6 @@
ContainerEngine,
ContainerRequest,
ContainerStatus,
Credential,
LogHandler,
)

Expand Down Expand Up @@ -73,7 +70,6 @@ def __init__(
self.client = get_podman_client()
LOGGER.debug(self.client.version())

self.auth_file = None
except APIError as e:
LOGGER.error(f"Failed to initialize podman engine: f{e}")
raise exceptions.ContainerEngineInitError(str(e))
Expand Down Expand Up @@ -113,7 +109,6 @@ def start(self, request: ContainerRequest, log_handler: LogHandler) -> str:
raise exceptions.ContainerStartError("Missing image url")

try:
self._set_auth_json_file()
self._login(request)
LOGGER.info(f"Image URL is {request.image_url}")
if request.pull_policy == "Always" or not self._image_exists(
Expand Down Expand Up @@ -326,44 +321,6 @@ def _login(self, request: ContainerRequest) -> None:
LOGGER.exception("Login failed: f{e}")
raise exceptions.ContainerStartError(str(e))

def _write_auth_json(self, request: ContainerRequest) -> None:
if not self.auth_file:
LOGGER.debug("No auth file to create")
return

auth_dict = {}
if os.path.exists(self.auth_file):
with open(self.auth_file, encoding="utf-8") as f:
auth_dict = json.load(f)

if "auths" not in auth_dict:
auth_dict["auths"] = {}
registry = request.image_url.split("/")[0]
auth_dict["auths"][registry] = self._create_auth_key(
request.credential
)

with open(self.auth_file, mode="w", encoding="utf-8") as f:
json.dump(auth_dict, f, indent=6)

def _create_auth_key(self, credential: Credential) -> dict:
data = f"{credential.username}:{credential.secret}"
encoded_data = data.encode("ascii")
return {"auth": base64.b64encode(encoded_data).decode("ascii")}

def _set_auth_json_file(self) -> None:
xdg_runtime_dir = os.getenv(
"XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}"
)
auth_file = f"{xdg_runtime_dir}/containers/auth.json"
dir_name = os.path.dirname(auth_file)
if os.path.exists(dir_name):
self.auth_file = auth_file
LOGGER.debug("Will use auth file %s", auth_file)
else:
self.auth_file = None
LOGGER.debug("Will not use auth file")

def _pull_image(
self, request: ContainerRequest, log_handler: LogHandler
) -> Image:
Expand All @@ -376,7 +333,6 @@ def _pull_image(
"username": request.credential.username,
"password": request.credential.secret,
}
self._write_auth_json(request)
image = self.client.images.pull(request.image_url, **kwargs)

# https://github.com/containers/podman-py/issues/301
Expand Down
24 changes: 0 additions & 24 deletions tests/integration/services/activation/engine/test_podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,27 +587,3 @@ def raise_error(*args, **kwargs):

with pytest.raises(ContainerUpdateLogsError, match="Not found"):
engine.update_logs("100", log_handler)


@pytest.mark.django_db
def test_set_auth_json(podman_engine):
engine = podman_engine

with mock.patch("os.path.dirname"):
engine._set_auth_json_file()

xdg_runtime_dir = os.getenv(
"XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}"
)

assert engine.auth_file == f"{xdg_runtime_dir}/containers/auth.json"


@pytest.mark.django_db
def test_write_auth_json(init_data, podman_engine):
engine = podman_engine
engine.auth_file = f"{DATA_DIR}/auth.json"
request = get_request_with_credential(init_data)

engine._write_auth_json(request)
assert engine.auth_file is not None

0 comments on commit ab8ec1b

Please sign in to comment.