Skip to content

Commit

Permalink
Add ability to override sessions cookie domain in training portal def…
Browse files Browse the repository at this point in the history
…inition.
  • Loading branch information
GrahamDumpleton committed Jul 20, 2023
1 parent 166a8f6 commit 5ded974
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ spec:
type: string
namespace:
type: string
cookies:
type: object
properties:
domain:
type: string
registration:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ spec:
properties:
name:
type: string
cookies:
type: object
properties:
domain:
type: string
status:
type: object
x-kubernetes-preserve-unknown-fields: true
Expand Down
36 changes: 30 additions & 6 deletions client-programs/pkg/cmd/cluster_portal_create_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
)

type ClusterConfigViewOptions struct {
Kubeconfig string
Portal string
Capacity uint
Password string
Kubeconfig string
Portal string
Capacity uint
Password string
ThemeName string
CookieDomain string
}

func (o *ClusterConfigViewOptions) Run(isPasswordSet bool) error {
Expand All @@ -40,7 +42,7 @@ func (o *ClusterConfigViewOptions) Run(isPasswordSet bool) error {

// Update the training portal, creating it if necessary.

err = createTrainingPortal(dynamicClient, o.Portal, o.Capacity, o.Password, isPasswordSet)
err = createTrainingPortal(dynamicClient, o.Portal, o.Capacity, o.Password, isPasswordSet, o.ThemeName, o.CookieDomain)

if err != nil {
return err
Expand Down Expand Up @@ -88,11 +90,23 @@ func (p *ProjectInfo) NewClusterPortalCreateCmd() *cobra.Command {
"",
"override password for training portal access",
)
c.Flags().StringVar(
&o.ThemeName,
"theme-name",
"",
"override theme used by training portal and workshops",
)
c.Flags().StringVar(
&o.CookieDomain,
"cookie-domain",
"",
"override cookie domain used by training portal and workshops",
)

return c
}

func createTrainingPortal(client dynamic.Interface, portal string, capacity uint, password string, isPasswordSet bool) error {
func createTrainingPortal(client dynamic.Interface, portal string, capacity uint, password string, isPasswordSet bool, themeName string, cookieDomain string) error {
trainingPortalClient := client.Resource(trainingPortalResource)

_, err := trainingPortalClient.Get(context.TODO(), portal, metav1.GetOptions{})
Expand Down Expand Up @@ -142,6 +156,16 @@ func createTrainingPortal(client dynamic.Interface, portal string, capacity uint
Reserved: 0,
},
},
"theme": struct {
Name string `json:"name"`
}{
Name: themeName,
},
"cookies": struct {
Domain string `json:"domain"`
}{
Domain: cookieDomain,
},
},
"workshops": []interface{}{},
},
Expand Down
13 changes: 11 additions & 2 deletions session-manager/handlers/trainingportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,18 @@ def training_portal_create(name, uid, body, spec, status, patch, runtime, retry,
portal_index = xget(spec, "portal.index", "")
portal_logo = xget(spec, "portal.logo", "")

theme_name = xget(spec, "portal.theme.name", "default-website-theme")
theme_name = xget(spec, "portal.theme.name")

if not theme_name:
theme_name = "default-website-theme"

frame_ancestors = ",".join(xget(spec, "portal.theme.frame.ancestors", []))

cookie_domain = xget(spec, "portal.cookies.domain")

if not cookie_domain:
cookie_domain = SESSION_COOKIE_DOMAIN

registration_type = xget(spec, "portal.registration.type", "one-step")
enable_registration = str(xget(spec, "portal.registration.enabled", True)).lower()

Expand Down Expand Up @@ -658,7 +667,7 @@ def training_portal_create(name, uid, body, spec, status, patch, runtime, retry,
},
{
"name": "SESSION_COOKIE_DOMAIN",
"value": SESSION_COOKIE_DOMAIN,
"value": cookie_domain,
},
{
"name": "REGISTRATION_TYPE",
Expand Down
9 changes: 8 additions & 1 deletion session-manager/handlers/workshopsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ def workshop_session_create(name, meta, uid, spec, status, patch, logger, retry,

session_hostname = f"{session_namespace}.{INGRESS_DOMAIN}"

# Calculate session cookie domain to use.

cookie_domain = environment_instance.obj["spec"].get("cookies", {}).get("domain")

if not cookie_domain:
cookie_domain = SESSION_COOKIE_DOMAIN

# Calculate role, security policy and quota details for primary namespace.

role = "admin"
Expand Down Expand Up @@ -1451,7 +1458,7 @@ def resolve_security_policy(name):
{"name": "INGRESS_PROTOCOL", "value": INGRESS_PROTOCOL},
{
"name": "SESSION_COOKIE_DOMAIN",
"value": SESSION_COOKIE_DOMAIN,
"value": cookie_domain,
},
{
"name": "IMAGE_REPOSITORY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def process_workshop_environment(portal, workshop, position):
"environment": {"objects": [], "secrets": []},
"registry": environment.registry or None,
"theme": {"name": settings.THEME_NAME},
"cookies": {"domain": settings.SESSION_COOKIE_DOMAIN},
},
}

Expand Down

0 comments on commit 5ded974

Please sign in to comment.