Skip to content

Commit

Permalink
feat(app+ui): Make saml_sp_acs_url read only (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt authored Jan 9, 2025
1 parent d6f776f commit c29170c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
19 changes: 12 additions & 7 deletions frontend/src/components/organization/org-settings-sso.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Switch } from "@/components/ui/switch"
import { TooltipProvider } from "@/components/ui/tooltip"
import { CopyButton } from "@/components/copy-button"
import { CenteredSpinner } from "@/components/loading/spinner"
import { AlertNotification } from "@/components/notifications"
Expand Down Expand Up @@ -128,13 +129,17 @@ export function OrgSettingsSsoForm() {
name="saml_sp_acs_url"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Service Provider ACS URL</FormLabel>
{field.value && (
<CopyButton
value={field.value}
toastMessage="Copied Service Provider ACS URL to clipboard"
/>
)}
<FormLabel className="flex items-center gap-2">
<span>Service Provider ACS URL</span>
<TooltipProvider>
{field.value && (
<CopyButton
value={field.value}
toastMessage="Copied Service Provider ACS URL to clipboard"
/>
)}
</TooltipProvider>
</FormLabel>
<FormControl>
<Input
placeholder="http://localhost/api/auth/saml/acs"
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/test_organization_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ async def test_update_saml_settings(
test_params = SAMLSettingsUpdate(
saml_enabled=True,
saml_idp_metadata_url="https://test-idp.com",
saml_sp_acs_url="https://test-sp.com",
)
await service.update_saml_settings(test_params)

Expand All @@ -231,7 +230,6 @@ async def test_update_saml_settings(
}
assert settings_dict["saml_enabled"] is True
assert settings_dict["saml_idp_metadata_url"] == "https://test-idp.com"
assert settings_dict["saml_sp_acs_url"] == "https://test-sp.com"


@pytest.mark.anyio
Expand Down
10 changes: 6 additions & 4 deletions tracecat/settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ class BaseSettingsGroup(BaseModel):
"""Base class for configurable settings."""

@classmethod
def keys(cls) -> set[str]:
def keys(cls, *, exclude: set[str] | None = None) -> set[str]:
"""Get the setting keys as a set."""
return set(cls.model_fields.keys())
all_keys = set(cls.model_fields.keys())
if exclude:
all_keys -= exclude
return all_keys


class GitSettingsRead(BaseSettingsGroup):
Expand All @@ -32,7 +35,7 @@ class SAMLSettingsRead(BaseSettingsGroup):
saml_enabled: bool
saml_enforced: bool
saml_idp_metadata_url: str | None = Field(default=None)
saml_sp_acs_url: str | None = Field(default=None)
saml_sp_acs_url: str # Read only

@field_validator("saml_enforced", mode="before")
@classmethod
Expand All @@ -51,7 +54,6 @@ class SAMLSettingsUpdate(BaseSettingsGroup):
" Requires SAML to be enabled.",
)
saml_idp_metadata_url: str | None = Field(default=None)
saml_sp_acs_url: str | None = Field(default=None)


class AuthSettingsRead(BaseSettingsGroup):
Expand Down
8 changes: 6 additions & 2 deletions tracecat/settings/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from fastapi import APIRouter, HTTPException, status

from tracecat import config
from tracecat.auth.credentials import RoleACL
from tracecat.auth.dependencies import Role
from tracecat.auth.enums import AuthType
Expand Down Expand Up @@ -85,10 +86,13 @@ async def get_saml_settings(
session: AsyncDBSession,
) -> SAMLSettingsRead:
service = SettingsService(session, role)
keys = SAMLSettingsRead.keys()

# Exclude read-only keys
keys = SAMLSettingsRead.keys(exclude={"saml_sp_acs_url"})
settings = await service.list_org_settings(keys=keys)
settings_dict = {setting.key: service.get_value(setting) for setting in settings}
return SAMLSettingsRead(**settings_dict)

return SAMLSettingsRead(**settings_dict, saml_sp_acs_url=config.SAML_SP_ACS_URL)


@router.patch("/saml", status_code=status.HTTP_204_NO_CONTENT)
Expand Down

0 comments on commit c29170c

Please sign in to comment.