| package e2e |
| |
| import ( |
| "bytes" |
| "crypto/rand" |
| "encoding/hex" |
| "fmt" |
| "os" |
| "path/filepath" |
| "strings" |
| "text/template" |
| ) |
| |
| type fixtureValues struct { |
| KratosPublicURL string |
| KratosAdminURL string |
| KratosPublicPort int |
| KratosAdminPort int |
| HydraPublicURL string |
| HydraAdminURL string |
| HydraPublicPort int |
| HydraAdminPort int |
| UIURL string |
| SchemaURL string |
| SMTPPort int |
| CookieSecret string |
| CipherSecret string |
| HydraSecret string |
| } |
| |
| func renderFixtures(repo, configDir string, values fixtureValues) error { |
| if err := os.MkdirAll(configDir, 0o755); err != nil { |
| return err |
| } |
| schemaSource := filepath.Join(repo, "e2e", "testdata", "identity.schema.json") |
| schemaDestination := filepath.Join(configDir, "identity.schema.json") |
| data, err := os.ReadFile(schemaSource) |
| if err != nil { |
| return err |
| } |
| if err := os.WriteFile(schemaDestination, data, 0o600); err != nil { |
| return err |
| } |
| values.SchemaURL = "file://" + filepath.ToSlash(schemaDestination) |
| for _, name := range []string{"kratos.yml", "hydra.yml"} { |
| if err := renderFixture(filepath.Join(repo, "e2e", "testdata", name+".tmpl"), filepath.Join(configDir, name), values); err != nil { |
| return err |
| } |
| } |
| return nil |
| } |
| |
| func renderFixture(source, destination string, values fixtureValues) error { |
| data, err := os.ReadFile(source) |
| if err != nil { |
| return err |
| } |
| tmpl, err := template.New(filepath.Base(source)).Option("missingkey=error").Parse(string(data)) |
| if err != nil { |
| return err |
| } |
| var rendered bytes.Buffer |
| if err := tmpl.Execute(&rendered, values); err != nil { |
| return err |
| } |
| if strings.Contains(rendered.String(), "{{") || strings.Contains(rendered.String(), "}}") { |
| return fmt.Errorf("unresolved template marker in %s", source) |
| } |
| return os.WriteFile(destination, rendered.Bytes(), 0o600) |
| } |
| |
| func randomSecret(bytesCount int) (string, error) { |
| b := make([]byte, bytesCount) |
| if _, err := rand.Read(b); err != nil { |
| return "", err |
| } |
| return hex.EncodeToString(b), nil |
| } |