Skip to content

Commit

Permalink
Add print dockerconfigjson secret command (#194)
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Müller <[email protected]>
  • Loading branch information
jensmueller-com authored Nov 27, 2024
1 parent 8188155 commit 9741609
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
70 changes: 70 additions & 0 deletions cpo/commands/cluster/pull_secret/print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2024 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64

import click

from pydantic import BaseModel

from cpo.utils.logging import loglevel_command


class RegistryCredentials(BaseModel):
auth: str
email: str
password: str
username: str


class DockerConfigJSON(BaseModel):
auths: dict[str, RegistryCredentials]


@loglevel_command()
@click.option("--registry-location", help="Container registry location", required=True)
@click.option("--registry-location-username", help="Container registry username", required=True)
@click.option("--registry-location-password", help="Container registry password", required=True)
@click.option("--registry-location-email", default="", help="Container registry e-mail", show_default=True)
@click.option("--encode-base64", is_flag=True)
@click.pass_context
def print(
ctx: click.Context,
registry_location: str,
registry_location_username: str,
registry_location_password: str,
registry_location_email: str,
encode_base64: bool,
):
"""Print dockerconfigjson secret based on the given credentials"""

docker_config_json = DockerConfigJSON(
auths={
registry_location: RegistryCredentials(
auth=base64.standard_b64encode(
f"{registry_location_username}:{registry_location_password}".encode()
).decode("utf-8"),
email=registry_location_email,
password=registry_location_password,
username=registry_location_username,
)
}
)

docker_config_json_str = docker_config_json.model_dump_json()

if encode_base64:
click.echo(base64.standard_b64encode(docker_config_json_str.encode()).decode("utf-8"))
else:
click.echo(docker_config_json_str)
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ classifiers = [
"Programming Language :: Python :: 3.11"
]
dependencies = [
"ansible",
"ansible_runner",
"ansible",
"asyncssh",
"click",
"click-option-group",
"click",
"colorama",
"filelock",
"halo",
"jmespath",
"jsonschema",
"kubernetes >= 12",
"netifaces",
"pydantic",
"pypi_simple",
"pytest",
"pyyaml",
Expand Down

0 comments on commit 9741609

Please sign in to comment.