From 7296c5d9e078c9121a55a66363bc37330f0725e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Piku=C5=82a?= Date: Sat, 22 Apr 2023 05:30:58 +0000 Subject: [PATCH 1/5] Make the AUTH_TYPE validator more general MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - allow any casing style - use Basic th by default if the string is left empty - set some default values for user-password pair for BasicAuth Signed-off-by: Marek Pikuła --- config.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 31ad541..76d7973 100644 --- a/config.py +++ b/config.py @@ -61,8 +61,12 @@ class BasicAuthConfig(BaseSettings): Used only if "AUTH_TYPE" environment variable is set to "basic". """ - username: str = Field(env="BASIC_AUTH_USER", description="Username for basic auth.") - password: str = Field(env="BASIC_AUTH_PASS", description="Password for basic auth.") + username: str = Field( + "headscale", env="BASIC_AUTH_USER", description="Username for basic auth." + ) + password: str = Field( + "headscale", env="BASIC_AUTH_PASS", description="Password for basic auth." + ) class AuthType(StrEnum): @@ -383,6 +387,15 @@ class Config(BaseSettings): description="Application data path.", ) + @validator("auth_type", pre=True) + @classmethod + def validate_auth_type(cls, value: Any): + """Validate AUTH_TYPE so that it accepts more valid values.""" + value = str(value).lower() + if value == "": + return AuthType.BASIC + return AuthType(value) + @validator("log_level_name") @classmethod def validate_log_level_name(cls, value: Any): From a5bd6d2f7e8610538f62fb59407d106a6a754fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Piku=C5=82a?= Date: Sat, 22 Apr 2023 05:31:40 +0000 Subject: [PATCH 2/5] Use Pydantic validation error message in logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Should give better insight into the validation error. Signed-off-by: Marek Pikuła --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 76d7973..14b5972 100644 --- a/config.py +++ b/config.py @@ -160,7 +160,7 @@ class InitCheckError(RuntimeError): " %s with type %s: %s", field.field_info.extra["env"], field.type_.__name__, - sub_pydantic_error["type"], + sub_pydantic_error["msg"], ) new_error.append_error( From 3be00b83ac1a4299e05f982e720b8e256befac2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Piku=C5=82a?= Date: Sat, 22 Apr 2023 05:34:05 +0000 Subject: [PATCH 3/5] Make the BUILD_DATE validator more general MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It should be more permissive and fall back to current time if format error is detected, Signed-off-by: Marek Pikuła --- config.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 14b5972..cb2bc0f 100644 --- a/config.py +++ b/config.py @@ -321,8 +321,8 @@ class Config(BaseSettings): env="APP_VERSION", description="Application version. Should be set by Docker.", ) - build_date: str = Field( - default_factory=str(datetime.now), + build_date: datetime = Field( + default_factory=datetime.now, env="BUILD_DATE", description="Application build date. Should be set by Docker.", ) @@ -422,6 +422,23 @@ class Config(BaseSettings): except ZoneInfoNotFoundError as error: raise ValueError(f"Timezone {value} is invalid: {error}") from error + @validator("build_date", pre=True) + @classmethod + def validate_build_date(cls, value: Any): + """Validate build date and accept more values.""" + if isinstance(value, datetime): + return value + assert isinstance(value, str), "BUILD_DATE needs to be a string." + if value == "": + return datetime.now() + try: + return datetime.fromisoformat(value) + except ValueError as error: + current_app.logger.warning( + "BUILD_DATE in wrong format. Expected ISO format. Error: %s", str(error) + ) + return datetime.now() + @validator("hs_config_path", pre=True) @classmethod def validate_hs_config_path(cls, value: Any): From 3acfa64f116847b871970c1588422f27099e4d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Piku=C5=82a?= Date: Sat, 22 Apr 2023 05:36:03 +0000 Subject: [PATCH 4/5] Use ISO format for BUILD_DATE in Jenkins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Pikuła --- Jenkinsfile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a4c8aa5..1b080d5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -26,7 +26,7 @@ pipeline { steps { sh 'printenv' - script { BUILD_DATE = java.time.LocalDate.now() } + script { BUILD_DATE = java.time.DateTimeFormatter.ISO_DATE_TIME.format(java.time.OffsetDateTime.now()) } sh """ # Create the builder: docker buildx create --name $BUILDER_NAME --driver-opt=image=moby/buildkit @@ -62,7 +62,7 @@ pipeline { --label \"GIT_COMMIT=${env.GIT_COMMIT}\" \ --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 \ --push - """ + """ } else { // If I'm just testing, I don't need to build for ARM sh """ docker buildx build . \ @@ -98,10 +98,10 @@ pipeline { } else { sh """ - docker pull git.sysctl.io/albert/headscale-webui:testing - docker pull ghcr.io/ifargle/headscale-webui:testing - docker pull git.sysctl.io/albert/headscale-webui:${env.BRANCH_NAME} - docker pull ghcr.io/ifargle/headscale-webui:${env.BRANCH_NAME} + docker pull git.sysctl.io/albert/headscale-webui:testing + docker pull ghcr.io/ifargle/headscale-webui:testing + docker pull git.sysctl.io/albert/headscale-webui:${env.BRANCH_NAME} + docker pull ghcr.io/ifargle/headscale-webui:${env.BRANCH_NAME} """ } } From 0ae21b444ca60bb162ef0e72d9ee1a3207293ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Piku=C5=82a?= Date: Sat, 22 Apr 2023 05:36:46 +0000 Subject: [PATCH 5/5] Set valid empty defaults for AUTH_TYPE and BUILD_DATE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Pikuła --- Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 504f572..369e73d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,14 +9,14 @@ ARG WORKDIR ENV PYTHONUNBUFFERED=1 # Don't create `.pyc` files: ENV PYTHONDONTWRITEBYTECODE=1 -# https://github.com/rust-lang/cargo/issues/2808 +# https://github.com/rust-lang/cargo/issues/2808 ENV CARGO_NET_GIT_FETCH_WITH_CLI=true # For building CFFI / Crypgotraphy (needed on ARM builds): RUN apk add gcc make musl-dev libffi-dev rust cargo git openssl-dev RUN pip install poetry -RUN poetry config virtualenvs.in-project true +RUN poetry config virtualenvs.in-project true WORKDIR ${WORKDIR} @@ -45,7 +45,7 @@ ENV HS_SERVER=http://localhost/ ENV KEY="" # ENV SCRIPT_NAME=/ ENV DOMAIN_NAME=http://localhost -ENV AUTH_TYPE="None" +ENV AUTH_TYPE="" ENV LOG_LEVEL="Info" # BasicAuth variables @@ -61,7 +61,7 @@ ENV OIDC_CLIENT_SECRET=secret ARG GIT_COMMIT_ARG="" ARG GIT_BRANCH_ARG="" ARG APP_VERSION_ARG="" -ARG BUILD_DATE_ARG="NOT SET" +ARG BUILD_DATE_ARG="" ARG HS_VERSION_ARG="" # About section on the Settings page @@ -78,4 +78,4 @@ EXPOSE 5000/tcp ENTRYPOINT ["/app/entrypoint.sh"] # Temporarily reduce to 1 worker -CMD gunicorn -w 1 -b 0.0.0.0:5000 server:app \ No newline at end of file +CMD gunicorn -w 1 -b 0.0.0.0:5000 server:app