| gio | b7df27f | 2026-07-28 10:36:17 +0400 | [diff] [blame^] | 1 | package e2e |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/rand" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "text/template" |
| 12 | ) |
| 13 | |
| 14 | type fixtureValues struct { |
| 15 | KratosPublicURL string |
| 16 | KratosAdminURL string |
| 17 | KratosPublicPort int |
| 18 | KratosAdminPort int |
| 19 | HydraPublicURL string |
| 20 | HydraAdminURL string |
| 21 | HydraPublicPort int |
| 22 | HydraAdminPort int |
| 23 | UIURL string |
| 24 | SchemaURL string |
| 25 | SMTPPort int |
| 26 | CookieSecret string |
| 27 | CipherSecret string |
| 28 | HydraSecret string |
| 29 | } |
| 30 | |
| 31 | func renderFixtures(repo, configDir string, values fixtureValues) error { |
| 32 | if err := os.MkdirAll(configDir, 0o755); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | schemaSource := filepath.Join(repo, "e2e", "testdata", "identity.schema.json") |
| 36 | schemaDestination := filepath.Join(configDir, "identity.schema.json") |
| 37 | data, err := os.ReadFile(schemaSource) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if err := os.WriteFile(schemaDestination, data, 0o600); err != nil { |
| 42 | return err |
| 43 | } |
| 44 | values.SchemaURL = "file://" + filepath.ToSlash(schemaDestination) |
| 45 | for _, name := range []string{"kratos.yml", "hydra.yml"} { |
| 46 | if err := renderFixture(filepath.Join(repo, "e2e", "testdata", name+".tmpl"), filepath.Join(configDir, name), values); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | } |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | func renderFixture(source, destination string, values fixtureValues) error { |
| 54 | data, err := os.ReadFile(source) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | tmpl, err := template.New(filepath.Base(source)).Option("missingkey=error").Parse(string(data)) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | var rendered bytes.Buffer |
| 63 | if err := tmpl.Execute(&rendered, values); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if strings.Contains(rendered.String(), "{{") || strings.Contains(rendered.String(), "}}") { |
| 67 | return fmt.Errorf("unresolved template marker in %s", source) |
| 68 | } |
| 69 | return os.WriteFile(destination, rendered.Bytes(), 0o600) |
| 70 | } |
| 71 | |
| 72 | func randomSecret(bytesCount int) (string, error) { |
| 73 | b := make([]byte, bytesCount) |
| 74 | if _, err := rand.Read(b); err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | return hex.EncodeToString(b), nil |
| 78 | } |