Files
cheese/tesseract/genconf/env.go

85 lines
2.7 KiB
Go

package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
func generateEnv(yamlFile string, wantDiff bool, allowWrite bool, useColor bool) {
config := loadConfig(yamlFile)
// Check that all local directories exist
for _, logEntry := range config.Logs {
if _, err := os.Stat(logEntry.LocalDirectory); os.IsNotExist(err) {
log.Fatalf("User is required to create %s", logEntry.LocalDirectory)
}
}
// Generate .env file for each log
for _, logEntry := range config.Logs {
envPath := filepath.Join(logEntry.LocalDirectory, ".env")
// Create combined roots.pem file
rootsPemPath := filepath.Join(logEntry.LocalDirectory, "roots.pem")
err := createCombinedRootsPemWithStatus(config.Roots, logEntry.ExtraRoots, rootsPemPath, wantDiff, allowWrite, useColor)
if err != nil {
log.Fatalf("Failed to create %s: %v", rootsPemPath, err)
}
// Build TESSERACT_ARGS string
args := []string{
fmt.Sprintf("--private_key=%s", logEntry.Secret),
fmt.Sprintf("--origin=%s.log.ct.ipng.ch", logEntry.ShortName),
fmt.Sprintf("--storage_dir=%s", logEntry.LocalDirectory),
fmt.Sprintf("--roots_pem_file=%s", rootsPemPath),
}
// Add http_endpoint if Listen is specified
if logEntry.Listen != "" {
args = append(args, fmt.Sprintf("--http_endpoint=%s", logEntry.Listen))
}
// Add not_after flags if specified
if !logEntry.NotAfterStart.IsZero() {
args = append(args, fmt.Sprintf("--not_after_start=%s", logEntry.NotAfterStart.Format("2006-01-02T15:04:05Z")))
}
if !logEntry.NotAfterLimit.IsZero() {
args = append(args, fmt.Sprintf("--not_after_limit=%s", logEntry.NotAfterLimit.Format("2006-01-02T15:04:05Z")))
}
tesseractArgs := strings.Join(args, " ")
envContent := fmt.Sprintf("TESSERACT_ARGS=\"%s\"\nOTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318\n", tesseractArgs)
err = writeFileWithStatus(envPath, []byte(envContent), wantDiff, allowWrite, useColor)
if err != nil {
log.Fatalf("Failed to write %s: %v", envPath, err)
}
}
}
func createCombinedRootsPemWithStatus(rootsFile, extraRootsFile, outputPath string, wantDiff bool, allowWrite bool, useColor bool) error {
// Read main roots file
var combinedContent []byte
if rootsFile != "" {
rootsData, err := os.ReadFile(rootsFile)
if err != nil {
return fmt.Errorf("failed to read roots file %s: %v", rootsFile, err)
}
combinedContent = append(combinedContent, rootsData...)
}
// Append extra roots file if it exists
if extraRootsFile != "" {
extraRootsData, err := os.ReadFile(extraRootsFile)
if err != nil {
return fmt.Errorf("failed to read extra roots file %s: %v", extraRootsFile, err)
}
combinedContent = append(combinedContent, extraRootsData...)
}
return writeFileWithStatus(outputPath, combinedContent, wantDiff, allowWrite, useColor)
}