People → Sign-in shows single sign-on, Active Directory and SCIM, and the directory's last sync. E sets up Entra ID from the tenant and client IDs; D sets up Active Directory, tried from the control plane first: each server bound, people who may sign in, members of each mapped group. The secrets go into the store as store:auth/..., sealed by the master key, and the CA into the settings, so no control plane needs a file. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
665 lines
22 KiB
Go
665 lines
22 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"go.yaml.in/yaml/v3"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/settings"
|
|
)
|
|
|
|
// How people sign in, as the console shows and sets it: single sign-on
|
|
// through Entra ID or another OpenID provider, an Active Directory checked
|
|
// over LDAPS, and provisioning by SCIM. The secrets these need are kept in
|
|
// the store, so every control plane has them without a file put on each,
|
|
// and a directory is tried before its settings are kept.
|
|
|
|
// inChainRule is AD's matching rule for nested group membership.
|
|
const inChainRule = "1.2.840.113556.1.4.1941"
|
|
|
|
type signInState struct {
|
|
view adminapi.AuthView
|
|
err error
|
|
loaded bool
|
|
// ldap and oidc are the blocks the cluster's settings hold now, for the
|
|
// forms to start from.
|
|
ldap config.LDAP
|
|
oidc config.OIDC
|
|
}
|
|
|
|
type consoleSignInMsg struct {
|
|
view adminapi.AuthView
|
|
ldap config.LDAP
|
|
oidc config.OIDC
|
|
err error
|
|
}
|
|
|
|
// signInRows are the panel's rows, one per way in.
|
|
var signInRows = []string{"oidc", "ldap", "scim"}
|
|
|
|
func (m consoleModel) fetchSignIn() tea.Cmd {
|
|
c := m.client
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
var msg consoleSignInMsg
|
|
if msg.err = c.get(ctx, "/api/v1/auth", &msg.view); msg.err != nil {
|
|
return msg
|
|
}
|
|
var cur adminapi.SettingsView
|
|
if err := c.get(ctx, "/api/v1/settings", &cur); err == nil {
|
|
msg.ldap, msg.oidc = authBlocks(cur.Body)
|
|
}
|
|
return msg
|
|
}
|
|
}
|
|
|
|
// authBlocks are auth.ldap and auth.oidc as a settings document has them.
|
|
func authBlocks(body string) (config.LDAP, config.OIDC) {
|
|
var doc struct {
|
|
Auth struct {
|
|
LDAP yaml.Node `yaml:"ldap"`
|
|
OIDC yaml.Node `yaml:"oidc"`
|
|
} `yaml:"auth"`
|
|
}
|
|
var l config.LDAP
|
|
var o config.OIDC
|
|
if yaml.Unmarshal([]byte(body), &doc) != nil {
|
|
return l, o
|
|
}
|
|
if doc.Auth.LDAP.Kind != 0 {
|
|
if b, err := yaml.Marshal(&doc.Auth.LDAP); err == nil {
|
|
// Read as kept, whether or not it would validate now.
|
|
l, _ = config.ParseLDAP(b)
|
|
}
|
|
}
|
|
if doc.Auth.OIDC.Kind != 0 {
|
|
if m, err := yaml.Marshal(&doc.Auth.OIDC); err == nil {
|
|
if cfg, err := config.FromMap(map[string]any{"auth": map[string]any{"oidc": yamlMap(m)}}); err == nil {
|
|
o = cfg.Auth.OIDC
|
|
}
|
|
}
|
|
}
|
|
return l, o
|
|
}
|
|
|
|
func yamlMap(b []byte) map[string]any {
|
|
var m map[string]any
|
|
_ = yaml.Unmarshal(b, &m)
|
|
return m
|
|
}
|
|
|
|
func (m consoleModel) signInPanel(pw int) (string, string, string) {
|
|
keys := "→ or Enter open · ↑↓ choose · Q leave"
|
|
if m.inPanel {
|
|
keys = "↑↓ row · Enter actions · E Entra ID · D Active Directory · T try the directory · ← back · Q leave"
|
|
}
|
|
s := m.signIn
|
|
var b strings.Builder
|
|
b.WriteString("\n " + sDim.Render("How people sign in besides a password kept here. E for Entra ID, D for Active Directory.") + "\n\n")
|
|
if s.err != nil {
|
|
b.WriteString(" " + sBad.Render(s.err.Error()))
|
|
return "Sign-in", b.String(), keys
|
|
}
|
|
if !s.loaded {
|
|
b.WriteString(" " + sDim.Render("asking the control plane…"))
|
|
return "Sign-in", b.String(), keys
|
|
}
|
|
v := s.view
|
|
local := "passwords kept here; accounts made by an administrator"
|
|
if v.SignupOpen {
|
|
local = "passwords kept here; anyone may sign up, then wait for approval"
|
|
}
|
|
b.WriteString(" " + sTitle.Render("LOCAL") + " " + sDim.Render(local) + "\n\n")
|
|
|
|
var rows [][]string
|
|
for _, r := range signInRows {
|
|
switch r {
|
|
case "oidc":
|
|
if v.OIDC == nil {
|
|
rows = append(rows, []string{"Single sign-on (Entra ID, OIDC)", "off", "E to set up"})
|
|
continue
|
|
}
|
|
state := "on"
|
|
if v.OIDC.NoPasswords {
|
|
state = "on, the only way in"
|
|
}
|
|
rows = append(rows, []string{"Single sign-on (Entra ID, OIDC)", state, or(v.OIDC.Label, v.OIDC.Issuer)})
|
|
case "ldap":
|
|
if v.LDAP == nil {
|
|
rows = append(rows, []string{"Active Directory (LDAP)", "off", "D to set up"})
|
|
continue
|
|
}
|
|
state := "on"
|
|
if v.LDAP.Off != "" {
|
|
state = "broken here"
|
|
}
|
|
rows = append(rows, []string{"Active Directory (LDAP)", state, strings.Join(v.LDAP.URLs, " ")})
|
|
case "scim":
|
|
state, detail := "off", "Enter to make a token"
|
|
if v.SCIM {
|
|
state, detail = "on", "at /scim/v2"
|
|
}
|
|
rows = append(rows, []string{"Provisioning (SCIM)", state, detail})
|
|
}
|
|
}
|
|
b.WriteString(table([]string{"WAY IN", "STATE", ""}, rows, m.row, pw, m.inPanel) + "\n")
|
|
|
|
if v.OIDC != nil {
|
|
b.WriteString("\n " + sTitle.Render("SINGLE SIGN-ON") + "\n")
|
|
b.WriteString(" issuer " + v.OIDC.Issuer + "\n")
|
|
b.WriteString(" redirect " + redirectURL(v.BaseURL) + "\n")
|
|
b.WriteString(" " + sDim.Render(pickText(v.OIDC.CreateAccounts, "accounts made at first sign-in", "only people with an account sign in")) + "\n")
|
|
}
|
|
if l := v.LDAP; l != nil {
|
|
b.WriteString("\n " + sTitle.Render("ACTIVE DIRECTORY") + " " + sDim.Render(l.BaseDN) + "\n")
|
|
if l.Off != "" {
|
|
b.WriteString(" " + sBad.Render("off on this control plane: "+l.Off) + "\n")
|
|
}
|
|
switch ls := l.LastSync; {
|
|
case ls == nil:
|
|
b.WriteString(" " + sDim.Render("not read yet: the leader reads it every sync_interval") + "\n")
|
|
case ls.Error != "":
|
|
b.WriteString(" " + sBad.Render("last read "+agoText(ls.At)+": "+ls.Error) + "\n")
|
|
default:
|
|
b.WriteString(fmt.Sprintf(" last read %s: %d people there, %d accounts kept in step, %d suspended, %d reinstated\n",
|
|
agoText(ls.At), ls.People, ls.Accounts, ls.Suspended, ls.Reinstated))
|
|
}
|
|
}
|
|
return "Sign-in", b.String(), keys
|
|
}
|
|
|
|
func agoText(t time.Time) string { return time.Since(t).Round(time.Second).String() + " ago" }
|
|
|
|
func pickText(b bool, yes, no string) string {
|
|
if b {
|
|
return yes
|
|
}
|
|
return no
|
|
}
|
|
|
|
// redirectURL is what an identity provider sends people back to.
|
|
func redirectURL(base string) string {
|
|
if base == "" {
|
|
return "https://<this gateway>/login/oidc/callback"
|
|
}
|
|
return strings.TrimRight(base, "/") + "/login/oidc/callback"
|
|
}
|
|
|
|
// signInMenu is what can be done to one way in.
|
|
func (m consoleModel) signInMenu(row string) *menuDialog {
|
|
v := m.signIn.view
|
|
switch row {
|
|
case "oidc":
|
|
d := &menuDialog{title: "Single sign-on", items: []menuItem{
|
|
{"Set up Entra ID…", func(m *consoleModel) tea.Cmd { m.entraForm(); return nil }},
|
|
}}
|
|
if v.OIDC != nil {
|
|
d.items = append(d.items, menuItem{"Turn off…", func(m *consoleModel) tea.Cmd {
|
|
m.signInOffConfirm("oidc", "Single sign-on", "People sign in with a password kept here again; accounts made through the provider have none.")
|
|
return nil
|
|
}})
|
|
}
|
|
return d
|
|
case "ldap":
|
|
d := &menuDialog{title: "Active Directory", items: []menuItem{
|
|
{"Set up…", func(m *consoleModel) tea.Cmd { m.directoryForm(); return nil }},
|
|
}}
|
|
if v.LDAP != nil {
|
|
d.items = append(d.items,
|
|
menuItem{"Try it", func(m *consoleModel) tea.Cmd { return m.tryDirectory() }},
|
|
menuItem{"Turn off…", func(m *consoleModel) tea.Cmd {
|
|
m.signInOffConfirm("ldap", "Active Directory", "Its people can no longer sign in; their accounts stay, and are no longer kept in step.")
|
|
return nil
|
|
}})
|
|
}
|
|
return d
|
|
default:
|
|
d := &menuDialog{title: "Provisioning (SCIM)", items: []menuItem{
|
|
{"Make a new token", func(m *consoleModel) tea.Cmd { return m.scimToken() }},
|
|
}}
|
|
if v.SCIM {
|
|
d.items = append(d.items, menuItem{"Turn off…", func(m *consoleModel) tea.Cmd {
|
|
m.signInOffConfirm("scim", "Provisioning", "The provider can no longer make, change or suspend accounts here.")
|
|
return nil
|
|
}})
|
|
}
|
|
return d
|
|
}
|
|
}
|
|
|
|
// signInRowKey is the way in the cursor is on.
|
|
func (m consoleModel) signInRowKey() string {
|
|
if m.row >= 0 && m.row < len(signInRows) {
|
|
return signInRows[m.row]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// directoryForm sets up auth.ldap, starting from what is kept.
|
|
func (m *consoleModel) directoryForm() {
|
|
l := m.signIn.ldap
|
|
allowed := ""
|
|
if f := l.UserFilter; strings.HasPrefix(f, "(memberOf:"+inChainRule+":=") {
|
|
allowed = strings.TrimSuffix(strings.TrimPrefix(f, "(memberOf:"+inChainRule+":="), ")")
|
|
} else {
|
|
allowed = f
|
|
}
|
|
pwHint := "the service account's password"
|
|
if l.BindPasswordFile != "" {
|
|
pwHint = "empty keeps the one kept"
|
|
}
|
|
caHint := "a PEM file on this machine; empty uses the system's"
|
|
if l.CA != "" || l.CAFile != "" {
|
|
caHint = "empty keeps the one kept; - for the system's"
|
|
}
|
|
create := "yes"
|
|
if l.Enabled() && !l.CreateAccounts {
|
|
create = "no"
|
|
}
|
|
var teams []string
|
|
for g, place := range l.Memberships {
|
|
teams = append(teams, g+"="+place)
|
|
}
|
|
pw := textF("password", "", pwHint)
|
|
pw.text.Mask = true
|
|
m.openForm(&formDialog{title: "Active Directory",
|
|
note: "People sign in with their AD name and password, checked over LDAPS. Tried before it is kept.",
|
|
fields: []formField{
|
|
textF("servers", strings.Join(l.URLs, " "), "ldaps://dc1.corp.example ldaps://dc2.corp.example"),
|
|
textF("people under", l.BaseDN, "OU=Staff,DC=corp,DC=example"),
|
|
textF("service account", l.BindDN, "CN=svc-llmgw,OU=Service,DC=corp,DC=example"),
|
|
pw,
|
|
textF("CA certificate", "", caHint),
|
|
textF("who may sign in", allowed, "a group's DN, nested groups count; empty is everyone under it"),
|
|
textF("administrators", strings.Join(l.Roles["admin"], ", "), "group names or DNs"),
|
|
textF("operators", strings.Join(l.Roles["operator"], ", "), "group names or DNs"),
|
|
textF("teams", strings.Join(teams, "; "), "ml-team=team:Acme/ml; sales=org:Sales"),
|
|
textF("label", l.Label, "the form says: Sign in with your LABEL account"),
|
|
choiceF("make accounts", []string{"yes", "no"}, create),
|
|
},
|
|
submit: func(m *consoleModel, v []string) (tea.Cmd, error) {
|
|
next, err := directoryFromForm(l, v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m.saveDirectory(next, v[3]), nil
|
|
}})
|
|
}
|
|
|
|
// directoryFromForm is the auth.ldap block the form's values make, over
|
|
// what was kept.
|
|
func directoryFromForm(kept config.LDAP, v []string) (config.LDAP, error) {
|
|
l := kept
|
|
l.URLs = strings.Fields(strings.ReplaceAll(v[0], ",", " "))
|
|
l.BaseDN, l.BindDN = v[1], v[2]
|
|
if v[3] != "" || l.BindPasswordFile == "" {
|
|
l.BindPasswordFile = adminapi.AuthSecretRef("ldap-bind")
|
|
}
|
|
switch ca := v[4]; ca {
|
|
case "":
|
|
case "-":
|
|
l.CA, l.CAFile = "", ""
|
|
default:
|
|
b, err := os.ReadFile(ca) //nolint:gosec // a file the operator names
|
|
if err != nil {
|
|
return l, fmt.Errorf("CA certificate: %w", err)
|
|
}
|
|
l.CA, l.CAFile = string(b), ""
|
|
}
|
|
switch g := v[5]; {
|
|
case g == "":
|
|
l.UserFilter = ""
|
|
case strings.HasPrefix(g, "("):
|
|
l.UserFilter = g
|
|
case strings.Contains(g, "="):
|
|
l.UserFilter = "(memberOf:" + inChainRule + ":=" + g + ")"
|
|
default:
|
|
return l, errors.New("who may sign in: a group's DN (CN=…,OU=…,DC=…), or an LDAP filter in parentheses")
|
|
}
|
|
l.Roles = map[string][]string{}
|
|
for role, i := range map[string]int{"admin": 6, "operator": 7} {
|
|
for _, g := range strings.Split(v[i], ",") {
|
|
if g = strings.TrimSpace(g); g != "" {
|
|
l.Roles[role] = append(l.Roles[role], g)
|
|
}
|
|
}
|
|
}
|
|
if len(l.Roles) == 0 {
|
|
l.Roles = nil
|
|
}
|
|
l.Memberships = nil
|
|
for _, pair := range strings.Split(v[8], ";") {
|
|
if pair = strings.TrimSpace(pair); pair == "" {
|
|
continue
|
|
}
|
|
g, place, ok := strings.Cut(pair, "=")
|
|
if !ok {
|
|
return l, fmt.Errorf("teams: %q is not GROUP=team:ORG/TEAM", pair)
|
|
}
|
|
if l.Memberships == nil {
|
|
l.Memberships = map[string]string{}
|
|
}
|
|
l.Memberships[strings.TrimSpace(g)] = strings.TrimSpace(place)
|
|
}
|
|
l.Label, l.CreateAccounts = v[9], v[10] == "yes"
|
|
return l, l.Validate()
|
|
}
|
|
|
|
// ldapYAML is a block as the cluster's settings write it.
|
|
func ldapYAML(l config.LDAP) string {
|
|
m := map[string]any{"urls": l.URLs, "bind_dn": l.BindDN, "bind_password_file": l.BindPasswordFile,
|
|
"base_dn": l.BaseDN, "create_accounts": l.CreateAccounts}
|
|
put := func(k, v string) {
|
|
if v != "" {
|
|
m[k] = v
|
|
}
|
|
}
|
|
put("ca", l.CA)
|
|
put("ca_file", l.CAFile)
|
|
put("user_filter", l.UserFilter)
|
|
put("group_base_dn", l.GroupBaseDN)
|
|
put("label", l.Label)
|
|
if l.StartTLS {
|
|
m["start_tls"] = true
|
|
}
|
|
if len(l.Roles) > 0 {
|
|
m["roles"] = l.Roles
|
|
}
|
|
if len(l.Memberships) > 0 {
|
|
m["memberships"] = l.Memberships
|
|
}
|
|
if l.SyncInterval != 0 {
|
|
m["sync_interval"] = l.SyncInterval.String()
|
|
}
|
|
if l.Timeout != 0 {
|
|
m["timeout"] = l.Timeout.String()
|
|
}
|
|
b, _ := yaml.Marshal(m)
|
|
return string(b)
|
|
}
|
|
|
|
// saveDirectory tries the block, and only if every step passes keeps the
|
|
// password and puts the block in the cluster's settings.
|
|
func (m *consoleModel) saveDirectory(l config.LDAP, password string) tea.Cmd {
|
|
m.busy = "Active Directory"
|
|
c, block := m.client, ldapYAML(l)
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
var tv adminapi.LDAPTestView
|
|
if err := c.do(ctx, http.MethodPost, "/api/v1/auth/ldap/test",
|
|
adminapi.LDAPTestRequest{Settings: block, Password: password}, &tv); err != nil {
|
|
return actionDoneMsg{title: "Active Directory", err: err}
|
|
}
|
|
report := checksText(tv.Checks)
|
|
if !tv.OK {
|
|
return actionDoneMsg{title: "Active Directory", err: errors.New("not kept: the directory did not pass\n\n" + report)}
|
|
}
|
|
if password != "" {
|
|
if err := c.do(ctx, http.MethodPut, "/api/v1/auth/secrets/ldap-bind", adminapi.SecretRequest{Value: password}, nil); err != nil {
|
|
return actionDoneMsg{title: "Active Directory", err: err}
|
|
}
|
|
}
|
|
said, err := changeClusterSettings(ctx, c, "auth.ldap from the console", func(doc *settings.Doc) error {
|
|
return doc.SetYAML([]string{"auth", "ldap"}, block)
|
|
})
|
|
if err != nil {
|
|
return actionDoneMsg{title: "Active Directory", err: err}
|
|
}
|
|
return actionDoneMsg{title: "Active Directory", text: report + "\n\n" + said}
|
|
}
|
|
}
|
|
|
|
// tryDirectory tries the block in force.
|
|
func (m *consoleModel) tryDirectory() tea.Cmd {
|
|
m.busy = "Trying the directory"
|
|
c, block := m.client, ldapYAML(m.signIn.ldap)
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
var tv adminapi.LDAPTestView
|
|
if err := c.do(ctx, http.MethodPost, "/api/v1/auth/ldap/test", adminapi.LDAPTestRequest{Settings: block}, &tv); err != nil {
|
|
return actionDoneMsg{title: "The directory", err: err}
|
|
}
|
|
if !tv.OK {
|
|
return actionDoneMsg{title: "The directory", err: errors.New("it did not pass\n\n" + checksText(tv.Checks))}
|
|
}
|
|
return actionDoneMsg{title: "The directory", text: checksText(tv.Checks)}
|
|
}
|
|
}
|
|
|
|
func checksText(checks []adminapi.CheckView) string {
|
|
var b strings.Builder
|
|
for i, c := range checks {
|
|
if i > 0 {
|
|
b.WriteString("\n")
|
|
}
|
|
mark := sOK.Render("✓")
|
|
if !c.OK {
|
|
mark = sBad.Render("✗")
|
|
}
|
|
fmt.Fprintf(&b, "%s %s: %s", mark, c.Step, c.Detail)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// entraForm sets up single sign-on through Entra ID, or another provider
|
|
// when an issuer address is given in place of the tenant.
|
|
func (m *consoleModel) entraForm() {
|
|
o := m.signIn.oidc
|
|
tenant := strings.TrimSuffix(strings.TrimPrefix(o.Issuer, "https://login.microsoftonline.com/"), "/v2.0")
|
|
secretHint := "the app registration's client secret"
|
|
if o.ClientSecretFile != "" {
|
|
secretHint = "empty keeps the one kept"
|
|
}
|
|
create, only := "yes", "no"
|
|
if o.Enabled() && !o.CreateAccounts {
|
|
create = "no"
|
|
}
|
|
if o.NoPasswords {
|
|
only = "yes"
|
|
}
|
|
secret := textF("client secret", "", secretHint)
|
|
secret.text.Mask = true
|
|
m.openForm(&formDialog{title: "Single sign-on: Entra ID",
|
|
note: "Register an app in Entra with the redirect address " + redirectURL(m.signIn.view.BaseURL) +
|
|
", and a groups claim of the groups assigned to it.",
|
|
fields: []formField{
|
|
textF("tenant", tenant, "the directory (tenant) ID; or another provider's issuer URL"),
|
|
textF("client ID", o.ClientID, "the application (client) ID"),
|
|
secret,
|
|
textF("administrators", strings.Join(o.Roles["admin"], ", "), "group object IDs, as Entra sends them"),
|
|
textF("operators", strings.Join(o.Roles["operator"], ", "), "group object IDs"),
|
|
textF("label", or(o.Label, "Microsoft"), "the button says: Sign in with LABEL"),
|
|
choiceF("make accounts", []string{"yes", "no"}, create),
|
|
choiceF("only this way", []string{"no", "yes"}, only),
|
|
},
|
|
submit: func(m *consoleModel, v []string) (tea.Cmd, error) {
|
|
next, err := oidcFromForm(o, v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m.saveOIDC(next, v[2]), nil
|
|
}})
|
|
}
|
|
|
|
func oidcFromForm(kept config.OIDC, v []string) (config.OIDC, error) {
|
|
o := kept
|
|
switch t := strings.TrimSpace(v[0]); {
|
|
case t == "":
|
|
return o, errors.New("tenant: the directory (tenant) ID, or an issuer URL")
|
|
case strings.Contains(t, "://"):
|
|
o.Issuer = t
|
|
default:
|
|
o.Issuer = "https://login.microsoftonline.com/" + t + "/v2.0"
|
|
}
|
|
if o.ClientID = v[1]; o.ClientID == "" {
|
|
return o, errors.New("client ID: the application (client) ID")
|
|
}
|
|
if v[2] != "" || o.ClientSecretFile == "" {
|
|
if v[2] == "" {
|
|
return o, errors.New("client secret: the app registration's secret")
|
|
}
|
|
o.ClientSecretFile = adminapi.AuthSecretRef("oidc-client-secret")
|
|
}
|
|
o.Roles = map[string][]string{}
|
|
for role, i := range map[string]int{"admin": 3, "operator": 4} {
|
|
for _, g := range strings.Split(v[i], ",") {
|
|
if g = strings.TrimSpace(g); g != "" {
|
|
o.Roles[role] = append(o.Roles[role], g)
|
|
}
|
|
}
|
|
}
|
|
if len(o.Roles) == 0 {
|
|
o.Roles = nil
|
|
}
|
|
o.Label, o.CreateAccounts, o.NoPasswords = v[5], v[6] == "yes", v[7] == "yes"
|
|
return o, o.Validate()
|
|
}
|
|
|
|
func oidcYAML(o config.OIDC) string {
|
|
m := map[string]any{"issuer": o.Issuer, "client_id": o.ClientID, "client_secret_file": o.ClientSecretFile,
|
|
"create_accounts": o.CreateAccounts}
|
|
if o.Label != "" {
|
|
m["label"] = o.Label
|
|
}
|
|
if o.NoPasswords {
|
|
m["no_passwords"] = true
|
|
}
|
|
if o.RedirectURL != "" {
|
|
m["redirect_url"] = o.RedirectURL
|
|
}
|
|
if len(o.Scopes) > 0 {
|
|
m["scopes"] = o.Scopes
|
|
}
|
|
if o.GroupsClaim != "" {
|
|
m["groups_claim"] = o.GroupsClaim
|
|
}
|
|
if len(o.Roles) > 0 {
|
|
m["roles"] = o.Roles
|
|
}
|
|
if len(o.Memberships) > 0 {
|
|
m["memberships"] = o.Memberships
|
|
}
|
|
b, _ := yaml.Marshal(m)
|
|
return string(b)
|
|
}
|
|
|
|
func (m *consoleModel) saveOIDC(o config.OIDC, secret string) tea.Cmd {
|
|
m.busy = "Single sign-on"
|
|
c, block, redirect := m.client, oidcYAML(o), redirectURL(m.signIn.view.BaseURL)
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
if secret != "" {
|
|
if err := c.do(ctx, http.MethodPut, "/api/v1/auth/secrets/oidc-client-secret", adminapi.SecretRequest{Value: secret}, nil); err != nil {
|
|
return actionDoneMsg{title: "Single sign-on", err: err}
|
|
}
|
|
}
|
|
said, err := changeClusterSettings(ctx, c, "auth.oidc from the console", func(doc *settings.Doc) error {
|
|
return doc.SetYAML([]string{"auth", "oidc"}, block)
|
|
})
|
|
if err != nil {
|
|
return actionDoneMsg{title: "Single sign-on", err: err}
|
|
}
|
|
return actionDoneMsg{title: "Single sign-on", text: said + "\n\nIn the app registration the redirect address is " + redirect +
|
|
". For people in more than 200 groups, emit the groups assigned to the application."}
|
|
}
|
|
}
|
|
|
|
// scimToken makes a new provisioning token, keeps it, and shows it once.
|
|
func (m *consoleModel) scimToken() tea.Cmd {
|
|
m.busy = "Provisioning"
|
|
c, base := m.client, m.signIn.view.BaseURL
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
b := make([]byte, 32)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return actionDoneMsg{title: "Provisioning", err: err}
|
|
}
|
|
token := hex.EncodeToString(b)
|
|
if err := c.do(ctx, http.MethodPut, "/api/v1/auth/secrets/scim-token", adminapi.SecretRequest{Value: token}, nil); err != nil {
|
|
return actionDoneMsg{title: "Provisioning", err: err}
|
|
}
|
|
said, err := changeClusterSettings(ctx, c, "auth.scim from the console", func(doc *settings.Doc) error {
|
|
return doc.SetYAML([]string{"auth", "scim", "token_file"}, adminapi.AuthSecretRef("scim-token"))
|
|
})
|
|
if err != nil {
|
|
return actionDoneMsg{title: "Provisioning", err: err}
|
|
}
|
|
url := "https://<this gateway>/scim/v2"
|
|
if base != "" {
|
|
url = strings.TrimRight(base, "/") + "/scim/v2"
|
|
}
|
|
return actionDoneMsg{title: "Provisioning", text: "Give the provider the tenant URL " + url + " and this token, shown only now:\n\n " +
|
|
token + "\n\n" + said}
|
|
}
|
|
}
|
|
|
|
// signInOffConfirm turns one way in off, after asking.
|
|
func (m *consoleModel) signInOffConfirm(key, title, text string) {
|
|
m.confirmD = &confirmDialog{title: "Turn off " + strings.ToLower(title), text: text,
|
|
yes: func(m *consoleModel) tea.Cmd {
|
|
m.busy = title
|
|
c := m.client
|
|
return func() tea.Msg {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
said, err := changeClusterSettings(ctx, c, "auth."+key+" off from the console", func(doc *settings.Doc) error {
|
|
doc.Unset([]string{"auth", key})
|
|
return nil
|
|
})
|
|
return actionDoneMsg{title: title, text: said, err: err}
|
|
}
|
|
}}
|
|
}
|
|
|
|
// changeClusterSettings is changeSettings for the console: the newest
|
|
// settings read, changed, and put in force, said in a sentence.
|
|
func changeClusterSettings(ctx context.Context, c *ctlClient, note string, change func(doc *settings.Doc) error) (string, error) {
|
|
var cur adminapi.SettingsView
|
|
if err := c.get(ctx, "/api/v1/settings", &cur); err != nil {
|
|
return "", err
|
|
}
|
|
doc, err := settings.FromBytes("settings", []byte(cur.Body))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := change(doc); err != nil {
|
|
return "", err
|
|
}
|
|
body, err := doc.Bytes()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
before, _ := config.Parse([]byte(cur.Body))
|
|
after, err := config.Parse(body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
changed := config.Changed(before, after)
|
|
if len(changed) == 0 {
|
|
return "Nothing changed.", nil
|
|
}
|
|
var v adminapi.SettingsView
|
|
if err := c.do(ctx, http.MethodPut, "/api/v1/settings",
|
|
adminapi.SettingsRequest{Base: cur.Version, Body: string(body), Note: note}, &v); err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("Settings version %d. Every control plane starts again on it within seconds, standbys first; "+
|
|
"engines keep serving.", v.Version), nil
|
|
}
|