admin: Remove hardcoded strings

This commit is contained in:
nemunaire 2024-03-24 19:19:44 +01:00
parent 239e8ae88d
commit df08e1ec72
1 changed files with 68 additions and 41 deletions

View File

@ -8,7 +8,9 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strings"
"text/template" "text/template"
"unicode"
"srs.epita.fr/fic-server/admin/pki" "srs.epita.fr/fic-server/admin/pki"
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
@ -135,7 +137,7 @@ web:
http: 0.0.0.0:5556 http: 0.0.0.0:5556
frontend: frontend:
issuer: Challenge forensic issuer: Challenge forensic
logoURL: files/logo/ec2.png logoURL: {{ .LogoPath }}
dir: /srv/dex/web/ dir: /srv/dex/web/
oauth2: oauth2:
skipApprovalScreen: true skipApprovalScreen: true
@ -158,7 +160,7 @@ const dexpasswdtpl = `{{ "{{" }} template "header.html" . {{ "}}" }}
<div class="theme-panel"> <div class="theme-panel">
<h2 class="theme-heading"> <h2 class="theme-heading">
Bienvenue au challenge Forensic&nbsp;! Bienvenue au {{ .Name }}&nbsp;!
</h2> </h2>
<form method="post" action="{{ "{{" }} .PostURL {{ "}}" }}"> <form method="post" action="{{ "{{" }} .PostURL {{ "}}" }}">
<div class="theme-form-row"> <div class="theme-form-row">
@ -203,54 +205,79 @@ type dexConfigClient struct {
} }
type dexConfig struct { type dexConfig struct {
Issuer string Name string
Clients []dexConfigClient Issuer string
Teams []*fic.Team Clients []dexConfigClient
Teams []*fic.Team
LogoPath string
} }
func genDexConfig() ([]byte, error) { func genDexConfig() ([]byte, error) {
if teams, err := fic.GetTeams(); err != nil { if OidcSecret == "" {
return nil, err
} else if OidcSecret == "" {
return nil, fmt.Errorf("Unable to generate dex configuration: OIDC Secret not defined. Please define FICOIDC_SECRET in your environment.") return nil, fmt.Errorf("Unable to generate dex configuration: OIDC Secret not defined. Please define FICOIDC_SECRET in your environment.")
} else { }
b := bytes.NewBufferString("")
if challengeInfo, err := GetChallengeInfo(); err != nil { teams, err := fic.GetTeams()
return nil, fmt.Errorf("Cannot create template: %w", err) if err != nil {
} else if dexTmpl, err := template.New("dexcfg").Parse(dexcfgtpl); err != nil { return nil, err
return nil, fmt.Errorf("Cannot create template: %w", err) }
} else if err = dexTmpl.Execute(b, dexConfig{
Issuer: "https://" + OidcIssuer, b := bytes.NewBufferString("")
Clients: []dexConfigClient{
dexConfigClient{ challengeInfo, err := GetChallengeInfo()
Id: OidcClientId, if err != nil {
Name: challengeInfo.Title, return nil, fmt.Errorf("Cannot create template: %w", err)
RedirectURIs: []string{"https://" + OidcIssuer + "/challenge_access/auth"}, }
Secret: OidcSecret,
}, // Lower the first letter to be included in a sentence.
name := []rune(challengeInfo.Title)
if len(name) > 0 {
name[0] = unicode.ToLower(name[0])
}
logoPath := ""
if len(challengeInfo.MainLogo) > 0 {
logoPath = strings.Replace(challengeInfo.MainLogo[len(challengeInfo.MainLogo)-1], "$FILES$", fic.FilesDir, -1)
}
dexTmpl, err := template.New("dexcfg").Parse(dexcfgtpl)
if err != nil {
return nil, fmt.Errorf("Cannot create template: %w", err)
}
err = dexTmpl.Execute(b, dexConfig{
Name: string(name),
Issuer: "https://" + OidcIssuer,
Clients: []dexConfigClient{
dexConfigClient{
Id: OidcClientId,
Name: challengeInfo.Title,
RedirectURIs: []string{"https://" + OidcIssuer + "/challenge_access/auth"},
Secret: OidcSecret,
}, },
Teams: teams, },
}); err != nil { Teams: teams,
return nil, fmt.Errorf("An error occurs during template execution: %w", err) LogoPath: logoPath,
} else { })
// Also generate team associations if err != nil {
for _, team := range teams { return nil, fmt.Errorf("An error occurs during template execution: %w", err)
if _, err := os.Stat(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err == nil { }
if err = os.Remove(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil {
log.Println("Unable to remove existing association symlink:", err.Error())
return nil, fmt.Errorf("Unable to remove existing association symlink: %s", err.Error())
}
}
if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil {
log.Println("Unable to create association symlink:", err.Error())
return nil, fmt.Errorf("Unable to create association symlink: %s", err.Error())
}
}
return b.Bytes(), nil // Also generate team associations
for _, team := range teams {
if _, err := os.Stat(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err == nil {
if err = os.Remove(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil {
log.Println("Unable to remove existing association symlink:", err.Error())
return nil, fmt.Errorf("Unable to remove existing association symlink: %s", err.Error())
}
}
if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil {
log.Println("Unable to create association symlink:", err.Error())
return nil, fmt.Errorf("Unable to create association symlink: %s", err.Error())
} }
} }
return b.Bytes(), nil
} }
func genDexPasswordTpl() ([]byte, error) { func genDexPasswordTpl() ([]byte, error) {