2020-08-19 08:32:44 +00:00
|
|
|
"""passbook proxy views"""
|
2019-11-11 17:13:46 +00:00
|
|
|
import string
|
|
|
|
from random import SystemRandom
|
2020-07-26 20:01:37 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2019-11-11 17:13:46 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-07-26 20:01:37 +00:00
|
|
|
from django.db.models import Model
|
2019-10-07 14:33:48 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2020-08-01 17:47:40 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2019-10-07 14:33:48 +00:00
|
|
|
from django.views import View
|
2020-07-26 20:01:37 +00:00
|
|
|
from guardian.shortcuts import get_objects_for_user
|
2019-10-07 14:33:48 +00:00
|
|
|
from structlog import get_logger
|
2020-07-26 20:01:37 +00:00
|
|
|
from yaml import safe_dump
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2019-11-11 17:13:46 +00:00
|
|
|
from passbook import __version__
|
2020-07-26 20:01:37 +00:00
|
|
|
from passbook.core.models import User
|
2020-08-19 08:32:44 +00:00
|
|
|
from passbook.providers.proxy.models import ProxyProvider
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
ORIGINAL_URL = "HTTP_X_ORIGINAL_URL"
|
2019-10-07 14:33:48 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2020-07-26 20:01:37 +00:00
|
|
|
def get_object_for_user_or_404(user: User, perm: str, **filters) -> Model:
|
|
|
|
"""Wrapper that combines get_objects_for_user and get_object_or_404"""
|
|
|
|
return get_object_or_404(get_objects_for_user(user, perm), **filters)
|
|
|
|
|
|
|
|
|
2019-11-11 17:13:46 +00:00
|
|
|
def get_cookie_secret():
|
2020-07-26 20:01:37 +00:00
|
|
|
"""Generate random 32-character string for cookie-secret"""
|
2019-12-31 11:51:16 +00:00
|
|
|
return "".join(
|
2020-07-25 19:34:14 +00:00
|
|
|
SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(32)
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-10-07 14:33:48 +00:00
|
|
|
|
|
|
|
|
2020-07-26 20:01:37 +00:00
|
|
|
class DockerComposeView(LoginRequiredMixin, View):
|
|
|
|
"""Generate docker-compose yaml"""
|
|
|
|
|
2020-08-19 08:32:44 +00:00
|
|
|
def get_compose(self, provider: ProxyProvider) -> str:
|
2020-07-26 20:01:37 +00:00
|
|
|
"""Generate docker-compose yaml, version 3.5"""
|
2020-08-19 08:32:44 +00:00
|
|
|
issuer = provider.get_issuer(self.request)
|
2020-07-26 20:01:37 +00:00
|
|
|
env = {
|
2020-08-19 08:32:44 +00:00
|
|
|
"OAUTH2_PROXY_CLIENT_ID": provider.client_id,
|
|
|
|
"OAUTH2_PROXY_CLIENT_SECRET": provider.client_secret,
|
2020-07-26 20:01:37 +00:00
|
|
|
"OAUTH2_PROXY_REDIRECT_URL": f"{provider.external_host}/oauth2/callback",
|
2020-08-01 17:47:40 +00:00
|
|
|
"OAUTH2_PROXY_OIDC_ISSUER_URL": issuer,
|
2020-07-26 20:01:37 +00:00
|
|
|
"OAUTH2_PROXY_COOKIE_SECRET": get_cookie_secret(),
|
|
|
|
"OAUTH2_PROXY_UPSTREAMS": provider.internal_host,
|
|
|
|
}
|
|
|
|
if urlparse(provider.external_host).scheme != "https":
|
|
|
|
env["OAUTH2_PROXY_COOKIE_SECURE"] = "false"
|
|
|
|
compose = {
|
|
|
|
"version": "3.5",
|
|
|
|
"services": {
|
|
|
|
"passbook_gatekeeper": {
|
2020-08-19 08:32:44 +00:00
|
|
|
"image": f"beryju/passbook-proxy:{__version__}",
|
2020-07-26 20:01:37 +00:00
|
|
|
"ports": ["4180:4180"],
|
|
|
|
"environment": env,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return safe_dump(compose, default_flow_style=False)
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest, provider_pk: int) -> HttpResponse:
|
|
|
|
"""Render docker-compose file"""
|
2020-08-19 08:32:44 +00:00
|
|
|
provider: ProxyProvider = get_object_for_user_or_404(
|
2020-07-26 20:01:37 +00:00
|
|
|
request.user,
|
2020-08-19 08:32:44 +00:00
|
|
|
"passbook_providers_proxy.view_applicationgatewayprovider",
|
2020-07-26 20:01:37 +00:00
|
|
|
pk=provider_pk,
|
|
|
|
)
|
|
|
|
response = HttpResponse()
|
|
|
|
response.content_type = "application/x-yaml"
|
|
|
|
response.content = self.get_compose(provider.pk)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-11-11 17:13:46 +00:00
|
|
|
class K8sManifestView(LoginRequiredMixin, View):
|
|
|
|
"""Generate K8s Deployment and SVC for gatekeeper"""
|
|
|
|
|
2020-07-26 20:01:37 +00:00
|
|
|
def get(self, request: HttpRequest, provider_pk: int) -> HttpResponse:
|
2019-11-11 17:13:46 +00:00
|
|
|
"""Render deployment template"""
|
2020-08-19 08:32:44 +00:00
|
|
|
provider: ProxyProvider = get_object_for_user_or_404(
|
2020-07-26 20:01:37 +00:00
|
|
|
request.user,
|
|
|
|
"passbook_providers_app_gw.view_applicationgatewayprovider",
|
|
|
|
pk=provider_pk,
|
|
|
|
)
|
2019-12-31 11:51:16 +00:00
|
|
|
return render(
|
|
|
|
request,
|
2020-08-19 08:32:44 +00:00
|
|
|
"providers/proxy/k8s-manifest.yaml",
|
2019-12-31 11:51:16 +00:00
|
|
|
{
|
|
|
|
"provider": provider,
|
|
|
|
"cookie_secret": get_cookie_secret(),
|
|
|
|
"version": __version__,
|
2020-08-19 08:32:44 +00:00
|
|
|
"issuer": provider.get_issuer(request),
|
2019-12-31 11:51:16 +00:00
|
|
|
},
|
|
|
|
content_type="text/yaml",
|
|
|
|
)
|